Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libIverUtiles / src / com / iver / utiles / XMLEntity.java @ 28076

History | View | Annotate | Download (33.8 KB)

1
/* 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
package com.iver.utiles;
42

    
43
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
import java.util.Vector;
49

    
50
import org.exolab.castor.xml.MarshalException;
51
import org.exolab.castor.xml.Marshaller;
52
import org.exolab.castor.xml.ValidationException;
53

    
54
import com.iver.utiles.xmlEntity.generate.Property;
55
import com.iver.utiles.xmlEntity.generate.XmlTag;
56

    
57
/**
58
 * Adaptador de las llamadas a sus m?todos sobre los generados por castor para
59
 * generar despu?s los XML.
60
 *
61
 * @author Vicente Caballero Navarro
62
 */
63
public class XMLEntity {
64
        private Vector whiteAttrList;
65
        private XmlTag xmltag;
66

    
67
        /**
68
         * Crea un nuevo XMLEntity.
69
         *
70
         * @param tag
71
         *            DOCUMENT ME!
72
         */
73
        public XMLEntity(XmlTag tag) {
74
                xmltag = tag;
75
        }
76

    
77
        /**
78
         * Crea un nuevo XMLEntity.
79
         */
80
        public XMLEntity() {
81
                xmltag = new XmlTag();
82
        }
83

    
84
        /**
85
         * A?ade una propiedad con un String como clave y un String como valor.
86
         *
87
         * @param key
88
         *            clave.
89
         * @param value
90
         *            valor.
91
         * @param matters, if false this property will not take effect to the
92
         * result of toHashCode() method
93
         * @see toHashCode()
94
         */
95
        public void putProperty(String key, String value, boolean matters) {
96
                if ((key == null)) {
97
                        return;
98
                }
99

    
100
                Property p = new Property();
101
                p.setKey(key);
102
                p.setValue(value);
103
                putProperty(p, matters);
104
        }
105

    
106
        public void putProperty(String key, String value) {
107
                putProperty(key, value, true);
108
        }
109
        /**
110
         * A?ade una propiedad con un String como clave y un Object como valor.
111
         *
112
         * @param key
113
         *            clave.
114
         * @param value
115
         *            valor.
116
         * @param matters, if false this property will not take effect to the
117
         * result of toHashCode() method
118
         * @see toHashCode()
119
         */
120
        public void putProperty(String key, Object value, boolean matters) {
121
                String ret = "";
122

    
123
                if (key == null) {
124
                        return;
125
                }
126

    
127
                Class valueClass = value.getClass();
128

    
129
                if (valueClass.isArray()) {
130
                        Class compType = valueClass.getComponentType();
131

    
132
                        if (compType == byte.class) {
133
                                byte[] array = (byte[]) value;
134

    
135
                                if (!(array.length == 0)) {
136
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
137

    
138
                                        for (int i = 1; i < array.length; i++) {
139
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
140
                                                                "\\\\,"));
141
                                        }
142
                                }
143
                        } else if (compType == short.class) {
144
                                short[] array = (short[]) value;
145

    
146
                                if (!(array.length == 0)) {
147
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
148

    
149
                                        for (int i = 1; i < array.length; i++) {
150
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
151
                                                                "\\\\,"));
152
                                        }
153
                                }
154
                        } else if (compType == int.class) {
155
                                int[] array = (int[]) value;
156

    
157
                                if (!(array.length == 0)) {
158
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
159

    
160
                                        for (int i = 1; i < array.length; i++) {
161
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
162
                                                                "\\\\,"));
163
                                        }
164
                                }
165
                        } else if (compType == long.class) {
166
                                long[] array = (long[]) value;
167

    
168
                                if (!(array.length == 0)) {
169
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
170

    
171
                                        for (int i = 1; i < array.length; i++) {
172
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
173
                                                                "\\\\,"));
174
                                        }
175
                                }
176
                        } else if (compType == float.class) {
177
                                float[] array = (float[]) value;
178

    
179
                                if (!(array.length == 0)) {
180
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
181

    
182
                                        for (int i = 1; i < array.length; i++) {
183
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
184
                                                                "\\\\,"));
185
                                        }
186
                                }
187
                        } else if (compType == double.class) {
188
                                double[] array = (double[]) value;
189

    
190
                                if (!(array.length == 0)) {
191
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
192

    
193
                                        for (int i = 1; i < array.length; i++) {
194
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
195
                                                                "\\\\,"));
196
                                        }
197
                                }
198
                        } else if (compType == boolean.class) {
199
                                boolean[] array = (boolean[]) value;
200

    
201
                                if (!(array.length == 0)) {
202
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
203

    
204
                                        for (int i = 1; i < array.length; i++) {
205
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
206
                                                                "\\\\,"));
207
                                        }
208
                                }
209
                        } else if (compType == String.class) {
210
                                String[] array = (String[]) value;
211

    
212
                                if (!(array.length == 0)) {
213
                                        if (array[0] == null) {
214
                                                array[0] = "null";
215
                                        }
216

    
217
                                        ret += array[0].replaceAll("[,]", "\\\\,");
218

    
219
                                        for (int i = 1; i < array.length; i++) {
220
                                                if (array[i] == null) {
221
                                                        array[i] = "null";
222
                                                }
223

    
224
                                                array[i] = array[i].replaceAll("[,]", "\\\\,");
225
                                                ret += (" ," + array[i]);
226
                                        }
227
                                }
228
                        } else {
229
                                Object[] array = (Object[]) value;
230

    
231
                                if (!(array.length == 0)) {
232
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
233

    
234
                                        for (int i = 1; i < array.length; i++) {
235
                                                array[i] = ("" + array[i]).replaceAll("[,]", "\\\\,");
236
                                                ret += (" ," + array[i]);
237
                                        }
238
                                }
239
                        }
