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.lib / src / main / java / org / gvsig / fmap / dal / store / simplereader / SimpleReaderStoreParameters.java @ 47655
History | View | Annotate | Download (12.3 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.simplereader; |
25 |
|
26 |
import java.io.File; |
27 |
import java.util.Locale; |
28 |
import org.apache.commons.lang3.BooleanUtils; |
29 |
import org.apache.commons.lang3.StringEscapeUtils; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.cresques.cts.IProjection; |
32 |
import org.gvsig.basicformats.CPGFile; |
33 |
import org.gvsig.basicformats.FormatsFile; |
34 |
import org.gvsig.basicformats.PRJFile; |
35 |
import org.gvsig.fmap.dal.DALLocator; |
36 |
import static org.gvsig.fmap.dal.DataParameters.CRS_PARAMTER_NAME; |
37 |
import org.gvsig.fmap.dal.FileHelper; |
38 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
40 |
import org.gvsig.fmap.dal.feature.FeatureType; |
41 |
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters; |
42 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
43 |
import org.gvsig.fmap.dal.spi.AbstractDataStoreParameters; |
44 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
45 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
46 |
import org.gvsig.tools.dynobject.DynObject; |
47 |
import org.gvsig.tools.dynobject.Tags; |
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
@SuppressWarnings("UseSpecificCatch") |
52 |
public class SimpleReaderStoreParameters extends AbstractDataStoreParameters implements |
53 |
OpenFeatureStoreParameters, FilesystemStoreParameters { |
54 |
|
55 |
protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleReaderStoreParameters.class); |
56 |
|
57 |
protected static final String FILE = "file"; |
58 |
protected static final String IGNOREERRORS = "ignoreErrors"; |
59 |
protected static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
60 |
|
61 |
protected static final String CRS = CRS_PARAMTER_NAME; |
62 |
protected static final String FIELDTYPES = "fieldtypes"; |
63 |
protected static final String CHARSET = "charset"; // Default "UTF-8" |
64 |
protected static final String LOCALE = "locale"; |
65 |
public static final String HEADER = "header"; |
66 |
|
67 |
private static final String LIMIT = "limit"; |
68 |
|
69 |
protected DelegatedDynObject parameters;
|
70 |
protected FeatureType featureType;
|
71 |
protected boolean defaultValueOfAutomaticTypesDetection = true; |
72 |
|
73 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
74 |
public SimpleReaderStoreParameters(String parametersDefinitionName, String name) { |
75 |
super();
|
76 |
this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
|
77 |
this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
|
78 |
} |
79 |
|
80 |
protected SimpleReaderFeatureTypeLoader getFeatureTypeLoader() {
|
81 |
return null; |
82 |
} |
83 |
|
84 |
@Override
|
85 |
protected DelegatedDynObject getDelegatedDynObject() {
|
86 |
return parameters;
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void setDynValue(String name, Object value) { |
91 |
super.setDynValue(name, value);
|
92 |
this.featureType = null; |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public boolean isValid() { |
97 |
return getFileName(this) != null; |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public File getFile() { |
102 |
return (File) this.getDynValue(FILE); |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public void setFile(File file) { |
107 |
this.setDynValue(FILE, file);
|
108 |
} |
109 |
|
110 |
public static String getHeader(DynObject dynobj) { |
111 |
String s = (String) dynobj.getDynValue(HEADER); |
112 |
s = StringEscapeUtils.unescapeJava(s); |
113 |
if ( StringUtils.isBlank(s) ) {
|
114 |
return null; |
115 |
} |
116 |
return s;
|
117 |
} |
118 |
|
119 |
public static String getDelimiter(String line) { |
120 |
if( StringUtils.isBlank(line) ) {
|
121 |
return null; |
122 |
} |
123 |
String sep = null; |
124 |
// Cuidado con los ":", los he puesto al final a proposito
|
125 |
// ya que podian estar en la cadena para separar el size
|
126 |
// de cada tipo.
|
127 |
String seps = ",;-|@#/+$%&!:"; |
128 |
for ( int i = 0; i < seps.length(); i++ ) { |
129 |
sep = seps.substring(i, 1);
|
130 |
if ( line.contains(seps.substring(i, 1)) ) { |
131 |
break;
|
132 |
} |
133 |
sep = null;
|
134 |
} |
135 |
return sep;
|
136 |
} |
137 |
|
138 |
public static String[] getHeaders(DynObject dynobj) { |
139 |
//Este metodo debe sobreescribirlo el CSV por el delimiter
|
140 |
String s = getHeader(dynobj);
|
141 |
if ( StringUtils.isBlank(s) ) {
|
142 |
return null; |
143 |
} |
144 |
String sep = getDelimiter(s);
|
145 |
if ( sep == null ) { |
146 |
// Chungo
|
147 |
return null; |
148 |
} |
149 |
String[] ss = s.split("[" + sep + "]"); |
150 |
return ss;
|
151 |
} |
152 |
|
153 |
public static IProjection getCRS(DynObject dynobj) { |
154 |
return (IProjection) dynobj.getDynValue(CRS);
|
155 |
} |
156 |
|
157 |
public 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 |
public static File getFile(DynObject dynobj) { |
166 |
File f = (File) dynobj.getDynValue(FILE); |
167 |
return f;
|
168 |
} |
169 |
|
170 |
public static boolean isBlankOrDefaultLocale(DynObject dynobj) { |
171 |
String s = (String) dynobj.getDynValue(LOCALE); |
172 |
if (StringUtils.isBlank(s)) {
|
173 |
return true; |
174 |
} |
175 |
return StringUtils.equalsIgnoreCase("DEFAULT",s.trim()); |
176 |
} |
177 |
|
178 |
public static Locale getLocale(DynObject dynobj) { |
179 |
try {
|
180 |
String s = (String) dynobj.getDynValue(LOCALE); |
181 |
if ( StringUtils.isBlank(s) ) {
|
182 |
return null; |
183 |
} |
184 |
if (StringUtils.equalsIgnoreCase("DEFAULT",s.trim())) { |
185 |
return Locale.getDefault(); |
186 |
} |
187 |
Locale locale;
|
188 |
// locale = Locale.forLanguageTag(s); // Since java 1.7
|
189 |
String[] ss = s.split("-"); |
190 |
switch (ss.length) {
|
191 |
case 1: |
192 |
locale = new Locale(ss[0]); |
193 |
break;
|
194 |
case 2: |
195 |
locale = new Locale(ss[0], ss[1]); |
196 |
break;
|
197 |
case 3: |
198 |
default:
|
199 |
locale = new Locale(ss[0], ss[1], ss[2]); |
200 |
break;
|
201 |
} |
202 |
return locale;
|
203 |
} catch (Exception ex) { |
204 |
LOGGER.warn("Can't get locale from parameters.", ex);
|
205 |
return null; |
206 |
} |
207 |
} |
208 |
|
209 |
public static String getCharset(DynObject dynobj) { |
210 |
String s = (String) dynobj.getDynValue(CHARSET); |
211 |
return StringEscapeUtils.unescapeJava(s);
|
212 |
} |
213 |
|
214 |
public static boolean getAutomaticTypesDetection(DynObject dynobj) { |
215 |
Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION); |
216 |
return BooleanUtils.isTrue(b);
|
217 |
} |
218 |
|
219 |
public static boolean getIgnoreErrors(DynObject dynobj) { |
220 |
Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
221 |
return BooleanUtils.isTrue(b);
|
222 |
} |
223 |
|
224 |
public static String getRawFieldTypes(DynObject dynobj) { |
225 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
226 |
if ( StringUtils.isBlank(s) ) {
|
227 |
return null; |
228 |
} |
229 |
return s.trim();
|
230 |
} |
231 |
|
232 |
public static String getRawFieldsDefinition(DynObject dynobj) { |
233 |
String s = (String) dynobj.getDynValue("fieldsDefinition"); |
234 |
if ( StringUtils.isBlank(s) ) {
|
235 |
return null; |
236 |
} |
237 |
return s.trim();
|
238 |
} |
239 |
|
240 |
public static int getSkipLines(DynObject dynobj) { |
241 |
Integer n = (Integer) dynobj.getDynValue("skipLines"); |
242 |
if ( n == null ) { |
243 |
return 0; |
244 |
} |
245 |
return n;
|
246 |
} |
247 |
|
248 |
public static int getLimit(DynObject dynobj) { |
249 |
Integer n = (Integer) dynobj.getDynValue(LIMIT); |
250 |
if ( n == null ) { |
251 |
return -1; |
252 |
} |
253 |
return n;
|
254 |
} |
255 |
|
256 |
|
257 |
|
258 |
public static class FieldDefinition { |
259 |
|
260 |
private final int start; |
261 |
private final int end; |
262 |
|
263 |
public FieldDefinition(String def) { |
264 |
def = def.trim(); |
265 |
String[] ss = def.split(":"); |
266 |
this.start = Integer.parseInt(ss[0]); |
267 |
if ( ss.length < 2 ) { |
268 |
this.end = -1; |
269 |
} else {
|
270 |
this.end = Integer.parseInt(ss[1]); |
271 |
} |
272 |
} |
273 |
|
274 |
public int getStart() { |
275 |
return this.start; |
276 |
} |
277 |
|
278 |
public int getEnd() { |
279 |
return this.end; |
280 |
} |
281 |
|
282 |
public boolean getToEndOfLine() { |
283 |
return this.end == -1; |
284 |
} |
285 |
} |
286 |
|
287 |
public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) { |
288 |
String definition = getRawFieldsDefinition(dynobj);
|
289 |
if ( definition == null ) { |
290 |
return null; |
291 |
} |
292 |
|
293 |
int i=0; |
294 |
try {
|
295 |
String[] defs = StringUtils.split(definition); |
296 |
FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length]; |
297 |
for ( i = 0; i < defs.length; i++ ) { |
298 |
fieldsDefinition[i] = new FieldDefinition(defs[i]);
|
299 |
} |
300 |
return fieldsDefinition;
|
301 |
} catch (Exception ex) { |
302 |
throw new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+")."); |
303 |
} |
304 |
} |
305 |
|
306 |
@Override
|
307 |
public void validate() throws ValidateDataParametersException { |
308 |
File f = this.getFile(); |
309 |
if( f!=null ) { |
310 |
IProjection proj = null;
|
311 |
FeatureType ftype = this.getFeatureType();
|
312 |
if( ftype!=null ) { |
313 |
this.setDynValue(AUTOMATICTYPESDETECTION, defaultValueOfAutomaticTypesDetection);
|
314 |
proj = ftype.getDefaultSRS(); |
315 |
if( proj!=null ) { |
316 |
this.setDynValue(CRS_PARAMTER_NAME, proj);
|
317 |
} |
318 |
Tags ftypeTags = ftype.getTags(); |
319 |
for (String tagname : ftypeTags) { |
320 |
if( StringUtils.startsWithIgnoreCase(tagname, "storeparameters.") || |
321 |
StringUtils.startsWithIgnoreCase(tagname, "csvparameters.") /*por compativilidad*/ ) { |
322 |
String paramname = tagname.substring(14); |
323 |
String paramvalue = ftypeTags.getString(tagname,null); |
324 |
if( paramvalue!=null ) { |
325 |
this.setDynValue(paramname, paramvalue);
|
326 |
} |
327 |
} |
328 |
} |
329 |
} |
330 |
if( proj==null ) { |
331 |
PRJFile prjfile = FormatsFile.getPRJFile(f); |
332 |
if( prjfile!= null ) { |
333 |
this.setDynValue(CRS_PARAMTER_NAME, prjfile.getCRS());
|
334 |
} |
335 |
} |
336 |
String charsetName = getCharset(this); |
337 |
if( StringUtils.isBlank(charsetName) ) {
|
338 |
CPGFile cpgfile = FormatsFile.getCPGFile(f); |
339 |
if( cpgfile!=null ) { |
340 |
this.setDynValue(CHARSET, cpgfile.getCharsetName());
|
341 |
} |
342 |
} |
343 |
} |
344 |
super.validate();
|
345 |
} |
346 |
|
347 |
protected FeatureType getFeatureType() {
|
348 |
if( this.featureType==null ) { |
349 |
try {
|
350 |
SimpleReaderFeatureTypeLoader featureTypeLoader = this.getFeatureTypeLoader();
|
351 |
if( featureTypeLoader!=null ) { |
352 |
EditableFeatureType ftype = DALLocator.getDataManager().createFeatureType(); |
353 |
featureTypeLoader.loadFeatureType(ftype, false, null); |
354 |
boolean all_fields_declare_type = featureTypeLoader.isAllFieldsDeclareType();
|
355 |
defaultValueOfAutomaticTypesDetection = !all_fields_declare_type; |
356 |
this.featureType = ftype;
|
357 |
} |
358 |
} catch (Exception ex) { |
359 |
LOGGER.debug("Can't detect feature of json file", ex);
|
360 |
// Do nothing, continue
|
361 |
} |
362 |
} |
363 |
return this.featureType; |
364 |
} |
365 |
|
366 |
} |