Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / XMLEntity.java @ 40561

History | View | Annotate | Download (33.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils;
25

    
26
import java.io.IOException;
27
import java.io.StringReader;
28
import java.io.StringWriter;
29
import java.util.Iterator;
30
import java.util.NoSuchElementException;
31
import java.util.Vector;
32

    
33
import org.exolab.castor.xml.MarshalException;
34
import org.exolab.castor.xml.Marshaller;
35
import org.exolab.castor.xml.ValidationException;
36
import org.gvsig.utils.xmlEntity.generate.Property;
37
import org.gvsig.utils.xmlEntity.generate.XmlTag;
38

    
39

    
40
/**
41
 * Adaptador de las llamadas a sus m?todos sobre los generados por castor para
42
 * generar despu?s los XML.
43
 *
44
 * @author Vicente Caballero Navarro
45
 */
46
public class XMLEntity {
47
        private Vector whiteAttrList;
48
        private XmlTag xmltag;
49

    
50
        /**
51
         * Crea un nuevo XMLEntity.
52
         *
53
         * @param tag
54
         *            DOCUMENT ME!
55
         */
56
        public XMLEntity(XmlTag tag) {
57
                xmltag = tag;
58
        }
59

    
60
        /**
61
         * Crea un nuevo XMLEntity.
62
         */
63
        public XMLEntity() {
64
                xmltag = new XmlTag();
65
        }
66

    
67
        /**
68
         * A?ade una propiedad con un String como clave y un String como valor.
69
         *
70
         * @param key
71
         *            clave.
72
         * @param value
73
         *            valor.
74
         * @param matters, if false this property will not take effect to the
75
         * result of toHashCode() method
76
         * @see toHashCode()
77
         */
78
        public void putProperty(String key, String value, boolean matters) {
79
                if ((key == null)) {
80
                        return;
81
                }
82

    
83
                Property p = new Property();
84
                p.setKey(key);
85
                p.setValue(value);
86
                putProperty(p, matters);
87
        }
88

    
89
        public void putProperty(String key, String value) {
90
                putProperty(key, value, true);
91
        }
92
        /**
93
         * A?ade una propiedad con un String como clave y un Object como valor.
94
         *
95
         * @param key
96
         *            clave.
97
         * @param value
98
         *            valor.
99
         * @param matters, if false this property will not take effect to the
100
         * result of toHashCode() method
101
         * @see toHashCode()
102
         */
103
        public void putProperty(String key, Object value, boolean matters) {
104
                String ret = "";
105

    
106
                if (key == null) {
107
                        return;
108
                }
109

    
110
                Class valueClass = value.getClass();
111

    
112
                if (valueClass.isArray()) {
113
                        Class compType = valueClass.getComponentType();
114

    
115
                        if (compType == byte.class) {
116
                                byte[] array = (byte[]) value;
117

    
118
                                if (!(array.length == 0)) {
119
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
120

    
121
                                        for (int i = 1; i < array.length; i++) {
122
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
123
                                                                "\\\\,"));
124
                                        }
125
                                }
126
                        } else if (compType == short.class) {
127
                                short[] array = (short[]) value;
128

    
129
                                if (!(array.length == 0)) {
130
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
131

    
132
                                        for (int i = 1; i < array.length; i++) {
133
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
134
                                                                "\\\\,"));
135
                                        }
136
                                }
137
                        } else if (compType == int.class) {
138
                                int[] array = (int[]) value;
139

    
140
                                if (!(array.length == 0)) {
141
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
142

    
143
                                        for (int i = 1; i < array.length; i++) {
144
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
145
                                                                "\\\\,"));
146
                                        }
147
                                }
148
                        } else if (compType == long.class) {
149
                                long[] array = (long[]) value;
150

    
151
                                if (!(array.length == 0)) {
152
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
153

    
154
                                        for (int i = 1; i < array.length; i++) {
155
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
156
                                                                "\\\\,"));
157
                                        }
158
                                }
159
                        } else if (compType == float.class) {
160
                                float[] array = (float[]) value;
161

    
162
                                if (!(array.length == 0)) {
163
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
164

    
165
                                        for (int i = 1; i < array.length; i++) {
166
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
167
                                                                "\\\\,"));
168
                                        }
169
                                }
170
                        } else if (compType == double.class) {
171
                                double[] array = (double[]) value;
172

    
173
                                if (!(array.length == 0)) {
174
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
175

    
176
                                        for (int i = 1; i < array.length; i++) {
177
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
178
                                                                "\\\\,"));
179
                                        }
180
                                }
181
                        } else if (compType == boolean.class) {
182
                                boolean[] array = (boolean[]) value;
183

    
184
                                if (!(array.length == 0)) {
185
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
186

    
187
                                        for (int i = 1; i < array.length; i++) {
188
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
189
                                                                "\\\\,"));
190
                                        }
191
                                }
192
                        } else if (compType == String.class) {
193
                                String[] array = (String[]) value;
194

    
195
                                if (!(array.length == 0)) {
196
                                        if (array[0] == null) {
197
                                                array[0] = "null";
198
                                        }
199

    
200
                                        ret += array[0].replaceAll("[,]", "\\\\,");
201

    
202
                                        for (int i = 1; i < array.length; i++) {
203
                                                if (array[i] == null) {
204
                                                        array[i] = "null";
205
                                                }
206

    
207
                                                array[i] = array[i].replaceAll("[,]", "\\\\,");
208
                                                ret += (" ," + array[i]);
209
                                        }
210
                                }
211
                        } else {
212
                                Object[] array = (Object[]) value;
213

    
214
                                if (!(array.length == 0)) {
215
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
216

    
217
                                        for (int i = 1; i < array.length; i++) {
218
                                                array[i] = ("" + array[i]).replaceAll("[,]", "\\\\,");
219
                                                ret += (" ," + array[i]);
220
                                        }
221
                                }
222
                        }