240
                } else {
241
                        ret = value.toString();
242
                }
243

    
244
                Property p = new Property();
245
                p.setKey(key);
246
                p.setValue(ret);
247
                putProperty(p, matters);
248
        }
249

    
250
        /**
251
         * A?ade una propiedad con un String como clave y un double como valor.
252
         * La propiedad a?adida afecta al valor calculado por toHashCode()
253
         *
254
         * @param key
255
         *            clave.
256
         * @param value
257
         *            valor.
258
         * @see toHashCode()
259
         */
260
        public void putProperty(String key, Object value) {
261
                putProperty(key, value, true);
262
        }
263

    
264
        /**
265
         * A?ade una propiedad con un String como clave y un entero como valor.
266
         *
267
         * @param key
268
         *            clave.
269
         * @param value
270
         *            valor.
271
         * @param matters, if false this property will not take effect to the
272
         * result of toHashCode() method
273
         * @see toHashCode()
274
         */
275
        public void putProperty(String key, int value, boolean matters) {
276
                if (key == null) {
277
                        return;
278
                }
279

    
280
                Property p = new Property();
281
                p.setKey(key);
282
                p.setValue(new Integer(value).toString());
283
                putProperty(p, matters);
284
        }
285

    
286
        /**
287
         * A?ade una propiedad con un String como clave y un double como valor.
288
         * La propiedad a?adida afecta al valor calculado por toHashCode()
289
         *
290
         * @param key
291
         *            clave.
292
         * @param value
293
         *            valor.
294
         * @see toHashCode()
295
         */
296
        public void putProperty(String key, int value) {
297
                putProperty(key, value, true);
298
        }
299

    
300
        /**
301
         * A?ade una propiedad con un String como clave y un long como valor.
302
         *
303
         * @param key
304
         *            clave.
305
         * @param value
306
         *            valor.
307
         * @param matters, if false this property will not take effect to the
308
         * result of toHashCode() method
309
         * @see toHashCode()
310
         */
311
        public void putProperty(String key, long value, boolean matters) {
312
                if (key == null) {
313
                        return;
314
                }
315

    
316
                Property p = new Property();
317
                p.setKey(key);
318
                p.setValue(new Long(value).toString());
319
                putProperty(p, matters);
320
        }
321

    
322
        /**
323
         * A?ade una propiedad con un String como clave y un double como valor.
324
         * La propiedad a?adida afecta al valor calculado por toHashCode()
325
         *
326
         * @param key
327
         *            clave.
328
         * @param value
329
         *            valor.
330
         * @see toHashCode()
331
         */
332
        public void putProperty(String key, long value) {
333
                putProperty(key, value, true);
334

    
335
        }
336

    
337
        /**
338
         * A?ade una propiedad con un String como clave y un boolean como valor.
339
         *
340
         * @param key
341
         *            clave.
342
         * @param value
343
         *            valor.
344
         * @param matters, if false this property will not take effect to the
345
         * result of toHashCode() method
346
         * @see toHashCode()
347
         */
348
        public void putProperty(String key, boolean value, boolean matters) {
349
                if (key == null) {
350
                        return;
351
                }
352

    
353
                Property p = new Property();
354
                p.setKey(key);
355
                p.setValue(new Boolean(value).toString());
356
                putProperty(p, matters);
357
        }
358

    
359
        /**
360
         * A?ade una propiedad con un String como clave y un double como valor.
361
         * La propiedad a?adida afecta al valor calculado por toHashCode()
362
         *
363
         * @param key
364
         *            clave.
365
         * @param value
366
         *            valor.
367
         * @see toHashCode()
368
         */
369
        public void putProperty(String key, boolean value) {
370
                putProperty(key, value, true);
371
        }
372

    
373

    
374
        /**
375
         * A?ade una propiedad con un String como clave y un float como valor.
376
         *
377
         * @param key
378
         *            clave.
379
         * @param value
380
         *            valor.
381
         * @param matters, if false this property will not take effect to the
382
         * result of toHashCode() method
383
         * @see toHashCode()
384
         */
385
        public void putProperty(String key, float value, boolean matters) {
386
                if (key == null) {
387
                        return;
388
                }
389

    
390
                Property p = new Property();
391
                p.setKey(key);
392
                p.setValue(new Float(value).toString());
393
                putProperty(p, matters);
394
        }
395

    
396
        /**
397
         * A?ade una propiedad con un String como clave y un double como valor.
398
         * La propiedad a?adida afecta al valor calculado por toHashCode()
399
         *
400
         * @param key
401
         *            clave.
402
         * @param value
403
         *            valor.
404
         * @see toHashCode()
405
         */
406
        public void putProperty(String key, float value) {
407
                putProperty(key, value, true);
408
        }
409

    
410
        /**
411
         * A?ade una propiedad con un String como clave y un double como valor.
412
         *
413
         * @param key
414
         *            clave.
415
         * @param value
416
         *            valor.
417
         * @param matters, if false this property will not take effect to the
418
         * result of toHashCode() method
419
         * @see toHashCode()
420
         */
421
        public void putProperty(String key, double value, boolean matters) {
422
                if (key == null) {
423
                        return;
424
                }
425

    
426
                Property p = new Property();
427
                p.setKey(key);
428
                p.setValue(new Double(value).toString());
429
                putProperty(p, matters);
430
        }
