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

History | View | Annotate | Download (8.17 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(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
              }
179
          } else {
180
            values[i] = (String) this.convert.coerce(value, descriptor.getCoercionContext());
181
          }
182
        } catch (CoercionException|GeometryOperationNotSupportedException|GeometryOperationException e) {
183
          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
            }
194
          }
195
        }
196
        if( values[i]!=null ) {
197
            values[i] = values[i].replaceAll("\\n","\\\\n");
198
            values[i] = values[i].replaceAll("\\r","\\\\r");
199
        }
200
      }
201
    }
202
    try {
203
      this.listWriter.write(values);
204
    } 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
        }
211
      }
212
    }
213

    
214
  }
215

    
216
  public void end() throws PerformEditingException {
217
    if (this.errorcounts > 0) {
218
      throw new PerformEditingException(this.file.getAbsolutePath(), lasterror);
219
    }
220
    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
}