Statistics
| Revision:

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 @ 47789

History | View | Annotate | Download (12.7 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 all_fields_declare_type = false;
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 Object getDynValue(String name) {
97
        if(StringUtils.equalsIgnoreCase(name, AUTOMATICTYPESDETECTION)){
98
            //Si tenemos featureType y estan declarados todos los tipos, ya no es necesario hacer la deteccion automatica
99
            if(this.featureType != null){
100
                if( this.all_fields_declare_type ){
101
                    return false;
102
                }
103
            }
104
        }
105
        return super.getDynValue(name);
106
    }
107

    
108
    @Override
109
    public boolean isValid() {
110
        return getFileName(this) != null;
111
    }
112

    
113
    @Override
114
    public File getFile() {
115
        return (File) this.getDynValue(FILE);
116
    }
117

    
118
    @Override
119
    public void setFile(File file) {
120
        this.setDynValue(FILE, file);
121
    }
122

    
123
    public static String getHeader(DynObject dynobj) {
124
        String s = (String) dynobj.getDynValue(HEADER);
125
        s = StringEscapeUtils.unescapeJava(s);
126
        if ( StringUtils.isBlank(s) ) {
127
            return null;
128
        }
129
        return s;
130
    }
131

    
132
    public static String getDelimiter(String line) {
133
        if( StringUtils.isBlank(line) ) {
134
            return null;
135
        }
136
        String sep = null;
137
        // Cuidado con los ":", los he puesto al final a proposito
138
        // ya que podian estar en la cadena para separar el size
139
        // de cada tipo.
140
        String seps = ",;-|@#/+$%&!:";
141
        for ( int i = 0; i < seps.length(); i++ ) {
142
            sep = seps.substring(i, 1);
143
            if ( line.contains(seps.substring(i, 1)) ) {
144
                break;
145
            }
146
            sep = null;
147
        }
148
        return sep;
149
    }
150

    
151
    public static String[] getHeaders(DynObject dynobj) {
152
        //Este metodo debe sobreescribirlo el CSV por el delimiter
153
        String s = getHeader(dynobj);
154
        if ( StringUtils.isBlank(s) ) {
155
            return null;
156
        }
157
        String sep = getDelimiter(s);
158
        if ( sep == null ) {
159
            // Chungo
160
            return null;
161
        }
162
        String[] ss = s.split("[" + sep + "]");
163
        return ss;
164
    }
165

    
166
    public static IProjection getCRS(DynObject dynobj) {
167
        return (IProjection) dynobj.getDynValue(CRS);
168
    }
169

    
170
    public static String getFileName(DynObject dynobj) {
171
        File f = (File) dynobj.getDynValue(FILE);
172
        if ( f == null ) {
173
            return null;
174
        }
175
        return f.getPath();
176
    }
177

    
178
    public static File getFile(DynObject dynobj) {
179
        File f = (File) dynobj.getDynValue(FILE);
180
        return f;
181
    }
182

    
183
    public static boolean isBlankOrDefaultLocale(DynObject dynobj) {
184
        String s = (String) dynobj.getDynValue(LOCALE);
185
        if (StringUtils.isBlank(s)) {
186
            return true;
187
        }
188
        return StringUtils.equalsIgnoreCase("DEFAULT",s.trim());
189
    }
190

    
191
    public static Locale getLocale(DynObject dynobj) {
192
        try {
193
            String s = (String) dynobj.getDynValue(LOCALE);
194
            if ( StringUtils.isBlank(s) ) {
195
                return null;
196
            }
197
            if (StringUtils.equalsIgnoreCase("DEFAULT",s.trim())) {
198
                return Locale.getDefault();
199
            }
200
            Locale locale;
201
            // locale = Locale.forLanguageTag(s); // Since java 1.7
202
            String[] ss = s.split("-");
203
            switch (ss.length) {
204
            case 1:
205
                locale = new Locale(ss[0]);
206
                break;
207
            case 2:
208
                locale = new Locale(ss[0], ss[1]);
209
                break;
210
            case 3:
211
            default:
212
                locale = new Locale(ss[0], ss[1], ss[2]);
213
                break;
214
            }
215
            return locale;
216
        } catch (Exception ex) {
217
            LOGGER.warn("Can't get locale from parameters.", ex);
218
            return null;
219
        }
220
    }
221

    
222
    public static String getCharset(DynObject dynobj) {
223
        String s = (String) dynobj.getDynValue(CHARSET);
224
        return StringEscapeUtils.unescapeJava(s);
225
    }
226

    
227
    public static boolean getAutomaticTypesDetection(DynObject dynobj) {
228
        Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION);
229
        return BooleanUtils.isTrue(b);
230
    }
231

    
232
    public static boolean getIgnoreErrors(DynObject dynobj) {
233
        Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS);
234
        return BooleanUtils.isTrue(b);
235
    }