431

    
432
        /**
433
         * A?ade una propiedad con un String como clave y un double como valor.
434
         * La propiedad a?adida afecta al valor calculado por toHashCode()
435
         *
436
         * @param key
437
         *            clave.
438
         * @param value
439
         *            valor.
440
         * @see toHashCode()
441
         */
442
        public void putProperty(String key, double value) {
443
                putProperty(key, value, true);
444
        }
445
        /**
446
         * Devuelve el String que corresponda a la clave que se pasa como par?metro.
447
         *
448
         * @param key
449
         *            clave
450
         *
451
         * @return valor.
452
         *
453
         * @throws NotExistInXMLEntity
454
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
455
         *             esa clave.
456
         */
457
        public String getStringProperty(String key) {
458
                Property[] properties = xmltag.getProperty();
459
                String res = null;
460
                boolean exists = false;
461

    
462
                for (int i = 0; i < properties.length; i++) {
463
                        if (properties[i].getKey().compareTo(key) == 0) {
464
                                res = properties[i].getValue();
465
                                exists = true;
466
                        }
467
                }
468

    
469
                if (exists) {
470
                        return res;
471
                }
472

    
473
                throw new NotExistInXMLEntity();
474
        }
475

    
476
        /**
477
         * Devuelve el double que corresponda a la clave que se pasa como par?metro.
478
         *
479
         * @param key
480
         *            clave
481
         *
482
         * @return valor.
483
         *
484
         * @throws NotExistInXMLEntity
485
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
486
         *             esa clave.
487
         */
488
        public double getDoubleProperty(String key) {
489
                Property[] properties = xmltag.getProperty();
490
                double res = 0;
491
                boolean exists = false;
492

    
493
                for (int i = 0; i < properties.length; i++) {
494
                        if (properties[i].getKey().compareTo(key) == 0) {
495
                                res = Double.parseDouble(properties[i].getValue());
496
                                exists = true;
497
                        }
498
                }
499

    
500
                if (exists) {
501
                        return res;
502
                }
503

    
504
                throw new NotExistInXMLEntity();
505
        }
506

    
507
        /**
508
         * Devuelve el array de doubles que corresponda a la clave que se pasa como
509
         * par?metro.
510
         *
511
         * @param key
512
         *            clave
513
         *
514
         * @return valor.
515
         *
516
         * @throws NotExistInXMLEntity
517
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
518
         *             esa clave.
519
         */
520
        public double[] getDoubleArrayProperty(String key) {
521
                Property[] properties = xmltag.getProperty();
522
                String value = null;
523
                boolean exists = false;
524

    
525
                for (int i = 0; i < properties.length; i++) {
526
                        if (properties[i].getKey().compareTo(key) == 0) {
527
                                value = properties[i].getValue();
528
                                exists = true;
529
                        }
530
                }
531

    
532
                if (!exists) {
533
                        throw new NotExistInXMLEntity();
534
                }
535

    
536
                if (value.compareTo("") == 0) {
537
                        return new double[0];
538
                }
539

    
540
                String[] aux = (String[]) value.split(" ,");
541
                double[] ret = new double[aux.length];
542

    
543
                for (int i = 0; i < aux.length; i++) {
544
                        ret[i] = Double.parseDouble(aux[i].replaceAll("\\\\,", ","));
545
                }
546

    
547
                return ret;
548
        }
549

    
550
        /**
551
         * Devuelve el Object que corresponda a la clave que se pasa como par?metro.
552
         *
553
         * @param key
554
         *            clave
555
         *
556
         * @return valor.
557
         *
558
         * @throws NotExistInXMLEntity
559
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
560
         *             esa clave.
561
         */
562
        public Object getObjectProperty(String key) {
563
                Property[] properties = xmltag.getProperty();
564
                Object res = null;
565
                boolean exists = false;
566

    
567
                for (int i = 0; i < properties.length; i++) {
568
                        if (properties[i].getKey().compareTo(key) == 0) {
569
                                res = properties[i].getValue();
570
                                exists = true;
571
                        }
572
                }
573

    
574
                if (exists) {
575
                        return res;
576
                }
577

    
578
                throw new NotExistInXMLEntity();
579
        }
580

    
581
        /**
582
         * Devuelve el array de float que corresponda a la clave que se pasa como
583
         * par?metro.
584
         *
585
         * @param key
586
         *            clave
587
         *
588
         * @return valor.
589
         *
590
         * @throws NotExistInXMLEntity
591
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
592
         *             esa clave.
593
         */
594
        public float[] getFloatArrayProperty(String key) {
595
                Property[] properties = xmltag.getProperty();
596
                String value = null;
597
                boolean exists = false;
598

    
599
                for (int i = 0; i < properties.length; i++) {
600
                        if (properties[i].getKey().compareTo(key) == 0) {
601
                                value = properties[i].getValue();
602
                                exists = true;
603
                        }
604
                }
605

    
606
                if (!exists) {
607
                        throw new NotExistInXMLEntity();
608
                }
609

    
610
                if (value.compareTo("") == 0) {
611
                        return new float[0];
612
                }
613

    
614
                String[] aux = (String[]) value.split(" ,");
615
                float[] ret = new float[aux.length];
616

    
617
                for (int i = 0; i < aux.length; i++) {
618
                        ret[i] = Float.parseFloat(aux[i].replaceAll("\\\\,", ","));
619
                }
620

    
621
                return ret;
622
        }
