svn-gvsig-desktop / tags / v1_1_Build_1013 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / values / ComplexValue.java @ 13521
History | View | Annotate | Download (6.16 KB)
1 |
package com.hardcode.gdbms.engine.values; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.io.StringWriter; |
6 |
import java.io.Writer; |
7 |
import java.sql.Types; |
8 |
import java.text.ParseException; |
9 |
import java.util.Arrays; |
10 |
import java.util.Collection; |
11 |
import java.util.Iterator; |
12 |
import java.util.LinkedHashMap; |
13 |
import java.util.Map; |
14 |
import java.util.Set; |
15 |
|
16 |
import org.kxml2.io.KXmlParser; |
17 |
import org.xmlpull.v1.XmlPullParser; |
18 |
import org.xmlpull.v1.XmlPullParserException; |
19 |
|
20 |
public class ComplexValue extends StringValue implements Map { |
21 |
private LinkedHashMap mapValues =null; |
22 |
|
23 |
private static String indentString(int level) { |
24 |
if (level < 1) return ""; |
25 |
char[] chars =new char[level*4]; |
26 |
Arrays.fill(chars,' '); |
27 |
return String.copyValueOf(chars); |
28 |
} |
29 |
/**
|
30 |
* Construye un objeto ConplexValue con el parser
|
31 |
*
|
32 |
* @param text
|
33 |
*/
|
34 |
private ComplexValue(KXmlParser parser) {
|
35 |
super();
|
36 |
this.mapValues = new LinkedHashMap(); |
37 |
try {
|
38 |
this.parse(parser);
|
39 |
} catch (Exception e) { |
40 |
e.printStackTrace(); |
41 |
super.setValue(null); |
42 |
} |
43 |
} |
44 |
|
45 |
/**
|
46 |
* Construye un objeto ConplexValue con el texto que se pasa como parametro
|
47 |
*
|
48 |
* @param text
|
49 |
*/
|
50 |
ComplexValue(String text) {
|
51 |
super();
|
52 |
this.mapValues = new LinkedHashMap(); |
53 |
this.setValue(text);
|
54 |
} |
55 |
|
56 |
/**
|
57 |
* Creates a new ComplexValue object.
|
58 |
*/
|
59 |
ComplexValue() { |
60 |
super();
|
61 |
this.mapValues = new LinkedHashMap(); |
62 |
} |
63 |
|
64 |
private void parse() { |
65 |
String value = super.getValue(); |
66 |
this.mapValues.clear();
|
67 |
if (value == null || value.length() == 0) { |
68 |
return;
|
69 |
} |
70 |
KXmlParser parser = new KXmlParser();
|
71 |
try {
|
72 |
parser.setInput(new StringReader(value)); |
73 |
} catch (XmlPullParserException e) {
|
74 |
// TODO Auto-generated catch block
|
75 |
e.printStackTrace(); |
76 |
} |
77 |
try {
|
78 |
this.parse(parser);
|
79 |
} catch (Exception e) { |
80 |
super.setValue(null); |
81 |
} |
82 |
} |
83 |
|
84 |
|
85 |
|
86 |
private void parse(KXmlParser parser) throws XmlPullParserException, IOException, ParseException { |
87 |
/*
|
88 |
* Se espera que la le va a llegar una cadena
|
89 |
* con el siguiente formato:
|
90 |
*
|
91 |
* <dato1>valor1</dato1>
|
92 |
* <dato2>valor2</dato2>
|
93 |
* <dato3>
|
94 |
* <dato3_1>valor3</dato3_1>
|
95 |
* <dato3_2>
|
96 |
* <dato3_2_1>valor4</dato3_2_1>
|
97 |
* ....
|
98 |
* </dato3_2>
|
99 |
* </dato3>
|
100 |
* ....
|
101 |
*
|
102 |
*
|
103 |
*
|
104 |
* dentro del mapValues se registran los
|
105 |
* valores de 'dato1', 'dato2' y 'dato3'.
|
106 |
* Este ultimo sera a su vez un ComplexValue, y
|
107 |
* asi recursivamente.
|
108 |
*/
|
109 |
|
110 |
//FIXME: OJO!!!! que hacemos con la excepciones
|
111 |
|
112 |
try {
|
113 |
String key;
|
114 |
Value value; |
115 |
String cad;
|
116 |
String type;
|
117 |
if (parser.getEventType() == XmlPullParser.START_DOCUMENT) {
|
118 |
parser.nextTag(); |
119 |
} |
120 |
|
121 |
while (parser.getEventType() == XmlPullParser.START_TAG) {
|
122 |
key = parser.getName(); |
123 |
try {
|
124 |
type = parser.getAttributeValue(null,"_type"); |
125 |
cad = parser.nextText(); |
126 |
if (type != null) { |
127 |
value = ValueFactory.createValueByValueName(cad,type); |
128 |
} else {
|
129 |
value = ValueFactory.createValue(cad); |
130 |
} |
131 |
} catch (XmlPullParserException e) {
|
132 |
if (parser.getEventType() == XmlPullParser.START_TAG) {
|
133 |
value = new ComplexValue(parser);
|
134 |
} else if (parser.getEventType() == XmlPullParser.END_TAG) { |
135 |
continue;
|
136 |
} else {
|
137 |
throw e;
|
138 |
} |
139 |
} |
140 |
parser.require(XmlPullParser.END_TAG, null, key);
|
141 |
|
142 |
this.mapValues.put(key,value);
|
143 |
|
144 |
parser.nextTag(); |
145 |
} |
146 |
} catch (XmlPullParserException e) {
|
147 |
if (parser.getEventType() == XmlPullParser.END_DOCUMENT) return; |
148 |
throw e;
|
149 |
} |
150 |
|
151 |
} |
152 |
|
153 |
|
154 |
private String dump() { |
155 |
StringWriter buffer = new StringWriter(); |
156 |
try {
|
157 |
this.dumpToWriter(buffer,0); |
158 |
} catch (IOException e) { |
159 |
return null; |
160 |
} |
161 |
return buffer.toString();
|
162 |
} |
163 |
|
164 |
private void dumpToWriter(Writer buffer, int indent) throws IOException { |
165 |
Iterator iter = this.mapValues.entrySet().iterator(); |
166 |
String identStr = indentString(indent);
|
167 |
indent++; |
168 |
Entry entry; |
169 |
String key;
|
170 |
String typeString;
|
171 |
Value value; |
172 |
while (iter.hasNext()) {
|
173 |
entry = (Entry)iter.next(); |
174 |
key = (String)entry.getKey();
|
175 |
value = (Value)entry.getValue(); |
176 |
buffer.write(identStr+"<"+key+ getDumpTypePropertyString(value) +">"); |
177 |
if (value instanceof ComplexValue) { |
178 |
buffer.write("\n");
|
179 |
((ComplexValue)value).dumpToWriter(buffer,indent); |
180 |
buffer.write(identStr); |
181 |
} else {
|
182 |
buffer.write(value.toString()); |
183 |
} |
184 |
buffer.write("</"+key+">\n"); |
185 |
} |
186 |
|
187 |
} |
188 |
|
189 |
private String getDumpTypePropertyString(Value value) { |
190 |
|
191 |
if (value instanceof StringValue) { |
192 |
return ""; |
193 |
} else if (value instanceof ComplexValue) { |
194 |
return ""; |
195 |
} else{
|
196 |
String classname = value.getClass().getName();
|
197 |
return " _type=\"" + (classname.substring(classname.lastIndexOf(".")+1)) + "\""; |
198 |
} |
199 |
|
200 |
} |
201 |
|
202 |
public int size() { |
203 |
return this.mapValues.size(); |
204 |
} |
205 |
|
206 |
public void clear() { |
207 |
super.setValue(""); |
208 |
this.mapValues.clear();
|
209 |
|
210 |
} |
211 |
|
212 |
public boolean isEmpty() { |
213 |
return this.mapValues.isEmpty(); |
214 |
} |
215 |
|
216 |
public boolean containsKey(Object key) { |
217 |
return this.mapValues.containsKey(key); |
218 |
} |
219 |
|
220 |
public boolean containsValue(Object value) { |
221 |
return this.mapValues.containsValue(value); |
222 |
} |
223 |
|
224 |
public Collection values() { |
225 |
return this.mapValues.values(); |
226 |
} |
227 |
|
228 |
public void putAll(Map t) { |
229 |
throw new UnsupportedOperationException(); |
230 |
} |
231 |
|
232 |
public Set entrySet() { |
233 |
return this.mapValues.entrySet(); |
234 |
} |
235 |
|
236 |
public Set keySet() { |
237 |
return this.mapValues.keySet(); |
238 |
} |
239 |
|
240 |
public Object get(Object key) { |
241 |
return this.mapValues.get(key); |
242 |
} |
243 |
|
244 |
public Object remove(Object key) { |
245 |
return this.mapValues.remove(key); |
246 |
} |
247 |
|
248 |
public Object put(Object key, Object value) { |
249 |
throw new IllegalArgumentException("'value' must be a Value instance"); |
250 |
//return this.mapValues.put(key,value);
|
251 |
} |
252 |
|
253 |
public Object put(Object key, Value value) { |
254 |
return this.mapValues.put(key,value); |
255 |
} |
256 |
|
257 |
|
258 |
public String getStringValue(ValueWriter writer) { |
259 |
super.setValue(this.dump()); |
260 |
return super.getStringValue(writer); |
261 |
} |
262 |
|
263 |
public String getValue() { |
264 |
super.setValue(this.dump()); |
265 |
return super.getValue(); |
266 |
} |
267 |
|
268 |
public void setValue(String value) { |
269 |
super.setValue(value);
|
270 |
this.parse();
|
271 |
} |
272 |
|
273 |
public String toString() { |
274 |
return this.getValue(); |
275 |
} |
276 |
public int getSQLType() { |
277 |
//return super.getSQLType(); --> Types.LONGVARCHAR;
|
278 |
return Types.STRUCT; |
279 |
} |
280 |
|
281 |
} |