223
                } else {
224
                        ret = value.toString();
225
                }
226

    
227
                Property p = new Property();
228
                p.setKey(key);
229
                p.setValue(ret);
230
                putProperty(p, matters);
231
        }
232

    
233
        /**
234
         * A?ade una propiedad con un String como clave y un double como valor.
235
         * La propiedad a?adida afecta al valor calculado por toHashCode()
236
         *
237
         * @param key
238
         *            clave.
239
         * @param value
240
         *            valor.
241
         * @see toHashCode()
242
         */
243
        public void putProperty(String key, Object value) {
244
                putProperty(key, value, true);
245
        }
246

    
247
        /**
248
         * A?ade una propiedad con un String como clave y un entero como valor.
249
         *
250
         * @param key
251
         *            clave.
252
         * @param value
253
         *            valor.
254
         * @param matters, if false this property will not take effect to the
255
         * result of toHashCode() method
256
         * @see toHashCode()
257
         */
258
        public void putProperty(String key, int value, boolean matters) {
259
                if (key == null) {
260
                        return;
261
                }
262

    
263
                Property p = new Property();
264
                p.setKey(key);
265
                p.setValue(new Integer(value).toString());
266
                putProperty(p, matters);
267
        }
268

    
269
        /**
270
         * A?ade una propiedad con un String como clave y un double como valor.
271
         * La propiedad a?adida afecta al valor calculado por toHashCode()
272
         *
273
         * @param key
274
         *            clave.
275
         * @param value
276
         *            valor.
277
         * @see toHashCode()
278
         */
279
        public void putProperty(String key, int value) {
280
                putProperty(key, value, true);
281
        }
282

    
283
        /**
284
         * A?ade una propiedad con un String como clave y un long como valor.
285
         *
286
         * @param key
287
         *            clave.
288
         * @param value
289
         *            valor.
290
         * @param matters, if false this property will not take effect to the
291
         * result of toHashCode() method
292
         * @see toHashCode()
293
         */
294
        public void putProperty(String key, long value, boolean matters) {
295
                if (key == null) {
296
                        return;
297
                }
298

    
299
                Property p = new Property();
300
                p.setKey(key);
301
                p.setValue(new Long(value).toString());
302
                putProperty(p, matters);
303
        }
304

    
305
        /**
306
         * A?ade una propiedad con un String como clave y un double como valor.
307
         * La propiedad a?adida afecta al valor calculado por toHashCode()
308
         *
309
         * @param key
310
         *            clave.
311
         * @param value
312
         *            valor.
313
         * @see toHashCode()
314
         */
315
        public void putProperty(String key, long value) {
316
                putProperty(key, value, true);
317

    
318
        }
319

    
320
        /**
321
         * A?ade una propiedad con un String como clave y un boolean como valor.
322
         *
323
         * @param key
324
         *            clave.
325
         * @param value
326
         *            valor.
327
         * @param matters, if false this property will not take effect to the
328
         * result of toHashCode() method
329
         * @see toHashCode()
330
         */
331
        public void putProperty(String key, boolean value, boolean matters) {
332
                if (key == null) {
333
                        return;
334
                }
335

    
336
                Property p = new Property();
337
                p.setKey(key);
338
                p.setValue(new Boolean(value).toString());
339
                putProperty(p, matters);
340
        }
341

    
342
        /**
343
         * A?ade una propiedad con un String como clave y un double como valor.
344
         * La propiedad a?adida afecta al valor calculado por toHashCode()
345
         *
346
         * @param key
347
         *            clave.
348
         * @param value
349
         *            valor.
350
         * @see toHashCode()
351
         */
352
        public void putProperty(String key, boolean value) {
353
                putProperty(key, value, true);
354
        }
355

    
356

    
357
        /**
358
         * A?ade una propiedad con un String como clave y un float como valor.
359
         *
360
         * @param key
361
         *            clave.
362
         * @param value
363
         *            valor.
364
         * @param matters, if false this property will not take effect to the
365
         * result of toHashCode() method
366
         * @see toHashCode()
367
         */
368
        public void putProperty(String key, float value, boolean matters) {
369
                if (key == null) {
370
                        return;
371
                }
372

    
373
                Property p = new Property();
374
                p.setKey(key);
375
                p.setValue(new Float(value).toString());
376
                putProperty(p, matters);
377
        }
378

    
379
        /**
380
         * A?ade una propiedad con un String como clave y un double como valor.
381
         * La propiedad a?adida afecta al valor calculado por toHashCode()
382
         *
383
         * @param key
384
         *            clave.
385
         * @param value
386
         *            valor.
387
         * @see toHashCode()
388
         */
389
        public void putProperty(String key, float value) {
390
                putProperty(key, value, true);
391
        }
392

    
393
        /**
394
         * A?ade una propiedad con un String como clave y un double como valor.
395
         *
396
         * @param key
397
         *            clave.
398
         * @param value
399
         *            valor.
400
         * @param matters, if false this property will not take effect to the
401
         * result of toHashCode() method
402
         * @see toHashCode()
403
         */
