Revision 41617

View differences:

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/resources/org/gvsig/fmap/dal/store/csv/CSVParameters.xml
63 63
        <field name="escapeCharacter" type="string" mandatory="false" defaultValue="" group="Advanced">
64 64
          <description>Sets the escape character of the format to the specified character</description>
65 65
        </field>
66
         <field name="header" type="string" mandatory="false" defaultValue="" group="Advanced">
66
        <field name="header" type="string" mandatory="false" defaultValue="" group="Advanced">
67 67
          <description>The header or empty if parsed automatically</description>
68 68
        </field>
69
        <field name="skipLines" label="Number of lines to skip" type="integer" mandatory="false" defaultValue="0" group="Advanced">
70
          <description>The number of lines to skip at begin of file</description>
71
        </field>
72
        <field name="fieldsDefinition" label="Fields definition" type="string" mandatory="false" defaultValue="" group="Advanced">
73
          <description>If set use this to define fields of fixed length instead of use a separator.\nThe format is a list of values separateds by spaces of the form N:M\nwhere N is the position of the first character of field, and M the last.\nCan accept N: to use form the N character to the end of line.</description>
74
        </field>
69 75
        <field name="surroundingSpacesNeedQuotes" type="boolean" mandatory="false" defaultValue="false" group="Advanced">
70 76
          <description>the surroundingSpacesNeedQuotes flag.</description>
71 77
        </field>
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/CSVStoreParameters.java
10 10
 *
11 11
 * This program is distributed in the hope that it will be useful,
12 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14
 * GNU General Public License for more details.
15 15
 *
16 16
 * You should have received a copy of the GNU General Public License
17 17
 * along with this program; if not, write to the Free Software
18 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
19
 * MA 02110-1301, USA.
20 20
 *
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
......
25 25

  
26 26
import java.io.File;
27 27
import java.util.Locale;
28
import org.apache.commons.lang3.BooleanUtils;
28 29

  
29 30
import org.apache.commons.lang3.StringEscapeUtils;
31
import org.apache.commons.lang3.StringUtils;
30 32
import org.cresques.cts.IProjection;
31 33
import org.gvsig.fmap.dal.FileHelper;
32 34
import org.gvsig.fmap.dal.OpenDataStoreParameters;
......
45 47
import org.supercsv.quote.QuoteMode;
46 48

  
47 49
public class CSVStoreParameters extends AbstractDataParameters implements
48
		OpenDataStoreParameters, FilesystemStoreParameters {
50
        OpenDataStoreParameters, FilesystemStoreParameters {
49 51

  
50
	private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class);
51
	
52
    private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class);
53

  
52 54
    public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters";
53 55

  
54 56
    private static final String FILE = "file";
......
60 62
    private static final String DELIMITER = "delimiter";
61 63
    private static final String COMMENTSTARTMARKER = "commentStartMarker";
62 64
    private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
63
    
65

  
64 66
    //private static final String ESCAPECHARACTER = "escapeCharacter";
65
    
66 67
    private static final String HEADER = "header";
67 68
    private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes";
68
    
69

  
69 70
    //private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
70 71
    private static final String CRS = "CRS";
71 72
    private static final String FIELDTYPES = "fieldtypes";
72 73
//    private static final String NULLTO = "nullTo";
73 74
    private static final String CHARSET = "charset"; // Default "UTF-8"
74
    private static final String LOCALE = "locale"; 
75
    private static final String LOCALE = "locale";
75 76

  
76
    
77
    private DelegatedDynObject parameters;
77 78

  
78
	private DelegatedDynObject parameters;
79
    public CSVStoreParameters() {
80
        this(PARAMETERS_DEFINITION_NAME);
81
    }
79 82

  
80
	
81
	public CSVStoreParameters() {
82
		this(PARAMETERS_DEFINITION_NAME);
83
	}
83
    protected CSVStoreParameters(String parametersDefinitionName) {
84
        this(parametersDefinitionName, CSVStoreProvider.NAME);
85
    }
84 86

  
85
	protected CSVStoreParameters(String parametersDefinitionName) {
86
		this(parametersDefinitionName, CSVStoreProvider.NAME);
87
	}
87
    public CSVStoreParameters(String parametersDefinitionName, String name) {
88
        super();
89
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
90
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
91
    }
88 92

  
89
	public CSVStoreParameters(String parametersDefinitionName, String name) {
90
		super();
91
		this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
92
		this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
93
	}
93
    public String getDataStoreName() {
94
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
95
    }
94 96

  
95
	public String getDataStoreName() {
96
		return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
97
	}
97
    public String getDescription() {
98
        return this.getDynClass().getDescription();
99
    }
98 100

  
99
	public String getDescription() {
100
		return this.getDynClass().getDescription();
101
	}
101
    protected DelegatedDynObject getDelegatedDynObject() {
102
        return parameters;
103
    }
102 104

  
103
	protected DelegatedDynObject getDelegatedDynObject() {
104
		return parameters;
105
	}
105
    public boolean isValid() {
106
        if ( getFileName(this) == null ) {
107
            return false;
108
        }
109
        return true;
110
    }
106 111

  
107
	public boolean isValid() {
108
		if (getFileName(this) == null) {
109
			return false;
110
		}
111
		return true;
112
	}
112
    public File getFile() {
113
        return (File) this.getDynValue(FILE);
114
    }
113 115

  
114
	public File getFile() {
115
		return (File) this.getDynValue(FILE);
116
	}
116
    public void setFile(File file) {
117
        this.setDynValue(FILE, file);
118
    }
117 119

  
118
	public void setFile(File file) {
119
		this.setDynValue(FILE, file);
120
	}
121
	
120
    public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
121
        String s = (String) dynobj.getDynValue(PROFILE);
122
        if ( "NONE".equalsIgnoreCase(s) ) {
123
            return null;
124
        }
125
        if ( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) {
126
            return CsvPreference.STANDARD_PREFERENCE;
127
        }
128
        if ( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) {
129
            return CsvPreference.EXCEL_PREFERENCE;
130
        }
131
        if ( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) {
132
            return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
133
        }
134
        if ( "TAB_PREFERENCE".equalsIgnoreCase(s) ) {
135
            return CsvPreference.TAB_PREFERENCE;
136
        }
137
        return null;
138
    }
122 139

  
140
    public static QuoteMode getQuoteMode(DynObject dynobj) {
141
        String s = (String) dynobj.getDynValue(QUOTEPOLICY);
142
        if ( "AlwaysQuoteMode".equalsIgnoreCase(s) ) {
143
            return new AlwaysQuoteMode();
144
        }
145
        if ( "NormalQuoteMode".equalsIgnoreCase(s) ) {
146
            return new NormalQuoteMode();
147
        }
148
        return null;
149
    }