623

    
624
        /**
625
         * Devuelve el array de long que corresponda a la clave que se pasa como
626
         * par?metro.
627
         *
628
         * @param key
629
         *            clave
630
         *
631
         * @return valor.
632
         *
633
         * @throws NotExistInXMLEntity
634
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
635
         *             esa clave.
636
         */
637
        public long[] getLongArrayProperty(String key) {
638
                Property[] properties = xmltag.getProperty();
639
                String value = null;
640
                boolean exists = false;
641

    
642
                for (int i = 0; i < properties.length; i++) {
643
                        if (properties[i].getKey().compareTo(key) == 0) {
644
                                value = properties[i].getValue();
645
                                exists = true;
646
                        }
647
                }
648

    
649
                if (!exists) {
650
                        throw new NotExistInXMLEntity();
651
                }
652

    
653
                if (value.compareTo("") == 0) {
654
                        return new long[0];
655
                }
656

    
657
                String[] aux = (String[]) value.split(" ,");
658
                long[] ret = new long[aux.length];
659

    
660
                for (int i = 0; i < aux.length; i++) {
661
                        ret[i] = Long.parseLong(aux[i].replaceAll("\\\\,", ","));
662
                }
663

    
664
                return ret;
665
        }
666

    
667
        /**
668
         * Devuelve el array de bytes que corresponda a la clave que se pasa como
669
         * par?metro.
670
         *
671
         * @param key
672
         *            clave
673
         *
674
         * @return valor.
675
         *
676
         * @throws NotExistInXMLEntity
677
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
678
         *             esa clave.
679
         */
680
        public byte[] getByteArrayProperty(String key) {
681
                Property[] properties = xmltag.getProperty();
682
                String value = null;
683
                boolean exists = false;
684

    
685
                for (int i = 0; i < properties.length; i++) {
686
                        if (properties[i].getKey().compareTo(key) == 0) {
687
                                value = properties[i].getValue();
688
                                exists = true;
689
                        }
690
                }
691

    
692
                if (!exists) {
693
                        throw new NotExistInXMLEntity();
694
                }
695

    
696
                if (value.compareTo("") == 0) {
697
                        return new byte[0];
698
                }
699

    
700
                String[] aux = (String[]) value.split(" ,");
701
                byte[] ret = new byte[aux.length];
702

    
703
                for (int i = 0; i < aux.length; i++) {
704
                        ret[i] = Byte.parseByte(aux[i].replaceAll("\\\\,", ","));
705
                }
706

    
707
                return ret;
708
        }
709

    
710
        /**
711
         * Devuelve el array de enteros que corresponda a la clave que se pasa como
712
         * par?metro.
713
         *
714
         * @param key
715
         *            clave
716
         *
717
         * @return valor.
718
         *
719
         * @throws NotExistInXMLEntity
720
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
721
         *             esa clave.
722
         */
723
        public int[] getIntArrayProperty(String key) {
724
                Property[] properties = xmltag.getProperty();
725
                String value = null;
726
                boolean exists = false;
727

    
728
                for (int i = 0; i < properties.length; i++) {
729
                        if (properties[i].getKey().compareTo(key) == 0) {
730
                                value = properties[i].getValue();
731
                                exists = true;
732
                        }
733
                }
734

    
735
                if (!exists) {
736
                        throw new NotExistInXMLEntity();
737
                }
738

    
739
                if (value.compareTo("") == 0) {
740
                        return new int[0];
741
                }
742

    
743
                String[] aux = (String[]) value.split(" ,");
744
                int[] ret = new int[aux.length];
745

    
746
                for (int i = 0; i < aux.length; i++) {
747
                        ret[i] = Integer.parseInt(aux[i].replaceAll("\\\\,", ","));
748
                }
749

    
750
                return ret;
751
        }
752

    
753
        /**
754
         * Devuelve el array de boolean que corresponda a la clave que se pasa como
755
         * par?metro.
756
         *
757
         * @param key
758
         *            clave
759
         *
760
         * @return valor.
761
         *
762
         * @throws NotExistInXMLEntity
763
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
764
         *             esa clave.
765
         */
766
        public boolean[] getBooleanArrayProperty(String key) {
767
                Property[] properties = xmltag.getProperty();
768
                String value = null;
769
                boolean exists = false;
770

    
771
                for (int i = 0; i < properties.length; i++) {
772
                        if (properties[i].getKey().compareTo(key) == 0) {
773
                                value = properties[i].getValue();
774
                                exists = true;
775
                        }
776
                }
777

    
778
                if (!exists) {
779
                        throw new NotExistInXMLEntity();
780
                }
781

    
782
                if (value.compareTo("") == 0) {
783
                        return new boolean[0];
784
                }
785

    
786
                String[] aux = (String[]) value.split(" ,");
787
                boolean[] ret = new boolean[aux.length];
788

    
789
                for (int i = 0; i < aux.length; i++) {
790
                        ret[i] = Boolean.valueOf(aux[i].replaceAll("\\\\,", ","))
791
                                        .booleanValue();
792
                }
793

    
794
                return ret;
795
        }
796

    
797
        /**
798
         * Devuelve el array de String que corresponda a la clave que se pasa como
799
         * par?metro.
800
         *
801
         * @param key
802
         *            clave
803
         *
804
         * @return valor.
805
         *
806
         * @throws NotExistInXMLEntity
807
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
808
         *             esa clave.
809
         */