404
        public void putProperty(String key, double value, boolean matters) {
405
                if (key == null) {
406
                        return;
407
                }
408

    
409
                Property p = new Property();
410
                p.setKey(key);
411
                p.setValue(new Double(value).toString());
412
                putProperty(p, matters);
413
        }
414

    
415
        /**
416
         * A?ade una propiedad con un String como clave y un double como valor.
417
         * La propiedad a?adida afecta al valor calculado por toHashCode()
418
         *
419
         * @param key
420
         *            clave.
421
         * @param value
422
         *            valor.
423
         * @see toHashCode()
424
         */
425
        public void putProperty(String key, double value) {
426
                putProperty(key, value, true);
427
        }
428
        /**
429
         * Devuelve el String que corresponda a la clave que se pasa como par?metro.
430
         *
431
         * @param key
432
         *            clave
433
         *
434
         * @return valor.
435
         *
436
         * @throws NotExistInXMLEntity
437
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
438
         *             esa clave.
439
         */
440
        public String getStringProperty(String key) {
441
                Property[] properties = xmltag.getProperty();
442
                String res = null;
443
                boolean exists = false;
444

    
445
                for (int i = 0; i < properties.length; i++) {
446
                        if (properties[i].getKey().compareTo(key) == 0) {
447
                                res = properties[i].getValue();
448
                                exists = true;
449
                        }
450
                }
451

    
452
                if (exists) {
453
                        return res;
454
                }
455

    
456
                throw new NotExistInXMLEntity();
457
        }
458

    
459
        /**
460
         * Devuelve el double que corresponda a la clave que se pasa como par?metro.
461
         *
462
         * @param key
463
         *            clave
464
         *
465
         * @return valor.
466
         *
467
         * @throws NotExistInXMLEntity
468
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
469
         *             esa clave.
470
         */
471
        public double getDoubleProperty(String key) {
472
                Property[] properties = xmltag.getProperty();
473
                double res = 0;
474
                boolean exists = false;
475

    
476
                for (int i = 0; i < properties.length; i++) {
477
                        if (properties[i].getKey().compareTo(key) == 0) {
478
                                res = Double.parseDouble(properties[i].getValue());
479
                                exists = true;
480
                        }
481
                }
482

    
483
                if (exists) {
484
                        return res;
485
                }
486

    
487
                throw new NotExistInXMLEntity();
488
        }
489

    
490
        /**
491
         * Devuelve el array de doubles que corresponda a la clave que se pasa como
492
         * par?metro.
493
         *
494
         * @param key
495
         *            clave
496
         *
497
         * @return valor.
498
         *
499
         * @throws NotExistInXMLEntity
500
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
501
         *             esa clave.
502
         */
503
        public double[] getDoubleArrayProperty(String key) {
504
                Property[] properties = xmltag.getProperty();
505
                String value = null;
506
                boolean exists = false;
507

    
508
                for (int i = 0; i < properties.length; i++) {
509
                        if (properties[i].getKey().compareTo(key) == 0) {
510
                                value = properties[i].getValue();
511
                                exists = true;
512
                        }
513
                }
514

    
515
                if (!exists) {
516
                        throw new NotExistInXMLEntity();
517
                }
518

    
519
                if (value.compareTo("") == 0) {
520
                        return new double[0];
521
                }
522

    
523
                String[] aux = (String[]) value.split(" ,");
524
                double[] ret = new double[aux.length];
525

    
526
                for (int i = 0; i < aux.length; i++) {
527
                        ret[i] = Double.parseDouble(aux[i].replaceAll("\\\\,", ","));
528
                }
529

    
530
                return ret;
531
        }
532

    
533
        /**
534
         * Devuelve el Object que corresponda a la clave que se pasa como par?metro.
535
         *
536
         * @param key
537
         *            clave
538
         *
539
         * @return valor.
540
         *
541
         * @throws NotExistInXMLEntity
542
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
543
         *             esa clave.
544
         */
545
        public Object getObjectProperty(String key) {
546
                Property[] properties = xmltag.getProperty();
547
                Object res = null;
548
                boolean exists = false;
549

    
550
                for (int i = 0; i < properties.length; i++) {
551
                        if (properties[i].getKey().compareTo(key) == 0) {
552
                                res = properties[i].getValue();
553
                                exists = true;
554
                        }
555
                }
556

    
557
                if (exists) {
558
                        return res;
559
                }
560

    
561
                throw new NotExistInXMLEntity();
562
        }
563

    
564
        /**
565
         * Devuelve el array de float que corresponda a la clave que se pasa como
566
         * par?metro.
567
         *
568
         * @param key
569
         *            clave
570
         *
571
         * @return valor.
572
         *
573
         * @throws NotExistInXMLEntity
574
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
575
         *             esa clave.
576
         */
577
        public float[] getFloatArrayProperty(String key) {
578
                Property[] properties = xmltag.getProperty();
579
                String value = null;
580
                boolean exists = false;
581

    
582
                for (int i = 0; i < properties.length; i++) {
583
                        if (properties[i].getKey().compareTo(key) == 0) {
584
                                value = properties[i].getValue();
585
                                exists = true;
586
                        }
587
                }
588

    
589
                if (!exists) {
590
                        throw new NotExistInXMLEntity();
591
                }
592

    
593
                if (value.compareTo("") == 0) {
594
                        return new float[0];
595
                }
596

    
597
                String[] aux = (String[]) value.split(" ,");
598
                float[] ret = new float[aux.length];
599

    
600
                for (int i = 0; i < aux.length; i++) {
601
                        ret[i] = Float.parseFloat(aux[i].replaceAll("\\\\,", ","));
602
                }
603

    
604
                return ret;
605
        }
