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 @ 41059
History | View | Annotate | Download (9.57 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 |
|
62 |
//private static final String ESCAPECHARACTER = "escapeCharacter";
|
63 |
|
64 |
private static final String HEADER = "header"; |
65 |
private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
66 |
|
67 |
//private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
|
68 |
private static final String CRS = "CRS"; |
69 |
private static final String FIELDTYPES = "fieldtypes"; |
70 |
// private static final String NULLTO = "nullTo";
|
71 |
private static final String CHARSET = "charset"; // Default "UTF-8" |
72 |
|
73 |
|
74 |
|
75 |
private DelegatedDynObject parameters;
|
76 |
|
77 |
|
78 |
public CSVStoreParameters() {
|
79 |
this(PARAMETERS_DEFINITION_NAME);
|
80 |
} |
81 |
|
82 |
protected CSVStoreParameters(String parametersDefinitionName) { |
83 |
this(parametersDefinitionName, CSVStoreProvider.NAME);
|
84 |
} |
85 |
|
86 |
public CSVStoreParameters(String parametersDefinitionName, String name) { |
87 |
super();
|
88 |
this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
|
89 |
this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
|
90 |
} |
91 |
|
92 |
public String getDataStoreName() { |
93 |
return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
94 |
} |
95 |
|
96 |
public String getDescription() { |
97 |
return this.getDynClass().getDescription(); |
98 |
} |
99 |
|
100 |
protected DelegatedDynObject getDelegatedDynObject() {
|
101 |
return parameters;
|
102 |
} |
103 |
|
104 |
public boolean isValid() { |
105 |
if (getFileName(this) == null) { |
106 |
return false; |
107 |
} |
108 |
return true; |
109 |
} |
110 |
|
111 |
public File getFile() { |
112 |
return (File) this.getDynValue(FILE); |
113 |
} |
114 |
|
115 |
public void setFile(File file) { |
116 |
this.setDynValue(FILE, file);
|
117 |
} |
118 |
|
119 |
|
120 |
|
121 |
static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
|
122 |
String s = (String) dynobj.getDynValue(PROFILE); |
123 |
if( "NONE".equalsIgnoreCase(s) ) { |
124 |
return null; |
125 |
} |
126 |
if( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
127 |
return CsvPreference.STANDARD_PREFERENCE;
|
128 |
} |
129 |
if( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
130 |
return CsvPreference.EXCEL_PREFERENCE;
|
131 |
} |
132 |
if( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
133 |
return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
134 |
} |
135 |
if( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
136 |
return CsvPreference.TAB_PREFERENCE;
|
137 |
} |
138 |
return null ; |
139 |
} |
140 |
|
141 |
static QuoteMode getQuoteMode(DynObject dynobj) {
|
142 |
String s = (String) dynobj.getDynValue(QUOTEPOLICY); |
143 |
if( "AlwaysQuoteMode".equalsIgnoreCase(s) ) { |
144 |
return new AlwaysQuoteMode(); |
145 |
} |
146 |
if( "NormalQuoteMode".equalsIgnoreCase(s) ) { |
147 |
return new NormalQuoteMode(); |
148 |
} |
149 |
return null; |
150 |
} |
151 |
|
152 |
static IProjection getCRS(DynObject dynobj) {
|
153 |
return (IProjection) dynobj.getDynValue(CRS);
|
154 |
} |
155 |
|
156 |
static String getFileName(DynObject dynobj) { |
157 |
File f = (File) dynobj.getDynValue(FILE); |
158 |
if( f == null ) { |
159 |
return null; |
160 |
} |
161 |
return f.getPath();
|
162 |
} |
163 |
|
164 |
static File getFile(DynObject dynobj) { |
165 |
File f = (File) dynobj.getDynValue(FILE); |
166 |
return f;
|
167 |
} |
168 |
|
169 |
static String getRecordSeparator(DynObject dynobj) { |
170 |
String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
171 |
return StringEscapeUtils.unescapeJava(s);
|
172 |
} |
173 |
|
174 |
static String getCommentStartMarker(DynObject dynobj) { |
175 |
String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
176 |
return StringEscapeUtils.unescapeJava(s);
|
177 |
} |
178 |
|
179 |
// static String getEscapeCharacter(DynObject dynobj) {
|
180 |
// String s = (String) dynobj.getDynValue(ESCAPECHARACTER);
|
181 |
// return StringEscapeUtils.unescapeJava(s);
|
182 |
// }
|
183 |
//
|
184 |
static String getQuoteCharacter(DynObject dynobj) { |
185 |
String s = (String) dynobj.getDynValue(QUOTECHAR); |
186 |
s = StringEscapeUtils.unescapeJava(s); |
187 |
if( isEmpty(s) ) {
|
188 |
return null; |
189 |
} |
190 |
return s.substring(0, 1); |
191 |
} |
192 |
|
193 |
static String getDelimiter(DynObject dynobj) { |
194 |
String s = (String) dynobj.getDynValue(DELIMITER); |
195 |
s = StringEscapeUtils.unescapeJava(s); |
196 |
if( isEmpty(s) ) {
|
197 |
return null; |
198 |
} |
199 |
return s.substring(0, 1); |
200 |
} |
201 |
|
202 |
static String getHeader(DynObject dynobj) { |
203 |
String s = (String) dynobj.getDynValue(HEADER); |
204 |
s = StringEscapeUtils.unescapeJava(s); |
205 |
if( isEmpty(s) ) {
|
206 |
return null; |
207 |
} |
208 |
return s;
|
209 |
} |
210 |
|
211 |
static String[] getHeaders(DynObject dynobj) { |
212 |
String s = getHeader(dynobj);
|
213 |
if( isEmpty(s) ) {
|
214 |
return null; |
215 |
} |
216 |
String sep = getDelimiter(dynobj);
|
217 |
if( sep == null ) { |
218 |
sep = getDelimiter(s); |
219 |
if( sep == null ) { |
220 |
// Chungo
|
221 |
return null; |
222 |
} |
223 |
} |
224 |
String[] ss = s.split("["+sep+"]"); |
225 |
return ss;
|
226 |
} |
227 |
|
228 |
private static String getDelimiter(String line) { |
229 |
String sep = null; |
230 |
// Cuiaddo con los ":", los he puesto al final a proposito
|
231 |
// ya que podian estar en la cadena para separar el size
|
232 |
// size de cada tipo.
|
233 |
String seps = ",;-|@#/+$%&!:"; |
234 |
for( int i=0; i<seps.length(); i ++) { |
235 |
sep = seps.substring(i, 1);
|
236 |
if( line.contains(seps.substring(i, 1))) { |
237 |
break;
|
238 |
} |
239 |
sep = null;
|
240 |
} |
241 |
return sep;
|
242 |
} |
243 |
// static String getNullTo(DynObject dynobj) {
|
244 |
// String s = (String) dynobj.getDynValue(NULLTO);
|
245 |
// return StringEscapeUtils.unescapeJava(s);
|
246 |
// }
|
247 |
|
248 |
static String getCharset(DynObject dynobj) { |
249 |
String s = (String) dynobj.getDynValue(CHARSET); |
250 |
return StringEscapeUtils.unescapeJava(s);
|
251 |
} |
252 |
|
253 |
static String[] getPointDimensionNames(DynObject dynobj) { |
254 |
String s = (String) dynobj.getDynValue("point"); |
255 |
if( isEmpty(s) ) {
|
256 |
return null; |
257 |
} |
258 |
return s.split(","); |
259 |
} |
260 |
|
261 |
static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
262 |
Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
263 |
if( b==null ) { |
264 |
return false; |
265 |
} |
266 |
return b.booleanValue();
|
267 |
} |
268 |
|
269 |
static boolean getIgnoreErrors(DynObject dynobj) { |
270 |
Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
271 |
if( b==null ) { |
272 |
return false; |
273 |
} |
274 |
return b.booleanValue();
|
275 |
} |
276 |
|
277 |
// static boolean getIgnoreEmptyLines(DynObject dynobj) {
|
278 |
// Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES);
|
279 |
// return b.booleanValue();
|
280 |
// }
|
281 |
|
282 |
static int[] getFieldTypes(DynObject dynobj) { |
283 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
284 |
if( isEmpty(s) ) {
|
285 |
return null; |
286 |
} |
287 |
String sep = getDelimiter(s);
|
288 |
if( sep == null ) { |
289 |
return null; |
290 |
} |
291 |
DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
292 |
String fieldTypeNames[] = s.split("["+sep+"]"); |
293 |
int fieldTypes[] = new int[fieldTypeNames.length]; |
294 |
for( int i=0; i<fieldTypeNames.length; i++ ) { |
295 |
s = fieldTypeNames[i].trim(); |
296 |
if( s.contains(":") ) { |
297 |
s = s.split(":")[0]; |
298 |
} |
299 |
fieldTypes[i] = dataTypeManager.getType(s); |
300 |
} |
301 |
return fieldTypes;
|
302 |
} |
303 |
|
304 |
static int[] getFieldSizes(DynObject dynobj) { |
305 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
306 |
if( isEmpty(s) ) {
|
307 |
return null; |
308 |
} |
309 |
String sep = getDelimiter(s);
|
310 |
if( sep == null ) { |
311 |
return null; |
312 |
} |
313 |
String fieldTypeNames[] = s.split("["+sep+"]"); |
314 |
int fieldSizes[] = new int[fieldTypeNames.length]; |
315 |
for( int i=0; i<fieldTypeNames.length; i++ ) { |
316 |
String fieldtype = fieldTypeNames[i].trim();
|
317 |
if( fieldtype .contains(":") ) { |
318 |
try {
|
319 |
s = fieldtype .split(":")[1]; |
320 |
fieldSizes[i] = Integer.parseInt(s);
|
321 |
} catch(Exception ex) { |
322 |
logger.warn("Can't get size of field "+i+" ("+fieldtype +").",ex); |
323 |
} |
324 |
} else {
|
325 |
fieldSizes[i] = 0;
|
326 |
} |
327 |
} |
328 |
return fieldSizes;
|
329 |
} |
330 |
|
331 |
static private boolean isEmpty(String s) { |
332 |
if( s == null ) { |
333 |
return true; |
334 |
} |
335 |
if( s.trim().length() == 0 ) { |
336 |
return true; |
337 |
} |
338 |
return false; |
339 |
} |
340 |
} |