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 / feature / spi / simpleprovider / BaseSimpleStoreParameters.java @ 44057

History | View | Annotate | Download (8.24 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.feature.spi.simpleprovider;
25

    
26
import java.io.File;
27
import java.util.Arrays;
28
import java.util.List;
29
import java.util.Locale;
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.OpenFeatureStoreParameters;
34
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
35
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
36
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.dataTypes.DataTypesManager;
39
import org.gvsig.tools.dynobject.DelegatedDynObject;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
public class BaseSimpleStoreParameters extends AbstractDataParameters implements
44
        OpenFeatureStoreParameters, FilesystemStoreParameters {
45

    
46
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseSimpleStoreParameters.class);
47

    
48

    
49
    private static final String FILE = "file";
50
    private static final String IGNOREERRORS = "ignoreErrors";
51
    private static final String CRS = "CRS";
52
    private static final String FIELDTYPES = "fieldtypes";
53
    private static final String FIELDNAMES = "fieldnames";
54
    private static final String LOCALE = "locale";
55
    private static final String POINT = "pointFields";
56
    private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
57

    
58
    private final DelegatedDynObject parameters;
59
    private final SimpleReaderFactory readerFactory;
60
    
61
    public BaseSimpleStoreParameters(SimpleReaderFactory readerFactory) {
62
        super();
63
        this.readerFactory = readerFactory;
64
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(readerFactory.getName());
65
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, readerFactory.getName());
66
    }
67

    
68
    @Override
69
    public String getDataStoreName() {
70
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
71
    }
72

    
73
    @Override
74
    public String getDescription() {
75
        return this.getDynClass().getDescription();
76
    }
77

    
78
    @Override
79
    protected DelegatedDynObject getDelegatedDynObject() {
80
        return parameters;
81
    }
82

    
83
    @Override
84
    public boolean isValid() {
85
        if ( this.getPathName() == null ) {
86
            return false;
87
        }
88
        return true;
89
    }
90

    
91
    @Override
92
    public File getFile() {
93
        return (File) this.getDynValue(FILE);
94
    }
95

    
96
    @Override
97
    public void setFile(File file) {
98
        this.setDynValue(FILE, file);
99
    }
100

    
101
    public IProjection getCRS() {
102
        return (IProjection) this.parameters.getDynValue(CRS);
103
    }
104

    
105
    public String getPathName() {
106
        File f = (File) this.parameters.getDynValue(FILE);
107
        if ( f == null ) {
108
            return null;
109
        }
110
        return f.getPath();
111
    }
112

    
113
    @SuppressWarnings("UseSpecificCatch")
114
    public Locale getLocale() {
115
        try {
116
            String s = (String) this.parameters.getDynValue(LOCALE);
117
            if ( s.trim().length() == 0 ) {
118
                return null;
119
            }
120
            if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) {
121
                return Locale.getDefault();
122
            }
123
            Locale locale;
124
            // locale = Locale.forLanguageTag(s); // Since java 1.7
125
            String[] ss = s.split("-");
126
            switch (ss.length) {
127
            case 1:
128
                locale = new Locale(ss[0]);
129
                break;
130
            case 2:
131
                locale = new Locale(ss[0], ss[1]);
132
                break;
133
            case 3:
134
            default:
135
                locale = new Locale(ss[0], ss[1], ss[2]);
136
                break;
137
            }
138
            return locale;
139
        } catch (Exception ex) {
140
            LOGGER.warn("Can't get locale from ODS parameters.", ex);
141
            return null;
142
        }
143
    }
144

    
145
    public String[] getPointDimensionNames() {
146
        String s = (String) this.parameters.getDynValue(POINT);
147
        if ( StringUtils.isEmpty(s) ) {
148
            return null;
149
        }
150
        String sep = this.getDelimiter(s);
151
        if ( sep == null ) {
152
            return new String[] { s };
153
        }
154
        return s.split(sep);
155
    }
156

    
157
    public boolean getIgnoreErrors() {
158
        Boolean b = (Boolean) this.parameters.getDynValue(IGNOREERRORS);
159
        if ( b == null ) {
160
            return false;
161
        }
162
        return b;
163
    }
164

    
165
    private String getDelimiter(String line) {
166
        String sep = null;
167
        // Cuiaddo con los ":", los he puesto al final a proposito
168
        // ya que podian estar en la cadena para separar el size
169
        // de cada tipo.
170
        String seps = ",;-|@#/+$%&!:";
171
        for ( int i = 0; i < seps.length(); i++ ) {
172
            sep = seps.substring(i, 1);
173
            if ( line.contains(seps.substring(i, 1)) ) {
174
                break;
175
            }
176
            sep = null;
177
        }
178
        return sep;
179
    }
180

    
181
    public List<String> getFieldNames() {
182
        String s = (String) this.parameters.getDynValue(FIELDNAMES);
183
        if ( StringUtils.isEmpty(s) ) {
184
            return null;
185
        }
186
        String sep = this.getDelimiter(s);
187
        if ( sep == null ) {
188
            return null;
189
        }
190
        String fieldNames[] = s.split("[" + sep + "]");
191
        for (int i = 0; i < fieldNames.length; i++) {
192
            fieldNames[i] = fieldNames[i].trim();
193
        }
194
        return Arrays.asList(fieldNames);
195
    }
196
    
197
    public int[] getFieldTypes() {
198
        String s = (String) this.parameters.getDynValue(FIELDTYPES);
199
        if ( StringUtils.isEmpty(s) ) {
200
            return null;
201
        }
202
        String sep = this.getDelimiter(s);
203
        if ( sep == null ) {
204
            return null;
205
        }
206
        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
207
        String fieldTypeNames[] = s.split("[" + sep + "]");
208
        int fieldTypes[] = new int[fieldTypeNames.length];
209
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
210
            s = fieldTypeNames[i].trim();
211
            if ( s.contains(":") ) {
212
                s = s.split(":")[0];
213
            }
214
            fieldTypes[i] = dataTypeManager.getType(s);
215
        }
216
        return fieldTypes;
217
    }
218

    
219
    @SuppressWarnings("UseSpecificCatch")
220
    public int[] getFieldSizes() {
221
        String s = (String) this.parameters.getDynValue(FIELDTYPES);
222
        if ( StringUtils.isEmpty(s) ) {
223
            return null;
224
        }
225
        String sep = this.getDelimiter(s);
226
        if ( sep == null ) {
227
            return null;
228
        }
229
        String fieldTypeNames[] = s.split("[" + sep + "]");
230
        int fieldSizes[] = new int[fieldTypeNames.length];
231
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
232
            String fieldtype = fieldTypeNames[i].trim();
233
            if ( fieldtype.contains(":") ) {
234
                try {
235
                    s = fieldtype.split(":")[1];
236
                    fieldSizes[i] = Integer.parseInt(s);
237
                } catch (Exception ex) {
238
                    LOGGER.warn("Can't get size of field " + i + " (" + fieldtype + ").", ex);
239
                }
240
            } else {
241
                fieldSizes[i] = 0;
242
            }
243
        }
244
        return fieldSizes;
245
    }
246

    
247
    public boolean getAutomaticTypesDetection() {
248
        Boolean b = (Boolean) this.parameters.getDynValue(AUTOMATICTYPESDETECTION);
249
        if ( b == null ) {
250
            return false;
251
        }
252
        return b;
253
    }
254

    
255
}