606

    
607
        /**
608
         * Devuelve el array de long que corresponda a la clave que se pasa como
609
         * par?metro.
610
         *
611
         * @param key
612
         *            clave
613
         *
614
         * @return valor.
615
         *
616
         * @throws NotExistInXMLEntity
617
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
618
         *             esa clave.
619
         */
620
        public long[] getLongArrayProperty(String key) {
621
                Property[] properties = xmltag.getProperty();
622
                String value = null;
623
                boolean exists = false;
624

    
625
                for (int i = 0; i < properties.length; i++) {
626
                        if (properties[i].getKey().compareTo(key) == 0) {
627
                                value = properties[i].getValue();
628
                                exists = true;
629
                        }
630
                }
631

    
632
                if (!exists) {
633
                        throw new NotExistInXMLEntity();
634
                }
635

    
636
                if (value.compareTo("") == 0) {
637
                        return new long[0];
638
                }
639

    
640
                String[] aux = (String[]) value.split(" ,");
641
                long[] ret = new long[aux.length];
642

    
643
                for (int i = 0; i < aux.length; i++) {
644
                        ret[i] = Long.parseLong(aux[i].replaceAll("\\\\,", ","));
645
                }
646

    
647
                return ret;
648
        }
649

    
650
        /**
651
         * Devuelve el array de bytes que corresponda a la clave que se pasa como
652
         * par?metro.
653
         *
654
         * @param key
655
         *            clave
656
         *
657
         * @return valor.
658
         *
659
         * @throws NotExistInXMLEntity
660
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
661
         *             esa clave.
662
         */
663
        public byte[] getByteArrayProperty(String key) {
664
                Property[] properties = xmltag.getProperty();
665
                String value = null;
666
                boolean exists = false;
667

    
668
                for (int i = 0; i < properties.length; i++) {
669
                        if (properties[i].getKey().compareTo(key) == 0) {
670
                                value = properties[i].getValue();
671
                                exists = true;
672
                        }
673
                }
674

    
675
                if (!exists) {
676
                        throw new NotExistInXMLEntity();
677
                }
678

    
679
                if (value.compareTo("") == 0) {
680
                        return new byte[0];
681
                }
682

    
683
                String[] aux = (String[]) value.split(" ,");
684
                byte[] ret = new byte[aux.length];
685

    
686
                for (int i = 0; i < aux.length; i++) {
687
                        ret[i] = Byte.parseByte(aux[i].replaceAll("\\\\,", ","));
688
                }
689

    
690
                return ret;
691
        }
692

    
693
        /**
694
         * Devuelve el array de enteros que corresponda a la clave que se pasa como
695
         * par?metro.
696
         *
697
         * @param key
698
         *            clave
699
         *
700
         * @return valor.
701
         *
702
         * @throws NotExistInXMLEntity
703
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
704
         *             esa clave.
705
         */
706
        public int[] getIntArrayProperty(String key) {
707
                Property[] properties = xmltag.getProperty();
708
                String value = null;
709
                boolean exists = false;
710

    
711
                for (int i = 0; i < properties.length; i++) {
712
                        if (properties[i].getKey().compareTo(key) == 0) {
713
                                value = properties[i].getValue();
714
                                exists = true;
715
                        }
716
                }
717

    
718
                if (!exists) {
719
                        throw new NotExistInXMLEntity();
720
                }
721

    
722
                if (value.compareTo("") == 0) {
723
                        return new int[0];
724
                }
725

    
726
                String[] aux = (String[]) value.split(" ,");
727
                int[] ret = new int[aux.length];
728

    
729
                for (int i = 0; i < aux.length; i++) {
730
                        ret[i] = Integer.parseInt(aux[i].replaceAll("\\\\,", ","));
731
                }
732

    
733
                return ret;
734
        }
735

    
736
        /**
737
         * Devuelve el array de boolean que corresponda a la clave que se pasa como
738
         * par?metro.
739
         *
740
         * @param key
741
         *            clave
742
         *
743
         * @return valor.
744
         *
745
         * @throws NotExistInXMLEntity
746
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
747
         *             esa clave.
748
         */
749
        public boolean[] getBooleanArrayProperty(String key) {
750
                Property[] properties = xmltag.getProperty();
751
                String value = null;
752
                boolean exists = false;
753

    
754
                for (int i = 0; i < properties.length; i++) {
755
                        if (properties[i].getKey().compareTo(key) == 0) {
756
                                value = properties[i].getValue();
757
                                exists = true;
758
                        }
759
                }
760

    
761
                if (!exists) {
762
                        throw new NotExistInXMLEntity();
763
                }
764

    
765
                if (value.compareTo("") == 0) {
766
                        return new boolean[0];
767
                }
768

    
769
                String[] aux = (String[]) value.split(" ,");
770
                boolean[] ret = new boolean[aux.length];
771

    
772
                for (int i = 0; i < aux.length; i++) {
773
                        ret[i] = Boolean.valueOf(aux[i].replaceAll("\\\\,", ","))
774
                                        .booleanValue();
775
                }
776

    
777
                return ret;
778
        }
779

    
780
        /**
781
         * Devuelve el array de String que corresponda a la clave que se pasa como
782
         * par?metro.
783
         *
784
         * @param key
785
         *            clave
786
         *
787
         * @return valor.
788
         *
789
         * @throws NotExistInXMLEntity
790
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
791
         *             esa clave.
792
         */