236

    
237
    public static String getRawFieldTypes(DynObject dynobj) {
238
        String s = (String) dynobj.getDynValue(FIELDTYPES);
239
        if ( StringUtils.isBlank(s) ) {
240
            return null;
241
        }
242
        return s.trim();
243
    }
244

    
245
    public static String getRawFieldsDefinition(DynObject dynobj) {
246
        String s = (String) dynobj.getDynValue("fieldsDefinition");
247
        if ( StringUtils.isBlank(s) ) {
248
            return null;
249
        }
250
        return s.trim();
251
    }
252
    
253
    public static int getSkipLines(DynObject dynobj) {
254
        Integer n = (Integer) dynobj.getDynValue("skipLines");
255
        if ( n == null ) {
256
            return 0;
257
        }
258
        return n;
259
    }
260

    
261
    public static int getLimit(DynObject dynobj) {
262
        Integer n = (Integer) dynobj.getDynValue(LIMIT);
263
        if ( n == null ) {
264
            return -1;
265
        }
266
        return n;
267
    }
268

    
269
    
270

    
271
    public static class FieldDefinition {
272

    
273
        private final int start;
274
        private final int end;
275

    
276
        public FieldDefinition(String def) {
277
            def = def.trim();
278
            String[] ss = def.split(":");
279
            this.start = Integer.parseInt(ss[0]);
280
            if ( ss.length < 2 ) {
281
                this.end = -1;
282
            } else {
283
                this.end = Integer.parseInt(ss[1]);
284
            }
285
        }
286

    
287
        public int getStart() {
288
            return this.start;
289
        }
290

    
291
        public int getEnd() {
292
            return this.end;
293
        }
294

    
295
        public boolean getToEndOfLine() {
296
            return this.end == -1;
297
        }
298
    }
299
    
300
    public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) {
301
        String definition = getRawFieldsDefinition(dynobj);
302
        if ( definition == null ) {
303
            return null;
304
        }
305

    
306
        int i=0;
307
        try {
308
            String[] defs = StringUtils.split(definition);
309
            FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length];
310
            for ( i = 0; i < defs.length; i++ ) {
311
                fieldsDefinition[i] = new FieldDefinition(defs[i]);
312
            }
313
            return fieldsDefinition;
314
        } catch (Exception ex) {
315
            throw  new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+").");
316
        }
317
    }
318
    
319
    @Override
320
    public void validate() throws ValidateDataParametersException {
321
        File f = this.getFile();
322
        if( f!=null ) {
323
            IProjection proj = null;
324
            FeatureType ftype = this.getFeatureType();
325
            if( ftype!=null ) {
326
                proj = ftype.getDefaultSRS();
327
                if( proj!=null ) {
328
                    this.setDynValue(CRS_PARAMTER_NAME, proj);
329
                }
330
                Tags ftypeTags = ftype.getTags();
331
                for (String tagname : ftypeTags) {
332
                    if( StringUtils.startsWithIgnoreCase(tagname, "storeparameters.") ||
333
                            StringUtils.startsWithIgnoreCase(tagname, "csvparameters.") /*por compativilidad*/ ) {
334
                        String paramname = tagname.substring(14);
335
                        String paramvalue = ftypeTags.getString(tagname,null);
336
                        if( paramvalue!=null ) {
337
                            this.setDynValue(paramname, paramvalue);
338
                        }
339
                    }
340
                }
341
            }
342
            if( proj==null ) {
343
                PRJFile prjfile = FormatsFile.getPRJFile(f);
344
                if( prjfile!= null ) {
345
                    this.setDynValue(CRS_PARAMTER_NAME, prjfile.getCRS());
346
                }
347
            }
348
            String charsetName = getCharset(this);
349
            if( StringUtils.isBlank(charsetName) ) {
350
                CPGFile cpgfile = FormatsFile.getCPGFile(f);
351
                if( cpgfile!=null ) {
352
                    this.setDynValue(CHARSET, cpgfile.getCharsetName());
353
                }
354
            }
355
        }
356
        super.validate();
357
    }
358
    
359
    protected FeatureType getFeatureType() {
360
        if( this.featureType==null ) {
361
            try {
362
                SimpleReaderFeatureTypeLoader featureTypeLoader = this.getFeatureTypeLoader();
363
                if( featureTypeLoader!=null ) {
364
                    EditableFeatureType ftype = DALLocator.getDataManager().createFeatureType();
365
                    featureTypeLoader.loadFeatureType(ftype, false, null);
366
                    this.all_fields_declare_type = featureTypeLoader.isAllFieldsDeclareType();
367
                    this.featureType = ftype;
368
                }
369
            } catch (Exception ex) {
370
                LOGGER.debug("Can't detect feature of json file", ex);
371
                // Do nothing, continue
372
            }
373
        }
374
        return this.featureType;
375
    }
376
    
377
    @Override
378
    public String getSourceId() {
379
        return this.toString(); // FIXME: Check if is correnct
380
    }
381
}