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