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 / FieldTypeParser.java @ 47420
History | View | Annotate | Download (10.3 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.fmap.dal.store.csv; |
7 |
|
8 |
import java.util.HashMap; |
9 |
import java.util.Map; |
10 |
import org.apache.commons.lang3.StringUtils; |
11 |
import org.apache.commons.text.StringEscapeUtils; |
12 |
import org.gvsig.fmap.dal.DataTypes; |
13 |
import org.gvsig.fmap.geom.Geometry; |
14 |
import org.gvsig.fmap.geom.GeometryUtils; |
15 |
import org.gvsig.tools.ToolsLocator; |
16 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
17 |
import org.slf4j.Logger; |
18 |
import org.slf4j.LoggerFactory; |
19 |
|
20 |
/**
|
21 |
*
|
22 |
* @author jjdelcerro
|
23 |
*/
|
24 |
public class FieldTypeParser { |
25 |
private static final Logger LOGGER = LoggerFactory.getLogger(FieldTypeParser.class); |
26 |
|
27 |
public String name = null; |
28 |
public int type = DataTypes.UNKNOWN; |
29 |
public int size = 0; |
30 |
public int geomType = Geometry.TYPES.GEOMETRY; |
31 |
public int geomSubtype = Geometry.SUBTYPES.GEOM2D; |
32 |
public Map<String, String> tags = new HashMap<>(); |
33 |
public Map<String, String> typetags = new HashMap<>(); |
34 |
public Map<String, String> assignments = new HashMap<>(); |
35 |
public Map<String, String> typeAssignments = new HashMap<>(); |
36 |
// Valores obtenidos desde la deteccion automatica desde los datos
|
37 |
public AutomaticDetectionOfTypes.DetectedValue detectedValue = null; |
38 |
private String typename = "string"; |
39 |
private final String fullFileName; |
40 |
private final String providerName; |
41 |
|
42 |
FieldTypeParser(String providerName, String fullFileName) { |
43 |
this.providerName = providerName;
|
44 |
this.fullFileName = fullFileName;
|
45 |
} |
46 |
|
47 |
public String getProviderName() { |
48 |
return this.providerName; |
49 |
} |
50 |
|
51 |
public String getFullFileName() { |
52 |
return this.fullFileName; |
53 |
} |
54 |
|
55 |
public void clear() { |
56 |
this.name = null; |
57 |
this.type = DataTypes.UNKNOWN;
|
58 |
this.size = 0; |
59 |
this.tags = new HashMap<>(); |
60 |
this.typetags = new HashMap<>(); |
61 |
this.assignments = new HashMap<>(); |
62 |
this.typeAssignments = new HashMap<>(); |
63 |
} |
64 |
|
65 |
public void copyFrom(FieldTypeParser other) { |
66 |
this.name = other.name;
|
67 |
this.type = other.type;
|
68 |
this.size = other.size;
|
69 |
this.tags = new HashMap<>(); |
70 |
this.tags.putAll(other.tags);
|
71 |
this.typetags = new HashMap<>(); |
72 |
this.typetags.putAll(other.typetags);
|
73 |
this.assignments = new HashMap<>(); |
74 |
this.assignments.putAll(other.assignments);
|
75 |
this.typeAssignments = new HashMap<>(); |
76 |
this.typeAssignments.putAll(other.typeAssignments);
|
77 |
} |
78 |
|
79 |
private int getType(String value) { |
80 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
81 |
return dataTypesManager.getType(typename);
|
82 |
} |
83 |
|
84 |
private String[] split(String value, String separators) { |
85 |
int firstSeparatorPosition = 1000000; |
86 |
Character sep = null; |
87 |
for (char ch : separators.toCharArray()) { |
88 |
int pos = value.indexOf(ch);
|
89 |
if (pos > 0 && pos < firstSeparatorPosition) { |
90 |
sep = ch; |
91 |
firstSeparatorPosition = pos; |
92 |
} |
93 |
} |
94 |
if (sep == null) { |
95 |
return new String[]{value}; |
96 |
} |
97 |
return value.split("[" + sep + "]"); |
98 |
} |
99 |
|
100 |
@SuppressWarnings(value = "UseSpecificCatch") |
101 |
public boolean parse(String value) { |
102 |
String[] args; |
103 |
if (value.contains("__")) { |
104 |
args = value.split("__");
|
105 |
} else {
|
106 |
args = split(value, ":/#@!;-");
|
107 |
if (args.length == 1) { |
108 |
this.name = value;
|
109 |
return true; |
110 |
} |
111 |
} |
112 |
int n = 0; |
113 |
this.name = StringUtils.trim(args[n++]);
|
114 |
if (n >= args.length) {
|
115 |
return true; |
116 |
} |
117 |
this.typename = StringUtils.trim(args[n++]);
|
118 |
this.type = this.getType(this.typename); |
119 |
if (this.type == DataTypes.INVALID) { |
120 |
this.geomType = GeometryUtils.getGeometryType(this.typename); |
121 |
if (this.geomType == Geometry.TYPES.UNKNOWN) { |
122 |
this.type = DataTypes.STRING;
|
123 |
LOGGER.info("Type '" + this.typename + "' not valid for attribute '" + value + "' in '" + getProviderName() + "' file '" + getFullFileName() + "'."); |
124 |
} else {
|
125 |
this.typename = "GEOMETRY"; |
126 |
this.type = DataTypes.GEOMETRY;
|
127 |
} |
128 |
} |
129 |
this.size = 0; |
130 |
while (n < args.length) {
|
131 |
String escMode = null; |
132 |
String option = StringUtils.trim(args[n++].toLowerCase());
|
133 |
switch (option) {
|
134 |
case "size": |
135 |
try {
|
136 |
this.size = Integer.parseInt(args[n++]); |
137 |
} catch (Exception ex) { |
138 |
LOGGER.warn("Ignore incorrect field size for field " + value + " in '" + getProviderName() + "' file '" + getFullFileName() + "'.", ex); |
139 |
} |
140 |
break;
|
141 |
case "tagesc": |
142 |
escMode = StringUtils.trim(args[n++]); |
143 |
case "tag": |
144 |
{ |
145 |
String x = StringUtils.trim(args[n++]);
|
146 |
int pos = x.indexOf("="); |
147 |
if (pos < 0) { |
148 |
this.tags.put(x, null); |
149 |
} else {
|
150 |
String v = StringUtils.trim(StringUtils.substring(x, pos + 1)); |
151 |
if(escMode != null) { |
152 |
if(StringUtils.equalsIgnoreCase(escMode,"html")){ |
153 |
v = StringEscapeUtils.unescapeHtml3(v); |
154 |
} else {
|
155 |
LOGGER.warn("Illegal escape mode for argument '" + option + "' for field '" + this.name + "' in '" + getProviderName() + "' file '" + getFullFileName() + "' (" + value + ")."); |
156 |
} |
157 |
} |
158 |
this.tags.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), v); |
159 |
} |
160 |
break;
|
161 |
} |
162 |
case "typetagesc": |
163 |
escMode = StringUtils.trim(args[n++]); |
164 |
case "typetag": |
165 |
{ |
166 |
String x = StringUtils.trim(args[n++]);
|
167 |
int pos = x.indexOf("="); |
168 |
if (pos < 0) { |
169 |
this.typetags.put(x, null); |
170 |
} else {
|
171 |
String v = StringUtils.trim(StringUtils.substring(x, pos + 1)); |
172 |
if(escMode != null) { |
173 |
if(StringUtils.equalsIgnoreCase(escMode,"html")){ |
174 |
v = StringEscapeUtils.unescapeHtml3(v); |
175 |
} else {
|
176 |
LOGGER.warn("Illegal escape mode for argument '" + option + "' for field '" + this.name + "' in '" + getProviderName() + "' file '" + getFullFileName() + "' (" + value + ")."); |
177 |
} |
178 |
} |
179 |
this.typetags.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), v); |
180 |
} |
181 |
break;
|
182 |
} |
183 |
case "setesc": |
184 |
escMode = StringUtils.trim(args[n++]); |
185 |
case "set": |
186 |
{ |
187 |
String x = StringUtils.trim(args[n++]);
|
188 |
int pos = x.indexOf("="); |
189 |
if (pos < 0) { |
190 |
this.assignments.put(x, null); |
191 |
} else {
|
192 |
String v = StringUtils.trim(StringUtils.substring(x, pos + 1)); |
193 |
if(escMode != null) { |
194 |
if(StringUtils.equalsIgnoreCase(escMode,"html")){ |
195 |
v = StringEscapeUtils.unescapeHtml3(v); |
196 |
} else {
|
197 |
LOGGER.warn("Illegal escape mode for argument '" + option + "' for field '" + this.name + "' in '" + getProviderName() + "' file '" + getFullFileName() + "' (" + value + ")."); |
198 |
} |
199 |
} |
200 |
this.assignments.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), v); |
201 |
} |
202 |
break;
|
203 |
} |
204 |
case "typesetesc": |
205 |
escMode = StringUtils.trim(args[n++]); |
206 |
case "typeset": |
207 |
{ |
208 |
String x = StringUtils.trim(args[n++]);
|
209 |
int pos = x.indexOf("="); |
210 |
if (pos < 0) { |
211 |
this.typeAssignments.put(x, null); |
212 |
} else {
|
213 |
String v = StringUtils.trim(StringUtils.substring(x, pos + 1)); |
214 |
if(escMode != null) { |
215 |
if(StringUtils.equalsIgnoreCase(escMode,"html")){ |
216 |
v = StringEscapeUtils.unescapeHtml3(v); |
217 |
} else {
|
218 |
LOGGER.warn("Illegal escape mode for argument '" + option + "' for field '" + this.name + "' in '" + getProviderName() + "' file '" + getFullFileName() + "' (" + value + ")."); |
219 |
} |
220 |
} |
221 |
this.typeAssignments.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), v); |
222 |
} |
223 |
break;
|
224 |
} |
225 |
default:
|
226 |
if (StringUtils.isNumeric(option) && this.size == 0) { |
227 |
try {
|
228 |
this.size = Integer.parseInt(option); |
229 |
} catch (Exception ex) { |
230 |
LOGGER.warn("Ignore incorrect field size for field " + value + " in '" + getProviderName() + "' file '" + getFullFileName() + "'.", ex); |
231 |
} |
232 |
break;
|
233 |
} |
234 |
LOGGER.warn("Illegal argument '" + option + "' for field '" + this.name + "' in '" + getProviderName() + "' file '" + getFullFileName() + "' (" + value + ")."); |
235 |
} |
236 |
} |
237 |
return true; |
238 |
} |
239 |
|
240 |
} |