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.csv / src / main / java / org / gvsig / fmap / dal / store / simplereader / SimpleReaderStoreParameters.java @ 47642

History | View | Annotate | Download (11.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 static org.gvsig.fmap.dal.DataParameters.CRS_PARAMTER_NAME;
36
import org.gvsig.fmap.dal.FileHelper;
37
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
40
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
41
import org.gvsig.fmap.dal.spi.AbstractDataStoreParameters;
42
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
43
import org.gvsig.tools.dynobject.DelegatedDynObject;
44
import org.gvsig.tools.dynobject.DynObject;
45
import org.gvsig.tools.dynobject.Tags;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48

    
49
@SuppressWarnings("UseSpecificCatch")
50
public class SimpleReaderStoreParameters extends AbstractDataStoreParameters implements
51
        OpenFeatureStoreParameters, FilesystemStoreParameters {
52

    
53
    protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleReaderStoreParameters.class);
54

    
55
    protected static final String FILE = "file";
56
    protected static final String IGNOREERRORS = "ignoreErrors";
57
    protected static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
58

    
59
    protected static final String CRS = CRS_PARAMTER_NAME;
60
    protected static final String FIELDTYPES = "fieldtypes";
61
    protected static final String CHARSET = "charset"; // Default "UTF-8"
62
    protected static final String LOCALE = "locale";
63
    public static final String HEADER = "header";
64

    
65
    private static final String LIMIT = "limit";
66

    
67
    protected DelegatedDynObject parameters;
68
    protected FeatureType featureType;
69
    protected boolean defaultValueOfAutomaticTypesDetection = true;
70

    
71
    @SuppressWarnings("OverridableMethodCallInConstructor")
72
    public SimpleReaderStoreParameters(String parametersDefinitionName, String name) {
73
        super();
74
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
75
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
76
    }
77

    
78
    protected FeatureType getFeatureType() {
79
        return null;
80
    }
81
    
82
    @Override
83
    protected DelegatedDynObject getDelegatedDynObject() {
84
        return parameters;
85
    }
86

    
87
    @Override
88
    public void setDynValue(String name, Object value) {
89
        super.setDynValue(name, value);
90
        this.featureType = null;
91
    }
92

    
93
    @Override
94
    public boolean isValid() {
95
        return getFileName(this) != null;
96
    }
97

    
98
    @Override
99
    public File getFile() {
100
        return (File) this.getDynValue(FILE);
101
    }
102

    
103
    @Override
104
    public void setFile(File file) {
105
        this.setDynValue(FILE, file);
106
    }
107

    
108
    public static String getHeader(DynObject dynobj) {
109
        String s = (String) dynobj.getDynValue(HEADER);
110
        s = StringEscapeUtils.unescapeJava(s);
111
        if ( StringUtils.isBlank(s) ) {
112
            return null;
113
        }
114
        return s;
115
    }
116

    
117
    public static String getDelimiter(String line) {
118
        if( StringUtils.isBlank(line) ) {
119
            return null;
120
        }
121
        String sep = null;
122
        // Cuidado con los ":", los he puesto al final a proposito
123
        // ya que podian estar en la cadena para separar el size
124
        // de cada tipo.
125
        String seps = ",;-|@#/+$%&!:";
126
        for ( int i = 0; i < seps.length(); i++ ) {
127
            sep = seps.substring(i, 1);
128
            if ( line.contains(seps.substring(i, 1)) ) {
129
                break;
130
            }
131
            sep = null;
132
        }
133
        return sep;
134
    }
135

    
136
    public static String[] getHeaders(DynObject dynobj) {
137
        //Este metodo debe sobreescribirlo el CSV por el delimiter
138
        String s = getHeader(dynobj);
139
        if ( StringUtils.isBlank(s) ) {
140
            return null;
141
        }
142
        String sep = getDelimiter(s);
143
        if ( sep == null ) {
144
            // Chungo
145
            return null;
146
        }
147
        String[] ss = s.split("[" + sep + "]");
148
        return ss;
149
    }
150

    
151
    public static IProjection getCRS(DynObject dynobj) {
152
        return (IProjection) dynobj.getDynValue(CRS);
153
    }
154

    
155
    public static String getFileName(DynObject dynobj) {
156
        File f = (File) dynobj.getDynValue(FILE);
157
        if ( f == null ) {
158
            return null;
159
        }
160
        return f.getPath();
161
    }
162

    
163
    public static File getFile(DynObject dynobj) {
164
        File f = (File) dynobj.getDynValue(FILE);
165
        return f;
166
    }
167

    
168
    public static boolean isBlankOrDefaultLocale(DynObject dynobj) {
169
        String s = (String) dynobj.getDynValue(LOCALE);
170
        if (StringUtils.isBlank(s)) {
171
            return true;
172
        }
173
        return StringUtils.equalsIgnoreCase("DEFAULT",s.trim());
174
    }
175

    
176
    public static Locale getLocale(DynObject dynobj) {
177
        try {
178
            String s = (String) dynobj.getDynValue(LOCALE);
179
            if ( StringUtils.isBlank(s) ) {
180
                return null;
181
            }
182
            if (StringUtils.equalsIgnoreCase("DEFAULT",s.trim())) {
183
                return Locale.getDefault();
184
            }
185
            Locale locale;
186
            // locale = Locale.forLanguageTag(s); // Since java 1.7
187
            String[] ss = s.split("-");
188
            switch (ss.length) {
189
            case 1:
190
                locale = new Locale(ss[0]);
191
                break;
192
            case 2:
193
                locale = new Locale(ss[0], ss[1]);
194
                break;
195
            case 3:
196
            default:
197
                locale = new Locale(ss[0], ss[1], ss[2]);
198
                break;
199
            }
200
            return locale;
201
        } catch (Exception ex) {
202
            LOGGER.warn("Can't get locale from parameters.", ex);
203
            return null;
204
        }
205
    }
206

    
207
    public static String getCharset(DynObject dynobj) {
208
        String s = (String) dynobj.getDynValue(CHARSET);
209
        return StringEscapeUtils.unescapeJava(s);
210
    }
211

    
212
    public static boolean getAutomaticTypesDetection(DynObject dynobj) {
213
        Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION);
214
        return BooleanUtils.isTrue(b);
215
    }
216

    
217
    public static boolean getIgnoreErrors(DynObject dynobj) {
218
        Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS);
219
        return BooleanUtils.isTrue(b);
220
    }
221

    
222
    public static String getRawFieldTypes(DynObject dynobj) {
223
        String s = (String) dynobj.getDynValue(FIELDTYPES);
224
        if ( StringUtils.isBlank(s) ) {
225
            return null;
226
        }
227
        return s.trim();
228
    }
229

    
230
    public static String getRawFieldsDefinition(DynObject dynobj) {
231
        String s = (String) dynobj.getDynValue("fieldsDefinition");
232
        if ( StringUtils.isBlank(s) ) {
233
            return null;
234
        }
235
        return s.trim();
236
    }
237
    
238
    public static int getSkipLines(DynObject dynobj) {
239
        Integer n = (Integer) dynobj.getDynValue("skipLines");
240
        if ( n == null ) {
241
            return 0;
242
        }
243
        return n;
244
    }
245

    
246
    public static int getLimit(DynObject dynobj) {
247
        Integer n = (Integer) dynobj.getDynValue(LIMIT);
248
        if ( n == null ) {
249
            return -1;
250
        }
251
        return n;
252
    }
253

    
254
    
255

    
256
    public static class FieldDefinition {
257

    
258
        private final int start;
259
        private final int end;
260

    
261
        public FieldDefinition(String def) {
262
            def = def.trim();
263
            String[] ss = def.split(":");
264
            this.start = Integer.parseInt(ss[0]);
265
            if ( ss.length < 2 ) {
266
                this.end = -1;
267
            } else {
268
                this.end = Integer.parseInt(ss[1]);
269
            }
270
        }
271

    
272
        public int getStart() {
273
            return this.start;
274
        }
275

    
276
        public int getEnd() {
277
            return this.end;
278
        }
279

    
280
        public boolean getToEndOfLine() {
281
            return this.end == -1;
282
        }
283
    }
284
    
285
    public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) {
286
        String definition = getRawFieldsDefinition(dynobj);
287
        if ( definition == null ) {
288
            return null;
289
        }
290

    
291
        int i=0;
292
        try {
293
            String[] defs = StringUtils.split(definition);
294
            FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length];
295
            for ( i = 0; i < defs.length; i++ ) {
296
                fieldsDefinition[i] = new FieldDefinition(defs[i]);
297
            }
298
            return fieldsDefinition;
299
        } catch (Exception ex) {
300
            throw  new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+").");
301
        }
302
    }
303
    
304
    @Override
305
    public void validate() throws ValidateDataParametersException {
306
        File f = this.getFile();
307
        if( f!=null ) {
308
            IProjection proj = null;
309
            FeatureType ftype = this.getFeatureType();
310
            if( ftype!=null ) {
311
                this.setDynValue(AUTOMATICTYPESDETECTION, defaultValueOfAutomaticTypesDetection);
312
                proj = ftype.getDefaultSRS();
313
                if( proj!=null ) {
314
                    this.setDynValue(CRS_PARAMTER_NAME, proj);
315
                }
316
                Tags ftypeTags = ftype.getTags();
317
                for (String tagname : ftypeTags) {
318
                    if( StringUtils.startsWithIgnoreCase(tagname, "storeparameters.") ||
319
                            StringUtils.startsWithIgnoreCase(tagname, "csvparameters.") /*por compativilidad*/ ) {
320
                        String paramname = tagname.substring(14);
321
                        String paramvalue = ftypeTags.getString(tagname,null);
322
                        if( paramvalue!=null ) {
323
                            this.setDynValue(paramname, paramvalue);
324
                        }
325
                    }
326
                }
327
            }
328
            if( proj==null ) {
329
                PRJFile prjfile = FormatsFile.getPRJFile(f);
330
                if( prjfile!= null ) {
331
                    this.setDynValue(CRS_PARAMTER_NAME, prjfile.getCRS());
332
                }
333
            }
334
            String charsetName = getCharset(this);
335
            if( StringUtils.isBlank(charsetName) ) {
336
                CPGFile cpgfile = FormatsFile.getCPGFile(f);
337
                if( cpgfile!=null ) {
338
                    this.setDynValue(CHARSET, cpgfile.getCharsetName());
339
                }
340
            }
341
        }
342
        super.validate();
343
    }
344
}