Statistics
| Revision:

root / org.gvsig.jexcel / trunk / org.gvsig.jexcel / org.gvsig.jexcel.provider / src / main / java / org / gvsig / fmap / dal / store / jexcel / JExcelStoreParameters.java @ 272

History | View | Annotate | Download (9.01 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.jexcel;
25

    
26
import java.io.File;
27
import java.util.Locale;
28
import org.apache.commons.lang3.StringEscapeUtils;
29
import org.apache.commons.lang3.StringUtils;
30
import org.cresques.cts.IProjection;
31
import org.gvsig.fmap.dal.FileHelper;
32
import org.gvsig.fmap.dal.OpenDataStoreParameters;
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.gvsig.tools.dynobject.DynObject;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
public class JExcelStoreParameters extends AbstractDataParameters implements
45
        OpenFeatureStoreParameters, FilesystemStoreParameters {
46

    
47
    private static final Logger logger = LoggerFactory.getLogger(JExcelStoreParameters.class);
48

    
49
    public static final String PARAMETERS_DEFINITION_NAME = "JExcel";
50

    
51
    private static final String FILE = "file";
52
    private static final String IGNOREERRORS = "ignoreErrors";
53
    private static final String CRS = "CRS";
54
    private static final String FIELDTYPES = "fieldtypes";
55
    private static final String LOCALE = "locale";
56
    private static final String SHEET = "sheet";
57
    private static final String ISFIRSTROWHEADER = "isFirstRowHeadeer";
58
    private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
59
    private static final String HEADER = "header";
60

    
61
    private DelegatedDynObject parameters;
62

    
63
    public JExcelStoreParameters() {
64
        this(PARAMETERS_DEFINITION_NAME);
65
    }
66

    
67
    protected JExcelStoreParameters(String parametersDefinitionName) {
68
        this(parametersDefinitionName, JExcelStoreProvider.NAME);
69
    }
70

    
71
    public JExcelStoreParameters(String parametersDefinitionName, String name) {
72
        super();
73
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
74
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
75
    }
76

    
77
    public String getDataStoreName() {
78
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
79
    }
80

    
81
    public String getDescription() {
82
        return this.getDynClass().getDescription();
83
    }
84

    
85
    protected DelegatedDynObject getDelegatedDynObject() {
86
        return parameters;
87
    }
88

    
89
    public boolean isValid() {
90
        if ( getFileName(this) == null ) {
91
            return false;
92
        }
93
        return true;
94
    }
95

    
96
    public File getFile() {
97
        return (File) this.getDynValue(FILE);
98
    }
99

    
100
    public void setFile(File file) {
101
        this.setDynValue(FILE, file);
102
    }
103

    
104
    static IProjection getCRS(DynObject dynobj) {
105
        return (IProjection) dynobj.getDynValue(CRS);
106
    }
107

    
108
    static String getFileName(DynObject dynobj) {
109
        File f = (File) dynobj.getDynValue(FILE);
110
        if ( f == null ) {
111
            return null;
112
        }
113
        return f.getPath();
114
    }
115

    
116
    static File getFile(DynObject dynobj) {
117
        File f = (File) dynobj.getDynValue(FILE);
118
        return f;
119
    }
120

    
121
    static Locale getLocale(DynObject dynobj) {
122
        try {
123
            String s = (String) dynobj.getDynValue(LOCALE);
124
            if ( s.trim().length() == 0 ) {
125
                return null;
126
            }
127
            if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) {
128
                return Locale.getDefault();
129
            }
130
            Locale locale = null;
131
            // locale = Locale.forLanguageTag(s); // Since java 1.7
132
            String[] ss = s.split("-");
133
            switch (ss.length) {
134
            case 1:
135
                locale = new Locale(ss[0]);
136
                break;
137
            case 2:
138
                locale = new Locale(ss[0], ss[1]);
139
                break;
140
            case 3:
141
            default:
142
                locale = new Locale(ss[0], ss[1], ss[2]);
143
                break;
144
            }
145
            return locale;
146
        } catch (Exception ex) {
147
            logger.warn("Can't get locale from JExcel parameters.", ex);
148
            return null;
149
        }
150
    }
151

    
152
    private static String getDelimiter(String line) {
153
        String sep = null;
154
        // Cuiaddo con los ":", los he puesto al final a proposito
155
        // ya que podian estar en la cadena para separar el size
156
        // size de cada tipo.
157
        String seps = ",;-|@#/+$%&!:";
158
        for ( int i = 0; i < seps.length(); i++ ) {
159
            sep = seps.substring(i, 1);
160
            if ( line.contains(seps.substring(i, 1)) ) {
161
                break;
162
            }
163
            sep = null;
164
        }
165
        return sep;
166
    }
167

    
168
    static String[] getPointDimensionNames(DynObject dynobj) {
169
        String s = (String) dynobj.getDynValue("point");
170
        if ( StringUtils.isEmpty(s) ) {
171
            return null;
172
        }
173
        return s.split(",");
174
    }
175

    
176
    static boolean getIgnoreErrors(DynObject dynobj) {
177
        Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS);
178
        if ( b == null ) {
179
            return false;
180
        }
181
        return b.booleanValue();
182
    }
