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 @ 45904
History | View | Annotate | Download (8.17 KB)
1 | 44309 | omartinez | /*
|
---|---|---|---|
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 | 44395 | omartinez | import java.io.FileOutputStream; |
10 | 44309 | omartinez | import java.io.FileWriter; |
11 | import java.io.IOException; |
||
12 | 44395 | omartinez | import java.io.OutputStreamWriter; |
13 | import java.io.Writer; |
||
14 | 45359 | omartinez | import java.util.Locale; |
15 | 44669 | jjdelcerro | import org.apache.commons.lang3.ArrayUtils; |
16 | 45685 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
17 | 44309 | omartinez | 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 | 45685 | jjdelcerro | 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 | 44309 | omartinez | import org.gvsig.fmap.geom.primitive.Envelope; |
28 | import org.gvsig.tools.ToolsLocator; |
||
29 | import org.gvsig.tools.dataTypes.CoercionException; |
||
30 | 44669 | jjdelcerro | import org.gvsig.tools.dataTypes.Coercion; |
31 | 44309 | omartinez | 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 | 44669 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
41 | 44309 | omartinez | public class CSVFeatureWriter { |
42 | |||
43 | 44669 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(CSVFeatureWriter.class); |
44 | 44309 | omartinez | |
45 | 44669 | jjdelcerro | 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 | 44900 | omartinez | // private CoercionContext converContext = null;
|
56 | 44669 | jjdelcerro | private int errorcounts = 0; |
57 | private Throwable lasterror = null; |
||
58 | private CSVStoreParameters storeParameters;
|
||
59 | private String charset = null; |
||
60 | 45182 | omartinez | private boolean includeMetadataInHeader; |
61 | 45359 | omartinez | private Locale locale; |
62 | 45685 | jjdelcerro | private GeometryManager geometryManager;
|
63 | 44309 | omartinez | |
64 | 44669 | jjdelcerro | 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 | 44309 | omartinez | } |
73 | |||
74 | 44669 | jjdelcerro | if (this.ftype != null) { |
75 | this.descriptors = this.ftype.getAttributeDescriptors(); |
||
76 | if (ftype.getDefaultGeometryAttributeName() != null) { |
||
77 | this.calculate_envelope = true; |
||
78 | } |
||
79 | 44309 | omartinez | } |
80 | 44669 | jjdelcerro | this.convert = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.STRING);
|
81 | 44900 | omartinez | // this.converContext = DataTypeUtils.coerceContextLocale(CSVStoreParameters.getLocale(this.storeParameters));
|
82 | 44669 | jjdelcerro | this.errorcounts = 0; |
83 | this.charset = CSVStoreParameters.getCharset(storeParameters);
|
||
84 | 45182 | omartinez | this.includeMetadataInHeader = CSVStoreParameters.getIncludeMetadataInHeader(storeParameters);
|
85 | 45359 | omartinez | this.locale = CSVStoreParameters.getLocale(storeParameters);
|
86 | 45685 | jjdelcerro | this.geometryManager = GeometryLocator.getGeometryManager();
|
87 | 45182 | omartinez | } |
88 | 44309 | omartinez | |
89 | 44669 | jjdelcerro | 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 | 44309 | omartinez | int n = 0; |
101 | for (FeatureAttributeDescriptor descriptor : descriptors) {
|
||
102 | 44669 | jjdelcerro | if (descriptor.getEvaluator() == null) { |
103 | n++; |
||
104 | } |
||
105 | 44309 | omartinez | } |
106 | 44669 | jjdelcerro | this.values = new String[n]; |
107 | } |
||
108 | 44309 | omartinez | |
109 | 44669 | jjdelcerro | } 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 | 45121 | jjdelcerro | for (FeatureAttributeDescriptor descriptor : descriptors) {
|
134 | if (!descriptor.isComputed()) {
|
||
135 | 45182 | omartinez | if (includeMetadataInHeader) {
|
136 | header[n++] = (String) descriptor.get("all"); |
||
137 | } else {
|
||
138 | header[n++] = (String) descriptor.getName();
|
||
139 | } |
||
140 | 45121 | jjdelcerro | } |
141 | 44669 | jjdelcerro | } |
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 | 44309 | omartinez | |
150 | 44669 | jjdelcerro | 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 | 44309 | omartinez | } |
163 | 44669 | jjdelcerro | } |
164 | } |
||
165 | 44309 | omartinez | |
166 | 44669 | jjdelcerro | 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 | 44309 | omartinez | try {
|
171 | 45685 | jjdelcerro | if( descriptor.getType()==DataTypes.GEOMETRY ) {
|
172 | 45836 | fdiaz | if(value != null){ |
173 | if( StringUtils.equalsIgnoreCase("WKT", CSVStoreParameters.getGeometryFormat(storeParameters))) { |
||
174 | values[i] = ((Geometry)value).convertToWKT(); |
||
175 | } else {
|
||
176 | values[i] = ((Geometry)value).convertToHexWKB(); |
||
177 | } |
||
178 | 45685 | jjdelcerro | } |
179 | } else {
|
||
180 | values[i] = (String) this.convert.coerce(value, descriptor.getCoercionContext()); |
||
181 | } |
||
182 | } catch (CoercionException|GeometryOperationNotSupportedException|GeometryOperationException e) {
|
||
183 | 44669 | jjdelcerro | try {
|
184 | values[i] = value.toString(); |
||
185 | } catch (Exception ex) { |
||
186 | values[i] = "";
|
||
187 | } |
||
188 | if (errorcounts++ <= 10) { |
||
189 | this.lasterror = e;
|
||
190 | LOGGER.warn("Can't convert value of field " + i + " to string in CVS file '" + this.file.getAbsolutePath() + "'.", e); |
||
191 | if (errorcounts == 10) { |
||
192 | LOGGER.warn("Too many error writing CVS file '" + this.file.getAbsolutePath() + "', don't output more."); |
||
193 | 44309 | omartinez | } |
194 | 44669 | jjdelcerro | } |
195 | 44309 | omartinez | } |
196 | 45083 | jjdelcerro | if( values[i]!=null ) { |
197 | values[i] = values[i].replaceAll("\\n","\\\\n"); |
||
198 | values[i] = values[i].replaceAll("\\r","\\\\r"); |
||
199 | } |
||
200 | 44669 | jjdelcerro | } |
201 | 44309 | omartinez | } |
202 | 44669 | jjdelcerro | try {
|
203 | 45083 | jjdelcerro | this.listWriter.write(values);
|
204 | 44669 | jjdelcerro | } catch (IOException e) { |
205 | if (errorcounts++ <= 10) { |
||
206 | this.lasterror = e;
|
||
207 | LOGGER.warn("Can't write values to CVS file '" + this.file.getAbsolutePath() + "'.", e); |
||
208 | if (errorcounts == 10) { |
||
209 | LOGGER.warn("Too many error writing CVS file '" + this.file.getAbsolutePath() + "', don't output more."); |
||
210 | 44309 | omartinez | } |
211 | 44669 | jjdelcerro | } |
212 | 44309 | omartinez | } |
213 | |||
214 | 44669 | jjdelcerro | } |
215 | |||
216 | public void end() throws PerformEditingException { |
||
217 | if (this.errorcounts > 0) { |
||
218 | throw new PerformEditingException(this.file.getAbsolutePath(), lasterror); |
||
219 | 44309 | omartinez | } |
220 | 44669 | jjdelcerro | if (listWriter != null) { |
221 | try {
|
||
222 | listWriter.close(); |
||
223 | } catch (Exception ex) { |
||
224 | // Ignore error
|
||
225 | } |
||
226 | listWriter = null;
|
||
227 | } |
||
228 | if (writer != null) { |
||
229 | try {
|
||
230 | writer.close(); |
||
231 | } catch (Exception ex) { |
||
232 | // Ignore error
|
||
233 | } |
||
234 | writer = null;
|
||
235 | } |
||
236 | } |
||
237 | |||
238 | public Envelope getEnvelope() {
|
||
239 | return this.envelope; |
||
240 | } |
||
241 | 44309 | omartinez | } |