793
        public String[] getStringArrayProperty(String key) {
794
                // /String value = (String) properties.get(key);
795
                Property[] properties = xmltag.getProperty();
796
                String value = null;
797
                boolean exists = false;
798

    
799
                for (int i = 0; i < properties.length; i++) {
800
                        if (properties[i].getKey().compareTo(key) == 0) {
801
                                value = properties[i].getValue();
802
                                exists = true;
803
                        }
804
                }
805

    
806
                if (!exists) {
807
                        throw new NotExistInXMLEntity();
808
                }
809

    
810
                if (value.compareTo("") == 0) {
811
                        return new String[0];
812
                }
813

    
814
                String[] aux = (String[]) value.split(" ,");
815

    
816
                for (int i = 0; i < aux.length; i++) {
817
                        aux[i] = aux[i].replaceAll("\\\\,", ",");
818
                }
819

    
820
                return aux;
821
        }
822

    
823
        /**
824
         * Devuelve el boolean que corresponda a la clave que se pasa como
825
         * par?metro.
826
         *
827
         * @param key
828
         *            clave
829
         *
830
         * @return valor.
831
         *
832
         * @throws NotExistInXMLEntity
833
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
834
         *             esa clave.
835
         */
836
        public boolean getBooleanProperty(String key) {
837
                Property[] properties = xmltag.getProperty();
838
                boolean res = false;
839
                boolean exists = false;
840

    
841
                for (int i = 0; i < properties.length; i++) {
842
                        if (properties[i].getKey().compareTo(key) == 0) {
843
                                res = (boolean) Boolean.valueOf(properties[i].getValue())
844
                                                .booleanValue();
845
                                exists = true;
846
                        }
847
                }
848

    
849
                if (exists) {
850
                        return res;
851
                }
852

    
853
                throw new NotExistInXMLEntity();
854
        }
855

    
856
        /**
857
         * Devuelve el entero que corresponda a la clave que se pasa como par?metro.
858
         *
859
         * @param key
860
         *            clave
861
         *
862
         * @return valor.
863
         *
864
         * @throws NotExistInXMLEntity
865
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
866
         *             esa clave.
867
         */
868
        public int getIntProperty(String key) {
869
                Property[] properties = xmltag.getProperty();
870
                int res = 0;
871
                boolean exists = false;
872

    
873
                for (int i = 0; i < properties.length; i++) {
874
                        if (properties[i].getKey().compareTo(key) == 0) {
875
                                res = Integer.parseInt(properties[i].getValue());
876
                                exists = true;
877
                        }
878
                }
879

    
880
                if (exists) {
881
                        return res;
882
                }
883

    
884
                throw new NotExistInXMLEntity();
885
        }
886

    
887
        /**
888
         * Devuelve el long que corresponda a la clave que se pasa como par?metro.
889
         *
890
         * @param key
891
         *            clave
892
         *
893
         * @return valor.
894
         *
895
         * @throws NotExistInXMLEntity
896
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
897
         *             esa clave.
898
         */
899
        public long getLongProperty(String key) {
900
                Property[] properties = xmltag.getProperty();
901
                long res = 0;
902
                boolean exists = false;
903

    
904
                for (int i = 0; i < properties.length; i++) {
905
                        if (properties[i].getKey().compareTo(key) == 0) {
906
                                res = Long.valueOf(properties[i].getValue()).longValue();
907
                                exists = true;
908
                        }
909
                }
910

    
911
                if (exists) {
912
                        return res;
913
                }
914

    
915
                throw new NotExistInXMLEntity();
916
        }
917

    
918
        /**
919
         * Devuelve el float que corresponda a la clave que se pasa como par?metro.
920
         *
921
         * @param key
922
         *            clave
923
         *
924
         * @return valor.
925
         *
926
         * @throws NotExistInXMLEntity
927
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
928
         *             esa clave.
929
         */
930
        public float getFloatProperty(String key) {
931
                Property[] properties = xmltag.getProperty();
932
                float res = 0;
933
                boolean exists = false;
934

    
935
                for (int i = 0; i < properties.length; i++) {
936
                        if (properties[i].getKey().compareTo(key) == 0) {
937
                                res = Float.valueOf(properties[i].getValue()).floatValue();
938
                                exists = true;
939
                        }
940
                }
941

    
942
                if (exists) {
943
                        return res;
944
                }
945

    
946
                throw new NotExistInXMLEntity();
947
        }
948

    
949
        /**
950
         * A?ade el nombre con clave name y valor el String que se pasa como
951
         * par?metro.
952
         *
953
         * @param name
954
         *            nombre.
955
         */
956
        public void setName(String name) {
957
                Property p = new Property();
958
                p.setKey("name");
959
                p.setValue(name);
960
                xmltag.addProperty(p);
961
        }
962

    
963
        /**
964
         * Devuelve el nombre.
965
         *
966
         * @return nombre.
967
         */
968
        public String getName() {
969
                Property p = getProperty("name");
970

    
971
                return (String) p.getValue();
972
        }
973

    
974
        /**
975
         * Devuelve la clase que implementa.
976
         *
977
         * @return clase.
978
         */
979
        public Class getImplementingClass() {
980
                Property p = getProperty("class");
981
                Object ob = p.getValue();
982

    
983
                return (Class) ob;
984
        }
985

    
986
        /**
987
         * A?ade la clase que implementa
988
         *
989
         * @param c
990
         */
991
        /*
992
         * public void setImplementingClass(Class c) { Property p = new Property();
993
         * p.setKey("class"); p.setValue(c.toString()); xmltag.addProperty(p); }
994
         */
