Revision 41617 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
CSVStoreParameters.java | ||
---|---|---|
10 | 10 |
* |
11 | 11 |
* This program is distributed in the hope that it will be useful, |
12 | 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 |
* GNU General Public License for more details. |
15 | 15 |
* |
16 | 16 |
* You should have received a copy of the GNU General Public License |
17 | 17 |
* along with this program; if not, write to the Free Software |
18 | 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
19 |
* MA 02110-1301, USA.
|
|
19 |
* MA 02110-1301, USA. |
|
20 | 20 |
* |
21 | 21 |
* For any additional information, do not hesitate to contact us |
22 | 22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
... | ... | |
25 | 25 |
|
26 | 26 |
import java.io.File; |
27 | 27 |
import java.util.Locale; |
28 |
import org.apache.commons.lang3.BooleanUtils; |
|
28 | 29 |
|
29 | 30 |
import org.apache.commons.lang3.StringEscapeUtils; |
31 |
import org.apache.commons.lang3.StringUtils; |
|
30 | 32 |
import org.cresques.cts.IProjection; |
31 | 33 |
import org.gvsig.fmap.dal.FileHelper; |
32 | 34 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
... | ... | |
45 | 47 |
import org.supercsv.quote.QuoteMode; |
46 | 48 |
|
47 | 49 |
public class CSVStoreParameters extends AbstractDataParameters implements |
48 |
OpenDataStoreParameters, FilesystemStoreParameters {
|
|
50 |
OpenDataStoreParameters, FilesystemStoreParameters {
|
|
49 | 51 |
|
50 |
private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class);
|
|
51 |
|
|
52 |
private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class);
|
|
53 |
|
|
52 | 54 |
public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters"; |
53 | 55 |
|
54 | 56 |
private static final String FILE = "file"; |
... | ... | |
60 | 62 |
private static final String DELIMITER = "delimiter"; |
61 | 63 |
private static final String COMMENTSTARTMARKER = "commentStartMarker"; |
62 | 64 |
private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
63 |
|
|
65 |
|
|
64 | 66 |
//private static final String ESCAPECHARACTER = "escapeCharacter"; |
65 |
|
|
66 | 67 |
private static final String HEADER = "header"; |
67 | 68 |
private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
68 |
|
|
69 |
|
|
69 | 70 |
//private static final String IGNOREEMPTYLINES = "ignoreEmptyLines"; |
70 | 71 |
private static final String CRS = "CRS"; |
71 | 72 |
private static final String FIELDTYPES = "fieldtypes"; |
72 | 73 |
// private static final String NULLTO = "nullTo"; |
73 | 74 |
private static final String CHARSET = "charset"; // Default "UTF-8" |
74 |
private static final String LOCALE = "locale";
|
|
75 |
private static final String LOCALE = "locale"; |
|
75 | 76 |
|
76 |
|
|
77 |
private DelegatedDynObject parameters; |
|
77 | 78 |
|
78 |
private DelegatedDynObject parameters; |
|
79 |
public CSVStoreParameters() { |
|
80 |
this(PARAMETERS_DEFINITION_NAME); |
|
81 |
} |
|
79 | 82 |
|
80 |
|
|
81 |
public CSVStoreParameters() { |
|
82 |
this(PARAMETERS_DEFINITION_NAME); |
|
83 |
} |
|
83 |
protected CSVStoreParameters(String parametersDefinitionName) { |
|
84 |
this(parametersDefinitionName, CSVStoreProvider.NAME); |
|
85 |
} |
|
84 | 86 |
|
85 |
protected CSVStoreParameters(String parametersDefinitionName) { |
|
86 |
this(parametersDefinitionName, CSVStoreProvider.NAME); |
|
87 |
} |
|
87 |
public CSVStoreParameters(String parametersDefinitionName, String name) { |
|
88 |
super(); |
|
89 |
this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName); |
|
90 |
this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name); |
|
91 |
} |
|
88 | 92 |
|
89 |
public CSVStoreParameters(String parametersDefinitionName, String name) { |
|
90 |
super(); |
|
91 |
this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName); |
|
92 |
this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name); |
|
93 |
} |
|
93 |
public String getDataStoreName() { |
|
94 |
return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
|
95 |
} |
|
94 | 96 |
|
95 |
public String getDataStoreName() {
|
|
96 |
return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
|
|
97 |
}
|
|
97 |
public String getDescription() {
|
|
98 |
return this.getDynClass().getDescription();
|
|
99 |
}
|
|
98 | 100 |
|
99 |
public String getDescription() {
|
|
100 |
return this.getDynClass().getDescription();
|
|
101 |
}
|
|
101 |
protected DelegatedDynObject getDelegatedDynObject() {
|
|
102 |
return parameters;
|
|
103 |
}
|
|
102 | 104 |
|
103 |
protected DelegatedDynObject getDelegatedDynObject() { |
|
104 |
return parameters; |
|
105 |
} |
|
105 |
public boolean isValid() { |
|
106 |
if ( getFileName(this) == null ) { |
|
107 |
return false; |
|
108 |
} |
|
109 |
return true; |
|
110 |
} |
|
106 | 111 |
|
107 |
public boolean isValid() { |
|
108 |
if (getFileName(this) == null) { |
|
109 |
return false; |
|
110 |
} |
|
111 |
return true; |
|
112 |
} |
|
112 |
public File getFile() { |
|
113 |
return (File) this.getDynValue(FILE); |
|
114 |
} |
|
113 | 115 |
|
114 |
public File getFile() {
|
|
115 |
return (File) this.getDynValue(FILE);
|
|
116 |
}
|
|
116 |
public void setFile(File file) {
|
|
117 |
this.setDynValue(FILE, file);
|
|
118 |
}
|
|
117 | 119 |
|
118 |
public void setFile(File file) { |
|
119 |
this.setDynValue(FILE, file); |
|
120 |
} |
|
121 |
|
|
120 |
public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) { |
|
121 |
String s = (String) dynobj.getDynValue(PROFILE); |
|
122 |
if ( "NONE".equalsIgnoreCase(s) ) { |
|
123 |
return null; |
|
124 |
} |
|
125 |
if ( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
|
126 |
return CsvPreference.STANDARD_PREFERENCE; |
|
127 |
} |
|
128 |
if ( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
|
129 |
return CsvPreference.EXCEL_PREFERENCE; |
|
130 |
} |
|
131 |
if ( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
|
132 |
return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE; |
|
133 |
} |
|
134 |
if ( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
|
135 |
return CsvPreference.TAB_PREFERENCE; |
|
136 |
} |
|
137 |
return null; |
|
138 |
} |
|
122 | 139 |
|
140 |
public static QuoteMode getQuoteMode(DynObject dynobj) { |
|
141 |
String s = (String) dynobj.getDynValue(QUOTEPOLICY); |
|
142 |
if ( "AlwaysQuoteMode".equalsIgnoreCase(s) ) { |
|
143 |
return new AlwaysQuoteMode(); |
|
144 |
} |
|
145 |
if ( "NormalQuoteMode".equalsIgnoreCase(s) ) { |
|
146 |
return new NormalQuoteMode(); |
|
147 |
} |
|
148 |
return null; |
|
149 |
} |
|
123 | 150 |
|
124 |
static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) { |
|
125 |
String s = (String) dynobj.getDynValue(PROFILE); |
|
126 |
if( "NONE".equalsIgnoreCase(s) ) { |
|
127 |
return null; |
|
128 |
} |
|
129 |
if( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
|
130 |
return CsvPreference.STANDARD_PREFERENCE; |
|
131 |
} |
|
132 |
if( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
|
133 |
return CsvPreference.EXCEL_PREFERENCE; |
|
134 |
} |
|
135 |
if( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
|
136 |
return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE; |
|
137 |
} |
|
138 |
if( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
|
139 |
return CsvPreference.TAB_PREFERENCE; |
|
140 |
} |
|
141 |
return null ; |
|
142 |
} |
|
143 |
|
|
144 |
static QuoteMode getQuoteMode(DynObject dynobj) { |
|
145 |
String s = (String) dynobj.getDynValue(QUOTEPOLICY); |
|
146 |
if( "AlwaysQuoteMode".equalsIgnoreCase(s) ) { |
|
147 |
return new AlwaysQuoteMode(); |
|
148 |
} |
|
149 |
if( "NormalQuoteMode".equalsIgnoreCase(s) ) { |
|
150 |
return new NormalQuoteMode(); |
|
151 |
} |
|
152 |
return null; |
|
153 |
} |
|
154 |
|
|
155 |
static IProjection getCRS(DynObject dynobj) { |
|
156 |
return (IProjection) dynobj.getDynValue(CRS); |
|
157 |
} |
|
151 |
static IProjection getCRS(DynObject dynobj) { |
|
152 |
return (IProjection) dynobj.getDynValue(CRS); |
|
153 |
} |
|
158 | 154 |
|
159 |
static String getFileName(DynObject dynobj) {
|
|
160 |
File f = (File) dynobj.getDynValue(FILE);
|
|
161 |
if( f == null ) {
|
|
162 |
return null;
|
|
163 |
}
|
|
164 |
return f.getPath();
|
|
165 |
}
|
|
155 |
static String getFileName(DynObject dynobj) {
|
|
156 |
File f = (File) dynobj.getDynValue(FILE);
|
|
157 |
if ( f == null ) {
|
|
158 |
return null;
|
|
159 |
}
|
|
160 |
return f.getPath();
|
|
161 |
}
|
|
166 | 162 |
|
167 |
static File getFile(DynObject dynobj) {
|
|
168 |
File f = (File) dynobj.getDynValue(FILE);
|
|
169 |
return f;
|
|
170 |
}
|
|
163 |
static File getFile(DynObject dynobj) {
|
|
164 |
File f = (File) dynobj.getDynValue(FILE);
|
|
165 |
return f;
|
|
166 |
}
|
|
171 | 167 |
|
172 |
static String getRecordSeparator(DynObject dynobj) { |
|
173 |
String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
|
174 |
return StringEscapeUtils.unescapeJava(s); |
|
175 |
} |
|
176 |
|
|
177 |
static Locale getLocale(DynObject dynobj) { |
|
178 |
try { |
|
179 |
String s = (String) dynobj.getDynValue(LOCALE); |
|
180 |
if( s.trim().length()==0 ) { |
|
181 |
return null; |
|
182 |
} |
|
183 |
if( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
|
184 |
return Locale.getDefault(); |
|
185 |
} |
|
186 |
Locale locale = null; |
|
187 |
// locale = Locale.forLanguageTag(s); // Since java 1.7 |
|
188 |
String[] ss = s.split("-"); |
|
189 |
switch(ss.length) { |
|
190 |
case 1: |
|
191 |
locale = new Locale(ss[0]); |
|
192 |
break; |
|
193 |
case 2: |
|
194 |
locale = new Locale(ss[0],ss[1]); |
|
195 |
break; |
|
196 |
case 3: |
|
197 |
default: |
|
198 |
locale = new Locale(ss[0],ss[1],ss[2]); |
|
199 |
break; |
|
200 |
} |
|
201 |
return locale; |
|
202 |
} catch( Exception ex) { |
|
203 |
logger.warn("Can't get locale from CSV parameters.", ex); |
|
204 |
return null; |
|
205 |
} |
|
206 |
} |
|
207 |
|
|
208 |
static String getCommentStartMarker(DynObject dynobj) { |
|
209 |
String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
|
210 |
return StringEscapeUtils.unescapeJava(s); |
|
211 |
} |
|
168 |
public static String getRecordSeparator(DynObject dynobj) { |
|
169 |
String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
|
170 |
return StringEscapeUtils.unescapeJava(s); |
|
171 |
} |
|
212 | 172 |
|
213 |
// static String getEscapeCharacter(DynObject dynobj) { |
|
214 |
// String s = (String) dynobj.getDynValue(ESCAPECHARACTER); |
|
215 |
// return StringEscapeUtils.unescapeJava(s); |
|
216 |
// } |
|
217 |
// |
|
218 |
static String getQuoteCharacter(DynObject dynobj) { |
|
219 |
String s = (String) dynobj.getDynValue(QUOTECHAR); |
|
220 |
s = StringEscapeUtils.unescapeJava(s); |
|
221 |
if( isEmpty(s) ) { |
|
222 |
return null; |
|
223 |
} |
|
224 |
return s.substring(0, 1); |
|
225 |
} |
|
226 |
|
|
227 |
static String getDelimiter(DynObject dynobj) { |
|
228 |
String s = (String) dynobj.getDynValue(DELIMITER); |
|
229 |
s = StringEscapeUtils.unescapeJava(s); |
|
230 |
if( isEmpty(s) ) { |
|
231 |
return null; |
|
232 |
} |
|
233 |
return s.substring(0, 1); |
|
234 |
} |
|
235 |
|
|
236 |
static String getHeader(DynObject dynobj) { |
|
237 |
String s = (String) dynobj.getDynValue(HEADER); |
|
238 |
s = StringEscapeUtils.unescapeJava(s); |
|
239 |
if( isEmpty(s) ) { |
|
240 |
return null; |
|
241 |
} |
|
242 |
return s; |
|
243 |
} |
|
173 |
static Locale getLocale(DynObject dynobj) { |
|
174 |
try { |
|
175 |
String s = (String) dynobj.getDynValue(LOCALE); |
|
176 |
if ( s.trim().length() == 0 ) { |
|
177 |
return null; |
|
178 |
} |
|
179 |
if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
|
180 |
return Locale.getDefault(); |
|
181 |
} |
|
182 |
Locale locale = null; |
|
183 |
// locale = Locale.forLanguageTag(s); // Since java 1.7 |
|
184 |
String[] ss = s.split("-"); |
|
185 |
switch (ss.length) { |
|
186 |
case 1: |
|
187 |
locale = new Locale(ss[0]); |
|
188 |
break; |
|
189 |
case 2: |
|
190 |
locale = new Locale(ss[0], ss[1]); |
|
191 |
break; |
|
192 |
case 3: |
|
193 |
default: |
|
194 |
locale = new Locale(ss[0], ss[1], ss[2]); |
|
195 |
break; |
|
196 |
} |
|
197 |
return locale; |
|
198 |
} catch (Exception ex) { |
|
199 |
logger.warn("Can't get locale from CSV parameters.", ex); |
|
200 |
return null; |
|
201 |
} |
|
202 |
} |
|
244 | 203 |
|
245 |
static String[] getHeaders(DynObject dynobj) { |
|
246 |
String s = getHeader(dynobj); |
|
247 |
if( isEmpty(s) ) { |
|
248 |
return null; |
|
249 |
} |
|
250 |
String sep = getDelimiter(dynobj); |
|
251 |
if( sep == null ) { |
|
252 |
sep = getDelimiter(s); |
|
253 |
if( sep == null ) { |
|
254 |
// Chungo |
|
255 |
return null; |
|
256 |
} |
|
257 |
} |
|
258 |
String[] ss = s.split("["+sep+"]"); |
|
259 |
return ss; |
|
260 |
} |
|
204 |
public static String getCommentStartMarker(DynObject dynobj) { |
|
205 |
String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
|
206 |
return StringEscapeUtils.unescapeJava(s); |
|
207 |
} |
|
261 | 208 |
|
262 |
private static String getDelimiter(String line) { |
|
263 |
String sep = null; |
|
264 |
// Cuiaddo con los ":", los he puesto al final a proposito |
|
265 |
// ya que podian estar en la cadena para separar el size |
|
266 |
// size de cada tipo. |
|
267 |
String seps = ",;-|@#/+$%&!:"; |
|
268 |
for( int i=0; i<seps.length(); i ++) { |
|
269 |
sep = seps.substring(i, 1); |
|
270 |
if( line.contains(seps.substring(i, 1))) { |
|
271 |
break; |
|
272 |
} |
|
273 |
sep = null; |
|
274 |
} |
|
275 |
return sep; |
|
276 |
} |
|
277 |
// static String getNullTo(DynObject dynobj) { |
|
278 |
// String s = (String) dynobj.getDynValue(NULLTO); |
|
279 |
// return StringEscapeUtils.unescapeJava(s); |
|
280 |
// } |
|
281 |
|
|
282 |
static String getCharset(DynObject dynobj) { |
|
283 |
String s = (String) dynobj.getDynValue(CHARSET); |
|
284 |
return StringEscapeUtils.unescapeJava(s); |
|
285 |
} |
|
286 |
|
|
287 |
static String[] getPointDimensionNames(DynObject dynobj) { |
|
288 |
String s = (String) dynobj.getDynValue("point"); |
|
289 |
if( isEmpty(s) ) { |
|
290 |
return null; |
|
291 |
} |
|
292 |
return s.split(","); |
|
293 |
} |
|
294 |
|
|
295 |
static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
|
296 |
Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
|
297 |
if( b==null ) { |
|
298 |
return false; |
|
299 |
} |
|
300 |
return b.booleanValue(); |
|
301 |
} |
|
302 |
|
|
303 |
static boolean getAutomaticTypesDetection(DynObject dynobj) { |
|
304 |
Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION); |
|
305 |
if( b==null ) { |
|
306 |
return false; |
|
307 |
} |
|
308 |
return b.booleanValue(); |
|
309 |
} |
|
310 |
|
|
311 |
static boolean getIgnoreErrors(DynObject dynobj) { |
|
312 |
Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
|
313 |
if( b==null ) { |
|
314 |
return false; |
|
315 |
} |
|
316 |
return b.booleanValue(); |
|
317 |
} |
|
318 |
|
|
319 |
// static boolean getIgnoreEmptyLines(DynObject dynobj) { |
|
320 |
// Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES); |
|
321 |
// return b.booleanValue(); |
|
322 |
// } |
|
323 |
|
|
324 |
static int[] getFieldTypes(DynObject dynobj) { |
|
325 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
326 |
if( isEmpty(s) ) { |
|
327 |
return null; |
|
328 |
} |
|
329 |
String sep = getDelimiter(s); |
|
330 |
if( sep == null ) { |
|
331 |
return null; |
|
332 |
} |
|
333 |
DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
|
334 |
String fieldTypeNames[] = s.split("["+sep+"]"); |
|
335 |
int fieldTypes[] = new int[fieldTypeNames.length]; |
|
336 |
for( int i=0; i<fieldTypeNames.length; i++ ) { |
|
337 |
s = fieldTypeNames[i].trim(); |
|
338 |
if( s.contains(":") ) { |
|
339 |
s = s.split(":")[0]; |
|
340 |
} |
|
341 |
fieldTypes[i] = dataTypeManager.getType(s); |
|
342 |
} |
|
343 |
return fieldTypes; |
|
344 |
} |
|
345 |
|
|
346 |
static int[] getFieldSizes(DynObject dynobj) { |
|
347 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
348 |
if( isEmpty(s) ) { |
|
349 |
return null; |
|
350 |
} |
|
351 |
String sep = getDelimiter(s); |
|
352 |
if( sep == null ) { |
|
353 |
return null; |
|
354 |
} |
|
355 |
String fieldTypeNames[] = s.split("["+sep+"]"); |
|
356 |
int fieldSizes[] = new int[fieldTypeNames.length]; |
|
357 |
for( int i=0; i<fieldTypeNames.length; i++ ) { |
|
358 |
String fieldtype = fieldTypeNames[i].trim(); |
|
359 |
if( fieldtype .contains(":") ) { |
|
360 |
try { |
|
361 |
s = fieldtype .split(":")[1]; |
|
362 |
fieldSizes[i] = Integer.parseInt(s); |
|
363 |
} catch(Exception ex) { |
|
364 |
logger.warn("Can't get size of field "+i+" ("+fieldtype +").",ex); |
|
365 |
} |
|
366 |
} else { |
|
367 |
fieldSizes[i] = 0; |
|
368 |
} |
|
369 |
} |
|
370 |
return fieldSizes; |
|
371 |
} |
|
372 |
|
|
373 |
static private boolean isEmpty(String s) { |
|
374 |
if( s == null ) { |
|
375 |
return true; |
|
376 |
} |
|
377 |
if( s.trim().length() == 0 ) { |
|
378 |
return true; |
|
379 |
} |
|
380 |
return false; |
|
381 |
} |
|
209 |
public static String getQuoteCharacter(DynObject dynobj) { |
|
210 |
String s = (String) dynobj.getDynValue(QUOTECHAR); |
|
211 |
s = StringEscapeUtils.unescapeJava(s); |
|
212 |
if ( StringUtils.isBlank(s) ) { |
|
213 |
return null; |
|
214 |
} |
|
215 |
return s.substring(0, 1); |
|
216 |
} |
|
217 |
|
|
218 |
public static String getDelimiter(DynObject dynobj) { |
|
219 |
String s = (String) dynobj.getDynValue(DELIMITER); |
|
220 |
s = StringEscapeUtils.unescapeJava(s); |
|
221 |
if ( StringUtils.isBlank(s) ) { |
|
222 |
return null; |
|
223 |
} |
|
224 |
return s.substring(0, 1); |
|
225 |
} |
|
226 |
|
|
227 |
static String getHeader(DynObject dynobj) { |
|
228 |
String s = (String) dynobj.getDynValue(HEADER); |
|
229 |
s = StringEscapeUtils.unescapeJava(s); |
|
230 |
if ( StringUtils.isBlank(s) ) { |
|
231 |
return null; |
|
232 |
} |
|
233 |
return s; |
|
234 |
} |
|
235 |
|
|
236 |
static String[] getHeaders(DynObject dynobj) { |
|
237 |
String s = getHeader(dynobj); |
|
238 |
if ( StringUtils.isBlank(s) ) { |
|
239 |
return null; |
|
240 |
} |
|
241 |
String sep = getDelimiter(dynobj); |
|
242 |
if ( sep == null ) { |
|
243 |
sep = getDelimiter(s); |
|
244 |
if ( sep == null ) { |
|
245 |
// Chungo |
|
246 |
return null; |
|
247 |
} |
|
248 |
} |
|
249 |
String[] ss = s.split("[" + sep + "]"); |
|
250 |
return ss; |
|
251 |
} |
|
252 |
|
|
253 |
private static String getDelimiter(String line) { |
|
254 |
String sep = null; |
|
255 |
// Cuiaddo con los ":", los he puesto al final a proposito |
|
256 |
// ya que podian estar en la cadena para separar el size |
|
257 |
// size de cada tipo. |
|
258 |
String seps = ",;-|@#/+$%&!:"; |
|
259 |
for ( int i = 0; i < seps.length(); i++ ) { |
|
260 |
sep = seps.substring(i, 1); |
|
261 |
if ( line.contains(seps.substring(i, 1)) ) { |
|
262 |
break; |
|
263 |
} |
|
264 |
sep = null; |
|
265 |
} |
|
266 |
return sep; |
|
267 |
} |
|
268 |
|
|
269 |
static String getCharset(DynObject dynobj) { |
|
270 |
String s = (String) dynobj.getDynValue(CHARSET); |
|
271 |
return StringEscapeUtils.unescapeJava(s); |
|
272 |
} |
|
273 |
|
|
274 |
static String[] getPointDimensionNames(DynObject dynobj) { |
|
275 |
String s = (String) dynobj.getDynValue("point"); |
|
276 |
if ( StringUtils.isBlank(s) ) { |
|
277 |
return null; |
|
278 |
} |
|
279 |
return s.split(","); |
|
280 |
} |
|
281 |
|
|
282 |
public static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
|
283 |
Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
|
284 |
return BooleanUtils.isTrue(b); |
|
285 |
} |
|
286 |
|
|
287 |
static boolean getAutomaticTypesDetection(DynObject dynobj) { |
|
288 |
Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION); |
|
289 |
return BooleanUtils.isTrue(b); |
|
290 |
} |
|
291 |
|
|
292 |
static boolean getIgnoreErrors(DynObject dynobj) { |
|
293 |
Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
|
294 |
return BooleanUtils.isTrue(b); |
|
295 |
} |
|
296 |
|
|
297 |
static int[] getFieldTypes(DynObject dynobj) { |
|
298 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
299 |
if ( StringUtils.isBlank(s) ) { |
|
300 |
return null; |
|
301 |
} |
|
302 |
String sep = getDelimiter(s); |
|
303 |
if ( sep == null ) { |
|
304 |
return null; |
|
305 |
} |
|
306 |
DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
|
307 |
String fieldTypeNames[] = s.split("[" + sep + "]"); |
|
308 |
int fieldTypes[] = new int[fieldTypeNames.length]; |
|
309 |
for ( int i = 0; i < fieldTypeNames.length; i++ ) { |
|
310 |
s = fieldTypeNames[i].trim(); |
|
311 |
if ( s.contains(":") ) { |
|
312 |
s = s.split(":")[0]; |
|
313 |
} |
|
314 |
fieldTypes[i] = dataTypeManager.getType(s); |
|
315 |
} |
|
316 |
return fieldTypes; |
|
317 |
} |
|
318 |
|
|
319 |
static int[] getFieldSizes(DynObject dynobj) { |
|
320 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
321 |
if ( StringUtils.isBlank(s) ) { |
|
322 |
return null; |
|
323 |
} |
|
324 |
String sep = getDelimiter(s); |
|
325 |
if ( sep == null ) { |
|
326 |
return null; |
|
327 |
} |
|
328 |
String fieldTypeNames[] = s.split("[" + sep + "]"); |
|
329 |
int fieldSizes[] = new int[fieldTypeNames.length]; |
|
330 |
for ( int i = 0; i < fieldTypeNames.length; i++ ) { |
|
331 |
String fieldtype = fieldTypeNames[i].trim(); |
|
332 |
if ( fieldtype.contains(":") ) { |
|
333 |
try { |
|
334 |
s = fieldtype.split(":")[1]; |
|
335 |
fieldSizes[i] = Integer.parseInt(s); |
|
336 |
} catch (Exception ex) { |
|
337 |
logger.warn("Can't get size of field " + i + " (" + fieldtype + ").", ex); |
|
338 |
} |
|
339 |
} else { |
|
340 |
fieldSizes[i] = 0; |
|
341 |
} |
|
342 |
} |
|
343 |
return fieldSizes; |
|
344 |
} |
|
345 |
|
|
346 |
static int getSkipLines(DynObject dynobj) { |
|
347 |
Integer n = (Integer) dynobj.getDynValue("skipLines"); |
|
348 |
if ( n == null ) { |
|
349 |
return 0; |
|
350 |
} |
|
351 |
return new Integer(n); |
|
352 |
} |
|
353 |
|
|
354 |
static String getRawFieldsDefinition(DynObject dynobj) { |
|
355 |
String s = (String) dynobj.getDynValue("fieldsDefinition"); |
|
356 |
if ( StringUtils.isBlank(s) ) { |
|
357 |
return null; |
|
358 |
} |
|
359 |
return s.trim(); |
|
360 |
} |
|
361 |
|
|
362 |
public static class FieldDefinition { |
|
363 |
|
|
364 |
private int start; |
|
365 |
private int end; |
|
366 |
|
|
367 |
public FieldDefinition(String def) { |
|
368 |
def = def.trim(); |
|
369 |
String[] ss = def.split(":"); |
|
370 |
this.start = Integer.parseInt(ss[0])-1; |
|
371 |
if ( ss.length < 2 ) { |
|
372 |
this.end = -1; |
|
373 |
} else { |
|
374 |
this.end = Integer.parseInt(ss[1])-1; |
|
375 |
} |
|
376 |
} |
|
377 |
|
|
378 |
public int getStart() { |
|
379 |
return this.start; |
|
380 |
} |
|
381 |
|
|
382 |
public int getEnd() { |
|
383 |
return this.end; |
|
384 |
} |
|
385 |
|
|
386 |
public boolean getToEndOfLine() { |
|
387 |
return this.end == -1; |
|
388 |
} |
|
389 |
} |
|
390 |
|
|
391 |
public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) { |
|
392 |
String definition = getRawFieldsDefinition(dynobj); |
|
393 |
if ( definition == null ) { |
|
394 |
return null; |
|
395 |
} |
|
396 |
int i=0; |
|
397 |
try { |
|
398 |
String[] defs = StringUtils.split(definition); |
|
399 |
FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length]; |
|
400 |
for ( i = 0; i < defs.length; i++ ) { |
|
401 |
fieldsDefinition[i] = new FieldDefinition(defs[i]); |
|
402 |
} |
|
403 |
return fieldsDefinition; |
|
404 |
} catch (Exception ex) { |
|
405 |
throw new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+")."); |
|
406 |
} |
|
407 |
} |
|
408 |
|
|
382 | 409 |
} |
Also available in: Unified diff