svn-gvsig-desktop / tags / v1_1_Build_1008 / libraries / libIverUtiles / src / com / iver / utiles / XMLEntity.java @ 12520
History | View | Annotate | Download (27.8 KB)
1 | 1104 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 149 | fernando | package com.iver.utiles; |
42 | |||
43 | 8765 | jjdelcerro | import java.io.IOException; |
44 | import java.io.StringReader; |
||
45 | import java.io.StringWriter; |
||
46 | import java.util.Iterator; |
||
47 | import java.util.NoSuchElementException; |
||
48 | |||
49 | import org.exolab.castor.xml.MarshalException; |
||
50 | import org.exolab.castor.xml.Marshaller; |
||
51 | import org.exolab.castor.xml.ValidationException; |
||
52 | |||
53 | 433 | vcaballero | import com.iver.utiles.xmlEntity.generate.Property; |
54 | import com.iver.utiles.xmlEntity.generate.XmlTag; |
||
55 | 149 | fernando | |
56 | /**
|
||
57 | 478 | vcaballero | * Adaptador de las llamadas a sus m?todos sobre los generados por castor para
|
58 | * generar despu?s los XML.
|
||
59 | 433 | vcaballero | *
|
60 | * @author Vicente Caballero Navarro
|
||
61 | 149 | fernando | */
|
62 | public class XMLEntity { |
||
63 | 731 | vcaballero | private XmlTag xmltag;
|
64 | 478 | vcaballero | |
65 | 731 | vcaballero | /**
|
66 | * Crea un nuevo XMLEntity.
|
||
67 | *
|
||
68 | 6875 | jaume | * @param tag
|
69 | * DOCUMENT ME!
|
||
70 | 731 | vcaballero | */
|
71 | public XMLEntity(XmlTag tag) {
|
||
72 | xmltag = tag; |
||
73 | } |
||
74 | 149 | fernando | |
75 | 731 | vcaballero | /**
|
76 | * Crea un nuevo XMLEntity.
|
||
77 | */
|
||
78 | public XMLEntity() {
|
||
79 | xmltag = new XmlTag();
|
||
80 | } |
||
81 | 433 | vcaballero | |
82 | 731 | vcaballero | /**
|
83 | * A?ade una propiedad con un String como clave y un String como valor.
|
||
84 | *
|
||
85 | 6875 | jaume | * @param key
|
86 | * clave.
|
||
87 | * @param value
|
||
88 | * valor.
|
||
89 | 731 | vcaballero | */
|
90 | public void putProperty(String key, String value) { |
||
91 | 751 | vcaballero | if ((key == null)) { |
92 | 731 | vcaballero | return;
|
93 | } |
||
94 | 433 | vcaballero | |
95 | 731 | vcaballero | Property p = new Property();
|
96 | p.setKey(key); |
||
97 | p.setValue(value); |
||
98 | putProperty(p); |
||
99 | } |
||
100 | 149 | fernando | |
101 | 731 | vcaballero | /**
|
102 | * A?ade una propiedad con un String como clave y un Object como valor.
|
||
103 | *
|
||
104 | 6875 | jaume | * @param key
|
105 | * clave.
|
||
106 | * @param value
|
||
107 | * valor.
|
||
108 | 731 | vcaballero | */
|
109 | public void putProperty(String key, Object value) { |
||
110 | String ret = ""; |
||
111 | 433 | vcaballero | |
112 | 751 | vcaballero | if (key == null) { |
113 | 731 | vcaballero | return;
|
114 | } |
||
115 | 433 | vcaballero | |
116 | 731 | vcaballero | Class valueClass = value.getClass();
|
117 | 433 | vcaballero | |
118 | 731 | vcaballero | if (valueClass.isArray()) {
|
119 | Class compType = valueClass.getComponentType();
|
||
120 | 433 | vcaballero | |
121 | 731 | vcaballero | if (compType == byte.class) { |
122 | byte[] array = (byte[]) value; |
||
123 | 433 | vcaballero | |
124 | 731 | vcaballero | if (!(array.length == 0)) { |
125 | ret += ("" + array[0]).replaceAll("[,]", "\\\\,"); |
||
126 | 433 | vcaballero | |
127 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
128 | 6875 | jaume | ret += (" ," + ("" + array[i]).replaceAll("[,]", |
129 | "\\\\,"));
|
||
130 | 731 | vcaballero | } |
131 | } |
||
132 | } else if (compType == short.class) { |
||
133 | short[] array = (short[]) value; |
||
134 | 433 | vcaballero | |
135 | 731 | vcaballero | if (!(array.length == 0)) { |
136 | ret += ("" + array[0]).replaceAll("[,]", "\\\\,"); |
||
137 | 433 | vcaballero | |
138 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
139 | 6875 | jaume | ret += (" ," + ("" + array[i]).replaceAll("[,]", |
140 | "\\\\,"));
|
||
141 | 731 | vcaballero | } |
142 | } |
||
143 | } else if (compType == int.class) { |
||
144 | int[] array = (int[]) value; |
||
145 | 433 | vcaballero | |
146 | 731 | vcaballero | if (!(array.length == 0)) { |
147 | ret += ("" + array[0]).replaceAll("[,]", "\\\\,"); |
||
148 | 433 | vcaballero | |
149 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
150 | 6875 | jaume | ret += (" ," + ("" + array[i]).replaceAll("[,]", |
151 | "\\\\,"));
|
||
152 | 731 | vcaballero | } |
153 | } |
||
154 | } else if (compType == long.class) { |
||
155 | long[] array = (long[]) value; |
||
156 | 433 | vcaballero | |
157 | 731 | vcaballero | if (!(array.length == 0)) { |
158 | ret += ("" + array[0]).replaceAll("[,]", "\\\\,"); |
||
159 | 433 | vcaballero | |
160 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
161 | 6875 | jaume | ret += (" ," + ("" + array[i]).replaceAll("[,]", |
162 | "\\\\,"));
|
||
163 | 731 | vcaballero | } |
164 | } |
||
165 | } else if (compType == float.class) { |
||
166 | float[] array = (float[]) value; |
||
167 | 433 | vcaballero | |
168 | 731 | vcaballero | if (!(array.length == 0)) { |
169 | ret += ("" + array[0]).replaceAll("[,]", "\\\\,"); |
||
170 | 433 | vcaballero | |
171 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
172 | 6875 | jaume | ret += (" ," + ("" + array[i]).replaceAll("[,]", |
173 | "\\\\,"));
|
||
174 | 731 | vcaballero | } |
175 | } |
||
176 | } else if (compType == double.class) { |
||
177 | double[] array = (double[]) value; |
||
178 | 433 | vcaballero | |
179 | 731 | vcaballero | if (!(array.length == 0)) { |
180 | ret += ("" + array[0]).replaceAll("[,]", "\\\\,"); |
||
181 | 433 | vcaballero | |
182 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
183 | 6875 | jaume | ret += (" ," + ("" + array[i]).replaceAll("[,]", |
184 | "\\\\,"));
|
||
185 | 731 | vcaballero | } |
186 | } |
||
187 | } else if (compType == boolean.class) { |
||
188 | boolean[] array = (boolean[]) value; |
||
189 | 433 | vcaballero | |
190 | 731 | vcaballero | if (!(array.length == 0)) { |
191 | ret += ("" + array[0]).replaceAll("[,]", "\\\\,"); |
||
192 | 433 | vcaballero | |
193 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
194 | 6875 | jaume | ret += (" ," + ("" + array[i]).replaceAll("[,]", |
195 | "\\\\,"));
|
||
196 | 731 | vcaballero | } |
197 | } |
||
198 | } else if (compType == String.class) { |
||
199 | String[] array = (String[]) value; |
||
200 | 433 | vcaballero | |
201 | 731 | vcaballero | if (!(array.length == 0)) { |
202 | if (array[0] == null) { |
||
203 | array[0] = "null"; |
||
204 | } |
||
205 | 433 | vcaballero | |
206 | 731 | vcaballero | ret += array[0].replaceAll("[,]", "\\\\,"); |
207 | 433 | vcaballero | |
208 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
209 | if (array[i] == null) { |
||
210 | array[i] = "null";
|
||
211 | } |
||
212 | 433 | vcaballero | |
213 | 731 | vcaballero | array[i] = array[i].replaceAll("[,]", "\\\\,"); |
214 | ret += (" ," + array[i]);
|
||
215 | } |
||
216 | } |
||
217 | } else {
|
||
218 | Object[] array = (Object[]) value; |
||
219 | 433 | vcaballero | |
220 | 731 | vcaballero | if (!(array.length == 0)) { |
221 | ret += ("" + array[0]).replaceAll("[,]", "\\\\,"); |
||
222 | 433 | vcaballero | |
223 | 731 | vcaballero | for (int i = 1; i < array.length; i++) { |
224 | array[i] = ("" + array[i]).replaceAll("[,]", "\\\\,"); |
||
225 | ret += (" ," + array[i]);
|
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | } else {
|
||
230 | ret = value.toString(); |
||
231 | } |
||
232 | 433 | vcaballero | |
233 | 731 | vcaballero | Property p = new Property();
|
234 | p.setKey(key); |
||
235 | p.setValue(ret); |
||
236 | putProperty(p); |
||
237 | } |
||
238 | 149 | fernando | |
239 | 731 | vcaballero | /**
|
240 | * A?ade una propiedad con un String como clave y un entero como valor.
|
||
241 | *
|
||
242 | 6875 | jaume | * @param key
|
243 | * clave.
|
||
244 | * @param value
|
||
245 | * valor.
|
||
246 | 731 | vcaballero | */
|
247 | public void putProperty(String key, int value) { |
||
248 | if (key == null) { |
||
249 | return;
|
||
250 | } |
||
251 | 433 | vcaballero | |
252 | 731 | vcaballero | Property p = new Property();
|
253 | p.setKey(key); |
||
254 | p.setValue(new Integer(value).toString()); |
||
255 | putProperty(p); |
||
256 | } |
||
257 | 149 | fernando | |
258 | 731 | vcaballero | /**
|
259 | * A?ade una propiedad con un String como clave y un long como valor.
|
||
260 | *
|
||
261 | 6875 | jaume | * @param key
|
262 | * clave.
|
||
263 | * @param value
|
||
264 | * valor.
|
||
265 | 731 | vcaballero | */
|
266 | public void putProperty(String key, long value) { |
||
267 | if (key == null) { |
||
268 | return;
|
||
269 | } |
||
270 | 433 | vcaballero | |
271 | 731 | vcaballero | Property p = new Property();
|
272 | p.setKey(key); |
||
273 | p.setValue(new Long(value).toString()); |
||
274 | putProperty(p); |
||
275 | } |
||
276 | 149 | fernando | |
277 | 731 | vcaballero | /**
|
278 | * A?ade una propiedad con un String como clave y un boolean como valor.
|
||
279 | *
|
||
280 | 6875 | jaume | * @param key
|
281 | * clave.
|
||
282 | * @param value
|
||
283 | * valor.
|
||
284 | 731 | vcaballero | */
|
285 | public void putProperty(String key, boolean value) { |
||
286 | if (key == null) { |
||
287 | return;
|
||
288 | } |
||
289 | 433 | vcaballero | |
290 | 731 | vcaballero | Property p = new Property();
|
291 | p.setKey(key); |
||
292 | p.setValue(new Boolean(value).toString()); |
||
293 | putProperty(p); |
||
294 | } |
||
295 | 149 | fernando | |
296 | 731 | vcaballero | /**
|
297 | * A?ade una propiedad con un String como clave y un float como valor.
|
||
298 | *
|
||
299 | 6875 | jaume | * @param key
|
300 | * clave.
|
||
301 | * @param value
|
||
302 | * valor.
|
||
303 | 731 | vcaballero | */
|
304 | public void putProperty(String key, float value) { |
||
305 | if (key == null) { |
||
306 | return;
|
||
307 | } |
||
308 | 433 | vcaballero | |
309 | 731 | vcaballero | Property p = new Property();
|
310 | p.setKey(key); |
||
311 | p.setValue(new Float(value).toString()); |
||
312 | putProperty(p); |
||
313 | } |
||
314 | 149 | fernando | |
315 | 731 | vcaballero | /**
|
316 | * A?ade una propiedad con un String como clave y un double como valor.
|
||
317 | *
|
||
318 | 6875 | jaume | * @param key
|
319 | * clave.
|
||
320 | * @param value
|
||
321 | * valor.
|
||
322 | 731 | vcaballero | */
|
323 | public void putProperty(String key, double value) { |
||
324 | if (key == null) { |
||
325 | return;
|
||
326 | } |
||
327 | 433 | vcaballero | |
328 | 731 | vcaballero | Property p = new Property();
|
329 | p.setKey(key); |
||
330 | p.setValue(new Double(value).toString()); |
||
331 | putProperty(p); |
||
332 | } |
||
333 | 149 | fernando | |
334 | 731 | vcaballero | /**
|
335 | 6875 | jaume | * Devuelve el String que corresponda a la clave que se pasa como par?metro.
|
336 | 731 | vcaballero | *
|
337 | 6875 | jaume | * @param key
|
338 | * clave
|
||
339 | 731 | vcaballero | *
|
340 | * @return valor.
|
||
341 | *
|
||
342 | 6875 | jaume | * @throws NotExistInXMLEntity
|
343 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
344 | * esa clave.
|
||
345 | 731 | vcaballero | */
|
346 | public String getStringProperty(String key) { |
||
347 | Property[] properties = xmltag.getProperty();
|
||
348 | String res = null; |
||
349 | boolean exists = false; |
||
350 | 433 | vcaballero | |
351 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
352 | if (properties[i].getKey().compareTo(key) == 0) { |
||
353 | res = properties[i].getValue(); |
||
354 | exists = true;
|
||
355 | } |
||
356 | } |
||
357 | 433 | vcaballero | |
358 | 731 | vcaballero | if (exists) {
|
359 | return res;
|
||
360 | } |
||
361 | 149 | fernando | |
362 | 731 | vcaballero | throw new NotExistInXMLEntity(); |
363 | } |
||
364 | 433 | vcaballero | |
365 | 731 | vcaballero | /**
|
366 | 6875 | jaume | * Devuelve el double que corresponda a la clave que se pasa como par?metro.
|
367 | 731 | vcaballero | *
|
368 | 6875 | jaume | * @param key
|
369 | * clave
|
||
370 | 731 | vcaballero | *
|
371 | * @return valor.
|
||
372 | *
|
||
373 | 6875 | jaume | * @throws NotExistInXMLEntity
|
374 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
375 | * esa clave.
|
||
376 | 731 | vcaballero | */
|
377 | public double getDoubleProperty(String key) { |
||
378 | Property[] properties = xmltag.getProperty();
|
||
379 | double res = 0; |
||
380 | boolean exists = false; |
||
381 | 433 | vcaballero | |
382 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
383 | if (properties[i].getKey().compareTo(key) == 0) { |
||
384 | res = Double.parseDouble(properties[i].getValue());
|
||
385 | exists = true;
|
||
386 | } |
||
387 | } |
||
388 | 149 | fernando | |
389 | 731 | vcaballero | if (exists) {
|
390 | return res;
|
||
391 | } |
||
392 | 433 | vcaballero | |
393 | 731 | vcaballero | throw new NotExistInXMLEntity(); |
394 | } |
||
395 | 478 | vcaballero | |
396 | 731 | vcaballero | /**
|
397 | * Devuelve el array de doubles que corresponda a la clave que se pasa como
|
||
398 | * par?metro.
|
||
399 | *
|
||
400 | 6875 | jaume | * @param key
|
401 | * clave
|
||
402 | 731 | vcaballero | *
|
403 | * @return valor.
|
||
404 | *
|
||
405 | 6875 | jaume | * @throws NotExistInXMLEntity
|
406 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
407 | * esa clave.
|
||
408 | 731 | vcaballero | */
|
409 | public double[] getDoubleArrayProperty(String key) { |
||
410 | Property[] properties = xmltag.getProperty();
|
||
411 | String value = null; |
||
412 | boolean exists = false; |
||
413 | 478 | vcaballero | |
414 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
415 | if (properties[i].getKey().compareTo(key) == 0) { |
||
416 | value = properties[i].getValue(); |
||
417 | exists = true;
|
||
418 | } |
||
419 | } |
||
420 | 149 | fernando | |
421 | 731 | vcaballero | if (!exists) {
|
422 | throw new NotExistInXMLEntity(); |
||
423 | } |
||
424 | 149 | fernando | |
425 | 731 | vcaballero | if (value.compareTo("") == 0) { |
426 | return new double[0]; |
||
427 | } |
||
428 | 149 | fernando | |
429 | 731 | vcaballero | String[] aux = (String[]) value.split(" ,"); |
430 | double[] ret = new double[aux.length]; |
||
431 | 433 | vcaballero | |
432 | 731 | vcaballero | for (int i = 0; i < aux.length; i++) { |
433 | ret[i] = Double.parseDouble(aux[i].replaceAll("\\\\,", ",")); |
||
434 | } |
||
435 | 433 | vcaballero | |
436 | 731 | vcaballero | return ret;
|
437 | } |
||
438 | 149 | fernando | |
439 | 731 | vcaballero | /**
|
440 | 6875 | jaume | * Devuelve el Object que corresponda a la clave que se pasa como par?metro.
|
441 | 731 | vcaballero | *
|
442 | 6875 | jaume | * @param key
|
443 | * clave
|
||
444 | 731 | vcaballero | *
|
445 | * @return valor.
|
||
446 | *
|
||
447 | 6875 | jaume | * @throws NotExistInXMLEntity
|
448 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
449 | * esa clave.
|
||
450 | 731 | vcaballero | */
|
451 | public Object getObjectProperty(String key) { |
||
452 | Property[] properties = xmltag.getProperty();
|
||
453 | Object res = null; |
||
454 | boolean exists = false; |
||
455 | 433 | vcaballero | |
456 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
457 | if (properties[i].getKey().compareTo(key) == 0) { |
||
458 | res = properties[i].getValue(); |
||
459 | exists = true;
|
||
460 | } |
||
461 | } |
||
462 | 478 | vcaballero | |
463 | 731 | vcaballero | if (exists) {
|
464 | return res;
|
||
465 | } |
||
466 | 478 | vcaballero | |
467 | 731 | vcaballero | throw new NotExistInXMLEntity(); |
468 | } |
||
469 | 149 | fernando | |
470 | 731 | vcaballero | /**
|
471 | * Devuelve el array de float que corresponda a la clave que se pasa como
|
||
472 | * par?metro.
|
||
473 | *
|
||
474 | 6875 | jaume | * @param key
|
475 | * clave
|
||
476 | 731 | vcaballero | *
|
477 | * @return valor.
|
||
478 | *
|
||
479 | 6875 | jaume | * @throws NotExistInXMLEntity
|
480 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
481 | * esa clave.
|
||
482 | 731 | vcaballero | */
|
483 | public float[] getFloatArrayProperty(String key) { |
||
484 | Property[] properties = xmltag.getProperty();
|
||
485 | String value = null; |
||
486 | boolean exists = false; |
||
487 | 149 | fernando | |
488 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
489 | if (properties[i].getKey().compareTo(key) == 0) { |
||
490 | value = properties[i].getValue(); |
||
491 | exists = true;
|
||
492 | } |
||
493 | } |
||
494 | 149 | fernando | |
495 | 731 | vcaballero | if (!exists) {
|
496 | throw new NotExistInXMLEntity(); |
||
497 | } |
||
498 | 433 | vcaballero | |
499 | 731 | vcaballero | if (value.compareTo("") == 0) { |
500 | return new float[0]; |
||
501 | } |
||
502 | 478 | vcaballero | |
503 | 731 | vcaballero | String[] aux = (String[]) value.split(" ,"); |
504 | float[] ret = new float[aux.length]; |
||
505 | 478 | vcaballero | |
506 | 731 | vcaballero | for (int i = 0; i < aux.length; i++) { |
507 | ret[i] = Float.parseFloat(aux[i].replaceAll("\\\\,", ",")); |
||
508 | } |
||
509 | 149 | fernando | |
510 | 731 | vcaballero | return ret;
|
511 | } |
||
512 | 149 | fernando | |
513 | 731 | vcaballero | /**
|
514 | * Devuelve el array de long que corresponda a la clave que se pasa como
|
||
515 | * par?metro.
|
||
516 | *
|
||
517 | 6875 | jaume | * @param key
|
518 | * clave
|
||
519 | 731 | vcaballero | *
|
520 | * @return valor.
|
||
521 | *
|
||
522 | 6875 | jaume | * @throws NotExistInXMLEntity
|
523 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
524 | * esa clave.
|
||
525 | 731 | vcaballero | */
|
526 | public long[] getLongArrayProperty(String key) { |
||
527 | Property[] properties = xmltag.getProperty();
|
||
528 | String value = null; |
||
529 | boolean exists = false; |
||
530 | 149 | fernando | |
531 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
532 | if (properties[i].getKey().compareTo(key) == 0) { |
||
533 | value = properties[i].getValue(); |
||
534 | exists = true;
|
||
535 | } |
||
536 | } |
||
537 | 433 | vcaballero | |
538 | 731 | vcaballero | if (!exists) {
|
539 | throw new NotExistInXMLEntity(); |
||
540 | } |
||
541 | 478 | vcaballero | |
542 | 731 | vcaballero | if (value.compareTo("") == 0) { |
543 | return new long[0]; |
||
544 | } |
||
545 | 478 | vcaballero | |
546 | 731 | vcaballero | String[] aux = (String[]) value.split(" ,"); |
547 | long[] ret = new long[aux.length]; |
||
548 | 149 | fernando | |
549 | 731 | vcaballero | for (int i = 0; i < aux.length; i++) { |
550 | ret[i] = Long.parseLong(aux[i].replaceAll("\\\\,", ",")); |
||
551 | } |
||
552 | 149 | fernando | |
553 | 731 | vcaballero | return ret;
|
554 | } |
||
555 | 6875 | jaume | |
556 | 1915 | vcaballero | /**
|
557 | * Devuelve el array de bytes que corresponda a la clave que se pasa como
|
||
558 | * par?metro.
|
||
559 | *
|
||
560 | 6875 | jaume | * @param key
|
561 | * clave
|
||
562 | 1915 | vcaballero | *
|
563 | * @return valor.
|
||
564 | *
|
||
565 | 6875 | jaume | * @throws NotExistInXMLEntity
|
566 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
567 | * esa clave.
|
||
568 | 1915 | vcaballero | */
|
569 | public byte[] getByteArrayProperty(String key) { |
||
570 | Property[] properties = xmltag.getProperty();
|
||
571 | String value = null; |
||
572 | boolean exists = false; |
||
573 | 149 | fernando | |
574 | 1915 | vcaballero | for (int i = 0; i < properties.length; i++) { |
575 | if (properties[i].getKey().compareTo(key) == 0) { |
||
576 | value = properties[i].getValue(); |
||
577 | exists = true;
|
||
578 | } |
||
579 | } |
||
580 | |||
581 | if (!exists) {
|
||
582 | throw new NotExistInXMLEntity(); |
||
583 | } |
||
584 | |||
585 | if (value.compareTo("") == 0) { |
||
586 | return new byte[0]; |
||
587 | } |
||
588 | |||
589 | String[] aux = (String[]) value.split(" ,"); |
||
590 | byte[] ret = new byte[aux.length]; |
||
591 | |||
592 | for (int i = 0; i < aux.length; i++) { |
||
593 | ret[i] = Byte.parseByte(aux[i].replaceAll("\\\\,", ",")); |
||
594 | } |
||
595 | |||
596 | return ret;
|
||
597 | } |
||
598 | |||
599 | 731 | vcaballero | /**
|
600 | * Devuelve el array de enteros que corresponda a la clave que se pasa como
|
||
601 | * par?metro.
|
||
602 | *
|
||
603 | 6875 | jaume | * @param key
|
604 | * clave
|
||
605 | 731 | vcaballero | *
|
606 | * @return valor.
|
||
607 | *
|
||
608 | 6875 | jaume | * @throws NotExistInXMLEntity
|
609 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
610 | * esa clave.
|
||
611 | 731 | vcaballero | */
|
612 | public int[] getIntArrayProperty(String key) { |
||
613 | Property[] properties = xmltag.getProperty();
|
||
614 | String value = null; |
||
615 | boolean exists = false; |
||
616 | 433 | vcaballero | |
617 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
618 | if (properties[i].getKey().compareTo(key) == 0) { |
||
619 | value = properties[i].getValue(); |
||
620 | exists = true;
|
||
621 | } |
||
622 | } |
||
623 | 478 | vcaballero | |
624 | 731 | vcaballero | if (!exists) {
|
625 | throw new NotExistInXMLEntity(); |
||
626 | } |
||
627 | 478 | vcaballero | |
628 | 731 | vcaballero | if (value.compareTo("") == 0) { |
629 | return new int[0]; |
||
630 | } |
||
631 | 149 | fernando | |
632 | 731 | vcaballero | String[] aux = (String[]) value.split(" ,"); |
633 | int[] ret = new int[aux.length]; |
||
634 | 149 | fernando | |
635 | 731 | vcaballero | for (int i = 0; i < aux.length; i++) { |
636 | ret[i] = Integer.parseInt(aux[i].replaceAll("\\\\,", ",")); |
||
637 | } |
||
638 | 149 | fernando | |
639 | 731 | vcaballero | return ret;
|
640 | } |
||
641 | 433 | vcaballero | |
642 | 731 | vcaballero | /**
|
643 | * Devuelve el array de boolean que corresponda a la clave que se pasa como
|
||
644 | * par?metro.
|
||
645 | *
|
||
646 | 6875 | jaume | * @param key
|
647 | * clave
|
||
648 | 731 | vcaballero | *
|
649 | * @return valor.
|
||
650 | *
|
||
651 | 6875 | jaume | * @throws NotExistInXMLEntity
|
652 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
653 | * esa clave.
|
||
654 | 731 | vcaballero | */
|
655 | public boolean[] getBooleanArrayProperty(String key) { |
||
656 | Property[] properties = xmltag.getProperty();
|
||
657 | String value = null; |
||
658 | boolean exists = false; |
||
659 | 478 | vcaballero | |
660 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
661 | if (properties[i].getKey().compareTo(key) == 0) { |
||
662 | value = properties[i].getValue(); |
||
663 | exists = true;
|
||
664 | } |
||
665 | } |
||
666 | 478 | vcaballero | |
667 | 731 | vcaballero | if (!exists) {
|
668 | throw new NotExistInXMLEntity(); |
||
669 | } |
||
670 | 169 | fernando | |
671 | 731 | vcaballero | if (value.compareTo("") == 0) { |
672 | return new boolean[0]; |
||
673 | } |
||
674 | 149 | fernando | |
675 | 731 | vcaballero | String[] aux = (String[]) value.split(" ,"); |
676 | boolean[] ret = new boolean[aux.length]; |
||
677 | 149 | fernando | |
678 | 731 | vcaballero | for (int i = 0; i < aux.length; i++) { |
679 | ret[i] = Boolean.valueOf(aux[i].replaceAll("\\\\,", ",")) |
||
680 | 6875 | jaume | .booleanValue(); |
681 | 731 | vcaballero | } |
682 | 433 | vcaballero | |
683 | 731 | vcaballero | return ret;
|
684 | } |
||
685 | 433 | vcaballero | |
686 | 731 | vcaballero | /**
|
687 | * Devuelve el array de String que corresponda a la clave que se pasa como
|
||
688 | * par?metro.
|
||
689 | *
|
||
690 | 6875 | jaume | * @param key
|
691 | * clave
|
||
692 | 731 | vcaballero | *
|
693 | * @return valor.
|
||
694 | *
|
||
695 | 6875 | jaume | * @throws NotExistInXMLEntity
|
696 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
697 | * esa clave.
|
||
698 | 731 | vcaballero | */
|
699 | public String[] getStringArrayProperty(String key) { |
||
700 | 6875 | jaume | // /String value = (String) properties.get(key);
|
701 | 731 | vcaballero | Property[] properties = xmltag.getProperty();
|
702 | String value = null; |
||
703 | boolean exists = false; |
||
704 | 149 | fernando | |
705 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
706 | if (properties[i].getKey().compareTo(key) == 0) { |
||
707 | value = properties[i].getValue(); |
||
708 | exists = true;
|
||
709 | } |
||
710 | } |
||
711 | 433 | vcaballero | |
712 | 731 | vcaballero | if (!exists) {
|
713 | throw new NotExistInXMLEntity(); |
||
714 | } |
||
715 | 433 | vcaballero | |
716 | 731 | vcaballero | if (value.compareTo("") == 0) { |
717 | return new String[0]; |
||
718 | } |
||
719 | 149 | fernando | |
720 | 731 | vcaballero | String[] aux = (String[]) value.split(" ,"); |
721 | 433 | vcaballero | |
722 | 731 | vcaballero | for (int i = 0; i < aux.length; i++) { |
723 | aux[i] = aux[i].replaceAll("\\\\,", ","); |
||
724 | } |
||
725 | 433 | vcaballero | |
726 | 731 | vcaballero | return aux;
|
727 | } |
||
728 | 149 | fernando | |
729 | 731 | vcaballero | /**
|
730 | * Devuelve el boolean que corresponda a la clave que se pasa como
|
||
731 | * par?metro.
|
||
732 | *
|
||
733 | 6875 | jaume | * @param key
|
734 | * clave
|
||
735 | 731 | vcaballero | *
|
736 | * @return valor.
|
||
737 | *
|
||
738 | 6875 | jaume | * @throws NotExistInXMLEntity
|
739 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
740 | * esa clave.
|
||
741 | 731 | vcaballero | */
|
742 | public boolean getBooleanProperty(String key) { |
||
743 | Property[] properties = xmltag.getProperty();
|
||
744 | boolean res = false; |
||
745 | boolean exists = false; |
||
746 | 433 | vcaballero | |
747 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
748 | if (properties[i].getKey().compareTo(key) == 0) { |
||
749 | res = (boolean) Boolean.valueOf(properties[i].getValue()) |
||
750 | 6875 | jaume | .booleanValue(); |
751 | 731 | vcaballero | exists = true;
|
752 | } |
||
753 | } |
||
754 | 433 | vcaballero | |
755 | 731 | vcaballero | if (exists) {
|
756 | return res;
|
||
757 | } |
||
758 | 149 | fernando | |
759 | 731 | vcaballero | throw new NotExistInXMLEntity(); |
760 | } |
||
761 | 149 | fernando | |
762 | 731 | vcaballero | /**
|
763 | 6875 | jaume | * Devuelve el entero que corresponda a la clave que se pasa como par?metro.
|
764 | 731 | vcaballero | *
|
765 | 6875 | jaume | * @param key
|
766 | * clave
|
||
767 | 731 | vcaballero | *
|
768 | * @return valor.
|
||
769 | *
|
||
770 | 6875 | jaume | * @throws NotExistInXMLEntity
|
771 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
772 | * esa clave.
|
||
773 | 731 | vcaballero | */
|
774 | public int getIntProperty(String key) { |
||
775 | Property[] properties = xmltag.getProperty();
|
||
776 | int res = 0; |
||
777 | boolean exists = false; |
||
778 | 149 | fernando | |
779 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
780 | if (properties[i].getKey().compareTo(key) == 0) { |
||
781 | res = Integer.parseInt(properties[i].getValue());
|
||
782 | 6875 | jaume | exists = true;
|
783 | 731 | vcaballero | } |
784 | } |
||
785 | 149 | fernando | |
786 | 731 | vcaballero | if (exists) {
|
787 | return res;
|
||
788 | } |
||
789 | 433 | vcaballero | |
790 | 731 | vcaballero | throw new NotExistInXMLEntity(); |
791 | } |
||
792 | 149 | fernando | |
793 | 731 | vcaballero | /**
|
794 | * Devuelve el long que corresponda a la clave que se pasa como par?metro.
|
||
795 | *
|
||
796 | 6875 | jaume | * @param key
|
797 | * clave
|
||
798 | 731 | vcaballero | *
|
799 | * @return valor.
|
||
800 | *
|
||
801 | 6875 | jaume | * @throws NotExistInXMLEntity
|
802 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
803 | * esa clave.
|
||
804 | 731 | vcaballero | */
|
805 | public long getLongProperty(String key) { |
||
806 | Property[] properties = xmltag.getProperty();
|
||
807 | long res = 0; |
||
808 | boolean exists = false; |
||
809 | 433 | vcaballero | |
810 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
811 | if (properties[i].getKey().compareTo(key) == 0) { |
||
812 | res = Long.valueOf(properties[i].getValue()).longValue();
|
||
813 | exists = true;
|
||
814 | } |
||
815 | } |
||
816 | 149 | fernando | |
817 | 731 | vcaballero | if (exists) {
|
818 | return res;
|
||
819 | } |
||
820 | 149 | fernando | |
821 | 731 | vcaballero | throw new NotExistInXMLEntity(); |
822 | } |
||
823 | 433 | vcaballero | |
824 | 731 | vcaballero | /**
|
825 | * Devuelve el float que corresponda a la clave que se pasa como par?metro.
|
||
826 | *
|
||
827 | 6875 | jaume | * @param key
|
828 | * clave
|
||
829 | 731 | vcaballero | *
|
830 | * @return valor.
|
||
831 | *
|
||
832 | 6875 | jaume | * @throws NotExistInXMLEntity
|
833 | * Lanza esta excepci?n si no se encuentra ning?n elemento con
|
||
834 | * esa clave.
|
||
835 | 731 | vcaballero | */
|
836 | public float getFloatProperty(String key) { |
||
837 | Property[] properties = xmltag.getProperty();
|
||
838 | float res = 0; |
||
839 | boolean exists = false; |
||
840 | 149 | fernando | |
841 | 731 | vcaballero | for (int i = 0; i < properties.length; i++) { |
842 | if (properties[i].getKey().compareTo(key) == 0) { |
||
843 | res = Float.valueOf(properties[i].getValue()).floatValue();
|
||
844 | exists = true;
|
||
845 | } |
||
846 | } |
||
847 | 149 | fernando | |
848 | 731 | vcaballero | if (exists) {
|
849 | return res;
|
||
850 | } |
||
851 | 433 | vcaballero | |
852 | 731 | vcaballero | throw new NotExistInXMLEntity(); |
853 | } |
||
854 | 149 | fernando | |
855 | 731 | vcaballero | /**
|
856 | * A?ade el nombre con clave name y valor el String que se pasa como
|
||
857 | * par?metro.
|
||
858 | *
|
||
859 | 6875 | jaume | * @param name
|
860 | * nombre.
|
||
861 | 731 | vcaballero | */
|
862 | public void setName(String name) { |
||
863 | Property p = new Property();
|
||
864 | p.setKey("name");
|
||
865 | p.setValue(name); |
||
866 | xmltag.addProperty(p); |
||
867 | } |
||
868 | 149 | fernando | |
869 | 731 | vcaballero | /**
|
870 | * Devuelve el nombre.
|
||
871 | *
|
||
872 | * @return nombre.
|
||
873 | */
|
||
874 | public String getName() { |
||
875 | Property p = getProperty("name");
|
||
876 | 149 | fernando | |
877 | 731 | vcaballero | return (String) p.getValue(); |
878 | } |
||
879 | 433 | vcaballero | |
880 | 731 | vcaballero | /**
|
881 | * Devuelve la clase que implementa.
|
||
882 | *
|
||
883 | * @return clase.
|
||
884 | */
|
||
885 | public Class getImplementingClass() { |
||
886 | Property p = getProperty("class");
|
||
887 | Object ob = p.getValue();
|
||
888 | 433 | vcaballero | |
889 | 731 | vcaballero | return (Class) ob; |
890 | } |
||
891 | 433 | vcaballero | |
892 | 731 | vcaballero | /**
|
893 | * A?ade la clase que implementa
|
||
894 | *
|
||
895 | * @param c
|
||
896 | */
|
||
897 | 6875 | jaume | /*
|
898 | * public void setImplementingClass(Class c) { Property p = new Property();
|
||
899 | * p.setKey("class"); p.setValue(c.toString()); xmltag.addProperty(p); }
|
||
900 | */
|
||
901 | 149 | fernando | |
902 | 731 | vcaballero | /**
|
903 | * A?ade un hijo al XMLEntity.
|
||
904 | *
|
||
905 | 6875 | jaume | * @param entity
|
906 | * xml para a?adir.
|
||
907 | 731 | vcaballero | */
|
908 | public void addChild(XMLEntity entity) { |
||
909 | xmltag.addXmlTag(entity.getXmlTag()); |
||
910 | } |
||
911 | 433 | vcaballero | |
912 | 731 | vcaballero | /**
|
913 | * Devuelve un hijo a partir de un indice.
|
||
914 | *
|
||
915 | 6875 | jaume | * @param i
|
916 | * indice.
|
||
917 | 731 | vcaballero | *
|
918 | * @return hijo.
|
||
919 | */
|
||
920 | public XMLEntity getChild(int i) { |
||
921 | return new XMLEntity(xmltag.getXmlTag(i)); |
||
922 | } |
||
923 | 478 | vcaballero | |
924 | 731 | vcaballero | /**
|
925 | * Devuelve el n?mero de hijos que contiene el XMLEntity.
|
||
926 | *
|
||
927 | * @return n?mero de hijos.
|
||
928 | */
|
||
929 | 6875 | jaume | public int getChildrenCount() { |
930 | 731 | vcaballero | return xmltag.getXmlTagCount();
|
931 | } |
||
932 | |||
933 | /**
|
||
934 | * Devuelve el Property a partir de la clave.
|
||
935 | *
|
||
936 | 6875 | jaume | * @param key
|
937 | * clave.
|
||
938 | 731 | vcaballero | *
|
939 | * @return Property.
|
||
940 | */
|
||
941 | private Property getProperty(String key) { |
||
942 | Property[] ps = xmltag.getProperty();
|
||
943 | |||
944 | for (int i = 0; i < ps.length; i++) { |
||
945 | if (ps[i].getKey().compareTo(key) == 0) { |
||
946 | return ps[i];
|
||
947 | } |
||
948 | } |
||
949 | |||
950 | return null; |
||
951 | } |
||
952 | |||
953 | /**
|
||
954 | * Devuelve el xmltag.
|
||
955 | *
|
||
956 | * @return xmltag.
|
||
957 | */
|
||
958 | public XmlTag getXmlTag() {
|
||
959 | return xmltag;
|
||
960 | } |
||
961 | |||
962 | /**
|
||
963 | * A?ade el property que se pasa como par?metro.
|
||
964 | *
|
||
965 | 6875 | jaume | * @param p
|
966 | * property.
|
||
967 | 731 | vcaballero | */
|
968 | private void putProperty(Property p) { |
||
969 | for (int i = 0; i < xmltag.getPropertyCount(); i++) { |
||
970 | if (xmltag.getProperty(i).getKey().compareTo(p.getKey()) == 0) { |
||
971 | xmltag.getProperty(i).setValue(p.getValue()); |
||
972 | |||
973 | return;
|
||
974 | } |
||
975 | } |
||
976 | |||
977 | xmltag.addProperty(p); |
||
978 | } |
||
979 | 6875 | jaume | |
980 | 751 | vcaballero | public boolean contains(String key) { |
981 | Property[] properties = xmltag.getProperty();
|
||
982 | boolean exists = false; |
||
983 | |||
984 | for (int i = 0; i < properties.length; i++) { |
||
985 | if (properties[i].getKey().compareTo(key) == 0) { |
||
986 | exists = true;
|
||
987 | } |
||
988 | } |
||
989 | return exists;
|
||
990 | } |
||
991 | 6875 | jaume | |
992 | public int getPropertyCount() { |
||
993 | 1836 | fernando | return xmltag.getPropertyCount();
|
994 | } |
||
995 | 6875 | jaume | |
996 | public String getPropertyValue(int index) { |
||
997 | 1836 | fernando | return xmltag.getProperty(index).getValue();
|
998 | } |
||
999 | 6875 | jaume | |
1000 | public String getPropertyName(int index) { |
||
1001 | 1836 | fernando | return xmltag.getProperty(index).getKey();
|
1002 | } |
||
1003 | 6875 | jaume | |
1004 | 4381 | jaume | /**
|
1005 | * Removes a property of this XML-Entity.
|
||
1006 | *
|
||
1007 | 6875 | jaume | * @param p
|
1008 | * property.
|
||
1009 | 4381 | jaume | */
|
1010 | public void remove(String key) { |
||
1011 | Property[] properties = xmltag.getProperty();
|
||
1012 | 6875 | jaume | |
1013 | 4381 | jaume | for (int i = 0; i < properties.length; i++) { |
1014 | if (properties[i].getKey().compareTo(key) == 0) { |
||
1015 | 6875 | jaume | Property[] newProperties = new Property[properties.length - 1]; |
1016 | 4381 | jaume | int k = 0; |
1017 | for (int j = 0; j < newProperties.length; j++) { |
||
1018 | 6875 | jaume | if (j == i)
|
1019 | 4381 | jaume | k = 1;
|
1020 | 6875 | jaume | newProperties[j] = properties[j + k]; |
1021 | 4381 | jaume | } |
1022 | xmltag.setProperty(newProperties); |
||
1023 | return;
|
||
1024 | } |
||
1025 | } |
||
1026 | } |
||
1027 | 6875 | jaume | |
1028 | 6617 | jmvivo | /**
|
1029 | * Elimina el hijo n del XMLEntity.
|
||
1030 | *
|
||
1031 | 6875 | jaume | * @param indice
|
1032 | * del hijo a eliminar.
|
||
1033 | 6617 | jmvivo | */
|
1034 | public void removeChild(int index) { |
||
1035 | xmltag.removeXmlTag(index); |
||
1036 | } |
||
1037 | 6875 | jaume | |
1038 | 6626 | jmvivo | /**
|
1039 | * Elimina todos los hijos de XMLEntity.
|
||
1040 | *
|
||
1041 | */
|
||
1042 | 8765 | jjdelcerro | public void removeAllChildren() { |
1043 | 6626 | jmvivo | xmltag.removeAllXmlTag(); |
1044 | } |
||
1045 | 6875 | jaume | |
1046 | // Jaume (en proves)
|
||
1047 | public boolean equals(Object obj) { |
||
1048 | if (obj instanceof XMLEntity) { |
||
1049 | XMLEntity other = (XMLEntity) obj; |
||
1050 | |||
1051 | Property[] thisProperties;
|
||
1052 | Property[] otherProperties;
|
||
1053 | |||
1054 | thisProperties = xmltag.getProperty(); |
||
1055 | otherProperties = other.xmltag.getProperty(); |
||
1056 | |||
1057 | if (thisProperties.length != otherProperties.length)
|
||
1058 | return false; |
||
1059 | |||
1060 | for (int i = 0; i < thisProperties.length; i++) { |
||
1061 | if (!thisProperties[i].getKey().equals(
|
||
1062 | otherProperties[i].getKey())) |
||
1063 | return false; |
||
1064 | if (!thisProperties[i].getValue().equals(
|
||
1065 | otherProperties[i].getValue())) |
||
1066 | return false; |
||
1067 | } |
||
1068 | |||
1069 | if (this.getChildrenCount() != other.getChildrenCount()) |
||
1070 | return false; |
||
1071 | |||
1072 | for (int i = 0; i < this.getChildrenCount(); i++) { |
||
1073 | if (!this.getChild(i).equals(other.getChild(i))) |
||
1074 | return false; |
||
1075 | } |
||
1076 | |||
1077 | return true; |
||
1078 | } |
||
1079 | |||
1080 | return false; |
||
1081 | } |
||
1082 | |||
1083 | // jaume (en proves)
|
||
1084 | public int hashCode() { |
||
1085 | int result = 17; |
||
1086 | |||
1087 | if (xmltag.getName()!=null) { |
||
1088 | char[] name = xmltag.getName().toCharArray(); |
||
1089 | |||
1090 | for (int i = 0; i<name.length; i++) { |
||
1091 | result = 37 + result + (int) name[i]; |
||
1092 | } |
||
1093 | } |
||
1094 | |||
1095 | Property[] properties = xmltag.getProperty();
|
||
1096 | for (int i = 0; i < properties.length; i++) { |
||
1097 | char[] key = properties[i].getKey().toCharArray(); |
||
1098 | for (int j = 0; j < key.length; j++) { |
||
1099 | result = 37 + result + (int) key[j]; |
||
1100 | } |
||
1101 | |||
1102 | 8765 | jjdelcerro | if (properties[i]!=null) { |
1103 | String value = properties[i].getValue();
|
||
1104 | char[] chars = (value!=null)? value.toCharArray() : new char[0]; |
||
1105 | for (int j = 0; j < chars.length; j++) { |
||
1106 | result = 37 + result + (int) chars[j]; |
||
1107 | } |
||
1108 | } else {
|
||
1109 | result += 37;
|
||
1110 | 6875 | jaume | } |
1111 | } |
||
1112 | |||
1113 | for (int i = 0; i < this.getChildrenCount(); i++) { |
||
1114 | result = 37 + result + this.getChild(i).hashCode(); |
||
1115 | } |
||
1116 | return result;
|
||
1117 | } |
||
1118 | 8765 | jjdelcerro | /**
|
1119 | * Devuelve el primer hijo que el valor de su propieda 'key'
|
||
1120 | * es igual a 'value'
|
||
1121 | *
|
||
1122 | * @param key propiedad a comparar
|
||
1123 | * @param value valor a comparar
|
||
1124 | * @return XMLEntity hijo o null si no se encuentra
|
||
1125 | */
|
||
1126 | |||
1127 | public XMLEntity firstChild(String key, String value) { |
||
1128 | int num = this.getChildrenCount(); |
||
1129 | XMLEntity child; |
||
1130 | for (int i=0;i < num; i++) { |
||
1131 | child = this.getChild(i);
|
||
1132 | if (child.getStringProperty(key).equals(value)) {
|
||
1133 | return child;
|
||
1134 | } |
||
1135 | } |
||
1136 | return null; |
||
1137 | } |
||
1138 | |||
1139 | /**
|
||
1140 | * Devuelve el indice del primer hijo que el valor de su propieda 'key'
|
||
1141 | * es igual a 'value'
|
||
1142 | *
|
||
1143 | * @param key propiedad a comparar
|
||
1144 | * @param value valor a comparar
|
||
1145 | * @return int indice del hijo o -1 si no se encuentra
|
||
1146 | */
|
||
1147 | public int firstIndexOfChild(String key, String value) { |
||
1148 | int num = this.getChildrenCount(); |
||
1149 | XMLEntity child; |
||
1150 | for (int i=0;i < num; i++) { |
||
1151 | child = this.getChild(i);
|
||
1152 | if (child.getStringProperty(key).equals(value)) {
|
||
1153 | return i;
|
||
1154 | } |
||
1155 | } |
||
1156 | return -1; |
||
1157 | } |
||
1158 | |||
1159 | /**
|
||
1160 | * Devuelve un iterador sobre los hijos que cumplen la condicion
|
||
1161 | * que el valor de su propiedad 'key' es igual a 'value'
|
||
1162 | *
|
||
1163 | * El iterador no permite eliminacion
|
||
1164 | *
|
||
1165 | * @param key nombre de la propidedad
|
||
1166 | * @param value valor de la propiedad
|
||
1167 | * @return
|
||
1168 | */
|
||
1169 | public Iterator findChildren(String key, String value) { |
||
1170 | return new XMLEntityIterator(this,key,value); |
||
1171 | } |
||
1172 | |||
1173 | public String toString() { |
||
1174 | StringWriter buffer = new StringWriter(); |
||
1175 | |||
1176 | Marshaller m; |
||
1177 | try {
|
||
1178 | m = new Marshaller(buffer);
|
||
1179 | } catch (IOException e4) { |
||
1180 | // TODO Auto-generated catch block
|
||
1181 | e4.printStackTrace(); |
||
1182 | return null; |
||
1183 | } |
||
1184 | m.setEncoding("ISO-8859-1");
|
||
1185 | |||
1186 | try {
|
||
1187 | m.marshal(this.getXmlTag());
|
||
1188 | } catch (MarshalException e2) { |
||
1189 | // TODO Auto-generated catch block
|
||
1190 | e2.printStackTrace(); |
||
1191 | return null; |
||
1192 | } catch (ValidationException e3) {
|
||
1193 | // TODO Auto-generated catch block
|
||
1194 | e3.printStackTrace(); |
||
1195 | return null; |
||
1196 | } |
||
1197 | |||
1198 | return buffer.toString();
|
||
1199 | } |
||
1200 | |||
1201 | public static XMLEntity parse(String data) throws MarshalException, ValidationException { |
||
1202 | StringReader reader = new StringReader(data); |
||
1203 | |||
1204 | XmlTag tag; |
||
1205 | tag = (XmlTag) XmlTag.unmarshal(reader); |
||
1206 | return new XMLEntity(tag); |
||
1207 | } |
||
1208 | |||
1209 | 149 | fernando | } |
1210 | 8765 | jjdelcerro | |
1211 | class XMLEntityIterator implements Iterator { |
||
1212 | private int lastIndex; |
||
1213 | private int lastHasNextIndex; |
||
1214 | private XMLEntity entity;
|
||
1215 | private String key; |
||
1216 | private String value; |
||
1217 | |||
1218 | public XMLEntityIterator(XMLEntity entity,String key,String value) { |
||
1219 | this.entity = entity;
|
||
1220 | this.key = key;
|
||
1221 | this.value = value;
|
||
1222 | this.lastIndex = -1; |
||
1223 | this.lastHasNextIndex = -1; |
||
1224 | } |
||
1225 | public void remove() { |
||
1226 | throw new UnsupportedOperationException(); |
||
1227 | } |
||
1228 | |||
1229 | public boolean hasNext() { |
||
1230 | if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){ |
||
1231 | return false; |
||
1232 | } |
||
1233 | int num = this.entity.getChildrenCount(); |
||
1234 | XMLEntity child; |
||
1235 | for (int i=this.lastIndex+1;i < num; i++) { |
||
1236 | child = this.entity.getChild(i);
|
||
1237 | if (child.contains(key) && child.getStringProperty(key).equals(value)) {
|
||
1238 | this.lastHasNextIndex = i;
|
||
1239 | return true; |
||
1240 | } |
||
1241 | } |
||
1242 | return false; |
||
1243 | } |
||
1244 | |||
1245 | public Object next() { |
||
1246 | if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){ |
||
1247 | throw new NoSuchElementException(); |
||
1248 | } |
||
1249 | |||
1250 | XMLEntity child; |
||
1251 | |||
1252 | if (this.lastHasNextIndex > -1) { |
||
1253 | if (this.entity.getChildrenCount() > this.lastHasNextIndex) { |
||
1254 | child = this.entity.getChild(this.lastHasNextIndex); |
||
1255 | if (child.contains(key) && child.getStringProperty(key).equals(value)) {
|
||
1256 | this.lastIndex = this.lastHasNextIndex; |
||
1257 | this.lastHasNextIndex = -1; |
||
1258 | return child;
|
||
1259 | } |
||
1260 | } |
||
1261 | } |
||
1262 | |||
1263 | |||
1264 | int num = this.entity.getChildrenCount(); |
||
1265 | |||
1266 | for (int i=this.lastIndex+1;i < num; i++) { |
||
1267 | child = this.entity.getChild(i);
|
||
1268 | if (child.contains(key) && child.getStringProperty(key).equals(value)) {
|
||
1269 | this.lastIndex = i;
|
||
1270 | this.lastHasNextIndex = -1; |
||
1271 | return child;
|
||
1272 | } |
||
1273 | } |
||
1274 | this.lastHasNextIndex = -1; |
||
1275 | throw new NoSuchElementException(); |
||
1276 | } |
||
1277 | } |