123 150

  
124
	static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
125
		String s = (String) dynobj.getDynValue(PROFILE);
126
		if( "NONE".equalsIgnoreCase(s) ) {
127
			return null;
128
		}
129
		if( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) {
130
			return CsvPreference.STANDARD_PREFERENCE;
131
		}
132
		if( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) {
133
			return CsvPreference.EXCEL_PREFERENCE;
134
		}
135
		if( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) {
136
			return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
137
		}
138
		if( "TAB_PREFERENCE".equalsIgnoreCase(s) ) {
139
			return CsvPreference.TAB_PREFERENCE;
140
		}
141
		return null ;
142
	}
143
	
144
	static QuoteMode getQuoteMode(DynObject dynobj) {
145
		String s = (String) dynobj.getDynValue(QUOTEPOLICY);
146
		if( "AlwaysQuoteMode".equalsIgnoreCase(s) ) {
147
			return new AlwaysQuoteMode();
148
		}
149
		if( "NormalQuoteMode".equalsIgnoreCase(s) ) {
150
			return new NormalQuoteMode();
151
		}
152
		return null;
153
	}
154
	
155
	static IProjection getCRS(DynObject dynobj) {
156
		return (IProjection) dynobj.getDynValue(CRS);
157
	}
151
    static IProjection getCRS(DynObject dynobj) {
152
        return (IProjection) dynobj.getDynValue(CRS);
153
    }
158 154

  
159
	static String getFileName(DynObject dynobj) {
160
		File f = (File) dynobj.getDynValue(FILE);
161
		if( f == null ) {
162
			return null;
163
		}
164
		return f.getPath();
165
	}
155
    static String getFileName(DynObject dynobj) {
156
        File f = (File) dynobj.getDynValue(FILE);
157
        if ( f == null ) {
158
            return null;
159
        }
160
        return f.getPath();
161
    }
166 162

  
167
	static File getFile(DynObject dynobj) {
168
		File f = (File) dynobj.getDynValue(FILE);
169
		return f;
170
	}
163
    static File getFile(DynObject dynobj) {
164
        File f = (File) dynobj.getDynValue(FILE);
165
        return f;
166
    }
171 167

  
172
	static String getRecordSeparator(DynObject dynobj) {
173
		String s = (String) dynobj.getDynValue(RECORDSEPARATOR);
174
		return StringEscapeUtils.unescapeJava(s);
175
	}
176
	
177
	static Locale getLocale(DynObject dynobj) {
178
		try {
179
			String s = (String) dynobj.getDynValue(LOCALE);
180
			if( s.trim().length()==0 ) {
181
				return null;
182
			}
183
                        if( "DEFAULT".equalsIgnoreCase(s.trim()) ) {
184
                            return Locale.getDefault();
185
                        }
186
                        Locale locale = null;
187
			// locale = Locale.forLanguageTag(s); // Since java 1.7
188
                        String[] ss = s.split("-");
189
                        switch(ss.length) {
190
                        case 1:
191
                           locale = new Locale(ss[0]);
192
                            break;
193
                        case 2:
194
                           locale = new Locale(ss[0],ss[1]);
195
                           break;
196
                        case 3:
197
                        default:
198
                           locale = new Locale(ss[0],ss[1],ss[2]);
199
                           break;
200
                        }
201
                        return locale;
202
		} catch( Exception ex) {
203
                        logger.warn("Can't get locale from CSV parameters.", ex);
204
			return null;
205
		}
206
	}
207
	
208
	static String getCommentStartMarker(DynObject dynobj) {
209
		String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER);
210
		return StringEscapeUtils.unescapeJava(s);
211
	}
168
    public static String getRecordSeparator(DynObject dynobj) {
169
        String s = (String) dynobj.getDynValue(RECORDSEPARATOR);
170
        return StringEscapeUtils.unescapeJava(s);
171
    }
212 172

  
213
//	static String getEscapeCharacter(DynObject dynobj) {
214
//		String s = (String) dynobj.getDynValue(ESCAPECHARACTER);
215
//		return StringEscapeUtils.unescapeJava(s);
216
//	}
217
//	
218
	static String getQuoteCharacter(DynObject dynobj) {
219
		String s = (String) dynobj.getDynValue(QUOTECHAR);
220
		s = StringEscapeUtils.unescapeJava(s);
221
		if( isEmpty(s) ) {
222
			return null;
223
		}
224
		return s.substring(0, 1);
225
	}
226
	
227
	static String getDelimiter(DynObject dynobj) {
228
		String s = (String) dynobj.getDynValue(DELIMITER);
229
		s = StringEscapeUtils.unescapeJava(s);
230
		if( isEmpty(s) ) {
231
			return null;
232
		}
233
		return s.substring(0, 1);
234
	}
235
	
236
	static String getHeader(DynObject dynobj) {
237
		String s = (String) dynobj.getDynValue(HEADER);
238
		s = StringEscapeUtils.unescapeJava(s);
239
		if( isEmpty(s) ) {
240
			return null;
241
		}
242
		return s;
243
	}
173
    static Locale getLocale(DynObject dynobj) {
174
        try {
175
            String s = (String) dynobj.getDynValue(LOCALE);
176
            if ( s.trim().length() == 0 ) {
177
                return null;
178
            }
179
            if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) {
180
                return Locale.getDefault();
181
            }
182
            Locale locale = null;
183
            // locale = Locale.forLanguageTag(s); // Since java 1.7
184
            String[] ss = s.split("-");
185
            switch (ss.length) {
186
            case 1:
187
                locale = new Locale(ss[0]);
188
                break;
189
            case 2:
190
                locale = new Locale(ss[0], ss[1]);
191
                break;
192
            case 3:
193
            default:
194
                locale = new Locale(ss[0], ss[1], ss[2]);
195
                break;
196
            }
197
            return locale;
198
        } catch (Exception ex) {
199
            logger.warn("Can't get locale from CSV parameters.", ex);
200
            return null;
201
        }
202
    }
244 203

  
245
	static String[] getHeaders(DynObject dynobj) {
246
		String s = getHeader(dynobj);
247
		if( isEmpty(s) ) {
248
			return null;
249
		}
250
		String sep = getDelimiter(dynobj);
251
		if( sep == null ) {
252
			sep = getDelimiter(s);
253
			if( sep == null ) {
254
				// Chungo
255
				return null;
256
			}
257
		}
258
		String[] ss = s.split("["+sep+"]");
259
		return ss;
260
	}
