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 / csv / CSVStoreParameters.java @ 47655

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

    
26
import java.io.File;
27
import org.apache.commons.lang3.BooleanUtils;
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.exception.ValidateDataParametersException;
32
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
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.store.simplereader.SimpleReaderFeatureTypeLoader;
37
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderStoreParameters;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.tools.dynobject.DynObject;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42
import org.supercsv.prefs.CsvPreference;
43
import org.supercsv.quote.AlwaysQuoteMode;
44
import org.supercsv.quote.NormalQuoteMode;
45
import org.supercsv.quote.QuoteMode;
46

    
47
@SuppressWarnings("UseSpecificCatch")
48
public class CSVStoreParameters extends SimpleReaderStoreParameters implements
49
        OpenFeatureStoreParameters, FilesystemStoreParameters {
50

    
51
    private static final Logger LOGGER = LoggerFactory.getLogger(CSVStoreParameters.class);
52

    
53
    public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters";
54

    
55
//    private static final String FILE = "file";
56
//    private static final String IGNOREERRORS = "ignoreErrors";
57
    private static final String PROFILE = "profile";
58
    private static final String QUOTEPOLICY = "quotePolicy";
59
    private static final String QUOTECHAR = "quoteCharacter";
60
    private static final String RECORDSEPARATOR = "recordSeparator";
61
    private static final String DELIMITER = "delimiter";
62
    private static final String COMMENTSTARTMARKER = "commentStartMarker";
63
//    private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
64

    
65
    private static final String ESCAPECHARACTER = "escapeCharacter";
66
    public static final String FIRST_LINE_HEADER = "firstLineHeader";
67
//    public static final String HEADER = "header";
68
    private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes";
69

    
70
    //private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
71
//    private static final String CRS = CRS_PARAMTER_NAME;
72
//    private static final String FIELDTYPES = "fieldtypes";
73
//    private static final String CHARSET = "charset"; // Default "UTF-8"
74
//    private static final String LOCALE = "locale";
75
    private static final String POINT_COLUMN_NAME = "pointColumnName";
76
    private static final String INCLUDE_METADATA_IN_HEADER = "includeMetadataInHeader";
77
    private static final String GEOMETRY_COLUMN = "geometry_column";
78
    private static final String GEOMETRY_TYPE = "GeometryType";
79
    private static final String GEOMETRY_SUBTYPE = "GeometrySubtype";
80
    private static final String GEOMETRY_FORMAT = "GeometryFormat";
81

    
82
    public CSVStoreParameters() {
83
        this(PARAMETERS_DEFINITION_NAME);
84
    }
85

    
86
    protected CSVStoreParameters(String parametersDefinitionName) {
87
        super(parametersDefinitionName, CSVStoreProvider.NAME);
88
    }
89

    
90
    @Override
91
    protected SimpleReaderFeatureTypeLoader getFeatureTypeLoader() {
92
        return new CSVFeatureTypeLoader(this);
93
    }
94
    
95
    @Override
96
    public void validate() throws ValidateDataParametersException {
97
        super.validate();
98
        FeatureType ftype = this.getFeatureType();
99
        if( ftype!=null ) {
100
            FeatureAttributeDescriptor attrgeom = ftype.getDefaultGeometryAttribute();
101
            if( attrgeom!=null ) {
102
                this.setDynValue(GEOMETRY_COLUMN, attrgeom.getName());
103
                this.setDynValue(GEOMETRY_TYPE, attrgeom.getGeomType().getType());
104
                this.setDynValue(GEOMETRY_SUBTYPE, attrgeom.getGeomType().getSubType());
105
            } else {
106
                this.setDynValue(GEOMETRY_COLUMN, null);
107
                this.setDynValue(GEOMETRY_TYPE, Geometry.TYPES.UNKNOWN);
108
                this.setDynValue(GEOMETRY_SUBTYPE, Geometry.SUBTYPES.UNKNOWN);
109
            }
110
        }
111
    }
112
    
113
    public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
114
        String s = (String) dynobj.getDynValue(PROFILE);
115
        if ( "NONE".equalsIgnoreCase(s) ) {
116
            return null;
117
        }
118
        if ( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) {
119
            return CsvPreference.STANDARD_PREFERENCE;
120
        }
121
        if ( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) {
122
            return CsvPreference.EXCEL_PREFERENCE;
123
        }
124
        if ( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) {
125
            return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
126
        }
127
        if ( "TAB_PREFERENCE".equalsIgnoreCase(s) ) {
128
            return CsvPreference.TAB_PREFERENCE;
129
        }
130
        return null;
131
    }