995

    
996
        /**
997
         * A?ade un hijo al XMLEntity.
998
         *
999
         * @param entity
1000
         *            xml para a?adir.
1001
         */
1002
        public void addChild(XMLEntity entity) {
1003
                xmltag.addXmlTag(entity.getXmlTag());
1004
        }
1005

    
1006
        /**
1007
         * Devuelve un hijo a partir de un indice.
1008
         *
1009
         * @param i
1010
         *            indice.
1011
         *
1012
         * @return hijo.
1013
         */
1014
        public XMLEntity getChild(int i) {
1015
                return new XMLEntity(xmltag.getXmlTag(i));
1016
        }
1017

    
1018
        /**
1019
         * Devuelve el n?mero de hijos que contiene el XMLEntity.
1020
         *
1021
         * @return n?mero de hijos.
1022
         */
1023
        public int getChildrenCount() {
1024
                return xmltag.getXmlTagCount();
1025
        }
1026

    
1027
        /**
1028
         * Devuelve el Property a partir de la clave.
1029
         *
1030
         * @param key
1031
         *            clave.
1032
         *
1033
         * @return Property.
1034
         */
1035
        private Property getProperty(String key) {
1036
                Property[] ps = xmltag.getProperty();
1037

    
1038
                for (int i = 0; i < ps.length; i++) {
1039
                        if (ps[i].getKey().compareTo(key) == 0) {
1040
                                return ps[i];
1041
                        }
1042
                }
1043

    
1044
                return null;
1045
        }
1046

    
1047
        /**
1048
         * Devuelve el xmltag.
1049
         *
1050
         * @return xmltag.
1051
         */
1052
        public XmlTag getXmlTag() {
1053
                return xmltag;
1054
        }
1055

    
1056
        /**
1057
         * A?ade el property que se pasa como par?metro.
1058
         *
1059
         * @param p
1060
         *            property.
1061
         */
1062
        private void putProperty(Property p, boolean matters) {
1063
                if (!matters) {
1064
                        getWhiteAttrList().add(p.getKey());
1065
                }
1066
                for (int i = 0; i < xmltag.getPropertyCount(); i++) {
1067
                        if (xmltag.getProperty(i).getKey().compareTo(p.getKey()) == 0) {
1068
                                xmltag.getProperty(i).setValue(p.getValue());
1069

    
1070
                                return;
1071
                        }
1072
                }
1073

    
1074
                xmltag.addProperty(p);
1075
        }
1076

    
1077
        private Vector getWhiteAttrList() {
1078
                if (whiteAttrList == null) {
1079
                        whiteAttrList = new Vector();
1080

    
1081
                }
1082

    
1083
                return whiteAttrList;
1084
        }
1085

    
1086
        public boolean contains(String key) {
1087
                Property[] properties = xmltag.getProperty();
1088
                boolean exists = false;
1089

    
1090
                for (int i = 0; i < properties.length; i++) {
1091
                        if (properties[i].getKey().compareTo(key) == 0) {
1092
                                exists = true;
1093
                        }
1094
                }
1095
                return exists;
1096
        }
1097

    
1098
        public int getPropertyCount() {
1099
                return xmltag.getPropertyCount();
1100
        }
1101

    
1102
        public String getPropertyValue(int index) {
1103
                return xmltag.getProperty(index).getValue();
1104
        }
1105

    
1106
        public String getPropertyName(int index) {
1107
                return xmltag.getProperty(index).getKey();
1108
        }
1109

    
1110
        /**
1111
         * Removes a property of this XML-Entity.
1112
         *
1113
         * @param p
1114
         *            property.
1115
         */
1116
        public void remove(String key) {
1117
                Property[] properties = xmltag.getProperty();
1118

    
1119
                for (int i = 0; i < properties.length; i++) {
1120
                        if (properties[i].getKey().compareTo(key) == 0) {
1121
                                Property[] newProperties = new Property[properties.length - 1];
1122
                                int k = 0;
1123
                                for (int j = 0; j < newProperties.length; j++) {
1124
                                        if (j == i)
1125
                                                k = 1;
1126
                                        newProperties[j] = properties[j + k];
1127
                                }
1128
                                xmltag.setProperty(newProperties);
1129
                                return;
1130
                        }
1131
                }
1132
        }
1133

    
1134
        /**
1135
         * Elimina el hijo n del XMLEntity.
1136
         *
1137
         * @param indice
1138
         *            del hijo a eliminar.
1139
         */
1140
        public void removeChild(int index) {
1141
                xmltag.removeXmlTag(index);
1142
        }
1143

    
1144
        /**
1145
         * Elimina todos los hijos de XMLEntity.
1146
         *
1147
         */
1148
        public void removeAllChildren() {
1149
                xmltag.removeAllXmlTag();
1150
        }
1151

    
1152
        // Jaume (en proves)
1153
        public boolean equals(Object obj) {
1154
                if (obj instanceof XMLEntity) {
1155
                        XMLEntity other = (XMLEntity) obj;
1156

    
1157
                        Property[] thisProperties;
1158
                        Property[] otherProperties;
1159

    
1160
                        thisProperties = xmltag.getProperty();
1161
                        otherProperties = other.xmltag.getProperty();
1162

    
1163
                        if (thisProperties.length != otherProperties.length)
1164
                                return false;
1165

    
1166
                        for (int i = 0; i < thisProperties.length; i++) {
1167
                                if (!thisProperties[i].getKey().equals(
1168
                                                otherProperties[i].getKey()))
1169
                                        return false;
1170
                                if (!thisProperties[i].getValue().equals(
1171
                                                otherProperties[i].getValue()))
1172
                                        return false;
1173
                        }
1174

    
1175
                        if (this.getChildrenCount() != other.getChildrenCount())
1176
                                return false;
1177

    
1178
                        for (int i = 0; i < this.getChildrenCount(); i++) {
1179
                                if (!this.getChild(i).equals(other.getChild(i)))
1180
                                        return false;
1181
                        }
1182

    
1183
                        return true;
1184
                }
1185

    
1186
                return false;
1187
        }