810
        public String[] getStringArrayProperty(String key) {
811
                // /String value = (String) properties.get(key);
812
                Property[] properties = xmltag.getProperty();
813
                String value = null;
814
                boolean exists = false;
815

    
816
                for (int i = 0; i < properties.length; i++) {
817
                        if (properties[i].getKey().compareTo(key) == 0) {
818
                                value = properties[i].getValue();
819
                                exists = true;
820
                        }
821
                }
822

    
823
                if (!exists) {
824
                        throw new NotExistInXMLEntity();
825
                }
826

    
827
                if (value.compareTo("") == 0) {
828
                        return new String[0];
829
                }
830

    
831
                String[] aux = (String[]) value.split(" ,");
832

    
833
                for (int i = 0; i < aux.length; i++) {
834
                        aux[i] = aux[i].replaceAll("\\\\,", ",");
835
                }
836

    
837
                return aux;
838
        }
839

    
840
        /**
841
         * Devuelve el boolean que corresponda a la clave que se pasa como
842
         * par?metro.
843
         *
844
         * @param key
845
         *            clave
846
         *
847
         * @return valor.
848
         *
849
         * @throws NotExistInXMLEntity
850
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
851
         *             esa clave.
852
         */
853
        public boolean getBooleanProperty(String key) {
854
                Property[] properties = xmltag.getProperty();
855
                boolean res = false;
856
                boolean exists = false;
857

    
858
                for (int i = 0; i < properties.length; i++) {
859
                        if (properties[i].getKey().compareTo(key) == 0) {
860
                                res = (boolean) Boolean.valueOf(properties[i].getValue())
861
                                                .booleanValue();
862
                                exists = true;
863
                        }
864
                }
865

    
866
                if (exists) {
867
                        return res;
868
                }
869

    
870
                throw new NotExistInXMLEntity();
871
        }
872

    
873
        /**
874
         * Devuelve el entero que corresponda a la clave que se pasa como par?metro.
875
         *
876
         * @param key
877
         *            clave
878
         *
879
         * @return valor.
880
         *
881
         * @throws NotExistInXMLEntity
882
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
883
         *             esa clave.
884
         */
885
        public int getIntProperty(String key) {
886
                Property[] properties = xmltag.getProperty();
887
                int res = 0;
888
                boolean exists = false;
889

    
890
                for (int i = 0; i < properties.length; i++) {
891
                        if (properties[i].getKey().compareTo(key) == 0) {
892
                                res = Integer.parseInt(properties[i].getValue());
893
                                exists = true;
894
                        }
895
                }
896

    
897
                if (exists) {
898
                        return res;
899
                }
900

    
901
                throw new NotExistInXMLEntity();
902
        }
903

    
904
        /**
905
         * Devuelve el long que corresponda a la clave que se pasa como par?metro.
906
         *
907
         * @param key
908
         *            clave
909
         *
910
         * @return valor.
911
         *
912
         * @throws NotExistInXMLEntity
913
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
914
         *             esa clave.
915
         */
916
        public long getLongProperty(String key) {
917
                Property[] properties = xmltag.getProperty();
918
                long res = 0;
919
                boolean exists = false;
920

    
921
                for (int i = 0; i < properties.length; i++) {
922
                        if (properties[i].getKey().compareTo(key) == 0) {
923
                                res = Long.valueOf(properties[i].getValue()).longValue();
924
                                exists = true;
925
                        }
926
                }
927

    
928
                if (exists) {
929
                        return res;
930
                }
931

    
932
                throw new NotExistInXMLEntity();
933
        }
934

    
935
        /**
936
         * Devuelve el float que corresponda a la clave que se pasa como par?metro.
937
         *
938
         * @param key
939
         *            clave
940
         *
941
         * @return valor.
942
         *
943
         * @throws NotExistInXMLEntity
944
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
945
         *             esa clave.
946
         */
947
        public float getFloatProperty(String key) {
948
                Property[] properties = xmltag.getProperty();
949
                float res = 0;
950
                boolean exists = false;
951

    
952
                for (int i = 0; i < properties.length; i++) {
953
                        if (properties[i].getKey().compareTo(key) == 0) {
954
                                res = Float.valueOf(properties[i].getValue()).floatValue();
955
                                exists = true;
956
                        }
957
                }
958

    
959
                if (exists) {
960
                        return res;
961
                }
962

    
963
                throw new NotExistInXMLEntity();
964
        }
965

    
966
        /**
967
         * A?ade el nombre con clave name y valor el String que se pasa como
968
         * par?metro.
969
         *
970
         * @param name
971
         *            nombre.
972
         */
973
        public void setName(String name) {
974
                Property p = new Property();
975
                p.setKey("name");
976
                p.setValue(name);
977
                xmltag.addProperty(p);
978
        }
979

    
980
        /**
981
         * Devuelve el nombre.
982
         *
983
         * @return nombre.
984
         */
985
        public String getName() {
986
                Property p = getProperty("name");
987

    
988
                return (String) p.getValue();
989
        }
990

    
991
        /**
992
         * Devuelve la clase que implementa.
993
         *
994
         * @return clase.
995
         */
996
        public Class getImplementingClass() {
997
                Property p = getProperty("class");
998
                Object ob = p.getValue();
999

    
1000
                return (Class) ob;
1001
        }
1002

    
1003
        /**
1004
         * A?ade la clase que implementa
1005
         *
1006
         * @param c
1007
         */
1008
        /*
1009
         * public void setImplementingClass(Class c) { Property p = new Property();
1010
         * p.setKey("class"); p.setValue(c.toString()); xmltag.addProperty(p); }
1011
         */