132

    
133
    public static QuoteMode getQuoteMode(DynObject dynobj) {
134
        String s = (String) dynobj.getDynValue(QUOTEPOLICY);
135
        if ( "AlwaysQuoteMode".equalsIgnoreCase(s) ) {
136
            return new AlwaysQuoteMode();
137
        }
138
        if ( "NormalQuoteMode".equalsIgnoreCase(s) ) {
139
            return new NormalQuoteMode();
140
        }
141
        return null;
142
    }
143

    
144
    public static IProjection getCRS(DynObject dynobj) {
145
        return (IProjection) dynobj.getDynValue(CRS);
146
    }
147

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

    
156
    public static String getRecordSeparator(DynObject dynobj) {
157
        String s = (String) dynobj.getDynValue(RECORDSEPARATOR);
158
        return StringEscapeUtils.unescapeJava(s);
159
    }
160

    
161
    public static String getGeometryColumn(DynObject dynobj) {
162
        String s = (String) dynobj.getDynValue(GEOMETRY_COLUMN);
163
        return s;
164
    }
165

    
166
    public static int getGeometryType(DynObject dynobj) {
167
        Integer gtype = (Integer) dynobj.getDynValue(GEOMETRY_TYPE);
168
        if( gtype == null ) {
169
            return Geometry.TYPES.UNKNOWN;
170
        }
171
        return gtype;
172
    }
173

    
174
    public static String getGeometryFormat(DynObject dynobj) {
175
        String gformat = (String) dynobj.getDynValue(GEOMETRY_FORMAT);
176
        if( StringUtils.isBlank(gformat) ) {
177
            return "WKT";
178
        }
179
        return gformat;
180
    }
181

    
182
    public static int getGeometrySubType(DynObject dynobj) {
183
        Integer gsubtype = (Integer) dynobj.getDynValue(GEOMETRY_SUBTYPE);
184
        if( gsubtype == null ) {
185
            return Geometry.SUBTYPES.UNKNOWN;
186
        }
187
        return gsubtype;
188
    }
189
    
190
    public static boolean isBlankOrDefaultLocale(DynObject dynobj) {
191
        String s = (String) dynobj.getDynValue(LOCALE);
192
        if (StringUtils.isBlank(s)) {
193
            return true;
194
        }
195
        return StringUtils.equalsIgnoreCase("DEFAULT",s.trim());
196
    }
197
    public static String getCommentStartMarker(DynObject dynobj) {
198
        String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER);
199
        return StringEscapeUtils.unescapeJava(s);
200
    }
201
    
202
    public static String getPointColumnName(DynObject dynobj) {
203
        String s = (String) dynobj.getDynValue(POINT_COLUMN_NAME);
204
        return s;
205
    }
206

    
207
    public static String getQuoteCharacter(DynObject dynobj) {
208
        String s = (String) dynobj.getDynValue(QUOTECHAR);
209
        s = StringEscapeUtils.unescapeJava(s);
210
        if ( StringUtils.isBlank(s) ) {
211
            return null;
212
        }
213
        return s.substring(0, 1);
214
    }
215

    
216
    public static String getDelimiter(DynObject dynobj) {
217
        String s = (String) dynobj.getDynValue(DELIMITER);
218
        s = StringEscapeUtils.unescapeJava(s);
219
        if ( StringUtils.isBlank(s) ) {
220
            return null;
221
        }
222
        return s.substring(0, 1);
223
    }
224

    
225
    public static String[] getPointDimensionNames(DynObject dynobj) {
226
        String s = (String) dynobj.getDynValue("point");
227
        if ( StringUtils.isBlank(s) ) {
228
            return null;
229
        }
230
        return s.split(",");
231
    }
232

    
233
    public static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) {
234
        Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES);
235
        return BooleanUtils.isTrue(b);
236
    }
237
    
238
    public static boolean getIncludeMetadataInHeader(DynObject dynobj) {
239
        Boolean b = (Boolean) dynobj.getDynValue(INCLUDE_METADATA_IN_HEADER);
240
        return BooleanUtils.isTrue(b);
241
    }
242

    
243
    public static boolean isFirstLineHeader(DynObject dynobj) {
244
        Boolean b = (Boolean) dynobj.getDynValue(FIRST_LINE_HEADER);
245
        return BooleanUtils.isTrue(b);
246
    }
247

    
248
    
249
}