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 @ 44395
History | View | Annotate | Download (8.21 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.gvsig.fmap.dal.DataTypes; |
16 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
17 |
import org.gvsig.fmap.dal.feature.FeatureType; |
18 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
19 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
20 |
import org.gvsig.fmap.geom.Geometry; |
21 |
import org.gvsig.fmap.geom.primitive.Envelope; |
22 |
import org.gvsig.tools.ToolsLocator; |
23 |
import org.gvsig.tools.dataTypes.CoercionException; |
24 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
import org.supercsv.io.CsvListWriter; |
28 |
import org.supercsv.prefs.CsvPreference; |
29 |
|
30 |
/**
|
31 |
*
|
32 |
* @author osc
|
33 |
*/
|
34 |
public class CSVFeatureWriter { |
35 |
|
36 |
private Envelope envelope = null; |
37 |
private boolean calculate_envelope = false; |
38 |
private CsvListWriter listWriter = null; |
39 |
private CsvPreference csvpreferences = null; |
40 |
private Writer writer = null; |
41 |
private FeatureType ftype;
|
42 |
private File file; |
43 |
private String[] values; |
44 |
private FeatureAttributeDescriptor[] descriptors; |
45 |
private DataTypesManager.Coercion convert = null; |
46 |
private int errorcounts = 0; |
47 |
private Throwable lasterror = null; |
48 |
private Locale locale = null; |
49 |
private CSVStoreParameters storeParameters;
|
50 |
private static final Logger logger = LoggerFactory.getLogger(CSVFeatureWriter.class); |
51 |
private String charset = null; |
52 |
|
53 |
public void initialize(CSVStoreParameters storeParameters, File file, FeatureType ftype, CsvPreference csvpreferences) { |
54 |
this.file = file;
|
55 |
this.ftype = ftype;
|
56 |
this.storeParameters = storeParameters;
|
57 |
this.locale = CSVStoreParameters.getLocale(this.storeParameters); |
58 |
if (csvpreferences == null) { |
59 |
this.csvpreferences = CSVStoreParameters.getPredefinedCSVPreferences(storeParameters);
|
60 |
} else {
|
61 |
this.csvpreferences = csvpreferences;
|
62 |
} |
63 |
|
64 |
if (this.ftype != null) { |
65 |
this.descriptors = this.ftype.getAttributeDescriptors(); |
66 |
if (ftype.getDefaultGeometryAttributeName() != null) { |
67 |
this.calculate_envelope = true; |
68 |
} |
69 |
} |
70 |
this.convert = ToolsLocator.getDataTypesManager().getCoercion(org.gvsig.tools.dataTypes.DataTypes.STRING);
|
71 |
this.errorcounts = 0; |
72 |
this.charset = CSVStoreParameters.getCharset(storeParameters);
|
73 |
} |
74 |
|
75 |
public void beginAppend() { |
76 |
try {
|
77 |
FileOutputStream fos = new FileOutputStream(file, true); |
78 |
if (this.charset != null) { |
79 |
this.writer = new OutputStreamWriter(fos, this.charset); |
80 |
} else {
|
81 |
this.writer = new OutputStreamWriter(fos); |
82 |
} |
83 |
this.listWriter = new CsvListWriter(this.writer, this.csvpreferences); |
84 |
if (ftype != null) { |
85 |
this.descriptors = ftype.getAttributeDescriptors();
|
86 |
int n = 0; |
87 |
for (FeatureAttributeDescriptor descriptor : descriptors) {
|
88 |
if (descriptor.getEvaluator() == null) { |
89 |
n++; |
90 |
} |
91 |
} |
92 |
this.values = new String[n]; |
93 |
} |
94 |
|
95 |
} catch (IOException e) { |
96 |
logger.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e); |
97 |
throw new RuntimeException(e); |
98 |
} |
99 |
} |
100 |
|
101 |
public void begin() { |
102 |
try {
|
103 |
this.writer = new FileWriter(file); |
104 |
} catch (IOException e) { |
105 |
logger.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e); |
106 |
throw new RuntimeException(e); |
107 |
} |
108 |
this.listWriter = new CsvListWriter(this.writer, this.csvpreferences); |
109 |
int n = 0; |
110 |
for (FeatureAttributeDescriptor descriptor : descriptors) {
|
111 |
if (descriptor.getEvaluator() == null) { |
112 |
n++; |
113 |
} |
114 |
} |
115 |
|
116 |
String[] header = new String[n]; |
117 |
this.values = new String[n]; |
118 |
n = 0;
|
119 |
for (int i = 0; i < descriptors.length; i++) { |
120 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
121 |
if (descriptor.getEvaluator() == null) { |
122 |
String name = descriptor.getName();
|
123 |
String typeName = descriptor.getDataTypeName();
|
124 |
if (descriptor.getDataType().getType() == DataTypes.STRING) {
|
125 |
header[n++] = name + "__" + typeName + "__" + descriptor.getSize(); |
126 |
} else {
|
127 |
header[n++] = name + "__" + typeName;
|
128 |
} |
129 |
} |
130 |
} |
131 |
try {
|
132 |
listWriter.writeHeader(header); |
133 |
} catch (Exception e) { |
134 |
logger.warn("Can't write header '" + header.toString() + "' file for write (" + file.getAbsolutePath() + ").", e); |
135 |
throw new RuntimeException(e); |
136 |
} |
137 |
} |
138 |
|
139 |
public void add(FeatureProvider feature) { |
140 |
if (this.calculate_envelope) { |
141 |
Geometry geom = feature.getDefaultGeometry(); |
142 |
if (geom != null) { |
143 |
if (envelope == null) { |
144 |
try {
|
145 |
envelope = (Envelope) geom.getEnvelope().clone(); |
146 |
} catch (CloneNotSupportedException e) { |
147 |
logger.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.", e);
|
148 |
} |
149 |
} else {
|
150 |
envelope.add(geom.getEnvelope()); |
151 |
} |
152 |
} |
153 |
} |
154 |
|
155 |
for (int i = 0; i < descriptors.length; i++) { |
156 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
157 |
if (descriptor.getEvaluator() == null) { |
158 |
Object value = feature.get(i);
|
159 |
try {
|
160 |
if (this.convert != null && this.convert instanceof DataTypesManager.CoercionWithLocale) { |
161 |
values[i] = (String) ((DataTypesManager.CoercionWithLocale) this.convert).coerce(value, this.locale); |
162 |
} else {
|
163 |
values[i] = (String) this.convert.coerce(value); |
164 |
} |
165 |
} catch (CoercionException e) {
|
166 |
try {
|
167 |
values[i] = value.toString(); |
168 |
} catch (Exception ex) { |
169 |
values[i] = "";
|
170 |
} |
171 |
if (errorcounts++ <= 10) { |
172 |
this.lasterror = e;
|
173 |
logger.warn("Can't convert value of field " + i + " to string in CVS file '" + this.file.getAbsolutePath() + "'.", e); |
174 |
if (errorcounts == 10) { |
175 |
logger.warn("Too many error writing CVS file '" + this.file.getAbsolutePath() + "', don't output more."); |
176 |
} |
177 |
} |
178 |
} |
179 |
} |
180 |
} |
181 |
try {
|
182 |
this.listWriter.writeHeader(values);
|
183 |
} catch (IOException e) { |
184 |
if (errorcounts++ <= 10) { |
185 |
this.lasterror = e;
|
186 |
logger.warn("Can't write values to CVS file '" + this.file.getAbsolutePath() + "'.", e); |
187 |
if (errorcounts == 10) { |
188 |
logger.warn("Too many error writing CVS file '" + this.file.getAbsolutePath() + "', don't output more."); |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
} |
194 |
|
195 |
public void end() throws PerformEditingException { |
196 |
if (this.errorcounts > 0) { |
197 |
throw new PerformEditingException(this.file.getAbsolutePath(), lasterror); |
198 |
} |
199 |
if (listWriter != null) { |
200 |
try {
|
201 |
listWriter.close(); |
202 |
} catch (Exception ex) { |
203 |
// Ignore error
|
204 |
} |
205 |
listWriter = null;
|
206 |
} |
207 |
if (writer != null) { |
208 |
try {
|
209 |
writer.close(); |
210 |
} catch (Exception ex) { |
211 |
// Ignore error
|
212 |
} |
213 |
writer = null;
|
214 |
} |
215 |
} |
216 |
|
217 |
public Envelope getEnvelope() {
|
218 |
return this.envelope; |
219 |
} |
220 |
} |