1012

    
1013
        /**
1014
         * A?ade un hijo al XMLEntity.
1015
         *
1016
         * @param entity
1017
         *            xml para a?adir.
1018
         */
1019
        public void addChild(XMLEntity entity) {
1020
                xmltag.addXmlTag(entity.getXmlTag());
1021
        }
1022

    
1023
        /**
1024
         * Devuelve un hijo a partir de un indice.
1025
         *
1026
         * @param i
1027
         *            indice.
1028
         *
1029
         * @return hijo.
1030
         */
1031
        public XMLEntity getChild(int i) {
1032
                return new XMLEntity(xmltag.getXmlTag(i));
1033
        }
1034

    
1035
        /**
1036
         * Devuelve el n?mero de hijos que contiene el XMLEntity.
1037
         *
1038
         * @return n?mero de hijos.
1039
         */
1040
        public int getChildrenCount() {
1041
                return xmltag.getXmlTagCount();
1042
        }
1043

    
1044
        /**
1045
         * Devuelve el Property a partir de la clave.
1046
         *
1047
         * @param key
1048
         *            clave.
1049
         *
1050
         * @return Property.
1051
         */
1052
        private Property getProperty(String key) {
1053
                Property[] ps = xmltag.getProperty();
1054

    
1055
                for (int i = 0; i < ps.length; i++) {
1056
                        if (ps[i].getKey().compareTo(key) == 0) {
1057
                                return ps[i];
1058
                        }
1059
                }
1060

    
1061
                return null;
1062
        }
1063

    
1064
        /**
1065
         * Devuelve el xmltag.
1066
         *
1067
         * @return xmltag.
1068
         */
1069
        public XmlTag getXmlTag() {
1070
                return xmltag;
1071
        }
1072

    
1073
        /**
1074
         * A?ade el property que se pasa como par?metro.
1075
         *
1076
         * @param p
1077
         *            property.
1078
         */
1079
        private void putProperty(Property p, boolean matters) {
1080
                if (!matters) {
1081
                        getWhiteAttrList().add(p.getKey());
1082
                }
1083
                for (int i = 0; i < xmltag.getPropertyCount(); i++) {
1084
                        if (xmltag.getProperty(i).getKey().compareTo(p.getKey()) == 0) {
1085
                                xmltag.getProperty(i).setValue(p.getValue());
1086

    
1087
                                return;
1088
                        }
1089
                }
1090

    
1091
                xmltag.addProperty(p);
1092
        }
1093

    
1094
        private Vector getWhiteAttrList() {
1095
                if (whiteAttrList == null) {
1096
                        whiteAttrList = new Vector();
1097

    
1098
                }
1099

    
1100
                return whiteAttrList;
1101
        }
1102

    
1103
        public boolean contains(String key) {
1104
                Property[] properties = xmltag.getProperty();
1105
                boolean exists = false;
1106

    
1107
                for (int i = 0; i < properties.length; i++) {
1108
                        if (properties[i].getKey().compareTo(key) == 0) {
1109
                                exists = true;
1110
                        }
1111
                }
1112
                return exists;
1113
        }
1114

    
1115
        public int getPropertyCount() {
1116
                return xmltag.getPropertyCount();
1117
        }
1118

    
1119
        public String getPropertyValue(int index) {
1120
                return xmltag.getProperty(index).getValue();
1121
        }
1122

    
1123
        public String getPropertyName(int index) {
1124
                return xmltag.getProperty(index).getKey();
1125
        }
1126

    
1127
        /**
1128
         * Removes a property of this XML-Entity.
1129
         *
1130
         * @param p
1131
         *            property.
1132
         */
1133
        public void remove(String key) {
1134
                Property[] properties = xmltag.getProperty();
1135

    
1136
                for (int i = 0; i < properties.length; i++) {
1137
                        if (properties[i].getKey().compareTo(key) == 0) {
1138
                                Property[] newProperties = new Property[properties.length - 1];
1139
                                int k = 0;
1140
                                for (int j = 0; j < newProperties.length; j++) {
1141
                                        if (j == i)
1142
                                                k = 1;
1143
                                        newProperties[j] = properties[j + k];
1144
                                }
1145
                                xmltag.setProperty(newProperties);
1146
                                return;
1147
                        }
1148
                }
1149
        }
1150

    
1151
        /**
1152
         * Elimina el hijo n del XMLEntity.
1153
         *
1154
         * @param indice
1155
         *            del hijo a eliminar.
1156
         */
1157
        public void removeChild(int index) {
1158
                xmltag.removeXmlTag(index);
1159
        }
1160

    
1161
        /**
1162
         * Elimina todos los hijos de XMLEntity.
1163
         *
1164
         */
1165
        public void removeAllChildren() {
1166
                xmltag.removeAllXmlTag();
1167
        }
1168

    
1169
        // Jaume (en proves)
1170
        public boolean equals(Object obj) {
1171
                if (obj instanceof XMLEntity) {
1172
                        XMLEntity other = (XMLEntity) obj;
1173

    
1174
                        Property[] thisProperties;
1175
                        Property[] otherProperties;
1176

    
1177
                        thisProperties = xmltag.getProperty();
1178
                        otherProperties = other.xmltag.getProperty();
1179

    
1180
                        if (thisProperties.length != otherProperties.length)
1181
                                return false;
1182

    
1183
                        for (int i = 0; i < thisProperties.length; i++) {
1184
                                if (!thisProperties[i].getKey().equals(
1185
                                                otherProperties[i].getKey()))
1186
                                        return false;
1187
                                if (!thisProperties[i].getValue().equals(
1188
                                                otherProperties[i].getValue()))
1189
                                        return false;
1190
                        }
1191

    
1192
                        if (this.getChildrenCount() != other.getChildrenCount())
1193
                                return false;
1194

    
1195
                        for (int i = 0; i < this.getChildrenCount(); i++) {
1196
                                if (!this.getChild(i).equals(other.getChild(i)))
1197
                                        return false;
1198
                        }
1199

    
1200
                        return true;
1201
                }
1202

    
1203
                return false;
1204
        }