204
    public static String getCommentStartMarker(DynObject dynobj) {
205
        String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER);
206
        return StringEscapeUtils.unescapeJava(s);
207
    }
261 208

  
262
	private static String getDelimiter(String line) {
263
		String sep = null;
264
		// Cuiaddo con los ":", los he puesto al final a proposito
265
		// ya que podian estar en la cadena para separar el size
266
		// size de cada tipo.
267
		String seps = ",;-|@#/+$%&!:";
268
		for( int i=0; i<seps.length(); i ++) {
269
			sep = seps.substring(i, 1);
270
			if( line.contains(seps.substring(i, 1))) {
271
				break;
272
			}
273
			sep = null;
274
		}
275
		return sep;
276
	}
277
//	static String getNullTo(DynObject dynobj) {
278
//		String s = (String) dynobj.getDynValue(NULLTO);
279
//		return StringEscapeUtils.unescapeJava(s);
280
//	}
281
	
282
	static String getCharset(DynObject dynobj) {
283
		String s = (String) dynobj.getDynValue(CHARSET);
284
		return StringEscapeUtils.unescapeJava(s);
285
	}
286
	
287
	static String[] getPointDimensionNames(DynObject dynobj) {
288
		String s = (String) dynobj.getDynValue("point");
289
		if( isEmpty(s) ) {
290
			return null;
291
		}
292
		return s.split(",");
293
	}
294
	
295
	static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) {
296
		Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES);
297
		if( b==null ) {
298
			return false;
299
		}
300
		return b.booleanValue();
301
	}
302
	
303
	static boolean getAutomaticTypesDetection(DynObject dynobj) {
304
		Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION);
305
		if( b==null ) {
306
			return false;
307
		}
308
		return b.booleanValue();
309
	}
310
	
311
	static boolean getIgnoreErrors(DynObject dynobj) {
312
		Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS);
313
		if( b==null ) {
314
			return false;
315
		}
316
		return b.booleanValue();
317
	}
318
	
319
//	static boolean getIgnoreEmptyLines(DynObject dynobj) {
320
//		Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES);
321
//		return b.booleanValue();
322
//	}
323
	
324
	static int[] getFieldTypes(DynObject dynobj) {
325
		String s = (String) dynobj.getDynValue(FIELDTYPES);
326
		if( isEmpty(s) ) {
327
			return null;
328
		}
329
		String sep = getDelimiter(s);
330
		if( sep == null ) {
331
			return null;
332
		}
333
		DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
334
		String fieldTypeNames[] = s.split("["+sep+"]");
335
		int fieldTypes[] = new int[fieldTypeNames.length];
336
		for( int i=0; i<fieldTypeNames.length; i++ ) {
337
			s = fieldTypeNames[i].trim();
338
			if( s.contains(":") ) {
339
				s = s.split(":")[0];
340
			}
341
			fieldTypes[i] = dataTypeManager.getType(s);
342
		}
343
		return fieldTypes;
344
	}
345
	
346
	static int[] getFieldSizes(DynObject dynobj) {
347
		String s = (String) dynobj.getDynValue(FIELDTYPES);
348
		if( isEmpty(s) ) {
349
			return null;
350
		}
351
		String sep = getDelimiter(s);
352
		if( sep == null ) {
353
			return null;
354
		}
355
		String fieldTypeNames[] = s.split("["+sep+"]");
356
		int fieldSizes[] = new int[fieldTypeNames.length];
357
		for( int i=0; i<fieldTypeNames.length; i++ ) {
358
			String fieldtype = fieldTypeNames[i].trim();
359
			if( fieldtype .contains(":") ) {
360
				try {
361
					s = fieldtype .split(":")[1];
362
					fieldSizes[i] = Integer.parseInt(s);
363
				} catch(Exception ex) {
364
					logger.warn("Can't get size of field "+i+" ("+fieldtype +").",ex);
365
				}
366
			} else {
367
				fieldSizes[i] = 0;
368
			}
369
		}
370
		return fieldSizes;
371
	}
372
	
373
	static private boolean isEmpty(String s) {
374
		if( s == null ) {
375
			return true;
376
		}
377
		if( s.trim().length() == 0 ) {
378
			return true;
379
		}
380
		return false;
381
	}
209
    public static String getQuoteCharacter(DynObject dynobj) {
210
        String s = (String) dynobj.getDynValue(QUOTECHAR);
211
        s = StringEscapeUtils.unescapeJava(s);
212
        if ( StringUtils.isBlank(s) ) {
213
            return null;
214
        }
215
        return s.substring(0, 1);
216
    }
217

  
218
    public static String getDelimiter(DynObject dynobj) {
219
        String s = (String) dynobj.getDynValue(DELIMITER);
220
        s = StringEscapeUtils.unescapeJava(s);
221
        if ( StringUtils.isBlank(s) ) {
222
            return null;
223
        }
224
        return s.substring(0, 1);
225
    }
226

  
227
    static String getHeader(DynObject dynobj) {
228
        String s = (String) dynobj.getDynValue(HEADER);
229
        s = StringEscapeUtils.unescapeJava(s);
230
        if ( StringUtils.isBlank(s) ) {
231
            return null;
232
        }
233
        return s;
234
    }
235

  
236
    static String[] getHeaders(DynObject dynobj) {
237
        String s = getHeader(dynobj);
238
        if ( StringUtils.isBlank(s) ) {
239
            return null;
240
        }
241
        String sep = getDelimiter(dynobj);
242
        if ( sep == null ) {
243
            sep = getDelimiter(s);
244
            if ( sep == null ) {
245
                // Chungo
246
                return null;
247
            }
248
        }
249
        String[] ss = s.split("[" + sep + "]");
250
        return ss;
251
    }
252

  
253
    private static String getDelimiter(String line) {
254
        String sep = null;
255
        // Cuiaddo con los ":", los he puesto al final a proposito
256
        // ya que podian estar en la cadena para separar el size
257
        // size de cada tipo.
258
        String seps = ",;-|@#/+$%&!:";
259
        for ( int i = 0; i < seps.length(); i++ ) {
260
            sep = seps.substring(i, 1);
261
            if ( line.contains(seps.substring(i, 1)) ) {
262
                break;
263
            }
264
            sep = null;
265
        }
266
        return sep;
267
    }
268

  
269
    static String getCharset(DynObject dynobj) {
270
        String s = (String) dynobj.getDynValue(CHARSET);
271
        return StringEscapeUtils.unescapeJava(s);
272
    }
273

  
274
    static String[] getPointDimensionNames(DynObject dynobj) {
275
        String s = (String) dynobj.getDynValue("point");
276
        if ( StringUtils.isBlank(s) ) {
277
            return null;
278
        }
279
        return s.split(",");
280
    }
