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 @ 45721
History | View | Annotate | Download (6.15 KB)
1 | 45685 | jjdelcerro | /*
|
---|---|---|---|
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> assignments = new HashMap<>(); |
||
33 | // Valores obtenidos desde la deteccion automatica desde los datos
|
||
34 | public AutomaticDetectionOfTypes.DetectedValue detectedValue = null; |
||
35 | private String typename = "string"; |
||
36 | private final String fullFileName; |
||
37 | private final String providerName; |
||
38 | |||
39 | FieldTypeParser(String providerName, String fullFileName) { |
||
40 | this.providerName = providerName;
|
||
41 | this.fullFileName = fullFileName;
|
||
42 | } |
||
43 | |||
44 | public String getProviderName() { |
||
45 | return this.providerName; |
||
46 | } |
||
47 | |||
48 | public String getFullFileName() { |
||
49 | return this.fullFileName; |
||
50 | } |
||
51 | |||
52 | public void clear() { |
||
53 | this.name = null; |
||
54 | this.type = DataTypes.UNKNOWN;
|
||
55 | this.size = 0; |
||
56 | this.tags = new HashMap<>(); |
||
57 | this.assignments = new HashMap<>(); |
||
58 | } |
||
59 | |||
60 | public void copyFrom(FieldTypeParser other) { |
||
61 | this.name = other.name;
|
||
62 | this.type = other.type;
|
||
63 | this.size = other.size;
|
||
64 | this.tags = new HashMap<>(); |
||
65 | this.tags.putAll(other.tags);
|
||
66 | this.assignments = new HashMap<>(); |
||
67 | this.assignments.putAll(other.assignments);
|
||
68 | } |
||
69 | |||
70 | private int getType(String value) { |
||
71 | DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
||
72 | return dataTypesManager.getType(typename);
|
||
73 | } |
||
74 | |||
75 | private String[] split(String value, String separators) { |
||
76 | int firstSeparatorPosition = 1000000; |
||
77 | Character sep = null; |
||
78 | for (char ch : separators.toCharArray()) { |
||
79 | int pos = value.indexOf(ch);
|
||
80 | if (pos > 0 && pos < firstSeparatorPosition) { |
||
81 | sep = ch; |
||
82 | firstSeparatorPosition = pos; |
||
83 | } |
||
84 | } |
||
85 | if (sep == null) { |
||
86 | return new String[]{value}; |
||
87 | } |
||
88 | return value.split("[" + sep + "]"); |
||
89 | } |
||
90 | |||
91 | @SuppressWarnings(value = "UseSpecificCatch") |
||
92 | public boolean parse(String value) { |
||
93 | String[] args; |
||
94 | if (value.contains("__")) { |
||
95 | args = value.split("__");
|
||
96 | } else {
|
||
97 | args = split(value, ":/#@!;-");
|
||
98 | if (args.length == 1) { |
||
99 | this.name = value;
|
||
100 | return true; |
||
101 | } |
||
102 | } |
||
103 | int n = 0; |
||
104 | this.name = StringUtils.trim(args[n++]);
|
||
105 | if (n >= args.length) {
|
||
106 | return true; |
||
107 | } |
||
108 | this.typename = StringUtils.trim(args[n++]);
|
||
109 | this.type = this.getType(this.typename); |
||
110 | if (this.type == DataTypes.INVALID) { |
||
111 | this.geomType = GeometryUtils.getGeometryType(this.typename); |
||
112 | if (this.geomType == Geometry.TYPES.UNKNOWN) { |
||
113 | this.type = DataTypes.STRING;
|
||
114 | LOGGER.info("Type '" + this.typename + "' not valid for attribute '" + value + "' in '" + getProviderName() + "' file '" + getFullFileName() + "'."); |
||
115 | } else {
|
||
116 | this.typename = "GEOMETRY"; |
||
117 | this.type = DataTypes.GEOMETRY;
|
||
118 | } |
||
119 | } |
||
120 | this.size = 0; |
||
121 | while (n < args.length) {
|
||
122 | String option = StringUtils.trim(args[n++].toLowerCase());
|
||
123 | switch (option) {
|
||
124 | case "size": |
||
125 | try {
|
||
126 | this.size = Integer.parseInt(args[n++]); |
||
127 | } catch (Exception ex) { |
||
128 | LOGGER.warn("Ignore incorrect field size for field " + value + " in '" + getProviderName() + "' file '" + getFullFileName() + "'.", ex); |
||
129 | } |
||
130 | break;
|
||
131 | case "tag": |
||
132 | { |
||
133 | String x = StringUtils.trim(args[n++]);
|
||
134 | int pos = x.indexOf("="); |
||
135 | if (pos < 0) { |
||
136 | this.tags.put(x, null); |
||
137 | } else {
|
||
138 | this.tags.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), StringUtils.trim(StringUtils.substring(x, pos + 1))); |
||
139 | } |
||
140 | break;
|
||
141 | } |
||
142 | case "set": |
||
143 | { |
||
144 | String x = StringUtils.trim(args[n++]);
|
||
145 | int pos = x.indexOf("="); |
||
146 | if (pos < 0) { |
||
147 | this.assignments.put(x, null); |
||
148 | } else {
|
||
149 | this.assignments.put(StringUtils.trim(StringUtils.substring(x, 0, pos)), StringUtils.trim(StringUtils.substring(x, pos + 1))); |
||
150 | } |
||
151 | break;
|
||
152 | } |
||
153 | default:
|
||
154 | if (StringUtils.isNumeric(option) && this.size == 0) { |
||
155 | try {
|
||
156 | this.size = Integer.parseInt(option); |
||
157 | } catch (Exception ex) { |
||
158 | LOGGER.warn("Ignore incorrect field size for field " + value + " in '" + getProviderName() + "' file '" + getFullFileName() + "'.", ex); |
||
159 | } |
||
160 | break;
|
||
161 | } |
||
162 | LOGGER.warn("Illegal argumente '" + option + "' for field '" + this.name + "' in '" + getProviderName() + "' file '" + getFullFileName() + "' (" + value + ")."); |
||
163 | } |
||
164 | } |
||
165 | return true; |
||
166 | } |
||
167 | |||
168 | } |