1205

    
1206
        // jaume (en proves)
1207
        /**
1208
         * <p>
1209
         * <code>hash()</code> method is used to know if this <b>XMLEntity</b>
1210
         * can be considered as changed. At the loading of the <b>XMLEntity</b> tree
1211
         * you can invoke this method. The result of it is a <code>int</code> hash
1212
         * value calculated from the contents of this and its children. Then, if you
1213
         * store this value you can recognize if the <b>XMLEntity</b> has changed
1214
         * since the last time you called <code>hash()</code> just by comparing
1215
         * the previous calculated value to the new calculated one.<br>
1216
         * </p>
1217
         * <p>
1218
         * Doing so, you can automatically detect if your persistent data has changed
1219
         * and you are required to save it.<br>
1220
         * </p>
1221
         * <p>
1222
         * <b>Notice</b> that you can mark the properties that you are using during
1223
         * the session but you don't care about the values between sessions as
1224
         * properties which don't matter to detect if your XMLEntity has changed.<br>
1225
         * </p>
1226
         *
1227
         */
1228
        public long hash() {
1229
                long result = 17;
1230

    
1231
                if (xmltag.getName()!=null) {
1232
                        char[] name = xmltag.getName().toCharArray();
1233

    
1234
                        for (int i = 0; i<name.length; i++) {
1235
                                result = 37 + result + (int) name[i];
1236
                        }
1237
                }
1238

    
1239
                Property[] properties = xmltag.getProperty();
1240
                for (int i = 0; i < properties.length; i++) {
1241
                        String strKey = properties[i].getKey();
1242
                        // if this key was put with the matter value set to false
1243
                        // then it will not take effect in the hash calculation
1244
                        if (whiteAttrList!=null && whiteAttrList.contains(strKey))
1245
                                continue;
1246

    
1247
                        char[] key = strKey.toCharArray();
1248
                        for (int j = 0; j < key.length; j++) {
1249
                                result = 37 + result + (int) key[j];
1250
                        }
1251

    
1252
                        if (properties[i]!=null) {
1253
                                String value = properties[i].getValue();
1254
                                char[] chars = (value!=null)? value.toCharArray() : new char[0];
1255
                                for (int j = 0; j < chars.length; j++) {
1256
                                        result = 37 + result + (int) chars[j];
1257
                                }
1258
                        } else {
1259
                                result += 37;
1260
                        }
1261
                }
1262

    
1263
                for (int i = 0; i < this.getChildrenCount(); i++) {
1264
                        result = 37 + result + this.getChild(i).hash();
1265
                }
1266
                return result;
1267
        }
1268

    
1269
        /**
1270
         * Devuelve el primer hijo que el valor de su propieda 'key'
1271
         * es igual a 'value'
1272
         *
1273
         * @param key propiedad a comparar
1274
         * @param value valor a comparar
1275
         * @return XMLEntity hijo o null si no se encuentra
1276
         */
1277
        public XMLEntity firstChild(String key, String value) {
1278
                int num = this.getChildrenCount();
1279
                XMLEntity child;
1280
                for (int i=0;i < num; i++) {
1281
                        child = this.getChild(i);
1282
                        try {
1283
                                if (child.getStringProperty(key).equals(value)) {
1284
                                        return child;
1285
                                }
1286
                        } catch (NotExistInXMLEntity e) {
1287
                                // Nothing to do
1288
                        }
1289
                }
1290
                return null;
1291
        }
1292

    
1293
        /**
1294
         * Devuelve el primer hijo cuyo nombre es igual a 'value'.
1295
         * El nombre de un XMLEntity viene determinado por el valor del
1296
         * atributo 'name' del xml-tag.
1297
         *
1298
         * @param value valor a comparar
1299
         * @return XMLEntity hijo o null si no se encuentra
1300
         */
1301
        public XMLEntity firstChild(String value) {
1302
                int num = this.getChildrenCount();
1303
                XMLEntity child;
1304
                for (int i=0;i < num; i++) {
1305
                        child = this.getChild(i);
1306
                        try {
1307
                                if (child.getXmlTag().getName().equals(value)) {
1308
                                        return child;
1309
                                }
1310
                        } catch (NotExistInXMLEntity e) {
1311
                                // Nothing to do
1312
                        }
1313
                }
1314
                return null;
1315
        }
1316

    
1317
        /**
1318
         * Devuelve el indice del primer hijo que el valor de su propieda 'key'
1319
         * es igual a 'value'
1320
         *
1321
         * @param key propiedad a comparar
1322
         * @param value valor a comparar
1323
         * @return int indice del hijo o -1 si no se encuentra
1324
         */
1325
        public int firstIndexOfChild(String key, String value) {
1326
                int num = this.getChildrenCount();
1327
                XMLEntity child;
1328
                for (int i=0;i < num; i++) {
1329
                        try {
1330
                                child = this.getChild(i);
1331
                                if (child.getStringProperty(key).equals(value)) {
1332
                                        return i;
1333
                                }
1334
                        } catch (NotExistInXMLEntity e) {
1335
                                // Nothing to do
1336
                        }
1337
                }
1338
                return -1;
1339
        }