183

    
184
    static int[] getFieldTypes(DynObject dynobj) {
185
        String s = (String) dynobj.getDynValue(FIELDTYPES);
186
        if ( StringUtils.isEmpty(s) ) {
187
            return null;
188
        }
189
        String sep = getDelimiter(s);
190
        if ( sep == null ) {
191
            return null;
192
        }
193
        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
194
        String fieldTypeNames[] = s.split("[" + sep + "]");
195
        int fieldTypes[] = new int[fieldTypeNames.length];
196
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
197
            s = fieldTypeNames[i].trim();
198
            if ( s.contains(":") ) {
199
                s = s.split(":")[0];
200
            }
201
            fieldTypes[i] = dataTypeManager.getType(s);
202
        }
203
        return fieldTypes;
204
    }
205

    
206
    static int[] getFieldSizes(DynObject dynobj) {
207
        String s = (String) dynobj.getDynValue(FIELDTYPES);
208
        if ( StringUtils.isEmpty(s) ) {
209
            return null;
210
        }
211
        String sep = getDelimiter(s);
212
        if ( sep == null ) {
213
            return null;
214
        }
215
        String fieldTypeNames[] = s.split("[" + sep + "]");
216
        int fieldSizes[] = new int[fieldTypeNames.length];
217
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
218
            String fieldtype = fieldTypeNames[i].trim();
219
            if ( fieldtype.contains(":") ) {
220
                try {
221
                    s = fieldtype.split(":")[1];
222
                    fieldSizes[i] = Integer.parseInt(s);
223
                } catch (Exception ex) {
224
                    logger.warn("Can't get size of field " + i + " (" + fieldtype + ").", ex);
225
                }
226
            } else {
227
                fieldSizes[i] = 0;
228
            }
229
        }
230
        return fieldSizes;
231
    }
232

    
233
    static int getSheetIndex(DynObject dynobj) {
234
        Integer sheet = (Integer) dynobj.getDynValue(SHEET);
235
        if ( sheet == null ) {
236
            return 0;
237
        }
238
        return sheet.intValue();
239
    }
240

    
241
    static String getHeader(DynObject dynobj) {
242
        String s = (String) dynobj.getDynValue(HEADER);
243
        s = StringEscapeUtils.unescapeJava(s);
244
        if ( StringUtils.isBlank(s) ) {
245
            return null;
246
        }
247
        return s;
248
    }
249

    
250
    static String[] getHeaders(DynObject dynobj) {
251
        String s = getHeader(dynobj);
252
        if ( StringUtils.isBlank(s) ) {
253
            return null;
254
        }
255
        String sep = getDelimiter(s);
256
        if ( sep == null ) {
257
            // Chungo
258
            return null;
259
        }
260
        String[] ss = s.split("[" + sep + "]");
261
        return ss;
262
    }
263

    
264
    static boolean getAutomaticTypesDetection(DynObject dynobj) {
265
        Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION);
266
        if ( b == null ) {
267
            return false;
268
        }
269
        return b.booleanValue();
270
    }
271

    
272
    static boolean isFirstRowHeader(DynObject dynobj) {
273
        Boolean b = (Boolean) dynobj.getDynValue(ISFIRSTROWHEADER);
274
        if ( b == null ) {
275
            return false;
276
        }
277
        return b.booleanValue();
278
    }
279

    
280
}