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 @ 45784
History | View | Annotate | Download (7.59 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.gvsig.fmap.dal.DataTypes; |
12 |
import org.gvsig.fmap.geom.Geometry; |
13 |
import org.gvsig.fmap.geom.GeometryUtils; |
14 |
import org.gvsig.tools.ToolsLocator; |
15 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
16 |
import org.slf4j.Logger; |
17 |
import org.slf4j.LoggerFactory; |
18 |
|
19 |
/**
|
20 |
*
|
21 |
* @author jjdelcerro
|
22 |
*/
|
23 |
public class FieldTypeParser { |
24 |
private static final Logger LOGGER = LoggerFactory.getLogger(FieldTypeParser.class); |
25 |
|
26 |
public String name = null; |
27 |
public int type = DataTypes.STRING; |
28 |
public int size = 0; |
29 |
public int geomType = Geometry.TYPES.GEOMETRY; |
30 |
public int geomSubtype = Geometry.SUBTYPES.GEOM2D; |
31 |
public Map<String, String> tags = new HashMap<>(); |
32 |
public Map<String, String> typetags = new HashMap<>(); |
33 |
public Map<String, String> assignments = new HashMap<>(); |
34 |
public Map<String, String> typeAssignments = new HashMap<>(); |
35 |
// Valores obtenidos desde la deteccion automatica desde los datos
|
36 |
public AutomaticDetectionOfTypes.DetectedValue detectedValue = null; |
37 |
private String typename = "string"; |
38 |
private final String fullFileName; |
39 |
private final String providerName; |
40 |
|
41 |
FieldTypeParser(String providerName, String fullFileName) { |
42 |
this.providerName = providerName;
|
43 |
this.fullFileName = fullFileName;
|
44 |
} |
45 |
|
46 |
public String getProviderName() { |
47 |
return this.providerName; |
48 |
} |
49 |
|
50 |
public String getFullFileName() { |
51 |
return this.fullFileName; |
52 |
} |
53 |
|
54 |
public void clear() { |
55 |
this.name = null; |
56 |
this.type = DataTypes.UNKNOWN;
|
57 |
this.size = 0; |
58 |
this.tags = new HashMap<>(); |
59 |
this.typetags = new HashMap<>(); |
60 |
this.assignments = new HashMap<>(); |
61 |
this.typeAssignments = new HashMap<>(); |
62 |
} |
63 |
|
64 |
public void copyFrom(FieldTypeParser other) { |
65 |
this.name = other.name;
|
66 |
this.type = other.type;
|
67 |
this.size = other.size;
|
68 |
this.tags = new HashMap<>(); |
69 |
this.tags.putAll(other.tags);
|
70 |
this.typetags = new HashMap<>(); |
71 |
this.typetags.putAll(other.typetags);
|
72 |
this.assignments = new HashMap<>(); |
73 |
this.assignments.putAll(other.assignments);
|
74 |
this.typeAssignments = new HashMap<>(); |
75 |
this.typeAssignments.putAll(other.typeAssignments);
|
76 |
} |
77 |
|
78 |
private int getType(String value) { |
79 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
80 |
return dataTypesManager.getType(typename);
|
81 |
} |
82 |
|
83 |
private String[] split(String value, String separators) { |
84 |
int firstSeparatorPosition = 1000000; |
85 |
Character sep = null; |
86 |
for (char ch : separators.toCharArray()) { |
87 |
int pos = value.indexOf(ch);
|
88 |
if (pos > 0 && pos < firstSeparatorPosition) { |
89 |
sep = ch; |
90 |
firstSeparatorPosition = pos; |
91 |
} |
92 |
} |
93 |
if (sep == null) { |
94 |
return new String[]{value}; |
95 |
} |
96 |
return value.split("[" + sep + "]"); |
97 |
} |
98 |
|
99 |
@SuppressWarnings(value = "UseSpecificCatch") |
100 |
public boolean parse(String value) { |
101 |
String[] args; |
102 |
if (value.contains("__")) { |
103 |
args = value.split("__");
|
104 |
} else {
|
105 |
args = split(value, ":/#@!;-");
|
106 |
if (args.length == 1) { |
107 |
this.name = value;
|
108 |
return true; |
109 |
} |
110 |
} |
111 |
int n = 0; |
112 |
this.name = StringUtils.trim(args[n++]);
|
113 |
if (n >= args.length) {
|
114 |
return true; |
115 |
} |
116 |
this.typename = StringUtils.trim(args[n++]);
|
117 |
this.type = this.getType(this.typename); |
118 |
if (this.type == DataTypes.INVALID) { |
119 |
this.geomType = GeometryUtils.getGeometryType(this.typename); |
120 |
if (this.geomType == Geometry.TYPES.UNKNOWN) { |
121 |
this.type = DataTypes.STRING;
|
122 |
LOGGER.info("Type '" + this.typename + "' not valid for attribute '" + value + "' in '" + getProviderName() + "' file '" + getFullFileName() + "'."); |
123 |
} else {
|
124 |
this.typename = "GEOMETRY"; |
125 |
this.type = DataTypes.GEOMETRY;
|
126 |
} |
127 |
} |
128 |
this.size = 0; |
129 |
while (n < args.length) {
|
130 |
String option = StringUtils.trim(args[n++].toLowerCase());
|
131 |
switch (option) {
|
132 |
case "size": |
133 |
try {
|
134 |
this.size = Integer.parseInt(args[n++]); |
135 |
} catch (Exception ex) { |
136 |
LOGGER.warn("Ignore incorrect field size for field " + value + " in '" + getProviderName() + "' file '" + getFullFileName() + "'.", ex); |
137 |
} |
138 |
break;
|
139 |
case "tag": |
140 |
{ |
141 |
String x = StringUtils.trim(args[n++]);
|
142 |
int pos = x.indexOf("="); |
143 |
if (pos < 0) { |
144 |
this.tags.put(x, null); |
145 |
} else {
|
146 |
this.tags.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), StringUtils.trim(StringUtils.substring(x, pos + 1))); |
147 |
} |
148 |
break;
|
149 |
} |
150 |
case "typetag": |
151 |
{ |
152 |
String x = StringUtils.trim(args[n++]);
|
153 |
int pos = x.indexOf("="); |
154 |
if (pos < 0) { |
155 |
this.typetags.put(x, null); |
156 |
} else {
|
157 |
this.typetags.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), StringUtils.trim(StringUtils.substring(x, pos + 1))); |
158 |
} |
159 |
break;
|
160 |
} |
161 |
case "set": |
162 |
{ |
163 |
String x = StringUtils.trim(args[n++]);
|
164 |
int pos = x.indexOf("="); |
165 |
if (pos < 0) { |
166 |
this.assignments.put(x, null); |
167 |
} else {
|
168 |
this.assignments.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), StringUtils.trim(StringUtils.substring(x, pos + 1))); |
169 |
} |
170 |
break;
|
171 |
} |
172 |
case "typeset": |
173 |
{ |
174 |
String x = StringUtils.trim(args[n++]);
|
175 |
int pos = x.indexOf("="); |
176 |
if (pos < 0) { |
177 |
this.typeAssignments.put(x, null); |
178 |
} else {
|
179 |
this.typeAssignments.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), StringUtils.trim(StringUtils.substring(x, pos + 1))); |
180 |
} |
181 |
break;
|
182 |
} |
183 |
default:
|
184 |
if (StringUtils.isNumeric(option) && this.size == 0) { |
185 |
try {
|
186 |
this.size = Integer.parseInt(option); |
187 |
} catch (Exception ex) { |
188 |
LOGGER.warn("Ignore incorrect field size for field " + value + " in '" + getProviderName() + "' file '" + getFullFileName() + "'.", ex); |
189 |
} |
190 |
break;
|
191 |
} |
192 |
LOGGER.warn("Illegal argumente '" + option + "' for field '" + this.name + "' in '" + getProviderName() + "' file '" + getFullFileName() + "' (" + value + ")."); |
193 |
} |
194 |
} |
195 |
return true; |
196 |
} |
197 |
|
198 |
} |