281

  
282
    public static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) {
283
        Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES);
284
        return BooleanUtils.isTrue(b);
285
    }
286

  
287
    static boolean getAutomaticTypesDetection(DynObject dynobj) {
288
        Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION);
289
        return BooleanUtils.isTrue(b);
290
    }
291

  
292
    static boolean getIgnoreErrors(DynObject dynobj) {
293
        Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS);
294
        return BooleanUtils.isTrue(b);
295
    }
296

  
297
    static int[] getFieldTypes(DynObject dynobj) {
298
        String s = (String) dynobj.getDynValue(FIELDTYPES);
299
        if ( StringUtils.isBlank(s) ) {
300
            return null;
301
        }
302
        String sep = getDelimiter(s);
303
        if ( sep == null ) {
304
            return null;
305
        }
306
        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
307
        String fieldTypeNames[] = s.split("[" + sep + "]");
308
        int fieldTypes[] = new int[fieldTypeNames.length];
309
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
310
            s = fieldTypeNames[i].trim();
311
            if ( s.contains(":") ) {
312
                s = s.split(":")[0];
313
            }
314
            fieldTypes[i] = dataTypeManager.getType(s);
315
        }
316
        return fieldTypes;
317
    }
318

  
319
    static int[] getFieldSizes(DynObject dynobj) {
320
        String s = (String) dynobj.getDynValue(FIELDTYPES);
321
        if ( StringUtils.isBlank(s) ) {
322
            return null;
323
        }
324
        String sep = getDelimiter(s);
325
        if ( sep == null ) {
326
            return null;
327
        }
328
        String fieldTypeNames[] = s.split("[" + sep + "]");
329
        int fieldSizes[] = new int[fieldTypeNames.length];
330
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
331
            String fieldtype = fieldTypeNames[i].trim();
332
            if ( fieldtype.contains(":") ) {
333
                try {
334
                    s = fieldtype.split(":")[1];
335
                    fieldSizes[i] = Integer.parseInt(s);
336
                } catch (Exception ex) {
337
                    logger.warn("Can't get size of field " + i + " (" + fieldtype + ").", ex);
338
                }
339
            } else {
340
                fieldSizes[i] = 0;
341
            }
342
        }
343
        return fieldSizes;
344
    }
345

  
346
    static int getSkipLines(DynObject dynobj) {
347
        Integer n = (Integer) dynobj.getDynValue("skipLines");
348
        if ( n == null ) {
349
            return 0;
350
        }
351
        return new Integer(n);
352
    }
353

  
354
    static String getRawFieldsDefinition(DynObject dynobj) {
355
        String s = (String) dynobj.getDynValue("fieldsDefinition");
356
        if ( StringUtils.isBlank(s) ) {
357
            return null;
358
        }
359
        return s.trim();
360
    }
361

  
362
    public static class FieldDefinition {
363

  
364
        private int start;
365
        private int end;
366

  
367
        public FieldDefinition(String def) {
368
            def = def.trim();
369
            String[] ss = def.split(":");
370
            this.start = Integer.parseInt(ss[0])-1;
371
            if ( ss.length < 2 ) {
372
                this.end = -1;
373
            } else {
374
                this.end = Integer.parseInt(ss[1])-1;
375
            }
376
        }
377

  
378
        public int getStart() {
379
            return this.start;
380
        }
381

  
382
        public int getEnd() {
383
            return this.end;
384
        }
385

  
386
        public boolean getToEndOfLine() {
387
            return this.end == -1;
388
        }
389
    }
390

  
391
    public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) {
392
        String definition = getRawFieldsDefinition(dynobj);
393
        if ( definition == null ) {
394
            return null;
395
        }
396
        int i=0;
397
        try {
398
            String[] defs = StringUtils.split(definition);
399
            FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length];
400
            for ( i = 0; i < defs.length; i++ ) {
401
                fieldsDefinition[i] = new FieldDefinition(defs[i]);
402
            }
403
            return fieldsDefinition;
404
        } catch (Exception ex) {
405
            throw  new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+").");
406
        }
407
    }
408

  
382 409
}
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/simplereaders/SimpleReader.java
1
package org.gvsig.fmap.dal.store.csv.simplereaders;
2

  
3
import java.io.IOException;
4
import java.util.List;
5

  
6
public interface SimpleReader {
7

  
8
    public String[] getHeader() throws IOException;
9

  
10
    public List<String> read() throws IOException;
11

  
12
    public void close() throws IOException;
13

  
14
    public List<String> skip(int lines) throws IOException;
15
}
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/simplereaders/CSVReader.java
1
package org.gvsig.fmap.dal.store.csv.simplereaders;
2

  
3
import java.io.FileReader;
4
import java.io.IOException;
5
import java.util.List;
6
import org.apache.commons.lang3.StringUtils;
7
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters;
8
import org.gvsig.tools.dynobject.DynObject;
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11
import org.supercsv.comment.CommentStartsWith;
12
import org.supercsv.io.CsvListReader;
13
import org.supercsv.prefs.CsvPreference;
14
import org.supercsv.quote.QuoteMode;
15

  
16
public class CSVReader implements SimpleReader {
17

  
18
    //
19
    // http://supercsv.sourceforge.net/examples_reading.html
20
    // http://supercsv.sourceforge.net/apidocs/index.html
21
    //
22
    private static final Logger logger = LoggerFactory.getLogger(CSVReader.class);
23
    CsvListReader reader;
24
    private CSVStoreParameters parameters;
25

  
26
    public CSVReader(FileReader in, CSVStoreParameters parameters) {
27
        this.parameters = parameters;
28
        CsvPreference preferences = getCSVPreferences();
29
        this.reader = new CsvListReader(in, preferences);
30
    }    
31
    
32
    public CSVReader(CSVStoreParameters parameters) {
33
        this.parameters = parameters;
34
        CsvPreference preferences = getCSVPreferences();
35
        this.reader = null;
36
    }
37

  
38
    public CSVStoreParameters getParameters() {
39
        return this.parameters;
40
    }
41

  
42
    public String[] getHeader() throws IOException {
43
        return this.reader.getHeader(true);
44
    }
45

  
46
    public List<String> read() throws IOException {
47
        return reader.read();
48
    }
49

  
50
    public void close() throws IOException {
51
        this.reader.close();
52
    }
53

  
54
    public List<String> skip(int lines) throws IOException {
55
        List<String> row = null;
56
        for ( int i = 0; i < lines; i++ ) {
57
            row = reader.read();
58
        }
59
        return row;
60
    }
61

  
62
    public CsvPreference getCSVPreferences() {
63
        try {
64
            String s = null;
65
            char quoteChar;
66
            int delimiterChar;
67
            String endOfLineSymbols;
68

  
69
            DynObject params = this.getParameters();
70

  
71
            CsvPreference.Builder builder = null;
72

  
73
            CsvPreference defaultPreference = CSVStoreParameters
74
                    .getPredefinedCSVPreferences(params);
75
            if ( defaultPreference == null ) {
76
                defaultPreference = CsvPreference.STANDARD_PREFERENCE;
77
            }
78

  
79
            endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params);
80
            if ( StringUtils.isBlank(endOfLineSymbols) ) {
81
                endOfLineSymbols = defaultPreference.getEndOfLineSymbols();
82
            }
83
            s = CSVStoreParameters.getQuoteCharacter(params);
84
            if ( StringUtils.isBlank(s) ) {
85
                quoteChar = (char) defaultPreference.getQuoteChar();
86
            } else {
87
                quoteChar = s.charAt(0);
88
            }
89
            s = CSVStoreParameters.getDelimiter(params);
90
            if ( StringUtils.isBlank(s) ) {
91
                delimiterChar = defaultPreference.getDelimiterChar();
92
            } else {
93
                delimiterChar = s.charAt(0);
94
            }
95

  
96
            builder = new CsvPreference.Builder(quoteChar, delimiterChar,
97
                    endOfLineSymbols);
98

  
99
            s = CSVStoreParameters.getCommentStartMarker(params);
100
            if ( !StringUtils.isBlank(s) ) {
101
                CommentStartsWith cs = new CommentStartsWith(s);
102
                builder.skipComments(cs);
103
            }
104

  
105
            builder.surroundingSpacesNeedQuotes(CSVStoreParameters
106
                    .getSurroundingSpacesNeedQuotes(params));
107
            QuoteMode quoteMode = CSVStoreParameters.getQuoteMode(params);
108
            if ( quoteMode != null ) {
109
                builder.useQuoteMode(quoteMode);
110
            }
111
            return builder.build();
112
        } catch (Exception e) {
113
            logger.warn("Can't make preferences for CSV '" + getFullFileName()
114
                    + "'.", e);
115
            return null;
116
        }
117
    }
