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 @ 41006
History | View | Annotate | Download (8.28 KB)
1 | 40846 | jjdelcerro | /**
|
---|---|---|---|
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 | |||
28 | import org.apache.commons.lang3.StringEscapeUtils; |
||
29 | import org.cresques.cts.IProjection; |
||
30 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
31 | import org.gvsig.fmap.dal.FileHelper; |
||
32 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
||
33 | import org.gvsig.fmap.dal.spi.AbstractDataParameters; |
||
34 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
35 | import org.gvsig.tools.ToolsLocator; |
||
36 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
37 | import org.gvsig.tools.dynobject.DelegatedDynObject; |
||
38 | import org.gvsig.tools.dynobject.DynObject; |
||
39 | 40878 | jjdelcerro | import org.supercsv.prefs.CsvPreference; |
40 | 41006 | jjdelcerro | import org.supercsv.quote.AlwaysQuoteMode; |
41 | import org.supercsv.quote.NormalQuoteMode; |
||
42 | import org.supercsv.quote.QuoteMode; |
||
43 | 40846 | jjdelcerro | |
44 | public class CSVStoreParameters extends AbstractDataParameters implements |
||
45 | 40886 | jjdelcerro | DataStoreParameters, FilesystemStoreParameters { |
46 | 40846 | jjdelcerro | |
47 | public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters"; |
||
48 | |||
49 | private static final String FILE = "file"; |
||
50 | private static final String PROFILE = "profile"; |
||
51 | 41006 | jjdelcerro | private static final String QUOTEPOLICY = "quotePolicy"; |
52 | private static final String QUOTECHAR = "quoteCharacter"; |
||
53 | 40846 | jjdelcerro | private static final String RECORDSEPARATOR = "recordSeparator"; |
54 | private static final String DELIMITER = "delimiter"; |
||
55 | private static final String COMMENTSTARTMARKER = "commentStartMarker"; |
||
56 | 40878 | jjdelcerro | |
57 | //private static final String ESCAPECHARACTER = "escapeCharacter";
|
||
58 | |||
59 | 40846 | jjdelcerro | private static final String HEADER = "header"; |
60 | 40878 | jjdelcerro | private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
61 | |||
62 | //private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
|
||
63 | 40846 | jjdelcerro | private static final String CRS = "CRS"; |
64 | private static final String FIELDTYPES = "fieldtypes"; |
||
65 | 40878 | jjdelcerro | // private static final String NULLTO = "nullTo";
|
66 | 40846 | jjdelcerro | private static final String CHARSET = "charset"; // Default "UTF-8" |
67 | |||
68 | |||
69 | |||
70 | private DelegatedDynObject parameters;
|
||
71 | |||
72 | |||
73 | public CSVStoreParameters() {
|
||
74 | this(PARAMETERS_DEFINITION_NAME);
|
||
75 | } |
||
76 | |||
77 | protected CSVStoreParameters(String parametersDefinitionName) { |
||
78 | this(parametersDefinitionName, CSVStoreProvider.NAME);
|
||
79 | } |
||
80 | |||
81 | public CSVStoreParameters(String parametersDefinitionName, String name) { |
||
82 | super();
|
||
83 | this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
|
||
84 | this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
|
||
85 | } |
||
86 | |||
87 | public String getDataStoreName() { |
||
88 | return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
||
89 | } |
||
90 | |||
91 | public String getDescription() { |
||
92 | return this.getDynClass().getDescription(); |
||
93 | } |
||
94 | |||
95 | protected DelegatedDynObject getDelegatedDynObject() {
|
||
96 | return parameters;
|
||
97 | } |
||
98 | |||
99 | public boolean isValid() { |
||
100 | if (getFileName(this) == null) { |
||
101 | return false; |
||
102 | } |
||
103 | return true; |
||
104 | } |
||
105 | |||
106 | public File getFile() { |
||
107 | return (File) this.getDynValue(FILE); |
||
108 | } |
||
109 | |||
110 | public void setFile(File file) { |
||
111 | this.setDynValue(FILE, file);
|
||
112 | } |
||
113 | 40878 | jjdelcerro | |
114 | 40846 | jjdelcerro | |
115 | 40878 | jjdelcerro | |
116 | static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
|
||
117 | 40846 | jjdelcerro | String s = (String) dynobj.getDynValue(PROFILE); |
118 | if( "NONE".equalsIgnoreCase(s) ) { |
||
119 | return null; |
||
120 | } |
||
121 | 40878 | jjdelcerro | if( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
122 | return CsvPreference.STANDARD_PREFERENCE;
|
||
123 | 40846 | jjdelcerro | } |
124 | 40878 | jjdelcerro | if( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
125 | return CsvPreference.EXCEL_PREFERENCE;
|
||
126 | 40846 | jjdelcerro | } |
127 | 40878 | jjdelcerro | if( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
128 | return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
||
129 | 40846 | jjdelcerro | } |
130 | 40878 | jjdelcerro | if( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
131 | return CsvPreference.TAB_PREFERENCE;
|
||
132 | 40846 | jjdelcerro | } |
133 | return null ; |
||
134 | } |
||
135 | |||
136 | 41006 | jjdelcerro | static QuoteMode getQuoteMode(DynObject dynobj) {
|
137 | String s = (String) dynobj.getDynValue(QUOTEPOLICY); |
||
138 | if( "AlwaysQuoteMode".equalsIgnoreCase(s) ) { |
||
139 | return new AlwaysQuoteMode(); |
||
140 | } |
||
141 | if( "NormalQuoteMode".equalsIgnoreCase(s) ) { |
||
142 | return new NormalQuoteMode(); |
||
143 | } |
||
144 | return null; |
||
145 | } |
||
146 | |||
147 | 40846 | jjdelcerro | static IProjection getCRS(DynObject dynobj) {
|
148 | return (IProjection) dynobj.getDynValue(CRS);
|
||
149 | } |
||
150 | |||
151 | static String getFileName(DynObject dynobj) { |
||
152 | File f = (File) dynobj.getDynValue(FILE); |
||
153 | if( f == null ) { |
||
154 | return null; |
||
155 | } |
||
156 | return f.getPath();
|
||
157 | } |
||
158 | |||
159 | static File getFile(DynObject dynobj) { |
||
160 | File f = (File) dynobj.getDynValue(FILE); |
||
161 | return f;
|
||
162 | } |
||
163 | |||
164 | static String getRecordSeparator(DynObject dynobj) { |
||
165 | String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
||
166 | return StringEscapeUtils.unescapeJava(s);
|
||
167 | } |
||
168 | |||
169 | static String getCommentStartMarker(DynObject dynobj) { |
||
170 | String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
||
171 | return StringEscapeUtils.unescapeJava(s);
|
||
172 | } |
||
173 | |||
174 | 40878 | jjdelcerro | // static String getEscapeCharacter(DynObject dynobj) {
|
175 | // String s = (String) dynobj.getDynValue(ESCAPECHARACTER);
|
||
176 | // return StringEscapeUtils.unescapeJava(s);
|
||
177 | // }
|
||
178 | //
|
||
179 | 40846 | jjdelcerro | static String getQuoteCharacter(DynObject dynobj) { |
180 | String s = (String) dynobj.getDynValue(QUOTECHAR); |
||
181 | 40878 | jjdelcerro | s = StringEscapeUtils.unescapeJava(s); |
182 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
183 | 40878 | jjdelcerro | return null; |
184 | } |
||
185 | return s.substring(0, 1); |
||
186 | 40846 | jjdelcerro | } |
187 | |||
188 | static String getDelimiter(DynObject dynobj) { |
||
189 | String s = (String) dynobj.getDynValue(DELIMITER); |
||
190 | 40878 | jjdelcerro | s = StringEscapeUtils.unescapeJava(s); |
191 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
192 | 40878 | jjdelcerro | return null; |
193 | } |
||
194 | return s.substring(0, 1); |
||
195 | 40846 | jjdelcerro | } |
196 | |||
197 | static String getHeader(DynObject dynobj) { |
||
198 | String s = (String) dynobj.getDynValue(HEADER); |
||
199 | s = StringEscapeUtils.unescapeJava(s); |
||
200 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
201 | 40886 | jjdelcerro | return null; |
202 | 40846 | jjdelcerro | } |
203 | return s;
|
||
204 | } |
||
205 | 40886 | jjdelcerro | |
206 | static String[] getHeaders(DynObject dynobj) { |
||
207 | String s = getHeader(dynobj);
|
||
208 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
209 | 40886 | jjdelcerro | return null; |
210 | } |
||
211 | String sep = getDelimiter(dynobj);
|
||
212 | if( sep == null ) { |
||
213 | sep = getDelimiter(s); |
||
214 | if( sep == null ) { |
||
215 | // Chungo
|
||
216 | return null; |
||
217 | } |
||
218 | } |
||
219 | String[] ss = s.split("["+sep+"]"); |
||
220 | return ss;
|
||
221 | } |
||
222 | |||
223 | private static String getDelimiter(String line) { |
||
224 | String sep = null; |
||
225 | String seps = ",;:-|@#/+$%&!"; |
||
226 | for( int i=0; i<seps.length(); i ++) { |
||
227 | sep = seps.substring(i, 1);
|
||
228 | if( line.contains(seps.substring(i, 1))) { |
||
229 | break;
|
||
230 | } |
||
231 | sep = null;
|
||
232 | } |
||
233 | return sep;
|
||
234 | } |
||
235 | 40878 | jjdelcerro | // static String getNullTo(DynObject dynobj) {
|
236 | // String s = (String) dynobj.getDynValue(NULLTO);
|
||
237 | // return StringEscapeUtils.unescapeJava(s);
|
||
238 | // }
|
||
239 | 40846 | jjdelcerro | |
240 | static String getCharset(DynObject dynobj) { |
||
241 | String s = (String) dynobj.getDynValue(CHARSET); |
||
242 | return StringEscapeUtils.unescapeJava(s);
|
||
243 | } |
||
244 | |||
245 | 41006 | jjdelcerro | static String[] getPointDimensionNames(DynObject dynobj) { |
246 | String s = (String) dynobj.getDynValue("point"); |
||
247 | if( isEmpty(s) ) {
|
||
248 | return null; |
||
249 | } |
||
250 | return s.split(","); |
||
251 | } |
||
252 | |||
253 | 40878 | jjdelcerro | static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
254 | Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
||
255 | if( b==null ) { |
||
256 | return false; |
||
257 | } |
||
258 | 40846 | jjdelcerro | return b.booleanValue();
|
259 | } |
||
260 | |||
261 | 40878 | jjdelcerro | // static boolean getIgnoreEmptyLines(DynObject dynobj) {
|
262 | // Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES);
|
||
263 | // return b.booleanValue();
|
||
264 | // }
|
||
265 | 40846 | jjdelcerro | |
266 | static int[] getFieldTypes(DynObject dynobj) { |
||
267 | String s = (String) dynobj.getDynValue(FIELDTYPES); |
||
268 | 40894 | jjdelcerro | if( isEmpty(s) ) {
|
269 | 40846 | jjdelcerro | return null; |
270 | } |
||
271 | 40886 | jjdelcerro | String sep = getDelimiter(s);
|
272 | if( sep == null ) { |
||
273 | return null; |
||
274 | 40846 | jjdelcerro | } |
275 | 40886 | jjdelcerro | DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
276 | String fieldTypeNames[] = s.split("["+sep+"]"); |
||
277 | 40846 | jjdelcerro | int fieldTypes[] = new int[fieldTypeNames.length]; |
278 | for( int i=0; i<fieldTypeNames.length; i++ ) { |
||
279 | 40886 | jjdelcerro | fieldTypes[i] = dataTypeManager.getType(fieldTypeNames[i].trim()); |
280 | 40846 | jjdelcerro | } |
281 | return fieldTypes;
|
||
282 | } |
||
283 | |||
284 | 40894 | jjdelcerro | static private boolean isEmpty(String s) { |
285 | if( s == null ) { |
||
286 | return true; |
||
287 | } |
||
288 | if( s.trim().length() == 0 ) { |
||
289 | return true; |
||
290 | } |
||
291 | return false; |
||
292 | } |
||
293 | 40846 | jjdelcerro | } |