1188

    
1189
        // jaume (en proves)
1190
        /**
1191
         * <p>
1192
         * <code>hash()</code> method is used to know if this <b>XMLEntity</b>
1193
         * can be considered as changed. At the loading of the <b>XMLEntity</b> tree
1194
         * you can invoke this method. The result of it is a <code>int</code> hash
1195
         * value calculated from the contents of this and its children. Then, if you
1196
         * store this value you can recognize if the <b>XMLEntity</b> has changed
1197
         * since the last time you called <code>hash()</code> just by comparing
1198
         * the previous calculated value to the new calculated one.<br>
1199
         * </p>
1200
         * <p>
1201
         * Doing so, you can automatically detect if your persistent data has changed
1202
         * and you are required to save it.<br>
1203
         * </p>
1204
         * <p>
1205
         * <b>Notice</b> that you can mark the properties that you are using during
1206
         * the session but you don't care about the values between sessions as
1207
         * properties which don't matter to detect if your XMLEntity has changed.<br>
1208
         * </p>
1209
         *
1210
         */
1211
        public long hash() {
1212
                long result = 17;
1213

    
1214
                if (xmltag.getName()!=null) {
1215
                        char[] name = xmltag.getName().toCharArray();
1216

    
1217
                        for (int i = 0; i<name.length; i++) {
1218
                                result = 37 + result + (int) name[i];
1219
                        }
1220
                }
1221

    
1222
                Property[] properties = xmltag.getProperty();
1223
                for (int i = 0; i < properties.length; i++) {
1224
                        String strKey = properties[i].getKey();
1225
                        // if this key was put with the matter value set to false
1226
                        // then it will not take effect in the hash calculation
1227
                        if (whiteAttrList!=null && whiteAttrList.contains(strKey))
1228
                                continue;
1229

    
1230
                        char[] key = strKey.toCharArray();
1231
                        for (int j = 0; j < key.length; j++) {
1232
                                result = 37 + result + (int) key[j];
1233
                        }
1234

    
1235
                        if (properties[i]!=null) {
1236
                                String value = properties[i].getValue();
1237
                                char[] chars = (value!=null)? value.toCharArray() : new char[0];
1238
                                for (int j = 0; j < chars.length; j++) {
1239
                                        result = 37 + result + (int) chars[j];
1240
                                }
1241
                        } else {
1242
                                result += 37;
1243
                        }
1244
                }
1245

    
1246
                for (int i = 0; i < this.getChildrenCount(); i++) {
1247
                        result = 37 + result + this.getChild(i).hash();
1248
                }
1249
                return result;
1250
        }
1251

    
1252
        /**
1253
         * Devuelve el primer hijo que el valor de su propieda 'key'
1254
         * es igual a 'value'
1255
         *
1256
         * @param key propiedad a comparar
1257
         * @param value valor a comparar
1258
         * @return XMLEntity hijo o null si no se encuentra
1259
         */
1260
        public XMLEntity firstChild(String key, String value) {
1261
                int num = this.getChildrenCount();
1262
                XMLEntity child;
1263
                for (int i=0;i < num; i++) {
1264
                        child = this.getChild(i);
1265
                        try {
1266
                                if (child.getStringProperty(key).equals(value)) {
1267
                                        return child;
1268
                                }
1269
                        } catch (NotExistInXMLEntity e) {
1270
                                // Nothing to do
1271
                        }
1272
                }
1273
                return null;
1274
        }
1275

    
1276
        /**
1277
         * Devuelve el primer hijo cuyo nombre es igual a 'value'.
1278
         * El nombre de un XMLEntity viene determinado por el valor del
1279
         * atributo 'name' del xml-tag.
1280
         *
1281
         * @param value valor a comparar
1282
         * @return XMLEntity hijo o null si no se encuentra
1283
         */
1284
        public XMLEntity firstChild(String value) {
1285
                int num = this.getChildrenCount();
1286
                XMLEntity child;
1287
                for (int i=0;i < num; i++) {
1288
                        child = this.getChild(i);
1289
                        try {
1290
                                if (child.getXmlTag().getName().equals(value)) {
1291
                                        return child;
1292
                                }
1293
                        } catch (NotExistInXMLEntity e) {
1294
                                // Nothing to do
1295
                        }
1296
                }
1297
                return null;
1298
        }
1299

    
1300
        /**
1301
         * Devuelve el indice del primer hijo que el valor de su propieda 'key'
1302
         * es igual a 'value'
1303
         *
1304
         * @param key propiedad a comparar
1305
         * @param value valor a comparar
1306
         * @return int indice del hijo o -1 si no se encuentra
1307
         */
1308
        public int firstIndexOfChild(String key, String value) {
1309
                int num = this.getChildrenCount();
1310
                XMLEntity child;
1311
                for (int i=0;i < num; i++) {
1312
                        try {
1313
                                child = this.getChild(i);
1314
                                if (child.getStringProperty(key).equals(value)) {
1315
                                        return i;
1316
                                }
1317
                        } catch (NotExistInXMLEntity e) {
1318
                                // Nothing to do
1319
                        }
1320
                }
1321
                return -1;
1322
        }
