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 @ 41062
History | View | Annotate | Download (9.85 KB)
1 | 40846 | jjdelcerro | /**
|
---|---|---|---|
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 | |||
28 | import org.apache.commons.lang3.StringEscapeUtils; |
||
29 | import org.cresques.cts.IProjection; |
||
30 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
31 | import org.gvsig.fmap.dal.FileHelper; |
||
32 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
||
33 | import org.gvsig.fmap.dal.spi.AbstractDataParameters; |
||
34 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
35 | import org.gvsig.tools.ToolsLocator; |
||
36 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
37 | import org.gvsig.tools.dynobject.DelegatedDynObject; |
||
38 | import org.gvsig.tools.dynobject.DynObject; |
||
39 | 41059 | jjdelcerro | import org.slf4j.Logger; |
40 | import org.slf4j.LoggerFactory; |
||
41 | 40878 | jjdelcerro | import org.supercsv.prefs.CsvPreference; |
42 | 41006 | jjdelcerro | import org.supercsv.quote.AlwaysQuoteMode; |
43 | import org.supercsv.quote.NormalQuoteMode; |
||
44 | import org.supercsv.quote.QuoteMode; |
||
45 | 40846 | jjdelcerro | |
46 | public class CSVStoreParameters extends AbstractDataParameters implements |
||
47 | 40886 | jjdelcerro | DataStoreParameters, FilesystemStoreParameters { |
48 | 40846 | jjdelcerro | |
49 | 41059 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class); |
50 | |||
51 | 40846 | jjdelcerro | public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters"; |
52 | |||
53 | private static final String FILE = "file"; |
||
54 | 41059 | jjdelcerro | private static final String IGNOREERRORS = "ignoreErrors"; |
55 | 40846 | jjdelcerro | private static final String PROFILE = "profile"; |
56 | 41006 | jjdelcerro | private static final String QUOTEPOLICY = "quotePolicy"; |
57 | private static final String QUOTECHAR = "quoteCharacter"; |
||
58 | 40846 | jjdelcerro | private static final String RECORDSEPARATOR = "recordSeparator"; |
59 | private static final String DELIMITER = "delimiter"; |
||
60 | private static final String COMMENTSTARTMARKER = "commentStartMarker"; |
||
61 | 41062 | jjdelcerro | private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
62 | 40878 | jjdelcerro | |
63 | //private static final String ESCAPECHARACTER = "escapeCharacter";
|
||
64 | |||
65 | 40846 | jjdelcerro | private static final String HEADER = "header"; |
66 | 40878 | jjdelcerro | private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
67 | |||
68 | //private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
|
||
69 | 40846 | jjdelcerro | private static final String CRS = "CRS"; |
70 | private static final String FIELDTYPES = "fieldtypes"; |
||
71 | 40878 | jjdelcerro | // private static final String NULLTO = "nullTo";
|
72 | 40846 | jjdelcerro | private static final String CHARSET = "charset"; // Default "UTF-8" |
73 | |||
74 | |||
75 | |||
76 | private DelegatedDynObject parameters;
|
||
77 | |||
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 | 40878 | jjdelcerro | |
120 | 40846 | jjdelcerro | |
121 | 40878 | jjdelcerro | |
122 | static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
|
||
123 | 40846 | jjdelcerro | String s = (String) dynobj.getDynValue(PROFILE); |
124 | if( "NONE".equalsIgnoreCase(s) ) { |
||
125 | return null; |
||
126 | } |
||
127 | 40878 | jjdelcerro | if( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
128 | return CsvPreference.STANDARD_PREFERENCE;
|
||
129 | 40846 | jjdelcerro | } |
130 | 40878 | jjdelcerro | if( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
131 | return CsvPreference.EXCEL_PREFERENCE;
|
||
132 | 40846 | jjdelcerro | } |
133 | 40878 | jjdelcerro | if( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
134 | return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
||
135 | 40846 | jjdelcerro | } |
136 | 40878 | jjdelcerro | if( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
137 | return CsvPreference.TAB_PREFERENCE;
|
||
138 | 40846 | jjdelcerro | } |
139 | return null ; |
||
140 | } |
||
141 | |||
142 | 41006 | jjdelcerro | static QuoteMode getQuoteMode(DynObject dynobj) {
|
143 | String s = (String) dynobj.getDynValue(QUOTEPOLICY); |
||
144 | if( "AlwaysQuoteMode".equalsIgnoreCase(s) ) { |
||
145 | return new AlwaysQuoteMode(); |
||
146 | } |
||
147 | if( "NormalQuoteMode".equalsIgnoreCase(s) ) { |
||
148 | return new NormalQuoteMode(); |
||
149 | } |
||
150 | return null; |
||
151 | } |
||
152 | |||
153 | 40846 | jjdelcerro | static IProjection getCRS(DynObject dynobj) {
|
154 | return (IProjection) dynobj.getDynValue(CRS);
|
||
155 | } |
||
156 | |||
157 | static String getFileName(DynObject dynobj) { |
||
158 | File f = (File) dynobj.getDynValue(FILE); |
||
159 | if( f == null ) { |
||
160 | return null; |
||
161 | } |
||
162 | return f.getPath();
|
||
163 | } |
||
164 | |||
165 | static File getFile(DynObject dynobj) { |
||
166 | File f = (File) dynobj.getDynValue(FILE); |
||
167 | return f;
|
||
168 | } |
||
169 | |||
170 | static String getRecordSeparator(DynObject dynobj) { |
||
171 | String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
||
172 | return StringEscapeUtils.unescapeJava(s);
|
||
173 | } |
||
174 | |||
175 | static String getCommentStartMarker(DynObject dynobj) { |
||
176 | String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
||
177 | return StringEscapeUtils.unescapeJava(s);
|
||
178 | } |
||
179 | |||
180 | 40878 | jjdelcerro | // static String getEscapeCharacter(DynObject dynobj) {
|
181 | // String s = (String) dynobj.getDynValue(ESCAPECHARACTER);
|
||
182 | // return StringEscapeUtils.unescapeJava(s);
|
||
183 | // }
|
||
184 | //
|
||
185 | 40846 | jjdelcerro | static String getQuoteCharacter(DynObject dynobj) { |
186 | String s = (String) dynobj.getDynValue(QUOTECHAR); |
||
187 | 40878 | jjdelcerro | s = StringEscapeUtils.unescapeJava(s); |
188 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
189 | 40878 | jjdelcerro | return null; |
190 | } |
||
191 | return s.substring(0, 1); |
||
192 | 40846 | jjdelcerro | } |
193 | |||
194 | static String getDelimiter(DynObject dynobj) { |
||
195 | String s = (String) dynobj.getDynValue(DELIMITER); |
||
196 | 40878 | jjdelcerro | s = StringEscapeUtils.unescapeJava(s); |
197 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
198 | 40878 | jjdelcerro | return null; |
199 | } |
||
200 | return s.substring(0, 1); |
||
201 | 40846 | jjdelcerro | } |
202 | |||
203 | static String getHeader(DynObject dynobj) { |
||
204 | String s = (String) dynobj.getDynValue(HEADER); |
||
205 | s = StringEscapeUtils.unescapeJava(s); |
||
206 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
207 | 40886 | jjdelcerro | return null; |
208 | 40846 | jjdelcerro | } |
209 | return s;
|
||
210 | } |
||
211 | 40886 | jjdelcerro | |
212 | static String[] getHeaders(DynObject dynobj) { |
||
213 | String s = getHeader(dynobj);
|
||
214 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
215 | 40886 | jjdelcerro | return null; |
216 | } |
||
217 | String sep = getDelimiter(dynobj);
|
||
218 | if( sep == null ) { |
||
219 | sep = getDelimiter(s); |
||
220 | if( sep == null ) { |
||
221 | // Chungo
|
||
222 | return null; |
||
223 | } |
||
224 | } |
||
225 | String[] ss = s.split("["+sep+"]"); |
||
226 | return ss;
|
||
227 | } |
||
228 | |||
229 | private static String getDelimiter(String line) { |
||
230 | String sep = null; |
||
231 | 41059 | jjdelcerro | // Cuiaddo con los ":", los he puesto al final a proposito
|
232 | // ya que podian estar en la cadena para separar el size
|
||
233 | // size de cada tipo.
|
||
234 | String seps = ",;-|@#/+$%&!:"; |
||
235 | 40886 | jjdelcerro | for( int i=0; i<seps.length(); i ++) { |
236 | sep = seps.substring(i, 1);
|
||
237 | if( line.contains(seps.substring(i, 1))) { |
||
238 | break;
|
||
239 | } |
||
240 | sep = null;
|
||
241 | } |
||
242 | return sep;
|
||
243 | } |
||
244 | 40878 | jjdelcerro | // static String getNullTo(DynObject dynobj) {
|
245 | // String s = (String) dynobj.getDynValue(NULLTO);
|
||
246 | // return StringEscapeUtils.unescapeJava(s);
|
||
247 | // }
|
||
248 | 40846 | jjdelcerro | |
249 | static String getCharset(DynObject dynobj) { |
||
250 | String s = (String) dynobj.getDynValue(CHARSET); |
||
251 | return StringEscapeUtils.unescapeJava(s);
|
||
252 | } |
||
253 | |||
254 | 41006 | jjdelcerro | static String[] getPointDimensionNames(DynObject dynobj) { |
255 | String s = (String) dynobj.getDynValue("point"); |
||
256 | if( isEmpty(s) ) {
|
||
257 | return null; |
||
258 | } |
||
259 | return s.split(","); |
||
260 | } |
||
261 | |||
262 | 40878 | jjdelcerro | static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
263 | Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
||
264 | if( b==null ) { |
||
265 | return false; |
||
266 | } |
||
267 | 40846 | jjdelcerro | return b.booleanValue();
|
268 | } |
||
269 | |||
270 | 41062 | jjdelcerro | static boolean getAutomaticTypesDetection(DynObject dynobj) { |
271 | Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION); |
||
272 | if( b==null ) { |
||
273 | return false; |
||
274 | } |
||
275 | return b.booleanValue();
|
||
276 | } |
||
277 | |||
278 | 41059 | jjdelcerro | static boolean getIgnoreErrors(DynObject dynobj) { |
279 | Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
||
280 | if( b==null ) { |
||
281 | return false; |
||
282 | } |
||
283 | return b.booleanValue();
|
||
284 | } |
||
285 | |||
286 | 40878 | jjdelcerro | // static boolean getIgnoreEmptyLines(DynObject dynobj) {
|
287 | // Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES);
|
||
288 | // return b.booleanValue();
|
||
289 | // }
|
||
290 | 40846 | jjdelcerro | |
291 | static int[] getFieldTypes(DynObject dynobj) { |
||
292 | String s = (String) dynobj.getDynValue(FIELDTYPES); |
||
293 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
294 | 40846 | jjdelcerro | return null; |
295 | } |
||
296 | 40886 | jjdelcerro | String sep = getDelimiter(s);
|
297 | if( sep == null ) { |
||
298 | return null; |
||
299 | 40846 | jjdelcerro | } |
300 | 40886 | jjdelcerro | DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
301 | String fieldTypeNames[] = s.split("["+sep+"]"); |
||
302 | 40846 | jjdelcerro | int fieldTypes[] = new int[fieldTypeNames.length]; |
303 | for( int i=0; i<fieldTypeNames.length; i++ ) { |
||
304 | 41059 | jjdelcerro | s = fieldTypeNames[i].trim(); |
305 | if( s.contains(":") ) { |
||
306 | s = s.split(":")[0]; |
||
307 | } |
||
308 | fieldTypes[i] = dataTypeManager.getType(s); |
||
309 | 40846 | jjdelcerro | } |
310 | return fieldTypes;
|
||
311 | } |
||
312 | |||
313 | 41059 | jjdelcerro | static int[] getFieldSizes(DynObject dynobj) { |
314 | String s = (String) dynobj.getDynValue(FIELDTYPES); |
||
315 | if( isEmpty(s) ) {
|
||
316 | return null; |
||
317 | } |
||
318 | String sep = getDelimiter(s);
|
||
319 | if( sep == null ) { |
||
320 | return null; |
||
321 | } |
||
322 | String fieldTypeNames[] = s.split("["+sep+"]"); |
||
323 | int fieldSizes[] = new int[fieldTypeNames.length]; |
||
324 | for( int i=0; i<fieldTypeNames.length; i++ ) { |
||
325 | String fieldtype = fieldTypeNames[i].trim();
|
||
326 | if( fieldtype .contains(":") ) { |
||
327 | try {
|
||
328 | s = fieldtype .split(":")[1]; |
||
329 | fieldSizes[i] = Integer.parseInt(s);
|
||
330 | } catch(Exception ex) { |
||
331 | logger.warn("Can't get size of field "+i+" ("+fieldtype +").",ex); |
||
332 | } |
||
333 | } else {
|
||
334 | fieldSizes[i] = 0;
|
||
335 | } |
||
336 | } |
||
337 | return fieldSizes;
|
||
338 | } |
||
339 | |||
340 | 40894 | jjdelcerro | static private boolean isEmpty(String s) { |
341 | if( s == null ) { |
||
342 | return true; |
||
343 | } |
||
344 | if( s.trim().length() == 0 ) { |
||
345 | return true; |
||
346 | } |
||
347 | return false; |
||
348 | } |
||
349 | 40846 | jjdelcerro | } |