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 @ 47638
History | View | Annotate | Download (12.2 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 |
private 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 |
// protected DelegatedDynObject getDelegatedDynObject() {
|
118 |
// return parameters;
|
119 |
// }
|
120 |
//
|
121 |
// @Override
|
122 |
// public void setDynValue(String name, Object value) {
|
123 |
// super.setDynValue(name, value);
|
124 |
// this.featureType = null;
|
125 |
// }
|
126 |
|
127 |
@Override
|
128 |
public void validate() throws ValidateDataParametersException { |
129 |
File f = this.getFile(); |
130 |
if( f!=null ) { |
131 |
IProjection proj = null;
|
132 |
FeatureType ftype = this.getFeatureType();
|
133 |
if( ftype!=null ) { |
134 |
this.setDynValue(AUTOMATICTYPESDETECTION, defaultValueOfAutomaticTypesDetection);
|
135 |
proj = ftype.getDefaultSRS(); |
136 |
if( proj!=null ) { |
137 |
this.setDynValue(CRS_PARAMTER_NAME, proj);
|
138 |
} |
139 |
FeatureAttributeDescriptor attrgeom = ftype.getDefaultGeometryAttribute(); |
140 |
if( attrgeom!=null ) { |
141 |
this.setDynValue(GEOMETRY_COLUMN, attrgeom.getName());
|
142 |
this.setDynValue(GEOMETRY_TYPE, attrgeom.getGeomType().getType());
|
143 |
this.setDynValue(GEOMETRY_SUBTYPE, attrgeom.getGeomType().getSubType());
|
144 |
} else {
|
145 |
this.setDynValue(GEOMETRY_COLUMN, null); |
146 |
this.setDynValue(GEOMETRY_TYPE, Geometry.TYPES.UNKNOWN);
|
147 |
this.setDynValue(GEOMETRY_SUBTYPE, Geometry.SUBTYPES.UNKNOWN);
|
148 |
} |
149 |
Tags ftypeTags = ftype.getTags(); |
150 |
for (String tagname : ftypeTags) { |
151 |
if( StringUtils.startsWithIgnoreCase(tagname, "csvparameters.") ) { |
152 |
String paramname = tagname.substring(14); |
153 |
String paramvalue = ftypeTags.getString(tagname,null); |
154 |
if( paramvalue!=null ) { |
155 |
this.setDynValue(paramname, paramvalue);
|
156 |
} |
157 |
} |
158 |
} |
159 |
} |
160 |
if( proj==null ) { |
161 |
PRJFile prjfile = FormatsFile.getPRJFile(f); |
162 |
if( prjfile!= null ) { |
163 |
this.setDynValue(CRS_PARAMTER_NAME, prjfile.getCRS());
|
164 |
} |
165 |
} |
166 |
String charsetName = getCharset(this); |
167 |
if( StringUtils.isBlank(charsetName) ) {
|
168 |
CPGFile cpgfile = FormatsFile.getCPGFile(f); |
169 |
if( cpgfile!=null ) { |
170 |
this.setDynValue(CHARSET, cpgfile.getCharsetName());
|
171 |
} |
172 |
} |
173 |
} |
174 |
super.validate();
|
175 |
} |
176 |
|
177 |
public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) { |
178 |
String s = (String) dynobj.getDynValue(PROFILE); |
179 |
if ( "NONE".equalsIgnoreCase(s) ) { |
180 |
return null; |
181 |
} |
182 |
if ( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
183 |
return CsvPreference.STANDARD_PREFERENCE;
|
184 |
} |
185 |
if ( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
186 |
return CsvPreference.EXCEL_PREFERENCE;
|
187 |
} |
188 |
if ( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
189 |
return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
190 |
} |
191 |
if ( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
192 |
return CsvPreference.TAB_PREFERENCE;
|
193 |
} |
194 |
return null; |
195 |
} |
196 |
|
197 |
public static QuoteMode getQuoteMode(DynObject dynobj) { |
198 |
String s = (String) dynobj.getDynValue(QUOTEPOLICY); |
199 |
if ( "AlwaysQuoteMode".equalsIgnoreCase(s) ) { |
200 |
return new AlwaysQuoteMode(); |
201 |
} |
202 |
if ( "NormalQuoteMode".equalsIgnoreCase(s) ) { |
203 |
return new NormalQuoteMode(); |
204 |
} |
205 |
return null; |
206 |
} |
207 |
|
208 |
public static IProjection getCRS(DynObject dynobj) { |
209 |
return (IProjection) dynobj.getDynValue(CRS);
|
210 |
} |
211 |
|
212 |
public static String getFileName(DynObject dynobj) { |
213 |
File f = (File) dynobj.getDynValue(FILE); |
214 |
if ( f == null ) { |
215 |
return null; |
216 |
} |
217 |
return f.getPath();
|
218 |
} |
219 |
|
220 |
public static String getRecordSeparator(DynObject dynobj) { |
221 |
String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
222 |
return StringEscapeUtils.unescapeJava(s);
|
223 |
} |
224 |
|
225 |
public static String getGeometryColumn(DynObject dynobj) { |
226 |
String s = (String) dynobj.getDynValue(GEOMETRY_COLUMN); |
227 |
return s;
|
228 |
} |
229 |
|
230 |
public static int getGeometryType(DynObject dynobj) { |
231 |
Integer gtype = (Integer) dynobj.getDynValue(GEOMETRY_TYPE); |
232 |
if( gtype == null ) { |
233 |
return Geometry.TYPES.UNKNOWN;
|
234 |
} |
235 |
return gtype;
|
236 |
} |
237 |
|
238 |
public static String getGeometryFormat(DynObject dynobj) { |
239 |
String gformat = (String) dynobj.getDynValue(GEOMETRY_FORMAT); |
240 |
if( StringUtils.isBlank(gformat) ) {
|
241 |
return "WKT"; |
242 |
} |
243 |
return gformat;
|
244 |
} |
245 |
|
246 |
public static int getGeometrySubType(DynObject dynobj) { |
247 |
Integer gsubtype = (Integer) dynobj.getDynValue(GEOMETRY_SUBTYPE); |
248 |
if( gsubtype == null ) { |
249 |
return Geometry.SUBTYPES.UNKNOWN;
|
250 |
} |
251 |
return gsubtype;
|
252 |
} |
253 |
|
254 |
public static boolean isBlankOrDefaultLocale(DynObject dynobj) { |
255 |
String s = (String) dynobj.getDynValue(LOCALE); |
256 |
if (StringUtils.isBlank(s)) {
|
257 |
return true; |
258 |
} |
259 |
return StringUtils.equalsIgnoreCase("DEFAULT",s.trim()); |
260 |
} |
261 |
public static String getCommentStartMarker(DynObject dynobj) { |
262 |
String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
263 |
return StringEscapeUtils.unescapeJava(s);
|
264 |
} |
265 |
|
266 |
public static String getPointColumnName(DynObject dynobj) { |
267 |
String s = (String) dynobj.getDynValue(POINT_COLUMN_NAME); |
268 |
return s;
|
269 |
} |
270 |
|
271 |
public static String getQuoteCharacter(DynObject dynobj) { |
272 |
String s = (String) dynobj.getDynValue(QUOTECHAR); |
273 |
s = StringEscapeUtils.unescapeJava(s); |
274 |
if ( StringUtils.isBlank(s) ) {
|
275 |
return null; |
276 |
} |
277 |
return s.substring(0, 1); |
278 |
} |
279 |
|
280 |
public static String getDelimiter(DynObject dynobj) { |
281 |
String s = (String) dynobj.getDynValue(DELIMITER); |
282 |
s = StringEscapeUtils.unescapeJava(s); |
283 |
if ( StringUtils.isBlank(s) ) {
|
284 |
return null; |
285 |
} |
286 |
return s.substring(0, 1); |
287 |
} |
288 |
|
289 |
public static String[] getPointDimensionNames(DynObject dynobj) { |
290 |
String s = (String) dynobj.getDynValue("point"); |
291 |
if ( StringUtils.isBlank(s) ) {
|
292 |
return null; |
293 |
} |
294 |
return s.split(","); |
295 |
} |
296 |
|
297 |
public static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
298 |
Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
299 |
return BooleanUtils.isTrue(b);
|
300 |
} |
301 |
|
302 |
public static boolean getIncludeMetadataInHeader(DynObject dynobj) { |
303 |
Boolean b = (Boolean) dynobj.getDynValue(INCLUDE_METADATA_IN_HEADER); |
304 |
return BooleanUtils.isTrue(b);
|
305 |
} |
306 |
|
307 |
public static boolean isFirstLineHeader(DynObject dynobj) { |
308 |
Boolean b = (Boolean) dynobj.getDynValue(FIRST_LINE_HEADER); |
309 |
return BooleanUtils.isTrue(b);
|
310 |
} |
311 |
|
312 |
|
313 |
} |