118
    
119
    private String getFullFileName() {
120
        // Usar solo para mostrar mensajes en el logger.
121
        String s = "(unknow)";
122
        try {
123
            s = getParameters().getFile().getAbsolutePath();
124
        } catch (Exception e2) {
125
            s = "(unknow)";
126
        }
127
        return s;        
128
    }
129

  
130
}
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/simplereaders/FixedLenReader.java
1

  
2
package org.gvsig.fmap.dal.store.csv.simplereaders;
3

  
4
import java.io.BufferedReader;
5
import java.io.FileReader;
6
import java.io.IOException;
7
import java.util.ArrayList;
8
import java.util.List;
9
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters;
10

  
11

  
12
public class FixedLenReader implements SimpleReader {
13

  
14
    BufferedReader reader = null;
15
    CSVStoreParameters.FieldDefinition fieldsDefinition[] = null;
16
    List fields = null;
17
    private final int fieldCount;
18
    private final CSVStoreParameters parameters;
19
    private String commentStartMarker;
20

  
21
    public FixedLenReader(FileReader reader, CSVStoreParameters parameters) {
22
        this.parameters = parameters;
23
        this.reader = new BufferedReader(reader);
24
        this.fieldsDefinition = CSVStoreParameters.getFieldsDefinition(parameters);
25
        this.fieldCount = this.fieldsDefinition.length;
26
        this.fields = new ArrayList(this.fieldCount);
27
        for( int i=0; i<this.fieldCount; i++ ) {
28
            this.fields.add(null);
29
        }
30
        this.commentStartMarker = CSVStoreParameters.getCommentStartMarker(parameters);
31
    }
32

  
33
    public String[] getHeader() throws IOException {
34
        String[] header = new String[this.fieldCount];
35
        for( int i=0; i<this.fieldCount; i++ ) {
36
            header[i] = Character.toString((char) (i+'A')).toUpperCase();
37
        }
38
        return header;
39
    }
40

  
41
    public List<String> read() throws IOException {
42
        String line = this.reader.readLine();
43
        while( line!=null && line.startsWith(this.commentStartMarker) ) {
44
            line = this.reader.readLine();
45
        }
46
        return this.parse(line);
47
    }
48

  
49
    public List<String> parse(String line) throws IOException {
50
        if( line == null ) {
51
            return null;
52
        }
53
        for ( int i = 0; i < this.fieldCount; i++ ) {
54
            CSVStoreParameters.FieldDefinition fieldDefinition = this.fieldsDefinition[i];
55
            String value;
56
            if ( fieldDefinition.getToEndOfLine() ) {
57
                value = line.substring(fieldDefinition.getStart());
58
            } else {
59
                value = line.substring(
60
                        fieldDefinition.getStart(),
61
                        fieldDefinition.getEnd()
62
                );
63
            }
64
            this.fields.set(i, value);
65
        }
66
        return this.fields;
67
    }
68

  
69
    public void close() throws IOException {
70
        this.reader.close();
71
        this.reader = null;
72
    }
73

  
74
    public List<String> skip(int lines) throws IOException {
75
        String line = null;
76
        for ( int i = 0; i < lines; i++ ) {
77
            line = this.reader.readLine();
78
            while( line!=null && line.startsWith(this.commentStartMarker) ) {
79
                line = this.reader.readLine();
80
            }
81
        }
82
        return this.parse(line);
83
    }
84

  
85
}
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/CSVStoreProvider.java
10 10
 *
11 11
 * This program is distributed in the hope that it will be useful,
12 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14
 * GNU General Public License for more details.
15 15
 *
16 16
 * You should have received a copy of the GNU General Public License
17 17
 * along with this program; if not, write to the Free Software
18 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
19
 * MA 02110-1301, USA.