1340

    
1341
        /**
1342
         * Devuelve el indice del primer hijo cuyo nombre es igual a 'value'.
1343
         * El nombre de un XMLEntity viene determinado por el valor del
1344
         * atributo 'name' del xml-tag.
1345
         *
1346
         * @param value valor a comparar
1347
         * @return int indice del hijo o -1 si no se encuentra
1348
         */
1349
        public int firstIndexOfChild(String value) {
1350
                int num = this.getChildrenCount();
1351
                XMLEntity child;
1352
                for (int i=0;i < num; i++) {
1353
                        try {
1354
                                child = this.getChild(i);
1355
                                if (child.getXmlTag().getName().equals(value)) {
1356
                                        return i;
1357
                                }
1358
                        } catch (NotExistInXMLEntity e) {
1359
                                // Nothing to do
1360
                        }
1361
                }
1362
                return -1;
1363
        }
1364

    
1365
        /**
1366
         * Devuelve un iterador sobre los hijos que cumplen la condicion
1367
         * que el valor de su propiedad 'key' es igual a 'value'
1368
         *
1369
         * El iterador no permite eliminacion
1370
         *
1371
         * @param key nombre de la propidedad
1372
         * @param value valor de la propiedad
1373
         * @return
1374
         */
1375
        public Iterator findChildren(String key, String value) {
1376
                return new XMLEntityIterator(this,key,value);
1377
        }
1378

    
1379
        public String toString() {
1380
                StringWriter buffer = new StringWriter();
1381

    
1382
                Marshaller m;
1383
                try {
1384
                        m = new Marshaller(buffer);
1385
                } catch (IOException e4) {
1386
                        // TODO Auto-generated catch block
1387
                        e4.printStackTrace();
1388
                        return null;
1389
                }
1390
                m.setEncoding("ISO-8859-1");
1391

    
1392
                try {
1393
                        m.marshal(this.getXmlTag());
1394
                } catch (MarshalException e2) {
1395
                        // TODO Auto-generated catch block
1396
                        e2.printStackTrace();
1397
                        return null;
1398
                } catch (ValidationException e3) {
1399
                        // TODO Auto-generated catch block
1400
                        e3.printStackTrace();
1401
                        return null;
1402
                }
1403

    
1404
                return buffer.toString();
1405
        }
1406

    
1407
        public static XMLEntity parse(String data) throws MarshalException, ValidationException {
1408
                StringReader reader = new StringReader(data);
1409

    
1410
                XmlTag tag;
1411
                tag = (XmlTag) XmlTag.unmarshal(reader);
1412
                return new XMLEntity(tag);
1413
        }
1414

    
1415
}
1416

    
1417
class XMLEntityIterator implements Iterator {
1418
        private int lastIndex;
1419
        private int lastHasNextIndex;
1420
        private XMLEntity entity;
1421
        private String key;
1422
        private String value;
1423

    
1424
        public XMLEntityIterator(XMLEntity entity,String key,String value) {
1425
                this.entity = entity;
1426
                this.key = key;
1427
                this.value = value;
1428
                this.lastIndex = -1;
1429
                this.lastHasNextIndex = -1;
1430
        }
1431
        public void remove() {
1432
                throw new UnsupportedOperationException();
1433
        }
1434

    
1435
        public boolean hasNext() {
1436
                if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){
1437
                        return false;
1438
                }
1439
                int num = this.entity.getChildrenCount();
1440
                XMLEntity child;
1441
                for (int i=this.lastIndex+1;i < num; i++) {
1442
                        child = this.entity.getChild(i);
1443
                        try {
1444
                                if (child.getStringProperty(key).equals(value)) {
1445
                                        this.lastHasNextIndex = i;
1446
                                        return true;
1447
                                }
1448
                        } catch (NotExistInXMLEntity e) {
1449
                                // Nothing to do
1450
                        }
1451
                }
1452
                return false;
1453
        }
1454

    
1455
        public Object next() {
1456
                if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){
1457
                        throw new NoSuchElementException();
1458
                }
1459

    
1460
                XMLEntity child;
1461

    
1462
                if (this.lastHasNextIndex > -1) {
1463
                        if (this.entity.getChildrenCount() > this.lastHasNextIndex) {
1464
                                child = this.entity.getChild(this.lastHasNextIndex);
1465
                                try {
1466
                                        if (child.getStringProperty(key).equals(value)) {
1467
                                                this.lastIndex = this.lastHasNextIndex;
1468
                                                this.lastHasNextIndex = -1;
1469
                                                return child;
1470
                                        }
1471
                                } catch (NotExistInXMLEntity e) {
1472
                                        // Nothing to do
1473
                                }
1474
                        }
1475
                }
1476

    
1477

    
1478
                int num = this.entity.getChildrenCount();
1479

    
1480
                for (int i=this.lastIndex+1;i < num; i++) {
1481
                        child = this.entity.getChild(i);
1482
                        try {
1483
                                if (child.getStringProperty(key).equals(value)) {
1484
                                        this.lastIndex = i;
1485
                                        this.lastHasNextIndex = -1;
1486
                                        return child;
1487
                                }
1488
                        } catch (NotExistInXMLEntity e) {
1489
                                // Nothing to do
1490
                        }
1491
                }
1492
                this.lastHasNextIndex = -1;
1493
                throw new NoSuchElementException();
1494
        }
1495
}