Statistics
| Revision:

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 @ 46627

History | View | Annotate | Download (9.02 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.nio.file.Files;
15
import java.util.Locale;
16
import org.apache.commons.lang3.ArrayUtils;
17
import org.apache.commons.lang3.StringUtils;
18
import org.gvsig.fmap.dal.DataTypes;
19
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
20
import org.gvsig.fmap.dal.feature.FeatureType;
21
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
22
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
23
import org.gvsig.fmap.dal.store.csv.simplereaders.CSVReaderSuperCSV;
24
import org.gvsig.fmap.geom.Geometry;
25
import org.gvsig.fmap.geom.GeometryLocator;
26
import org.gvsig.fmap.geom.GeometryManager;
27
import org.gvsig.fmap.geom.operation.GeometryOperationException;
28
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
29
import org.gvsig.fmap.geom.primitive.Envelope;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dataTypes.CoercionException;
32
import org.gvsig.tools.dataTypes.Coercion;
33
import org.gvsig.tools.dynobject.Tags;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
import org.supercsv.io.CsvListWriter;
37
import org.supercsv.prefs.CsvPreference;
38

    
39
/**
40
 *
41
 * @author osc
42
 */
43
@SuppressWarnings("UseSpecificCatch")
44
public class CSVFeatureWriter {
45

    
46
  private static final Logger LOGGER = LoggerFactory.getLogger(CSVFeatureWriter.class);
47

    
48
  private Envelope envelope = null;
49
  private boolean calculate_envelope = false;
50
  private CsvListWriter listWriter = null;
51
  private CsvPreference csvpreferences = null;
52
  private Writer writer = null;
53
  private FeatureType ftype;
54
  private File file;
55
  private String[] values;
56
  private FeatureAttributeDescriptor[] descriptors;
57
  private Coercion convert = null;
58
//  private CoercionContext converContext = null;
59
  private int errorcounts = 0;
60
  private Throwable lasterror = null;
61
  private CSVStoreParameters storeParameters;
62
  private String charset = null;
63
  private boolean includeMetadataInHeader;
64
    private Locale locale;
65
    private GeometryManager geometryManager;
66

    
67
  public void initialize(CSVStoreParameters storeParameters, File file, FeatureType ftype, CsvPreference csvpreferences) {
68
    this.file = file;
69
    this.ftype = ftype;
70
    this.storeParameters = storeParameters;
71
    if (csvpreferences == null) {
72
      this.csvpreferences = CSVStoreParameters.getPredefinedCSVPreferences(storeParameters);
73
    } else {
74
      this.csvpreferences = csvpreferences;
75
    }
76

    
77
    if (this.ftype != null) {
78
      this.descriptors = this.ftype.getAttributeDescriptors();
79
      if (ftype.getDefaultGeometryAttributeName() != null) {
80
        this.calculate_envelope = true;
81
      }
82
    }
83
    this.convert = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.STRING);
84
//    this.converContext = DataTypeUtils.coerceContextLocale(CSVStoreParameters.getLocale(this.storeParameters));
85
    this.errorcounts = 0;
86
    this.charset = CSVStoreParameters.getCharset(storeParameters);
87
    this.includeMetadataInHeader = CSVStoreParameters.getIncludeMetadataInHeader(storeParameters);
88
    this.locale = CSVStoreParameters.getLocale(storeParameters);
89
    this.geometryManager = GeometryLocator.getGeometryManager();
90
    }
91

    
92
  public void beginAppend() {
93
    try {
94
      FileOutputStream fos = new FileOutputStream(file, true);
95
      if (this.charset != null) {
96
        this.writer = new OutputStreamWriter(fos, this.charset);
97
      } else {
98
        this.writer = new OutputStreamWriter(fos);
99
      }
100
      this.listWriter = new CsvListWriter(this.writer, this.csvpreferences);
101
      if (ftype != null) {
102
        this.descriptors = ftype.getAttributeDescriptors();
103
        int n = 0;
104
        for (FeatureAttributeDescriptor descriptor : descriptors) {
105
          if (descriptor.getEvaluator() == null) {
106
            n++;
107
          }
108
        }
109
        this.values = new String[n];
110
      }
111

    
112
    } catch (IOException e) {
113
      LOGGER.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e);
114
      throw new RuntimeException(e);
115
    }
116
  }
117

    
118
  public void begin() {
119
    try {
120
      FileOutputStream fos = new FileOutputStream(file, false);
121
      if (this.charset != null) {
122
        this.writer = new OutputStreamWriter(fos, this.charset);
123
      } else {
124
        this.writer = new OutputStreamWriter(fos);
125
      }
126
    } catch (IOException e) {
127
      LOGGER.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e);
128
      throw new RuntimeException(e);
129
    }
130
    this.listWriter = new CsvListWriter(this.writer, this.csvpreferences);
131
    int n = 0;
