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 @ 47748
History | View | Annotate | Download (12.3 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 | 41617 | jjdelcerro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 40846 | jjdelcerro | * 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 | 41617 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40846 | jjdelcerro | *
|
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 | 47636 | fdiaz | package org.gvsig.fmap.dal.store.simplereader; |
25 | 40846 | jjdelcerro | |
26 | import java.io.File; |
||
27 | 41069 | jjdelcerro | import java.util.Locale; |
28 | 41617 | jjdelcerro | import org.apache.commons.lang3.BooleanUtils; |
29 | 40846 | jjdelcerro | import org.apache.commons.lang3.StringEscapeUtils; |
30 | 41617 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
31 | 40846 | jjdelcerro | import org.cresques.cts.IProjection; |
32 | 47642 | jjdelcerro | import org.gvsig.basicformats.CPGFile; |
33 | import org.gvsig.basicformats.FormatsFile; |
||
34 | import org.gvsig.basicformats.PRJFile; |
||
35 | 47643 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
36 | 47642 | jjdelcerro | import static org.gvsig.fmap.dal.DataParameters.CRS_PARAMTER_NAME; |
37 | 40846 | jjdelcerro | import org.gvsig.fmap.dal.FileHelper; |
38 | 47642 | jjdelcerro | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
39 | 47643 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
40 | 45685 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
41 | 43088 | jjdelcerro | import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters; |
42 | 40846 | jjdelcerro | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
43 | 46338 | fdiaz | import org.gvsig.fmap.dal.spi.AbstractDataStoreParameters; |
44 | 40846 | jjdelcerro | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
45 | import org.gvsig.tools.dynobject.DelegatedDynObject; |
||
46 | import org.gvsig.tools.dynobject.DynObject; |
||
47 | 47642 | jjdelcerro | import org.gvsig.tools.dynobject.Tags; |
48 | 41059 | jjdelcerro | import org.slf4j.Logger; |
49 | import org.slf4j.LoggerFactory; |
||
50 | 40846 | jjdelcerro | |
51 | 45685 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
52 | 47636 | fdiaz | public class SimpleReaderStoreParameters extends AbstractDataStoreParameters implements |
53 | 43088 | jjdelcerro | OpenFeatureStoreParameters, FilesystemStoreParameters { |
54 | 40846 | jjdelcerro | |
55 | 47636 | fdiaz | protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleReaderStoreParameters.class); |
56 | 41617 | jjdelcerro | |
57 | 47636 | fdiaz | protected static final String FILE = "file"; |
58 | protected static final String IGNOREERRORS = "ignoreErrors"; |
||
59 | protected static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
||
60 | 40846 | jjdelcerro | |
61 | 47636 | fdiaz | 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 | 45567 | jolivas | public static final String HEADER = "header"; |
66 | 41617 | jjdelcerro | |
67 | 42775 | jjdelcerro | private static final String LIMIT = "limit"; |
68 | 40846 | jjdelcerro | |
69 | 47636 | fdiaz | protected DelegatedDynObject parameters;
|
70 | protected FeatureType featureType;
|
||
71 | protected boolean defaultValueOfAutomaticTypesDetection = true; |
||
72 | 40846 | jjdelcerro | |
73 | 45685 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
74 | 47636 | fdiaz | public SimpleReaderStoreParameters(String parametersDefinitionName, String name) { |
75 | 41617 | jjdelcerro | super();
|
76 | this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
|
||
77 | this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
|
||
78 | } |
||
79 | 47642 | jjdelcerro | |
80 | 47643 | jjdelcerro | protected SimpleReaderFeatureTypeLoader getFeatureTypeLoader() {
|
81 | 47642 | jjdelcerro | return null; |
82 | } |
||
83 | 45685 | jjdelcerro | |
84 | @Override
|
||
85 | 41617 | jjdelcerro | protected DelegatedDynObject getDelegatedDynObject() {
|
86 | return parameters;
|
||
87 | } |
||
88 | 40846 | jjdelcerro | |
89 | 45684 | jjdelcerro | @Override
|
90 | 45685 | jjdelcerro | public void setDynValue(String name, Object value) { |
91 | super.setDynValue(name, value);
|
||
92 | 47098 | jjdelcerro | this.featureType = null; |
93 | 45775 | jjdelcerro | } |
94 | |||
95 | @Override
|
||
96 | 41617 | jjdelcerro | public boolean isValid() { |
97 | 47636 | fdiaz | return getFileName(this) != null; |
98 | 41617 | jjdelcerro | } |
99 | 40846 | jjdelcerro | |
100 | 45685 | jjdelcerro | @Override
|
101 | 41617 | jjdelcerro | public File getFile() { |
102 | return (File) this.getDynValue(FILE); |
||
103 | } |
||
104 | 40846 | jjdelcerro | |
105 | 45685 | jjdelcerro | @Override
|
106 | 41617 | jjdelcerro | public void setFile(File file) { |
107 | this.setDynValue(FILE, file);
|
||
108 | } |
||
109 | 40846 | jjdelcerro | |
110 | 47636 | fdiaz | public static String getHeader(DynObject dynobj) { |
111 | String s = (String) dynobj.getDynValue(HEADER); |
||
112 | s = StringEscapeUtils.unescapeJava(s); |
||
113 | if ( StringUtils.isBlank(s) ) {
|
||
114 | 41617 | jjdelcerro | return null; |
115 | } |
||
116 | 47636 | fdiaz | return s;
|
117 | } |
||
118 | |||
119 | public static String getDelimiter(String line) { |
||
120 | if( StringUtils.isBlank(line) ) {
|
||
121 | return null; |
||
122 | 41617 | jjdelcerro | } |
123 | 47636 | fdiaz | 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 | 41617 | jjdelcerro | } |
135 | 47636 | fdiaz | return sep;
|
136 | 41617 | jjdelcerro | } |
137 | 40846 | jjdelcerro | |
138 | 47636 | fdiaz | 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 | 41617 | jjdelcerro | } |
144 | 47636 | fdiaz | String sep = getDelimiter(s);
|
145 | if ( sep == null ) { |
||
146 | // Chungo
|
||
147 | return null; |
||
148 | 41617 | jjdelcerro | } |
149 | 47636 | fdiaz | String[] ss = s.split("[" + sep + "]"); |
150 | return ss;
|
||
151 | 41617 | jjdelcerro | } |
152 | 40878 | jjdelcerro | |
153 | 45727 | jjdelcerro | public static IProjection getCRS(DynObject dynobj) { |
154 | 41617 | jjdelcerro | return (IProjection) dynobj.getDynValue(CRS);
|
155 | } |
||
156 | 40846 | jjdelcerro | |
157 | 45727 | jjdelcerro | public static String getFileName(DynObject dynobj) { |
158 | 41617 | jjdelcerro | File f = (File) dynobj.getDynValue(FILE); |
159 | if ( f == null ) { |
||
160 | return null; |
||
161 | } |
||
162 | return f.getPath();
|
||
163 | } |
||
164 | 40846 | jjdelcerro | |
165 | 45727 | jjdelcerro | public static File getFile(DynObject dynobj) { |
166 | 41617 | jjdelcerro | File f = (File) dynobj.getDynValue(FILE); |
167 | return f;
|
||
168 | } |
||
169 | 40846 | jjdelcerro | |
170 | 46093 | fdiaz | 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 | 45685 | jjdelcerro | |
178 | 45727 | jjdelcerro | public static Locale getLocale(DynObject dynobj) { |
179 | 41617 | jjdelcerro | try {
|
180 | String s = (String) dynobj.getDynValue(LOCALE); |
||
181 | 46093 | fdiaz | if ( StringUtils.isBlank(s) ) {
|
182 | 41617 | jjdelcerro | return null; |
183 | } |
||
184 | 46093 | fdiaz | if (StringUtils.equalsIgnoreCase("DEFAULT",s.trim())) { |
185 | 41617 | jjdelcerro | return Locale.getDefault(); |
186 | } |
||
187 | 45685 | jjdelcerro | Locale locale;
|
188 | 41617 | jjdelcerro | // 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 | 47636 | fdiaz | LOGGER.warn("Can't get locale from parameters.", ex);
|
205 | 41617 | jjdelcerro | return null; |
206 | } |
||
207 | } |
||
208 | 40886 | jjdelcerro | |
209 | 45727 | jjdelcerro | public static String getCharset(DynObject dynobj) { |
210 | 41617 | jjdelcerro | String s = (String) dynobj.getDynValue(CHARSET); |
211 | return StringEscapeUtils.unescapeJava(s);
|
||
212 | } |
||
213 | |||
214 | 45727 | jjdelcerro | public static boolean getAutomaticTypesDetection(DynObject dynobj) { |
215 | 41617 | jjdelcerro | Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION); |
216 | return BooleanUtils.isTrue(b);
|
||
217 | } |
||
218 | |||
219 | 45727 | jjdelcerro | public static boolean getIgnoreErrors(DynObject dynobj) { |
220 | 41617 | jjdelcerro | Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
221 | return BooleanUtils.isTrue(b);
|
||
222 | } |
||
223 | |||
224 | 45727 | jjdelcerro | public static String getRawFieldTypes(DynObject dynobj) { |
225 | 41617 | jjdelcerro | String s = (String) dynobj.getDynValue(FIELDTYPES); |
226 | if ( StringUtils.isBlank(s) ) {
|
||
227 | return null; |
||
228 | } |
||
229 | 41876 | jjdelcerro | return s.trim();
|
230 | 41617 | jjdelcerro | } |
231 | |||
232 | 47636 | fdiaz | 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 | 45727 | jjdelcerro | public static int getSkipLines(DynObject dynobj) { |
241 | 41617 | jjdelcerro | Integer n = (Integer) dynobj.getDynValue("skipLines"); |
242 | if ( n == null ) { |
||
243 | return 0; |
||
244 | } |
||
245 | 42775 | jjdelcerro | return n;
|
246 | 41617 | jjdelcerro | } |
247 | |||
248 | 45727 | jjdelcerro | public static int getLimit(DynObject dynobj) { |
249 | 42775 | jjdelcerro | Integer n = (Integer) dynobj.getDynValue(LIMIT); |
250 | if ( n == null ) { |
||
251 | return -1; |
||
252 | } |
||
253 | return n;
|
||
254 | } |
||
255 | |||
256 | 47636 | fdiaz | |
257 | 41617 | jjdelcerro | |
258 | public static class FieldDefinition { |
||
259 | |||
260 | 45685 | jjdelcerro | private final int start; |
261 | private final int end; |
||
262 | 41617 | jjdelcerro | |
263 | public FieldDefinition(String def) { |
||
264 | def = def.trim(); |
||
265 | String[] ss = def.split(":"); |
||
266 | 42775 | jjdelcerro | this.start = Integer.parseInt(ss[0]); |
267 | 41617 | jjdelcerro | if ( ss.length < 2 ) { |
268 | this.end = -1; |
||
269 | } else {
|
||
270 | 42775 | jjdelcerro | this.end = Integer.parseInt(ss[1]); |
271 | 41617 | jjdelcerro | } |
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 | 45182 | omartinez | |
287 | 47638 | jjdelcerro | 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 | 47642 | jjdelcerro | |
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 | 47643 | jjdelcerro | |
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 | 40846 | jjdelcerro | } |