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 / CSVStoreParameters.java @ 41617
History | View | Annotate | Download (13.4 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.csv; |
25 |
|
26 |
import java.io.File; |
27 |
import java.util.Locale; |
28 |
import org.apache.commons.lang3.BooleanUtils; |
29 |
|
30 |
import org.apache.commons.lang3.StringEscapeUtils; |
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.cresques.cts.IProjection; |
33 |
import org.gvsig.fmap.dal.FileHelper; |
34 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
35 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
36 |
import org.gvsig.fmap.dal.spi.AbstractDataParameters; |
37 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
40 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
41 |
import org.gvsig.tools.dynobject.DynObject; |
42 |
import org.slf4j.Logger; |
43 |
import org.slf4j.LoggerFactory; |
44 |
import org.supercsv.prefs.CsvPreference; |
45 |
import org.supercsv.quote.AlwaysQuoteMode; |
46 |
import org.supercsv.quote.NormalQuoteMode; |
47 |
import org.supercsv.quote.QuoteMode; |
48 |
|
49 |
public class CSVStoreParameters extends AbstractDataParameters implements |
50 |
OpenDataStoreParameters, FilesystemStoreParameters { |
51 |
|
52 |
private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class); |
53 |
|
54 |
public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters"; |
55 |
|
56 |
private static final String FILE = "file"; |
57 |
private static final String IGNOREERRORS = "ignoreErrors"; |
58 |
private static final String PROFILE = "profile"; |
59 |
private static final String QUOTEPOLICY = "quotePolicy"; |
60 |
private static final String QUOTECHAR = "quoteCharacter"; |
61 |
private static final String RECORDSEPARATOR = "recordSeparator"; |
62 |
private static final String DELIMITER = "delimiter"; |
63 |
private static final String COMMENTSTARTMARKER = "commentStartMarker"; |
64 |
private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
65 |
|
66 |
//private static final String ESCAPECHARACTER = "escapeCharacter";
|
67 |
private static final String HEADER = "header"; |
68 |
private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
69 |
|
70 |
//private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
|
71 |
private static final String CRS = "CRS"; |
72 |
private static final String FIELDTYPES = "fieldtypes"; |
73 |
// private static final String NULLTO = "nullTo";
|
74 |
private static final String CHARSET = "charset"; // Default "UTF-8" |
75 |
private static final String LOCALE = "locale"; |
76 |
|
77 |
private DelegatedDynObject parameters;
|
78 |
|
79 |
public CSVStoreParameters() {
|
80 |
this(PARAMETERS_DEFINITION_NAME);
|
81 |
} |
82 |
|
83 |
protected CSVStoreParameters(String parametersDefinitionName) { |
84 |
this(parametersDefinitionName, CSVStoreProvider.NAME);
|
85 |
} |
86 |
|
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 |
} |
92 |
|
93 |
public String getDataStoreName() { |
94 |
return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
95 |
} |
96 |
|
97 |
public String getDescription() { |
98 |
return this.getDynClass().getDescription(); |
99 |
} |
100 |
|
101 |
protected DelegatedDynObject getDelegatedDynObject() {
|
102 |
return parameters;
|
103 |
} |
104 |
|
105 |
public boolean isValid() { |
106 |
if ( getFileName(this) == null ) { |
107 |
return false; |
108 |
} |
109 |
return true; |
110 |
} |
111 |
|
112 |
public File getFile() { |
113 |
return (File) this.getDynValue(FILE); |
114 |
} |
115 |
|
116 |
public void setFile(File file) { |
117 |
this.setDynValue(FILE, file);
|
118 |
} |
119 |
|
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 |
} |
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 |
} |
150 |
|
151 |
static IProjection getCRS(DynObject dynobj) {
|
152 |
return (IProjection) dynobj.getDynValue(CRS);
|
153 |
} |
154 |
|
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 |
} |
162 |
|
163 |
static File getFile(DynObject dynobj) { |
164 |
File f = (File) dynobj.getDynValue(FILE); |
165 |
return f;
|
166 |
} |
167 |
|
168 |
public static String getRecordSeparator(DynObject dynobj) { |
169 |
String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
170 |
return StringEscapeUtils.unescapeJava(s);
|
171 |
} |
172 |
|
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 |
} |
203 |
|
204 |
public static String getCommentStartMarker(DynObject dynobj) { |
205 |
String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
206 |
return StringEscapeUtils.unescapeJava(s);
|
207 |
} |
208 |
|
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 |
|
409 |
} |