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 |
/**
|
---|---|
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 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
import org.supercsv.prefs.CsvPreference; |
42 |
import org.supercsv.quote.AlwaysQuoteMode; |
43 |
import org.supercsv.quote.NormalQuoteMode; |
44 |
import org.supercsv.quote.QuoteMode; |
45 |
|
46 |
public class CSVStoreParameters extends AbstractDataParameters implements |
47 |
DataStoreParameters, FilesystemStoreParameters { |
48 |
|
49 |
private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class); |
50 |
|
51 |
public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters"; |
52 |
|
53 |
private static final String FILE = "file"; |
54 |
private static final String IGNOREERRORS = "ignoreErrors"; |
55 |
private static final String PROFILE = "profile"; |
56 |
private static final String QUOTEPOLICY = "quotePolicy"; |
57 |
private static final String QUOTECHAR = "quoteCharacter"; |
58 |
private static final String RECORDSEPARATOR = "recordSeparator"; |
59 |
private static final String DELIMITER = "delimiter"; |
60 |
private static final String COMMENTSTARTMARKER = "commentStartMarker"; |
61 |
private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
62 |
|
63 |
//private static final String ESCAPECHARACTER = "escapeCharacter";
|
64 |
|
65 |
private static final String HEADER = "header"; |
66 |
private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
67 |
|
68 |
//private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
|
69 |
private static final String CRS = "CRS"; |
70 |
private static final String FIELDTYPES = "fieldtypes"; |
71 |
// private static final String NULLTO = "nullTo";
|
72 |
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 |
|
120 |
|
121 |
|
122 |
static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
|
123 |
String s = (String) dynobj.getDynValue(PROFILE); |
124 |
if( "NONE".equalsIgnoreCase(s) ) { |
125 |
return null; |
126 |
} |
127 |
if( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
128 |
return CsvPreference.STANDARD_PREFERENCE;
|
129 |
} |
130 |
if( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
131 |
return CsvPreference.EXCEL_PREFERENCE;
|
132 |
} |
133 |
if( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
134 |
return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
135 |
} |
136 |
if( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
137 |
return CsvPreference.TAB_PREFERENCE;
|
138 |
} |
139 |
return null ; |
140 |
} |
141 |
|
142 |
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 |
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 |
// static String getEscapeCharacter(DynObject dynobj) {
|
181 |
// String s = (String) dynobj.getDynValue(ESCAPECHARACTER);
|
182 |
// return StringEscapeUtils.unescapeJava(s);
|
183 |
// }
|
184 |
//
|
185 |
static String getQuoteCharacter(DynObject dynobj) { |
186 |
String s = (String) dynobj.getDynValue(QUOTECHAR); |
187 |
s = StringEscapeUtils.unescapeJava(s); |
188 |
if( isEmpty(s) ) {
|
189 |
return null; |
190 |
} |
191 |
return s.substring(0, 1); |
192 |
} |
193 |
|
194 |
static String getDelimiter(DynObject dynobj) { |
195 |
String s = (String) dynobj.getDynValue(DELIMITER); |
196 |
s = StringEscapeUtils.unescapeJava(s); |
197 |
if( isEmpty(s) ) {
|
198 |
return null; |
199 |
} |
200 |
return s.substring(0, 1); |
201 |
} |
202 |
|
203 |
static String getHeader(DynObject dynobj) { |
204 |
String s = (String) dynobj.getDynValue(HEADER); |
205 |
s = StringEscapeUtils.unescapeJava(s); |
206 |
if( isEmpty(s) ) {
|
207 |
return null; |
208 |
} |
209 |
return s;
|
210 |
} |
211 |
|
212 |
static String[] getHeaders(DynObject dynobj) { |
213 |
String s = getHeader(dynobj);
|
214 |
if( isEmpty(s) ) {
|
215 |
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 |
// 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 |
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 |
// static String getNullTo(DynObject dynobj) {
|
245 |
// String s = (String) dynobj.getDynValue(NULLTO);
|
246 |
// return StringEscapeUtils.unescapeJava(s);
|
247 |
// }
|
248 |
|
249 |
static String getCharset(DynObject dynobj) { |
250 |
String s = (String) dynobj.getDynValue(CHARSET); |
251 |
return StringEscapeUtils.unescapeJava(s);
|
252 |
} |
253 |
|
254 |
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 |
static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
263 |
Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
264 |
if( b==null ) { |
265 |
return false; |
266 |
} |
267 |
return b.booleanValue();
|
268 |
} |
269 |
|
270 |
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 |
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 |
// static boolean getIgnoreEmptyLines(DynObject dynobj) {
|
287 |
// Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES);
|
288 |
// return b.booleanValue();
|
289 |
// }
|
290 |
|
291 |
static int[] getFieldTypes(DynObject dynobj) { |
292 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
293 |
if( isEmpty(s) ) {
|
294 |
return null; |
295 |
} |
296 |
String sep = getDelimiter(s);
|
297 |
if( sep == null ) { |
298 |
return null; |
299 |
} |
300 |
DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
301 |
String fieldTypeNames[] = s.split("["+sep+"]"); |
302 |
int fieldTypes[] = new int[fieldTypeNames.length]; |
303 |
for( int i=0; i<fieldTypeNames.length; i++ ) { |
304 |
s = fieldTypeNames[i].trim(); |
305 |
if( s.contains(":") ) { |
306 |
s = s.split(":")[0]; |
307 |
} |
308 |
fieldTypes[i] = dataTypeManager.getType(s); |
309 |
} |
310 |
return fieldTypes;
|
311 |
} |
312 |
|
313 |
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 |
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 |
} |