20 20
 *
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
......
69 69
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
70 70
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
71 71
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
72
import org.gvsig.fmap.dal.store.csv.simplereaders.CSVReader;
73
import org.gvsig.fmap.dal.store.csv.simplereaders.FixedLenReader;
74
import org.gvsig.fmap.dal.store.csv.simplereaders.SimpleReader;
72 75
import org.gvsig.fmap.geom.Geometry;
73 76
import org.gvsig.fmap.geom.GeometryLocator;
74 77
import org.gvsig.fmap.geom.GeometryManager;
......
83 86
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
84 87
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
85 88
import org.gvsig.tools.dispose.DisposableIterator;
86
import org.gvsig.tools.dynobject.DynObject;
87 89
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
88 90
import org.gvsig.tools.evaluator.AbstractEvaluator;
89 91
import org.gvsig.tools.evaluator.EvaluatorData;
......
98 100
import org.gvsig.tools.visitor.Visitor;
99 101
import org.slf4j.Logger;
100 102
import org.slf4j.LoggerFactory;
101
import org.supercsv.comment.CommentStartsWith;
102
import org.supercsv.io.CsvListReader;
103 103
import org.supercsv.io.CsvListWriter;
104 104
import org.supercsv.prefs.CsvPreference;
105
import org.supercsv.quote.QuoteMode;
106 105

  
107 106
public class CSVStoreProvider extends AbstractMemoryStoreProvider implements
108
ResourceConsumer {
107
        ResourceConsumer {
108

  
109 109
    private static final Logger logger = LoggerFactory.getLogger(CSVStoreProvider.class);
110 110

  
111 111
    public static final String NAME = "CSV";
......
120 120
    private boolean need_calculate_envelope = false;
121 121
    private SimpleTaskStatus taskStatus;
122 122

  
123

  
124 123
    public CSVStoreProvider(CSVStoreParameters parameters,
125
        DataStoreProviderServices storeServices) throws InitializeException {
124
            DataStoreProviderServices storeServices) throws InitializeException {
126 125
        super(
127
            parameters, 
128
            storeServices,
129
            FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
126
                parameters,
127
                storeServices,
128
                FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
130 129
        );
131 130

  
132 131
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
......
136 135

  
137 136
        File file = getCSVParameters().getFile();
138 137
        resource = this.createResource(
139
            FileResource.NAME,
140
            new Object[] { file.getAbsolutePath() }
138
                FileResource.NAME,
139
                new Object[]{file.getAbsolutePath()}
141 140
        );
142 141

  
143 142
        resource.addConsumer(this);
......
157 156
    }
158 157

  
159 158
    private String getFullFileName() {
160
    	// Usar solo para mostrar mensajes en el logger.
161
		String s = "(unknow)";
162
		try { 
163
			s = getCSVParameters().getFile().getAbsolutePath();
164
		} catch(Exception e2) {
165
			s = "(unknow)";
166
		}
167
		return s;
159
        // Usar solo para mostrar mensajes en el logger.
160
        String s = "(unknow)";
161
        try {
162
            s = getCSVParameters().getFile().getAbsolutePath();
163
        } catch (Exception e2) {
164
            s = "(unknow)";
165
        }
166
        return s;
168 167
    }
169
    
168

  
170 169
    public void open() throws OpenException {
171
        if (this.data != null) {
170
        if ( this.data != null ) {
172 171
            return;
173 172
        }
174 173
        this.data = new ArrayList<FeatureProvider>();
175 174
        resource.setData(new HashMap());
176 175
        counterNewsOIDs = 0;
177
		try {
178
			loadFeatures();
179
		} catch (RuntimeException e) {
180
			logger.warn("Can't load features from CSV '"+getFullFileName()+"'.", e);
181
			throw e;
182
		} catch (Exception e) {
183
			logger.warn("Can't load features from CSV '"+getFullFileName()+"'.", e);
184
			throw new RuntimeException(e);
185
		}
176
        try {
177
            loadFeatures();
178
        } catch (RuntimeException e) {
179
            logger.warn("Can't load features from CSV '" + getFullFileName() + "'.", e);
180
            throw e;
181
        } catch (Exception e) {
182
            logger.warn("Can't load features from CSV '" + getFullFileName() + "'.", e);
183
            throw new RuntimeException(e);
184
        }
186 185
    }
187 186

  
188 187
    public DataServerExplorer getExplorer() throws ReadException {
......
190 189
        FilesystemServerExplorerParameters params;
191 190
        try {
192 191
            params = (FilesystemServerExplorerParameters) manager
193
            .createServerExplorerParameters(FilesystemServerExplorer.NAME);
192
                    .createServerExplorerParameters(FilesystemServerExplorer.NAME);
194 193
            params.setRoot(this.getCSVParameters().getFile().getParent());
195
            return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
194
            return manager.openServerExplorer(FilesystemServerExplorer.NAME, params);
196 195
        } catch (DataException e) {
197 196
            throw new ReadException(this.getProviderName(), e);
198 197
        } catch (ValidateDataParametersException e) {
......
202 201
    }
203 202

  
204 203
    class Writer {
205
    	private Envelope envelope = null;
206
		private boolean calculate_envelope = false;
207
		private CsvListWriter listWriter = null;
208
		private CsvPreference csvpreferences = null;
209
		private FileWriter fwriter = null;
210
		private FeatureType ftype;
211
		private File file;
212
		private String[] values;
213
		private FeatureAttributeDescriptor[] descriptors;
214
		private Coercion convert = null;
215
		private int errorcounts =0;
216
		private Throwable lasterror = null;
217
		private Locale locale = null;
218
    	
219
    	public void initialize(File file, FeatureType ftype, CsvPreference csvpreferences) {
220
    		this.file = file;
221
    		this.ftype = ftype;
222
			this.csvpreferences = csvpreferences;
223
			this.locale = CSVStoreParameters.getLocale(getCSVParameters());
224
    		if( csvpreferences == null ) {
225
    			this.csvpreferences = CsvPreference.STANDARD_PREFERENCE;
226
    		}
227
    		if( ftype.getDefaultGeometryAttributeName() != null ) {
228
    			this.calculate_envelope = true;
229
    		}
230
    		this.descriptors = this.ftype.getAttributeDescriptors();
231
    		this.convert = ToolsLocator.getDataTypesManager().getCoercion(org.gvsig.tools.dataTypes.DataTypes.STRING);
232
    		this.errorcounts = 0;
233
    	}
234
    	
235
    	public void begin() {
236
    		try {
237
				this.fwriter = new FileWriter(file);
238
			} catch (IOException e) {
239
				logger.warn("Can't open file for write ("+file.getAbsolutePath()+").",e);
240
				throw new RuntimeException(e);
241
			}
242
    		this.listWriter = new CsvListWriter(this.fwriter,this.csvpreferences);
243
    		int n = 0;
244
    		for(int i=0; i<descriptors.length; i++ ) {
245
    			FeatureAttributeDescriptor descriptor = descriptors[i];
246
    			if( descriptor.getEvaluator()== null ) {
247
    				n++;
248
    			}
249
    		}
250
    			
251
    		String[] header = new String[n];
252
    		this.values = new String[n];
253
    		n = 0;
254
    		for(int i=0; i<descriptors.length; i++ ) {
255
    			FeatureAttributeDescriptor descriptor = descriptors[i];
256
    			if( descriptor.getEvaluator()== null ) {
257
	    			String name = descriptor.getName();
258
	    			String typeName = descriptor.getDataTypeName();
259
	    			if( descriptor.getDataType().getType() == DataTypes.STRING ) {
260
		    			header[n++] = name + "__" + typeName + "__" + descriptor.getSize();
261
	    			} else {
262
	    				header[n++] = name + "__" + typeName;
263
	    			}
264
    			}
265
    		}
204

  
205
        private Envelope envelope = null;
206
        private boolean calculate_envelope = false;
207
        private CsvListWriter listWriter = null;
208
        private CsvPreference csvpreferences = null;
209
        private FileWriter fwriter = null;
210
        private FeatureType ftype;
211
        private File file;
212
        private String[] values;
213
        private FeatureAttributeDescriptor[] descriptors;
214
        private Coercion convert = null;
215
        private int errorcounts = 0;
216
        private Throwable lasterror = null;
217
        private Locale locale = null;
218

  
219
        public void initialize(File file, FeatureType ftype, CsvPreference csvpreferences) {
220
            this.file = file;
221
            this.ftype = ftype;
222
            this.csvpreferences = csvpreferences;
223
            this.locale = CSVStoreParameters.getLocale(getCSVParameters());
224
            if ( csvpreferences == null ) {
225
                this.csvpreferences = CsvPreference.STANDARD_PREFERENCE;
226
            }
227
            if ( ftype.getDefaultGeometryAttributeName() != null ) {
228
                this.calculate_envelope = true;
229
            }
230
            this.descriptors = this.ftype.getAttributeDescriptors();
231
            this.convert = ToolsLocator.getDataTypesManager().getCoercion(org.gvsig.tools.dataTypes.DataTypes.STRING);
232
            this.errorcounts = 0;
233
        }
234

  
235
        public void begin() {
266 236
            try {
267
				listWriter.writeHeader(header);
268
			} catch (Exception e) {
269
				logger.warn("Can't write header '"+header.toString()+"' file for write ("+file.getAbsolutePath()+").",e);
270
				throw new RuntimeException(e);
271
			}
272
    	}
273
    	
274
    	public void add(FeatureProvider feature) {
275
			if (this.calculate_envelope) {
276
				Geometry geom = feature.getDefaultGeometry();
277
				if (geom != null) {
278
					if (envelope == null) {
279
						try {
280
							envelope = (Envelope) geom.getEnvelope().clone();
281
						} catch (CloneNotSupportedException e) {
282
							logger.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.",e);
283
						}
284
					} else {
285
						envelope.add(geom.getEnvelope());
286
					}
287
				}
288
			}
289
    		int n = 0;
290
    		for(int i=0; i<descriptors.length; i++ ) {
291
    			FeatureAttributeDescriptor descriptor = descriptors[i];
292
    			if( descriptor.getEvaluator()== null ) {
293
					Object value = feature.get(i);
294
    				try {
295
    					n++;
296
    					if( this.convert!=null && this.convert instanceof CoercionWithLocale ) {
297
    						values[n] = (String) ((CoercionWithLocale)this.convert).coerce(value,this.locale);
298
    					} else {
299
    						values[n] = (String) this.convert.coerce(value);
300
    					}
301
					} catch (CoercionException e) {
302
						try {
303
							values[n] = value.toString();
304
						} catch(Exception ex) {
305
							values[n] = "";
306
						}
307
						if( errorcounts++ <= 10 ) {
308
							this.lasterror = e;
309
							logger.warn("Can't convert value of field "+i+" to string in CVS file '"+getFullFileName()+"'.",e);
310
							if( errorcounts == 10 ) {
311
								logger.warn("Too many error writing CVS file '"+getFullFileName()+"', don't output more.");
312
							}
313
						} 
314
					} 
315
    			}
316
    		}
317
    		try {
318
				this.listWriter.writeHeader(values);
319
			} catch (IOException e) {
320
				if( errorcounts++ <= 10 ) {
321
					this.lasterror = e;
322
					logger.warn("Can't write values to CVS file '"+getFullFileName()+"'.",e);
323
					if( errorcounts == 10 ) {
324
						logger.warn("Too many error writing CVS file '"+getFullFileName()+"', don't output more.");
325
					}
326
				} 
327
			}
328
			
329
    	}
330
    	
331
    	public void end() throws PerformEditingException {
332
    		if( this.errorcounts>0 ) {
333
    			throw new PerformEditingException(this.file.getAbsolutePath(), lasterror);
334
    		}
335
			if( listWriter!=null ) {
336
	    		try {
337
	    			listWriter.close();
338
	    		} catch(Exception ex) {
339
	    			// Ignore error
340
	    		}
341
				listWriter=null ;
342
			}
343
			if( fwriter!=null ) {
344
	    		try {
345
	    			fwriter.close();
346
	    		} catch(Exception ex) {
347
	    			// Ignore error
348
	    		}
349
				fwriter=null ;
350
			}
351
    	}
352
    	
353
    	public Envelope getEnvelope() {
354
    		return this.envelope;
355
    	}
237
                this.fwriter = new FileWriter(file);
238
            } catch (IOException e) {
239
                logger.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e);
240
                throw new RuntimeException(e);
241
            }
242
            this.listWriter = new CsvListWriter(this.fwriter, this.csvpreferences);
243
            int n = 0;
244
            for ( int i = 0; i < descriptors.length; i++ ) {
245
                FeatureAttributeDescriptor descriptor = descriptors[i];
246
                if ( descriptor.getEvaluator() == null ) {
247
                    n++;
248
                }
249
            }
250

  
251
            String[] header = new String[n];
252
            this.values = new String[n];
253
            n = 0;
254
            for ( int i = 0; i < descriptors.length; i++ ) {
255
                FeatureAttributeDescriptor descriptor = descriptors[i];
256
                if ( descriptor.getEvaluator() == null ) {
257
                    String name = descriptor.getName();
258
                    String typeName = descriptor.getDataTypeName();
259
                    if ( descriptor.getDataType().getType() == DataTypes.STRING ) {
260
                        header[n++] = name + "__" + typeName + "__" + descriptor.getSize();
261
                    } else {
262
                        header[n++] = name + "__" + typeName;
263
                    }
264
                }
265
            }
266
            try {
267
                listWriter.writeHeader(header);
268
            } catch (Exception e) {
269
                logger.warn("Can't write header '" + header.toString() + "' file for write (" + file.getAbsolutePath() + ").", e);
270
                throw new RuntimeException(e);
271
            }
272
        }
273

  
274
        public void add(FeatureProvider feature) {
275
            if ( this.calculate_envelope ) {
276
                Geometry geom = feature.getDefaultGeometry();
277
                if ( geom != null ) {
278
                    if ( envelope == null ) {
279
                        try {
280
                            envelope = (Envelope) geom.getEnvelope().clone();
281
                        } catch (CloneNotSupportedException e) {
282
                            logger.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.", e);
283
                        }
284
                    } else {
285
                        envelope.add(geom.getEnvelope());
286
                    }
287
                }
288
            }
289
            int n = 0;
290
            for ( int i = 0; i < descriptors.length; i++ ) {
291
                FeatureAttributeDescriptor descriptor = descriptors[i];
292
                if ( descriptor.getEvaluator() == null ) {
293
                    Object value = feature.get(i);
294
                    try {
295
                        n++;
296
                        if ( this.convert != null && this.convert instanceof CoercionWithLocale ) {
297
                            values[n] = (String) ((CoercionWithLocale) this.convert).coerce(value, this.locale);
298
                        } else {
299
                            values[n] = (String) this.convert.coerce(value);
300
                        }
301
                    } catch (CoercionException e) {
302
                        try {
303
                            values[n] = value.toString();
304
                        } catch (Exception ex) {
305
                            values[n] = "";
306
                        }
307
                        if ( errorcounts++ <= 10 ) {
308
                            this.lasterror = e;
309
                            logger.warn("Can't convert value of field " + i + " to string in CVS file '" + getFullFileName() + "'.", e);
310
                            if ( errorcounts == 10 ) {
311
                                logger.warn("Too many error writing CVS file '" + getFullFileName() + "', don't output more.");
312
                            }
313
                        }
314
                    }
315
                }
316
            }
317
            try {
318
                this.listWriter.writeHeader(values);
319
            } catch (IOException e) {
320
                if ( errorcounts++ <= 10 ) {
321
                    this.lasterror = e;
322
                    logger.warn("Can't write values to CVS file '" + getFullFileName() + "'.", e);
323
                    if ( errorcounts == 10 ) {
324
                        logger.warn("Too many error writing CVS file '" + getFullFileName() + "', don't output more.");
325
                    }
326
                }
327
            }
328

  
329
        }
330

  
331
        public void end() throws PerformEditingException {
332
            if ( this.errorcounts > 0 ) {
333
                throw new PerformEditingException(this.file.getAbsolutePath(), lasterror);
334
            }
335
            if ( listWriter != null ) {
336
                try {
337
                    listWriter.close();
338
                } catch (Exception ex) {
339
                    // Ignore error
340
                }
341
                listWriter = null;
342
            }
343
            if ( fwriter != null ) {
344
                try {
345
                    fwriter.close();
346
                } catch (Exception ex) {
347
                    // Ignore error
348
                }
349
                fwriter = null;
350
            }
351
        }
352

  
353
        public Envelope getEnvelope() {
354
            return this.envelope;
355
        }
356 356
    }
357 357

  
358 358
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException {
......
366 366
                    DisposableIterator it = null;
367 367
                    try {
368 368
                        File file = (File) resource.get();
369
                        
369

  
370 370
                        Writer writer = new Writer();
371
                        writer.initialize(file,getStoreServices().getDefaultFeatureType(),getCSVPreferences());
372
                        features =
373
                            getStoreServices().getFeatureStore()
374
                            .getFeatureSet();
371
                        writer.initialize(file, getStoreServices().getDefaultFeatureType(), getCSVPreferences());
372
                        features
373
                                = getStoreServices().getFeatureStore()
374
                                .getFeatureSet();
375 375
                        List<FeatureProvider> newdata = new ArrayList<FeatureProvider>();
376 376
                        writer.begin();
377 377
                        it = features.fastIterator();
378
                        taskStatus.setRangeOfValues(0,0);
379
                        long counter=0;
380
                        while (it.hasNext()) {
378
                        taskStatus.setRangeOfValues(0, 0);
379
                        long counter = 0;
380
                        while ( it.hasNext() ) {
381 381
                            taskStatus.setCurValue(counter++);
382 382
                            FeatureProvider feature = getStoreServices().getFeatureProviderFromFeature(
383
                                (org.gvsig.fmap.dal.feature.Feature) it.next());
383
                                    (org.gvsig.fmap.dal.feature.Feature) it.next());
384 384
                            writer.add(feature);
385
                            if (feature.getOID() == null){
385
                            if ( feature.getOID() == null ) {
386 386
                                logger.warn("feature without OID");
387 387
                                feature.setOID(createNewOID());
388 388
                            }
389 389
                            newdata.add(feature);
390 390
                        }
391 391
                        data = newdata;
392
                        if (writer.getEnvelope() != null){
392
                        if ( writer.getEnvelope() != null ) {
393 393
                            envelope = writer.getEnvelope().getGeometry().getEnvelope();
394 394
                        }
395 395
                        resource.notifyChanges();
396 396
                        writer.end();
397 397
                    } finally {
398
                        if (it != null) {
398
                        if ( it != null ) {
399 399
                            it.dispose();
400 400
                        }
401
                        if (features != null) {
401
                        if ( features != null ) {
402 402
                            features.dispose();
403 403
                        }
404 404
                    }
405 405
                    return null;
406 406
                }
407

  
408
                private CsvPreference getCSVPreferences() {
409
                    CSVReader reader = new CSVReader(getCSVParameters());
410
                    return reader.getCSVPreferences();
411
                }
412
                
407 413
            });
408 414
            this.taskStatus.terminate();
409 415
        } catch (Exception e) {
......
427 433
    }
428 434

  
429 435
    public void append(FeatureProvider featureProvider) {
430
    	throw new UnsupportedOperationException();
436
        throw new UnsupportedOperationException();
431 437
    }
432 438

  
433 439
    public void beginAppend() {
434
    	throw new UnsupportedOperationException();
440
        throw new UnsupportedOperationException();
435 441
    }
436 442

  
437 443
    public void endAppend() {
438
    	throw new UnsupportedOperationException();
444
        throw new UnsupportedOperationException();
439 445
    }
440 446

  
441 447
    public void saveToState(PersistentState state) throws PersistenceException {
......
460 466

  
461 467
    public Envelope getEnvelope() throws DataException {
462 468
        this.open();
463
        if( this.envelope!= null )  {
464
        	return this.envelope;
469
        if ( this.envelope != null ) {
470
            return this.envelope;
465 471
        }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff