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

History | View | Annotate | Download (9.23 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.fmap.dal.FileHelper;
33
import org.gvsig.fmap.dal.feature.FeatureType;
34
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
35
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
36
import org.gvsig.fmap.dal.spi.AbstractDataStoreParameters;
37
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
38
import org.gvsig.tools.dynobject.DelegatedDynObject;
39
import org.gvsig.tools.dynobject.DynObject;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
@SuppressWarnings("UseSpecificCatch")
44
public class SimpleReaderStoreParameters extends AbstractDataStoreParameters implements
45
        OpenFeatureStoreParameters, FilesystemStoreParameters {
46

    
47
    protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleReaderStoreParameters.class);
48

    
49
    protected static final String FILE = "file";
50
    protected static final String IGNOREERRORS = "ignoreErrors";
51
    protected static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
52

    
53
    protected static final String CRS = CRS_PARAMTER_NAME;
54
    protected static final String FIELDTYPES = "fieldtypes";
55
    protected static final String CHARSET = "charset"; // Default "UTF-8"
56
    protected static final String LOCALE = "locale";
57
    public static final String HEADER = "header";
58

    
59
    private static final String LIMIT = "limit";
60

    
61
    protected DelegatedDynObject parameters;
62
    protected FeatureType featureType;
63
    protected boolean defaultValueOfAutomaticTypesDetection = true;
64

    
65
    @SuppressWarnings("OverridableMethodCallInConstructor")
66
    public SimpleReaderStoreParameters(String parametersDefinitionName, String name) {
67
        super();
68
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
69
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
70
    }
71
    
72
    @Override
73
    protected DelegatedDynObject getDelegatedDynObject() {
74
        return parameters;
75
    }
76

    
77
    @Override
78
    public void setDynValue(String name, Object value) {
79
        super.setDynValue(name, value);
80
        this.featureType = null;
81
    }
82

    
83
    @Override
84
    public boolean isValid() {
85
        return getFileName(this) != null;
86
    }
87

    
88
    @Override
89
    public File getFile() {
90
        return (File) this.getDynValue(FILE);
91
    }
92

    
93
    @Override
94
    public void setFile(File file) {
95
        this.setDynValue(FILE, file);
96
    }
97

    
98
    public static String getHeader(DynObject dynobj) {
99
        String s = (String) dynobj.getDynValue(HEADER);
100
        s = StringEscapeUtils.unescapeJava(s);
101
        if ( StringUtils.isBlank(s) ) {
102
            return null;
103
        }
104
        return s;
105
    }
106

    
107
    public static String getDelimiter(String line) {
108
        if( StringUtils.isBlank(line) ) {
109
            return null;
110
        }
111
        String sep = null;
112
        // Cuidado con los ":", los he puesto al final a proposito
113
        // ya que podian estar en la cadena para separar el size
114
        // de cada tipo.
115
        String seps = ",;-|@#/+$%&!:";
116
        for ( int i = 0; i < seps.length(); i++ ) {
117
            sep = seps.substring(i, 1);
118
            if ( line.contains(seps.substring(i, 1)) ) {
119
                break;
120
            }
121
            sep = null;
122
        }
123
        return sep;
124
    }
125

    
126
    public static String[] getHeaders(DynObject dynobj) {
127
        //Este metodo debe sobreescribirlo el CSV por el delimiter
128
        String s = getHeader(dynobj);
129
        if ( StringUtils.isBlank(s) ) {
130
            return null;
131
        }
132
        String sep = getDelimiter(s);
133
        if ( sep == null ) {
134
            // Chungo
135
            return null;
136
        }
137
        String[] ss = s.split("[" + sep + "]");
138
        return ss;
139
    }
140

    
141
    public static IProjection getCRS(DynObject dynobj) {
142
        return (IProjection) dynobj.getDynValue(CRS);
143
    }
144

    
145
    public static String getFileName(DynObject dynobj) {
146
        File f = (File) dynobj.getDynValue(FILE);
147
        if ( f == null ) {
148
            return null;
149
        }
150
        return f.getPath();
151
    }
152

    
153
    public static File getFile(DynObject dynobj) {
154
        File f = (File) dynobj.getDynValue(FILE);
155
        return f;
156
    }
157

    
158
    public static boolean isBlankOrDefaultLocale(DynObject dynobj) {
159
        String s = (String) dynobj.getDynValue(LOCALE);
160
        if (StringUtils.isBlank(s)) {
161
            return true;
162
        }
163
        return StringUtils.equalsIgnoreCase("DEFAULT",s.trim());
164
    }
165

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

    
197
    public static String getCharset(DynObject dynobj) {
198
        String s = (String) dynobj.getDynValue(CHARSET);
199
        return StringEscapeUtils.unescapeJava(s);
200
    }
201

    
202
    public static boolean getAutomaticTypesDetection(DynObject dynobj) {
203
        Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION);
204
        return BooleanUtils.isTrue(b);
205
    }
206

    
207
    public static boolean getIgnoreErrors(DynObject dynobj) {
208
        Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS);
209
        return BooleanUtils.isTrue(b);
210
    }
211

    
212
    public static String getRawFieldTypes(DynObject dynobj) {
213
        String s = (String) dynobj.getDynValue(FIELDTYPES);
214
        if ( StringUtils.isBlank(s) ) {
215
            return null;
216
        }
217
        return s.trim();
218
    }
219

    
220
    public static String getRawFieldsDefinition(DynObject dynobj) {
221
        String s = (String) dynobj.getDynValue("fieldsDefinition");
222
        if ( StringUtils.isBlank(s) ) {
223
            return null;
224
        }
225
        return s.trim();
226
    }
227
    
228
    public static int getSkipLines(DynObject dynobj) {
229
        Integer n = (Integer) dynobj.getDynValue("skipLines");
230
        if ( n == null ) {
231
            return 0;
232
        }
233
        return n;
234
    }
235

    
236
    public static int getLimit(DynObject dynobj) {
237
        Integer n = (Integer) dynobj.getDynValue(LIMIT);
238
        if ( n == null ) {
239
            return -1;
240
        }
241
        return n;
242
    }
243

    
244
    
245

    
246
    public static class FieldDefinition {
247

    
248
        private final int start;
249
        private final int end;
250

    
251
        public FieldDefinition(String def) {
252
            def = def.trim();
253
            String[] ss = def.split(":");
254
            this.start = Integer.parseInt(ss[0]);
255
            if ( ss.length < 2 ) {
256
                this.end = -1;
257
            } else {
258
                this.end = Integer.parseInt(ss[1]);
259
            }
260
        }
261

    
262
        public int getStart() {
263
            return this.start;
264
        }
265

    
266
        public int getEnd() {
267
            return this.end;
268
        }
269

    
270
        public boolean getToEndOfLine() {
271
            return this.end == -1;
272
        }
273
    }
274
    
275
    public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) {
276
        String definition = getRawFieldsDefinition(dynobj);
277
        if ( definition == null ) {
278
            return null;
279
        }
280

    
281
        int i=0;
282
        try {
283
            String[] defs = StringUtils.split(definition);
284
            FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length];
285
            for ( i = 0; i < defs.length; i++ ) {
286
                fieldsDefinition[i] = new FieldDefinition(defs[i]);
287
            }
288
            return fieldsDefinition;
289
        } catch (Exception ex) {
290
            throw  new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+").");
291
        }
292
    }
293
}