132
    for (FeatureAttributeDescriptor descriptor : descriptors) {
133
      if (!descriptor.isComputed()) { //descriptor.getEvaluator() == null) {
134
        n++;
135
      }
136
    }
137

    
138
    String[] header = new String[n];
139
    this.values = new String[n];
140
    n = 0;
141
      for (FeatureAttributeDescriptor descriptor : descriptors) {
142
          if (!descriptor.isComputed()) {
143
              if (includeMetadataInHeader) {
144
                header[n++] = (String) descriptor.get("all");
145
              } else {
146
                header[n++] = (String) descriptor.getName();
147
              }
148
          }
149
      }
150
      Tags tags = this.ftype.getTags();
151
      if( tags!=null && !tags.isEmpty() ) {
152
        String s = header[0];
153
          for (String tagName : tags) {
154
              s += "/typetag/"+tagName+"="+tags.getString(tagName, "?");
155
          }
156
          header[0] = s;
157
      }
158
    try {
159
      listWriter.writeHeader(header);
160
    } catch (Exception e) {
161
      LOGGER.warn("Can't write header '" + ArrayUtils.toString(header) + "' file for write (" + file.getAbsolutePath() + ").", e);
162
      throw new RuntimeException(e);
163
    }
164
  }
165

    
166
  public void add(FeatureProvider feature) {
167
    if (this.calculate_envelope) {
168
      Geometry geom = feature.getDefaultGeometry();
169
      if (geom != null) {
170
        if (envelope == null) {
171
          try {
172
            envelope = (Envelope) geom.getEnvelope().clone();
173
          } catch (CloneNotSupportedException e) {
174
            LOGGER.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.", e);
175
          }
176
        } else {
177
          envelope.add(geom.getEnvelope());
178
        }
179
      }
180
    }
181

    
182
    for (int i = 0; i < descriptors.length; i++) {
183
      FeatureAttributeDescriptor descriptor = descriptors[i];
184
      if (descriptor.getEvaluator() == null) {
185
        Object value = feature.get(i);
186
        try {
187
          if( descriptor.getType()==DataTypes.GEOMETRY ) {
188
              if(value != null){
189
                if( StringUtils.equalsIgnoreCase("WKT", CSVStoreParameters.getGeometryFormat(storeParameters))) {
190
                  values[i] = ((Geometry)value).convertToWKT();
191
                } else {
192
                  values[i] = ((Geometry)value).convertToHexWKB();
193
                }
194
              }
195
          } else {
196
            values[i] = (String) this.convert.coerce(value, descriptor.getCoercionContext());
197
          }
198
        } catch (CoercionException|GeometryOperationNotSupportedException|GeometryOperationException e) {
199
          try {
200
            values[i] = value.toString();
201
          } catch (Exception ex) {
202
            values[i] = "";
203
          }
204
          if (errorcounts++ <= 10) {
205
            this.lasterror = e;
206
            LOGGER.warn("Can't convert value of field " + i + " to string in CVS file '" + this.file.getAbsolutePath() + "'.", e);
207
            if (errorcounts == 10) {
208
              LOGGER.warn("Too many error writing CVS file '" + this.file.getAbsolutePath() + "', don't output more.");
209
            }
210
          }
211
        }
212
        values[i] = CSVReaderSuperCSV.escapeCRLF(values[i]);
213
      }
214
    }
215
    try {
216
      this.listWriter.write(values);
217
    } catch (IOException e) {
218
      if (errorcounts++ <= 10) {
219
        this.lasterror = e;
220
        LOGGER.warn("Can't write values to CVS file '" + this.file.getAbsolutePath() + "'.", e);
221
        if (errorcounts == 10) {
222
          LOGGER.warn("Too many error writing CVS file '" + this.file.getAbsolutePath() + "', don't output more.");
223
        }
224
      }
225
    }
226

    
227
  }
228

    
229
  public void end() throws PerformEditingException {
230
    if (this.errorcounts > 0) {
231
      throw new PerformEditingException(this.file.getAbsolutePath(), lasterror);
232
    }
233
    if (listWriter != null) {
234
      try {
235
        listWriter.close();
236
      } catch (Exception ex) {
237
        // Ignore error
238
      }
239
      listWriter = null;
240
    }
241
    if (writer != null) {
242
      try {
243
        writer.close();
244
      } catch (Exception ex) {
245
        // Ignore error
246
      }
247
      writer = null;
248
    }
249
    File indexFile = null;
250
    try {
251
        indexFile = CSVReaderSuperCSV.getIndexFile(this.file);
252
        if(indexFile != null){
253
            Files.deleteIfExists(indexFile.toPath());
254
        }
255
    } catch (Exception ex){
256
        LOGGER.warn("Can't delete index file '"+(indexFile == null?"null":indexFile.getAbsolutePath())+"'", ex);
257
    }
258
  }
259

    
260
  public Envelope getEnvelope() {
261
    return this.envelope;
262
  }
263
}