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 @ 47642
History | View | Annotate | Download (10.4 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 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 org.gvsig.fmap.dal.DALLocator; |
36 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
37 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
38 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
39 |
import org.gvsig.fmap.dal.feature.FeatureType; |
40 |
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters; |
41 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
42 |
import org.gvsig.fmap.dal.store.gml.GMLFeatureTypeLoader; |
43 |
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderFeatureTypeLoader; |
44 |
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderStoreParameters; |
45 |
import org.gvsig.fmap.geom.Geometry; |
46 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
47 |
import org.gvsig.tools.dynobject.DynObject; |
48 |
import org.gvsig.tools.dynobject.Tags; |
49 |
import org.slf4j.Logger; |
50 |
import org.slf4j.LoggerFactory; |
51 |
import org.supercsv.prefs.CsvPreference; |
52 |
import org.supercsv.quote.AlwaysQuoteMode; |
53 |
import org.supercsv.quote.NormalQuoteMode; |
54 |
import org.supercsv.quote.QuoteMode; |
55 |
|
56 |
@SuppressWarnings("UseSpecificCatch") |
57 |
public class CSVStoreParameters extends SimpleReaderStoreParameters implements |
58 |
OpenFeatureStoreParameters, FilesystemStoreParameters { |
59 |
|
60 |
private static final Logger LOGGER = LoggerFactory.getLogger(CSVStoreParameters.class); |
61 |
|
62 |
public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters"; |
63 |
|
64 |
// private static final String FILE = "file";
|
65 |
// private static final String IGNOREERRORS = "ignoreErrors";
|
66 |
private static final String PROFILE = "profile"; |
67 |
private static final String QUOTEPOLICY = "quotePolicy"; |
68 |
private static final String QUOTECHAR = "quoteCharacter"; |
69 |
private static final String RECORDSEPARATOR = "recordSeparator"; |
70 |
private static final String DELIMITER = "delimiter"; |
71 |
private static final String COMMENTSTARTMARKER = "commentStartMarker"; |
72 |
// private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
|
73 |
|
74 |
private static final String ESCAPECHARACTER = "escapeCharacter"; |
75 |
public static final String FIRST_LINE_HEADER = "firstLineHeader"; |
76 |
// public static final String HEADER = "header";
|
77 |
private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
78 |
|
79 |
//private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
|
80 |
// private static final String CRS = CRS_PARAMTER_NAME;
|
81 |
// private static final String FIELDTYPES = "fieldtypes";
|
82 |
// private static final String CHARSET = "charset"; // Default "UTF-8"
|
83 |
// private static final String LOCALE = "locale";
|
84 |
private static final String POINT_COLUMN_NAME = "pointColumnName"; |
85 |
private static final String INCLUDE_METADATA_IN_HEADER = "includeMetadataInHeader"; |
86 |
private static final String GEOMETRY_COLUMN = "geometry_column"; |
87 |
private static final String GEOMETRY_TYPE = "GeometryType"; |
88 |
private static final String GEOMETRY_SUBTYPE = "GeometrySubtype"; |
89 |
private static final String GEOMETRY_FORMAT = "GeometryFormat"; |
90 |
|
91 |
public CSVStoreParameters() {
|
92 |
this(PARAMETERS_DEFINITION_NAME);
|
93 |
} |
94 |
|
95 |
protected CSVStoreParameters(String parametersDefinitionName) { |
96 |
super(parametersDefinitionName, CSVStoreProvider.NAME);
|
97 |
} |
98 |
|
99 |
protected FeatureType getFeatureType() {
|
100 |
if( this.featureType==null ) { |
101 |
try {
|
102 |
EditableFeatureType ftype = DALLocator.getDataManager().createFeatureType(); |
103 |
SimpleReaderFeatureTypeLoader featureTypeLoader = new CSVFeatureTypeLoader(this); |
104 |
featureTypeLoader.loadFeatureType(ftype, false, null); |
105 |
boolean all_fields_declare_type = featureTypeLoader.isAllFieldsDeclareType();
|
106 |
defaultValueOfAutomaticTypesDetection = !all_fields_declare_type; |
107 |
this.featureType = ftype;
|
108 |
} catch (Exception ex) { |
109 |
LOGGER.debug("Can't detect feature type from csv header", ex);
|
110 |
// Do nothing, continue
|
111 |
} |
112 |
} |
113 |
return this.featureType; |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void validate() throws ValidateDataParametersException { |
118 |
super.validate();
|
119 |
FeatureType ftype = this.getFeatureType();
|
120 |
if( ftype!=null ) { |
121 |
FeatureAttributeDescriptor attrgeom = ftype.getDefaultGeometryAttribute(); |
122 |
if( attrgeom!=null ) { |
123 |
this.setDynValue(GEOMETRY_COLUMN, attrgeom.getName());
|
124 |
this.setDynValue(GEOMETRY_TYPE, attrgeom.getGeomType().getType());
|
125 |
this.setDynValue(GEOMETRY_SUBTYPE, attrgeom.getGeomType().getSubType());
|
126 |
} else {
|
127 |
this.setDynValue(GEOMETRY_COLUMN, null); |
128 |
this.setDynValue(GEOMETRY_TYPE, Geometry.TYPES.UNKNOWN);
|
129 |
this.setDynValue(GEOMETRY_SUBTYPE, Geometry.SUBTYPES.UNKNOWN);
|
130 |
} |
131 |
} |
132 |
} |
133 |
|
134 |
public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) { |
135 |
String s = (String) dynobj.getDynValue(PROFILE); |
136 |
if ( "NONE".equalsIgnoreCase(s) ) { |
137 |
return null; |
138 |
} |
139 |
if ( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
140 |
return CsvPreference.STANDARD_PREFERENCE;
|
141 |
} |
142 |
if ( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
143 |
return CsvPreference.EXCEL_PREFERENCE;
|
144 |
} |
145 |
if ( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
146 |
return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
147 |
} |
148 |
if ( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
149 |
return CsvPreference.TAB_PREFERENCE;
|
150 |
} |
151 |
return null; |
152 |
} |
153 |
|
154 |
public static QuoteMode getQuoteMode(DynObject dynobj) { |
155 |
String s = (String) dynobj.getDynValue(QUOTEPOLICY); |
156 |
if ( "AlwaysQuoteMode".equalsIgnoreCase(s) ) { |
157 |
return new AlwaysQuoteMode(); |
158 |
} |
159 |
if ( "NormalQuoteMode".equalsIgnoreCase(s) ) { |
160 |
return new NormalQuoteMode(); |
161 |
} |
162 |
return null; |
163 |
} |
164 |
|
165 |
public static IProjection getCRS(DynObject dynobj) { |
166 |
return (IProjection) dynobj.getDynValue(CRS);
|
167 |
} |
168 |
|
169 |
public static String getFileName(DynObject dynobj) { |
170 |
File f = (File) dynobj.getDynValue(FILE); |
171 |
if ( f == null ) { |
172 |
return null; |
173 |
} |
174 |
return f.getPath();
|
175 |
} |
176 |
|
177 |
public static String getRecordSeparator(DynObject dynobj) { |
178 |
String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
179 |
return StringEscapeUtils.unescapeJava(s);
|
180 |
} |
181 |
|
182 |
public static String getGeometryColumn(DynObject dynobj) { |
183 |
String s = (String) dynobj.getDynValue(GEOMETRY_COLUMN); |
184 |
return s;
|
185 |
} |
186 |
|
187 |
public static int getGeometryType(DynObject dynobj) { |
188 |
Integer gtype = (Integer) dynobj.getDynValue(GEOMETRY_TYPE); |
189 |
if( gtype == null ) { |
190 |
return Geometry.TYPES.UNKNOWN;
|
191 |
} |
192 |
return gtype;
|
193 |
} |
194 |
|
195 |
public static String getGeometryFormat(DynObject dynobj) { |
196 |
String gformat = (String) dynobj.getDynValue(GEOMETRY_FORMAT); |
197 |
if( StringUtils.isBlank(gformat) ) {
|
198 |
return "WKT"; |
199 |
} |
200 |
return gformat;
|
201 |
} |
202 |
|
203 |
public static int getGeometrySubType(DynObject dynobj) { |
204 |
Integer gsubtype = (Integer) dynobj.getDynValue(GEOMETRY_SUBTYPE); |
205 |
if( gsubtype == null ) { |
206 |
return Geometry.SUBTYPES.UNKNOWN;
|
207 |
} |
208 |
return gsubtype;
|
209 |
} |
210 |
|
211 |
public static boolean isBlankOrDefaultLocale(DynObject dynobj) { |
212 |
String s = (String) dynobj.getDynValue(LOCALE); |
213 |
if (StringUtils.isBlank(s)) {
|
214 |
return true; |
215 |
} |
216 |
return StringUtils.equalsIgnoreCase("DEFAULT",s.trim()); |
217 |
} |
218 |
public static String getCommentStartMarker(DynObject dynobj) { |
219 |
String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
220 |
return StringEscapeUtils.unescapeJava(s);
|
221 |
} |
222 |
|
223 |
public static String getPointColumnName(DynObject dynobj) { |
224 |
String s = (String) dynobj.getDynValue(POINT_COLUMN_NAME); |
225 |
return s;
|
226 |
} |
227 |
|
228 |
public static String getQuoteCharacter(DynObject dynobj) { |
229 |
String s = (String) dynobj.getDynValue(QUOTECHAR); |
230 |
s = StringEscapeUtils.unescapeJava(s); |
231 |
if ( StringUtils.isBlank(s) ) {
|
232 |
return null; |
233 |
} |
234 |
return s.substring(0, 1); |
235 |
} |
236 |
|
237 |
public static String getDelimiter(DynObject dynobj) { |
238 |
String s = (String) dynobj.getDynValue(DELIMITER); |
239 |
s = StringEscapeUtils.unescapeJava(s); |
240 |
if ( StringUtils.isBlank(s) ) {
|
241 |
return null; |
242 |
} |
243 |
return s.substring(0, 1); |
244 |
} |
245 |
|
246 |
public static String[] getPointDimensionNames(DynObject dynobj) { |
247 |
String s = (String) dynobj.getDynValue("point"); |
248 |
if ( StringUtils.isBlank(s) ) {
|
249 |
return null; |
250 |
} |
251 |
return s.split(","); |
252 |
} |
253 |
|
254 |
public static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
255 |
Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
256 |
return BooleanUtils.isTrue(b);
|
257 |
} |
258 |
|
259 |
public static boolean getIncludeMetadataInHeader(DynObject dynobj) { |
260 |
Boolean b = (Boolean) dynobj.getDynValue(INCLUDE_METADATA_IN_HEADER); |
261 |
return BooleanUtils.isTrue(b);
|
262 |
} |
263 |
|
264 |
public static boolean isFirstLineHeader(DynObject dynobj) { |
265 |
Boolean b = (Boolean) dynobj.getDynValue(FIRST_LINE_HEADER); |
266 |
return BooleanUtils.isTrue(b);
|
267 |
} |
268 |
|
269 |
|
270 |
} |