1323

    
1324
        /**
1325
         * Devuelve el indice del primer hijo cuyo nombre es igual a 'value'.
1326
         * El nombre de un XMLEntity viene determinado por el valor del
1327
         * atributo 'name' del xml-tag.
1328
         *
1329
         * @param value valor a comparar
1330
         * @return int indice del hijo o -1 si no se encuentra
1331
         */
1332
        public int firstIndexOfChild(String value) {
1333
                int num = this.getChildrenCount();
1334
                XMLEntity child;
1335
                for (int i=0;i < num; i++) {
1336
                        try {
1337
                                child = this.getChild(i);
1338
                                if (child.getXmlTag().getName().equals(value)) {
1339
                                        return i;
1340
                                }
1341
                        } catch (NotExistInXMLEntity e) {
1342
                                // Nothing to do
1343
                        }
1344
                }
1345
                return -1;
1346
        }
1347

    
1348
        /**
1349
         * Devuelve un iterador sobre los hijos que cumplen la condicion
1350
         * que el valor de su propiedad 'key' es igual a 'value'
1351
         *
1352
         * El iterador no permite eliminacion
1353
         *
1354
         * @param key nombre de la propidedad
1355
         * @param value valor de la propiedad
1356
         * @return
1357
         */
1358
        public Iterator findChildren(String key, String value) {
1359
                return new XMLEntityIterator(this,key,value);
1360
        }
1361

    
1362
        public String toString() {
1363
                StringWriter buffer = new StringWriter();
1364

    
1365
                Marshaller m;
1366
                try {
1367
                        m = new Marshaller(buffer);
1368
                } catch (IOException e4) {
1369
                        // TODO Auto-generated catch block
1370
                        e4.printStackTrace();
1371
                        return null;
1372
                }
1373
                m.setEncoding("ISO-8859-1");
1374

    
1375
                try {
1376
                        m.marshal(this.getXmlTag());
1377
                } catch (MarshalException e2) {
1378
                        // TODO Auto-generated catch block
1379
                        e2.printStackTrace();
1380
                        return null;
1381
                } catch (ValidationException e3) {
1382
                        // TODO Auto-generated catch block
1383
                        e3.printStackTrace();
1384
                        return null;
1385
                }
1386

    
1387
                return buffer.toString();
1388
        }
1389

    
1390
        public static XMLEntity parse(String data) throws MarshalException, ValidationException {
1391
                StringReader reader = new StringReader(data);
1392

    
1393
                XmlTag tag;
1394
                tag = (XmlTag) XmlTag.unmarshal(reader);
1395
                return new XMLEntity(tag);
1396
        }
1397

    
1398
}
1399

    
1400
class XMLEntityIterator implements Iterator {
1401
        private int lastIndex;
1402
        private int lastHasNextIndex;
1403
        private XMLEntity entity;
1404
        private String key;
1405
        private String value;
1406

    
1407
        public XMLEntityIterator(XMLEntity entity,String key,String value) {
1408
                this.entity = entity;
1409
                this.key = key;
1410
                this.value = value;
1411
                this.lastIndex = -1;
1412
                this.lastHasNextIndex = -1;
1413
        }
1414
        public void remove() {
1415
                throw new UnsupportedOperationException();
1416
        }
1417

    
1418
        public boolean hasNext() {
1419
                if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){
1420
                        return false;
1421
                }
1422
                int num = this.entity.getChildrenCount();
1423
                XMLEntity child;
1424
                for (int i=this.lastIndex+1;i < num; i++) {
1425
                        child = this.entity.getChild(i);
1426
                        try {
1427
                                if (child.getStringProperty(key).equals(value)) {
1428
                                        this.lastHasNextIndex = i;
1429
                                        return true;
1430
                                }
1431
                        } catch (NotExistInXMLEntity e) {
1432
                                // Nothing to do
1433
                        }
1434
                }
1435
                return false;
1436
        }
1437

    
1438
        public Object next() {
1439
                if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){
1440
                        throw new NoSuchElementException();
1441
                }
1442

    
1443
                XMLEntity child;
1444

    
1445
                if (this.lastHasNextIndex > -1) {
1446
                        if (this.entity.getChildrenCount() > this.lastHasNextIndex) {
1447
                                child = this.entity.getChild(this.lastHasNextIndex);
1448
                                try {
1449
                                        if (child.getStringProperty(key).equals(value)) {
1450
                                                this.lastIndex = this.lastHasNextIndex;
1451
                                                this.lastHasNextIndex = -1;
1452
                                                return child;
1453
                                        }
1454
                                } catch (NotExistInXMLEntity e) {
1455
                                        // Nothing to do
1456
                                }
1457
                        }
1458
                }
1459

    
1460

    
1461
                int num = this.entity.getChildrenCount();
1462

    
1463
                for (int i=this.lastIndex+1;i < num; i++) {
1464
                        child = this.entity.getChild(i);
1465
                        try {
1466
                                if (child.getStringProperty(key).equals(value)) {
1467
                                        this.lastIndex = i;
1468
                                        this.lastHasNextIndex = -1;
1469
                                        return child;
1470
                                }
1471
                        } catch (NotExistInXMLEntity e) {
1472
                                // Nothing to do
1473
                        }
1474
                }
1475
                this.lastHasNextIndex = -1;
1476
                throw new NoSuchElementException();
1477
        }
1478
}