svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / csv / CSVFeatureWriter.java @ 45721
History | View | Annotate | Download (8.11 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.fmap.dal.store.csv; |
7 |
|
8 |
import java.io.File; |
9 |
import java.io.FileOutputStream; |
10 |
import java.io.FileWriter; |
11 |
import java.io.IOException; |
12 |
import java.io.OutputStreamWriter; |
13 |
import java.io.Writer; |
14 |
import java.util.Locale; |
15 |
import org.apache.commons.lang3.ArrayUtils; |
16 |
import org.apache.commons.lang3.StringUtils; |
17 |
import org.gvsig.fmap.dal.DataTypes; |
18 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
19 |
import org.gvsig.fmap.dal.feature.FeatureType; |
20 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
21 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
22 |
import org.gvsig.fmap.geom.Geometry; |
23 |
import org.gvsig.fmap.geom.GeometryLocator; |
24 |
import org.gvsig.fmap.geom.GeometryManager; |
25 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
26 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
27 |
import org.gvsig.fmap.geom.primitive.Envelope; |
28 |
import org.gvsig.tools.ToolsLocator; |
29 |
import org.gvsig.tools.dataTypes.CoercionException; |
30 |
import org.gvsig.tools.dataTypes.Coercion; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
import org.supercsv.io.CsvListWriter; |
34 |
import org.supercsv.prefs.CsvPreference; |
35 |
|
36 |
/**
|
37 |
*
|
38 |
* @author osc
|
39 |
*/
|
40 |
@SuppressWarnings("UseSpecificCatch") |
41 |
public class CSVFeatureWriter { |
42 |
|
43 |
private static final Logger LOGGER = LoggerFactory.getLogger(CSVFeatureWriter.class); |
44 |
|
45 |
private Envelope envelope = null; |
46 |
private boolean calculate_envelope = false; |
47 |
private CsvListWriter listWriter = null; |
48 |
private CsvPreference csvpreferences = null; |
49 |
private Writer writer = null; |
50 |
private FeatureType ftype;
|
51 |
private File file; |
52 |
private String[] values; |
53 |
private FeatureAttributeDescriptor[] descriptors; |
54 |
private Coercion convert = null; |
55 |
// private CoercionContext converContext = null;
|
56 |
private int errorcounts = 0; |
57 |
private Throwable lasterror = null; |
58 |
private CSVStoreParameters storeParameters;
|
59 |
private String charset = null; |
60 |
private boolean includeMetadataInHeader; |
61 |
private Locale locale; |
62 |
private GeometryManager geometryManager;
|
63 |
|
64 |
public void initialize(CSVStoreParameters storeParameters, File file, FeatureType ftype, CsvPreference csvpreferences) { |
65 |
this.file = file;
|
66 |
this.ftype = ftype;
|
67 |
this.storeParameters = storeParameters;
|
68 |
if (csvpreferences == null) { |
69 |
this.csvpreferences = CSVStoreParameters.getPredefinedCSVPreferences(storeParameters);
|
70 |
} else {
|
71 |
this.csvpreferences = csvpreferences;
|
72 |
} |
73 |
|
74 |
if (this.ftype != null) { |
75 |
this.descriptors = this.ftype.getAttributeDescriptors(); |
76 |
if (ftype.getDefaultGeometryAttributeName() != null) { |
77 |
this.calculate_envelope = true; |
78 |
} |
79 |
} |
80 |
this.convert = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.STRING);
|
81 |
// this.converContext = DataTypeUtils.coerceContextLocale(CSVStoreParameters.getLocale(this.storeParameters));
|
82 |
this.errorcounts = 0; |
83 |
this.charset = CSVStoreParameters.getCharset(storeParameters);
|
84 |
this.includeMetadataInHeader = CSVStoreParameters.getIncludeMetadataInHeader(storeParameters);
|
85 |
this.locale = CSVStoreParameters.getLocale(storeParameters);
|
86 |
this.geometryManager = GeometryLocator.getGeometryManager();
|
87 |
} |
88 |
|
89 |
public void beginAppend() { |
90 |
try {
|
91 |
FileOutputStream fos = new FileOutputStream(file, true); |
92 |
if (this.charset != null) { |
93 |
this.writer = new OutputStreamWriter(fos, this.charset); |
94 |
} else {
|
95 |
this.writer = new OutputStreamWriter(fos); |
96 |
} |
97 |
this.listWriter = new CsvListWriter(this.writer, this.csvpreferences); |
98 |
if (ftype != null) { |
99 |
this.descriptors = ftype.getAttributeDescriptors();
|
100 |
int n = 0; |
101 |
for (FeatureAttributeDescriptor descriptor : descriptors) {
|
102 |
if (descriptor.getEvaluator() == null) { |
103 |
n++; |
104 |
} |
105 |
} |
106 |
this.values = new String[n]; |
107 |
} |
108 |
|
109 |
} catch (IOException e) { |
110 |
LOGGER.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e); |
111 |
throw new RuntimeException(e); |
112 |
} |
113 |
} |
114 |
|
115 |
public void begin() { |
116 |
try {
|
117 |
this.writer = new FileWriter(file); |
118 |
} catch (IOException e) { |
119 |
LOGGER.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e); |
120 |
throw new RuntimeException(e); |
121 |
} |
122 |
this.listWriter = new CsvListWriter(this.writer, this.csvpreferences); |
123 |
int n = 0; |
124 |
for (FeatureAttributeDescriptor descriptor : descriptors) {
|
125 |
if (descriptor.getEvaluator() == null) { |
126 |
n++; |
127 |
} |
128 |
} |
129 |
|
130 |
String[] header = new String[n]; |
131 |
this.values = new String[n]; |
132 |
n = 0;
|
133 |
for (FeatureAttributeDescriptor descriptor : descriptors) {
|
134 |
if (!descriptor.isComputed()) {
|
135 |
if (includeMetadataInHeader) {
|
136 |
header[n++] = (String) descriptor.get("all"); |
137 |
} else {
|
138 |
header[n++] = (String) descriptor.getName();
|
139 |
} |
140 |
} |
141 |
} |
142 |
try {
|
143 |
listWriter.writeHeader(header); |
144 |
} catch (Exception e) { |
145 |
LOGGER.warn("Can't write header '" + ArrayUtils.toString(header) + "' file for write (" + file.getAbsolutePath() + ").", e); |
146 |
throw new RuntimeException(e); |
147 |
} |
148 |
} |
149 |
|
150 |
public void add(FeatureProvider feature) { |
151 |
if (this.calculate_envelope) { |
152 |
Geometry geom = feature.getDefaultGeometry(); |
153 |
if (geom != null) { |
154 |
if (envelope == null) { |
155 |
try {
|
156 |
envelope = (Envelope) geom.getEnvelope().clone(); |
157 |
} catch (CloneNotSupportedException e) { |
158 |
LOGGER.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.", e);
|
159 |
} |
160 |
} else {
|
161 |
envelope.add(geom.getEnvelope()); |
162 |
} |
163 |
} |
164 |
} |
165 |
|
166 |
for (int i = 0; i < descriptors.length; i++) { |
167 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
168 |
if (descriptor.getEvaluator() == null) { |
169 |
Object value = feature.get(i);
|
170 |
try {
|
171 |
if( descriptor.getType()==DataTypes.GEOMETRY ) {
|
172 |
if( StringUtils.equalsIgnoreCase("WKT", CSVStoreParameters.getGeometryFormat(storeParameters))) { |
173 |
values[i] = ((Geometry)value).convertToWKT(); |
174 |
} else {
|
175 |
values[i] = ((Geometry)value).convertToHexWKB(); |
176 |
} |
177 |
} else {
|
178 |
values[i] = (String) this.convert.coerce(value, descriptor.getCoercionContext()); |
179 |
} |
180 |
} catch (CoercionException|GeometryOperationNotSupportedException|GeometryOperationException e) {
|
181 |
try {
|
182 |
values[i] = value.toString(); |
183 |
} catch (Exception ex) { |
184 |
values[i] = "";
|
185 |
} |
186 |
if (errorcounts++ <= 10) { |
187 |
this.lasterror = e;
|
188 |
LOGGER.warn("Can't convert value of field " + i + " to string in CVS file '" + this.file.getAbsolutePath() + "'.", e); |
189 |
if (errorcounts == 10) { |
190 |
LOGGER.warn("Too many error writing CVS file '" + this.file.getAbsolutePath() + "', don't output more."); |
191 |
} |
192 |
} |
193 |
} |
194 |
if( values[i]!=null ) { |
195 |
values[i] = values[i].replaceAll("\\n","\\\\n"); |
196 |
values[i] = values[i].replaceAll("\\r","\\\\r"); |
197 |
} |
198 |
} |
199 |
} |
200 |
try {
|
201 |
this.listWriter.write(values);
|
202 |
} catch (IOException e) { |
203 |
if (errorcounts++ <= 10) { |
204 |
this.lasterror = e;
|
205 |
LOGGER.warn("Can't write values to CVS file '" + this.file.getAbsolutePath() + "'.", e); |
206 |
if (errorcounts == 10) { |
207 |
LOGGER.warn("Too many error writing CVS file '" + this.file.getAbsolutePath() + "', don't output more."); |
208 |
} |
209 |
} |
210 |
} |
211 |
|
212 |
} |
213 |
|
214 |
public void end() throws PerformEditingException { |
215 |
if (this.errorcounts > 0) { |
216 |
throw new PerformEditingException(this.file.getAbsolutePath(), lasterror); |
217 |
} |
218 |
if (listWriter != null) { |
219 |
try {
|
220 |
listWriter.close(); |
221 |
} catch (Exception ex) { |
222 |
// Ignore error
|
223 |
} |
224 |
listWriter = null;
|
225 |
} |
226 |
if (writer != null) { |
227 |
try {
|
228 |
writer.close(); |
229 |
} catch (Exception ex) { |
230 |
// Ignore error
|
231 |
} |
232 |
writer = null;
|
233 |
} |
234 |
} |
235 |
|
236 |
public Envelope getEnvelope() {
|
237 |
return this.envelope; |
238 |
} |
239 |
} |