Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1002 / libraries / libIverUtiles / src / com / iver / utiles / XMLEntity.java @ 12070

History | View | Annotate | Download (27.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

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

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

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

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

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

    
82
        /**
83
         * A?ade una propiedad con un String como clave y un String como valor.
84
         *
85
         * @param key
86
         *            clave.
87
         * @param value
88
         *            valor.
89
         */
90
        public void putProperty(String key, String value) {
91
                if ((key == null)) {
92
                        return;
93
                }
94

    
95
                Property p = new Property();
96
                p.setKey(key);
97
                p.setValue(value);
98
                putProperty(p);
99
        }
100

    
101
        /**
102
         * A?ade una propiedad con un String como clave y un Object como valor.
103
         *
104
         * @param key
105
         *            clave.
106
         * @param value
107
         *            valor.
108
         */
109
        public void putProperty(String key, Object value) {
110
                String ret = "";
111

    
112
                if (key == null) {
113
                        return;
114
                }
115

    
116
                Class valueClass = value.getClass();
117

    
118
                if (valueClass.isArray()) {
119
                        Class compType = valueClass.getComponentType();
120

    
121
                        if (compType == byte.class) {
122
                                byte[] array = (byte[]) value;
123

    
124
                                if (!(array.length == 0)) {
125
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
126

    
127
                                        for (int i = 1; i < array.length; i++) {
128
                                                ret += (" ," + ("" + array[i]).replaceAll("[,]",
129
                                                                "\\\\,"));
130
                                        }
131
                                }
132
                        } else if (compType == short.class) {
133
                                short[] array = (short[]) 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 == int.class) {
144
                                int[] array = (int[]) 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 == long.class) {
155
                                long[] array = (long[]) 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 == float.class) {
166
                                float[] array = (float[]) 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 == double.class) {
177
                                double[] array = (double[]) 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 == boolean.class) {
188
                                boolean[] array = (boolean[]) 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 == String.class) {
199
                                String[] array = (String[]) value;
200

    
201
                                if (!(array.length == 0)) {
202
                                        if (array[0] == null) {
203
                                                array[0] = "null";
204
                                        }
205

    
206
                                        ret += array[0].replaceAll("[,]", "\\\\,");
207

    
208
                                        for (int i = 1; i < array.length; i++) {
209
                                                if (array[i] == null) {
210
                                                        array[i] = "null";
211
                                                }
212

    
213
                                                array[i] = array[i].replaceAll("[,]", "\\\\,");
214
                                                ret += (" ," + array[i]);
215
                                        }
216
                                }
217
                        } else {
218
                                Object[] array = (Object[]) value;
219

    
220
                                if (!(array.length == 0)) {
221
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
222

    
223
                                        for (int i = 1; i < array.length; i++) {
224
                                                array[i] = ("" + array[i]).replaceAll("[,]", "\\\\,");
225
                                                ret += (" ," + array[i]);
226
                                        }
227
                                }
228
                        }
229
                } else {
230
                        ret = value.toString();
231
                }
232

    
233
                Property p = new Property();
234
                p.setKey(key);
235
                p.setValue(ret);
236
                putProperty(p);
237
        }
238

    
239
        /**
240
         * A?ade una propiedad con un String como clave y un entero como valor.
241
         *
242
         * @param key
243
         *            clave.
244
         * @param value
245
         *            valor.
246
         */
247
        public void putProperty(String key, int value) {
248
                if (key == null) {
249
                        return;
250
                }
251

    
252
                Property p = new Property();
253
                p.setKey(key);
254
                p.setValue(new Integer(value).toString());
255
                putProperty(p);
256
        }
257

    
258
        /**
259
         * A?ade una propiedad con un String como clave y un long como valor.
260
         *
261
         * @param key
262
         *            clave.
263
         * @param value
264
         *            valor.
265
         */
266
        public void putProperty(String key, long value) {
267
                if (key == null) {
268
                        return;
269
                }
270

    
271
                Property p = new Property();
272
                p.setKey(key);
273
                p.setValue(new Long(value).toString());
274
                putProperty(p);
275
        }
276

    
277
        /**
278
         * A?ade una propiedad con un String como clave y un boolean como valor.
279
         *
280
         * @param key
281
         *            clave.
282
         * @param value
283
         *            valor.
284
         */
285
        public void putProperty(String key, boolean value) {
286
                if (key == null) {
287
                        return;
288
                }
289

    
290
                Property p = new Property();
291
                p.setKey(key);
292
                p.setValue(new Boolean(value).toString());
293
                putProperty(p);
294
        }
295

    
296
        /**
297
         * A?ade una propiedad con un String como clave y un float como valor.
298
         *
299
         * @param key
300
         *            clave.
301
         * @param value
302
         *            valor.
303
         */
304
        public void putProperty(String key, float value) {
305
                if (key == null) {
306
                        return;
307
                }
308

    
309
                Property p = new Property();
310
                p.setKey(key);
311
                p.setValue(new Float(value).toString());
312
                putProperty(p);
313
        }
314

    
315
        /**
316
         * A?ade una propiedad con un String como clave y un double como valor.
317
         *
318
         * @param key
319
         *            clave.
320
         * @param value
321
         *            valor.
322
         */
323
        public void putProperty(String key, double value) {
324
                if (key == null) {
325
                        return;
326
                }
327

    
328
                Property p = new Property();
329
                p.setKey(key);
330
                p.setValue(new Double(value).toString());
331
                putProperty(p);
332
        }
333

    
334
        /**
335
         * Devuelve el String que corresponda a la clave que se pasa como par?metro.
336
         *
337
         * @param key
338
         *            clave
339
         *
340
         * @return valor.
341
         *
342
         * @throws NotExistInXMLEntity
343
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
344
         *             esa clave.
345
         */
346
        public String getStringProperty(String key) {
347
                Property[] properties = xmltag.getProperty();
348
                String res = null;
349
                boolean exists = false;
350

    
351
                for (int i = 0; i < properties.length; i++) {
352
                        if (properties[i].getKey().compareTo(key) == 0) {
353
                                res = properties[i].getValue();
354
                                exists = true;
355
                        }
356
                }
357

    
358
                if (exists) {
359
                        return res;
360
                }
361

    
362
                throw new NotExistInXMLEntity();
363
        }
364

    
365
        /**
366
         * Devuelve el double que corresponda a la clave que se pasa como par?metro.
367
         *
368
         * @param key
369
         *            clave
370
         *
371
         * @return valor.
372
         *
373
         * @throws NotExistInXMLEntity
374
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
375
         *             esa clave.
376
         */
377
        public double getDoubleProperty(String key) {
378
                Property[] properties = xmltag.getProperty();
379
                double res = 0;
380
                boolean exists = false;
381

    
382
                for (int i = 0; i < properties.length; i++) {
383
                        if (properties[i].getKey().compareTo(key) == 0) {
384
                                res = Double.parseDouble(properties[i].getValue());
385
                                exists = true;
386
                        }
387
                }
388

    
389
                if (exists) {
390
                        return res;
391
                }
392

    
393
                throw new NotExistInXMLEntity();
394
        }
395

    
396
        /**
397
         * Devuelve el array de doubles que corresponda a la clave que se pasa como
398
         * par?metro.
399
         *
400
         * @param key
401
         *            clave
402
         *
403
         * @return valor.
404
         *
405
         * @throws NotExistInXMLEntity
406
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
407
         *             esa clave.
408
         */
409
        public double[] getDoubleArrayProperty(String key) {
410
                Property[] properties = xmltag.getProperty();
411
                String value = null;
412
                boolean exists = false;
413

    
414
                for (int i = 0; i < properties.length; i++) {
415
                        if (properties[i].getKey().compareTo(key) == 0) {
416
                                value = properties[i].getValue();
417
                                exists = true;
418
                        }
419
                }
420

    
421
                if (!exists) {
422
                        throw new NotExistInXMLEntity();
423
                }
424

    
425
                if (value.compareTo("") == 0) {
426
                        return new double[0];
427
                }
428

    
429
                String[] aux = (String[]) value.split(" ,");
430
                double[] ret = new double[aux.length];
431

    
432
                for (int i = 0; i < aux.length; i++) {
433
                        ret[i] = Double.parseDouble(aux[i].replaceAll("\\\\,", ","));
434
                }
435

    
436
                return ret;
437
        }
438

    
439
        /**
440
         * Devuelve el Object que corresponda a la clave que se pasa como par?metro.
441
         *
442
         * @param key
443
         *            clave
444
         *
445
         * @return valor.
446
         *
447
         * @throws NotExistInXMLEntity
448
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
449
         *             esa clave.
450
         */
451
        public Object getObjectProperty(String key) {
452
                Property[] properties = xmltag.getProperty();
453
                Object res = null;
454
                boolean exists = false;
455

    
456
                for (int i = 0; i < properties.length; i++) {
457
                        if (properties[i].getKey().compareTo(key) == 0) {
458
                                res = properties[i].getValue();
459
                                exists = true;
460
                        }
461
                }
462

    
463
                if (exists) {
464
                        return res;
465
                }
466

    
467
                throw new NotExistInXMLEntity();
468
        }
469

    
470
        /**
471
         * Devuelve el array de float que corresponda a la clave que se pasa como
472
         * par?metro.
473
         *
474
         * @param key
475
         *            clave
476
         *
477
         * @return valor.
478
         *
479
         * @throws NotExistInXMLEntity
480
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
481
         *             esa clave.
482
         */
483
        public float[] getFloatArrayProperty(String key) {
484
                Property[] properties = xmltag.getProperty();
485
                String value = null;
486
                boolean exists = false;
487

    
488
                for (int i = 0; i < properties.length; i++) {
489
                        if (properties[i].getKey().compareTo(key) == 0) {
490
                                value = properties[i].getValue();
491
                                exists = true;
492
                        }
493
                }
494

    
495
                if (!exists) {
496
                        throw new NotExistInXMLEntity();
497
                }
498

    
499
                if (value.compareTo("") == 0) {
500
                        return new float[0];
501
                }
502

    
503
                String[] aux = (String[]) value.split(" ,");
504
                float[] ret = new float[aux.length];
505

    
506
                for (int i = 0; i < aux.length; i++) {
507
                        ret[i] = Float.parseFloat(aux[i].replaceAll("\\\\,", ","));
508
                }
509

    
510
                return ret;
511
        }
512

    
513
        /**
514
         * Devuelve el array de long que corresponda a la clave que se pasa como
515
         * par?metro.
516
         *
517
         * @param key
518
         *            clave
519
         *
520
         * @return valor.
521
         *
522
         * @throws NotExistInXMLEntity
523
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
524
         *             esa clave.
525
         */
526
        public long[] getLongArrayProperty(String key) {
527
                Property[] properties = xmltag.getProperty();
528
                String value = null;
529
                boolean exists = false;
530

    
531
                for (int i = 0; i < properties.length; i++) {
532
                        if (properties[i].getKey().compareTo(key) == 0) {
533
                                value = properties[i].getValue();
534
                                exists = true;
535
                        }
536
                }
537

    
538
                if (!exists) {
539
                        throw new NotExistInXMLEntity();
540
                }
541

    
542
                if (value.compareTo("") == 0) {
543
                        return new long[0];
544
                }
545

    
546
                String[] aux = (String[]) value.split(" ,");
547
                long[] ret = new long[aux.length];
548

    
549
                for (int i = 0; i < aux.length; i++) {
550
                        ret[i] = Long.parseLong(aux[i].replaceAll("\\\\,", ","));
551
                }
552

    
553
                return ret;
554
        }
555

    
556
        /**
557
         * Devuelve el array de bytes que corresponda a la clave que se pasa como
558
         * par?metro.
559
         *
560
         * @param key
561
         *            clave
562
         *
563
         * @return valor.
564
         *
565
         * @throws NotExistInXMLEntity
566
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
567
         *             esa clave.
568
         */
569
        public byte[] getByteArrayProperty(String key) {
570
                Property[] properties = xmltag.getProperty();
571
                String value = null;
572
                boolean exists = false;
573

    
574
                for (int i = 0; i < properties.length; i++) {
575
                        if (properties[i].getKey().compareTo(key) == 0) {
576
                                value = properties[i].getValue();
577
                                exists = true;
578
                        }
579
                }
580

    
581
                if (!exists) {
582
                        throw new NotExistInXMLEntity();
583
                }
584

    
585
                if (value.compareTo("") == 0) {
586
                        return new byte[0];
587
                }
588

    
589
                String[] aux = (String[]) value.split(" ,");
590
                byte[] ret = new byte[aux.length];
591

    
592
                for (int i = 0; i < aux.length; i++) {
593
                        ret[i] = Byte.parseByte(aux[i].replaceAll("\\\\,", ","));
594
                }
595

    
596
                return ret;
597
        }
598

    
599
        /**
600
         * Devuelve el array de enteros que corresponda a la clave que se pasa como
601
         * par?metro.
602
         *
603
         * @param key
604
         *            clave
605
         *
606
         * @return valor.
607
         *
608
         * @throws NotExistInXMLEntity
609
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
610
         *             esa clave.
611
         */
612
        public int[] getIntArrayProperty(String key) {
613
                Property[] properties = xmltag.getProperty();
614
                String value = null;
615
                boolean exists = false;
616

    
617
                for (int i = 0; i < properties.length; i++) {
618
                        if (properties[i].getKey().compareTo(key) == 0) {
619
                                value = properties[i].getValue();
620
                                exists = true;
621
                        }
622
                }
623

    
624
                if (!exists) {
625
                        throw new NotExistInXMLEntity();
626
                }
627

    
628
                if (value.compareTo("") == 0) {
629
                        return new int[0];
630
                }
631

    
632
                String[] aux = (String[]) value.split(" ,");
633
                int[] ret = new int[aux.length];
634

    
635
                for (int i = 0; i < aux.length; i++) {
636
                        ret[i] = Integer.parseInt(aux[i].replaceAll("\\\\,", ","));
637
                }
638

    
639
                return ret;
640
        }
641

    
642
        /**
643
         * Devuelve el array de boolean que corresponda a la clave que se pasa como
644
         * par?metro.
645
         *
646
         * @param key
647
         *            clave
648
         *
649
         * @return valor.
650
         *
651
         * @throws NotExistInXMLEntity
652
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
653
         *             esa clave.
654
         */
655
        public boolean[] getBooleanArrayProperty(String key) {
656
                Property[] properties = xmltag.getProperty();
657
                String value = null;
658
                boolean exists = false;
659

    
660
                for (int i = 0; i < properties.length; i++) {
661
                        if (properties[i].getKey().compareTo(key) == 0) {
662
                                value = properties[i].getValue();
663
                                exists = true;
664
                        }
665
                }
666

    
667
                if (!exists) {
668
                        throw new NotExistInXMLEntity();
669
                }
670

    
671
                if (value.compareTo("") == 0) {
672
                        return new boolean[0];
673
                }
674

    
675
                String[] aux = (String[]) value.split(" ,");
676
                boolean[] ret = new boolean[aux.length];
677

    
678
                for (int i = 0; i < aux.length; i++) {
679
                        ret[i] = Boolean.valueOf(aux[i].replaceAll("\\\\,", ","))
680
                                        .booleanValue();
681
                }
682

    
683
                return ret;
684
        }
685

    
686
        /**
687
         * Devuelve el array de String que corresponda a la clave que se pasa como
688
         * par?metro.
689
         *
690
         * @param key
691
         *            clave
692
         *
693
         * @return valor.
694
         *
695
         * @throws NotExistInXMLEntity
696
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
697
         *             esa clave.
698
         */
699
        public String[] getStringArrayProperty(String key) {
700
                // /String value = (String) properties.get(key);
701
                Property[] properties = xmltag.getProperty();
702
                String value = null;
703
                boolean exists = false;
704

    
705
                for (int i = 0; i < properties.length; i++) {
706
                        if (properties[i].getKey().compareTo(key) == 0) {
707
                                value = properties[i].getValue();
708
                                exists = true;
709
                        }
710
                }
711

    
712
                if (!exists) {
713
                        throw new NotExistInXMLEntity();
714
                }
715

    
716
                if (value.compareTo("") == 0) {
717
                        return new String[0];
718
                }
719

    
720
                String[] aux = (String[]) value.split(" ,");
721

    
722
                for (int i = 0; i < aux.length; i++) {
723
                        aux[i] = aux[i].replaceAll("\\\\,", ",");
724
                }
725

    
726
                return aux;
727
        }
728

    
729
        /**
730
         * Devuelve el boolean que corresponda a la clave que se pasa como
731
         * par?metro.
732
         *
733
         * @param key
734
         *            clave
735
         *
736
         * @return valor.
737
         *
738
         * @throws NotExistInXMLEntity
739
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
740
         *             esa clave.
741
         */
742
        public boolean getBooleanProperty(String key) {
743
                Property[] properties = xmltag.getProperty();
744
                boolean res = false;
745
                boolean exists = false;
746

    
747
                for (int i = 0; i < properties.length; i++) {
748
                        if (properties[i].getKey().compareTo(key) == 0) {
749
                                res = (boolean) Boolean.valueOf(properties[i].getValue())
750
                                                .booleanValue();
751
                                exists = true;
752
                        }
753
                }
754

    
755
                if (exists) {
756
                        return res;
757
                }
758

    
759
                throw new NotExistInXMLEntity();
760
        }
761

    
762
        /**
763
         * Devuelve el entero que corresponda a la clave que se pasa como par?metro.
764
         *
765
         * @param key
766
         *            clave
767
         *
768
         * @return valor.
769
         *
770
         * @throws NotExistInXMLEntity
771
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
772
         *             esa clave.
773
         */
774
        public int getIntProperty(String key) {
775
                Property[] properties = xmltag.getProperty();
776
                int res = 0;
777
                boolean exists = false;
778

    
779
                for (int i = 0; i < properties.length; i++) {
780
                        if (properties[i].getKey().compareTo(key) == 0) {
781
                                res = Integer.parseInt(properties[i].getValue());
782
                                exists = true;
783
                        }
784
                }
785

    
786
                if (exists) {
787
                        return res;
788
                }
789

    
790
                throw new NotExistInXMLEntity();
791
        }
792

    
793
        /**
794
         * Devuelve el long que corresponda a la clave que se pasa como par?metro.
795
         *
796
         * @param key
797
         *            clave
798
         *
799
         * @return valor.
800
         *
801
         * @throws NotExistInXMLEntity
802
         *             Lanza esta excepci?n si no se encuentra ning?n elemento con
803
         *             esa clave.
804
         */
805
        public long getLongProperty(String key) {
806
                Property[] properties = xmltag.getProperty();
807
                long res = 0;
808
                boolean exists = false;
809

    
810
                for (int i = 0; i < properties.length; i++) {
811
                        if (properties[i].getKey().compareTo(key) == 0) {
812
                                res = Long.valueOf(properties[i].getValue()).longValue();
813
                                exists = true;
814
                        }
815
                }
816

    
817
                if (exists) {
818
                        return res;
819
                }
820

    
821
                throw new NotExistInXMLEntity();
822
        }
823

    
824
        /**
825
         * Devuelve el float que corresponda a la clave que se pasa como 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 float getFloatProperty(String key) {
837
                Property[] properties = xmltag.getProperty();
838
                float res = 0;
839
                boolean exists = false;
840

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

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

    
852
                throw new NotExistInXMLEntity();
853
        }
854

    
855
        /**
856
         * A?ade el nombre con clave name y valor el String que se pasa como
857
         * par?metro.
858
         *
859
         * @param name
860
         *            nombre.
861
         */
862
        public void setName(String name) {
863
                Property p = new Property();
864
                p.setKey("name");
865
                p.setValue(name);
866
                xmltag.addProperty(p);
867
        }
868

    
869
        /**
870
         * Devuelve el nombre.
871
         *
872
         * @return nombre.
873
         */
874
        public String getName() {
875
                Property p = getProperty("name");
876

    
877
                return (String) p.getValue();
878
        }
879

    
880
        /**
881
         * Devuelve la clase que implementa.
882
         *
883
         * @return clase.
884
         */
885
        public Class getImplementingClass() {
886
                Property p = getProperty("class");
887
                Object ob = p.getValue();
888

    
889
                return (Class) ob;
890
        }
891

    
892
        /**
893
         * A?ade la clase que implementa
894
         *
895
         * @param c
896
         */
897
        /*
898
         * public void setImplementingClass(Class c) { Property p = new Property();
899
         * p.setKey("class"); p.setValue(c.toString()); xmltag.addProperty(p); }
900
         */
901

    
902
        /**
903
         * A?ade un hijo al XMLEntity.
904
         *
905
         * @param entity
906
         *            xml para a?adir.
907
         */
908
        public void addChild(XMLEntity entity) {
909
                xmltag.addXmlTag(entity.getXmlTag());
910
        }
911

    
912
        /**
913
         * Devuelve un hijo a partir de un indice.
914
         *
915
         * @param i
916
         *            indice.
917
         *
918
         * @return hijo.
919
         */
920
        public XMLEntity getChild(int i) {
921
                return new XMLEntity(xmltag.getXmlTag(i));
922
        }
923

    
924
        /**
925
         * Devuelve el n?mero de hijos que contiene el XMLEntity.
926
         *
927
         * @return n?mero de hijos.
928
         */
929
        public int getChildrenCount() {
930
                return xmltag.getXmlTagCount();
931
        }
932

    
933
        /**
934
         * Devuelve el Property a partir de la clave.
935
         *
936
         * @param key
937
         *            clave.
938
         *
939
         * @return Property.
940
         */
941
        private Property getProperty(String key) {
942
                Property[] ps = xmltag.getProperty();
943

    
944
                for (int i = 0; i < ps.length; i++) {
945
                        if (ps[i].getKey().compareTo(key) == 0) {
946
                                return ps[i];
947
                        }
948
                }
949

    
950
                return null;
951
        }
952

    
953
        /**
954
         * Devuelve el xmltag.
955
         *
956
         * @return xmltag.
957
         */
958
        public XmlTag getXmlTag() {
959
                return xmltag;
960
        }
961

    
962
        /**
963
         * A?ade el property que se pasa como par?metro.
964
         *
965
         * @param p
966
         *            property.
967
         */
968
        private void putProperty(Property p) {
969
                for (int i = 0; i < xmltag.getPropertyCount(); i++) {
970
                        if (xmltag.getProperty(i).getKey().compareTo(p.getKey()) == 0) {
971
                                xmltag.getProperty(i).setValue(p.getValue());
972

    
973
                                return;
974
                        }
975
                }
976

    
977
                xmltag.addProperty(p);
978
        }
979

    
980
        public boolean contains(String key) {
981
                Property[] properties = xmltag.getProperty();
982
                boolean exists = false;
983

    
984
                for (int i = 0; i < properties.length; i++) {
985
                        if (properties[i].getKey().compareTo(key) == 0) {
986
                                exists = true;
987
                        }
988
                }
989
                return exists;
990
        }
991

    
992
        public int getPropertyCount() {
993
                return xmltag.getPropertyCount();
994
        }
995

    
996
        public String getPropertyValue(int index) {
997
                return xmltag.getProperty(index).getValue();
998
        }
999

    
1000
        public String getPropertyName(int index) {
1001
                return xmltag.getProperty(index).getKey();
1002
        }
1003

    
1004
        /**
1005
         * Removes a property of this XML-Entity.
1006
         *
1007
         * @param p
1008
         *            property.
1009
         */
1010
        public void remove(String key) {
1011
                Property[] properties = xmltag.getProperty();
1012

    
1013
                for (int i = 0; i < properties.length; i++) {
1014
                        if (properties[i].getKey().compareTo(key) == 0) {
1015
                                Property[] newProperties = new Property[properties.length - 1];
1016
                                int k = 0;
1017
                                for (int j = 0; j < newProperties.length; j++) {
1018
                                        if (j == i)
1019
                                                k = 1;
1020
                                        newProperties[j] = properties[j + k];
1021
                                }
1022
                                xmltag.setProperty(newProperties);
1023
                                return;
1024
                        }
1025
                }
1026
        }
1027

    
1028
        /**
1029
         * Elimina el hijo n del XMLEntity.
1030
         *
1031
         * @param indice
1032
         *            del hijo a eliminar.
1033
         */
1034
        public void removeChild(int index) {
1035
                xmltag.removeXmlTag(index);
1036
        }
1037

    
1038
        /**
1039
         * Elimina todos los hijos de XMLEntity.
1040
         *
1041
         */
1042
        public void removeAllChildren() {
1043
                xmltag.removeAllXmlTag();
1044
        }
1045

    
1046
        // Jaume (en proves)
1047
        public boolean equals(Object obj) {
1048
                if (obj instanceof XMLEntity) {
1049
                        XMLEntity other = (XMLEntity) obj;
1050

    
1051
                        Property[] thisProperties;
1052
                        Property[] otherProperties;
1053

    
1054
                        thisProperties = xmltag.getProperty();
1055
                        otherProperties = other.xmltag.getProperty();
1056

    
1057
                        if (thisProperties.length != otherProperties.length)
1058
                                return false;
1059

    
1060
                        for (int i = 0; i < thisProperties.length; i++) {
1061
                                if (!thisProperties[i].getKey().equals(
1062
                                                otherProperties[i].getKey()))
1063
                                        return false;
1064
                                if (!thisProperties[i].getValue().equals(
1065
                                                otherProperties[i].getValue()))
1066
                                        return false;
1067
                        }
1068

    
1069
                        if (this.getChildrenCount() != other.getChildrenCount())
1070
                                return false;
1071

    
1072
                        for (int i = 0; i < this.getChildrenCount(); i++) {
1073
                                if (!this.getChild(i).equals(other.getChild(i)))
1074
                                        return false;
1075
                        }
1076

    
1077
                        return true;
1078
                }
1079

    
1080
                return false;
1081
        }
1082

    
1083
        // jaume (en proves)
1084
        public int hashCode() {
1085
                int result = 17;
1086

    
1087
                if (xmltag.getName()!=null) {
1088
                        char[] name = xmltag.getName().toCharArray();
1089

    
1090
                        for (int i = 0; i<name.length; i++) {
1091
                                result = 37 + result + (int) name[i];
1092
                        }
1093
                }
1094

    
1095
                Property[] properties = xmltag.getProperty();
1096
                for (int i = 0; i < properties.length; i++) {
1097
                        char[] key = properties[i].getKey().toCharArray();
1098
                        for (int j = 0; j < key.length; j++) {
1099
                                result = 37 + result + (int) key[j];
1100
                        }
1101

    
1102
                        if (properties[i]!=null) {
1103
                                String value = properties[i].getValue();
1104
                                char[] chars = (value!=null)? value.toCharArray() : new char[0];
1105
                                for (int j = 0; j < chars.length; j++) {
1106
                                        result = 37 + result + (int) chars[j];
1107
                                }
1108
                        } else {
1109
                                result += 37;
1110
                        }
1111
                }
1112

    
1113
                for (int i = 0; i < this.getChildrenCount(); i++) {
1114
                        result = 37 + result + this.getChild(i).hashCode();
1115
                }
1116
                return result;
1117
        }
1118
        /**
1119
         * Devuelve el primer hijo que el valor de su propieda 'key'
1120
         * es igual a 'value'
1121
         * 
1122
         * @param key propiedad a comparar 
1123
         * @param value valor a comparar
1124
         * @return XMLEntity hijo o null si no se encuentra
1125
         */
1126
        
1127
        public XMLEntity firstChild(String key, String value) {
1128
                int num = this.getChildrenCount();
1129
                XMLEntity child;
1130
                for (int i=0;i < num; i++) {
1131
                        child = this.getChild(i);
1132
                        if (child.getStringProperty(key).equals(value)) {
1133
                                return child;
1134
                        }
1135
                }
1136
                return null;                
1137
        }
1138
        
1139
        /**
1140
         * Devuelve el indice del primer hijo que el valor de su propieda 'key'
1141
         * es igual a 'value'
1142
         * 
1143
         * @param key propiedad a comparar 
1144
         * @param value valor a comparar
1145
         * @return int indice del hijo o -1 si no se encuentra
1146
         */
1147
        public int firstIndexOfChild(String key, String value) {
1148
                int num = this.getChildrenCount();
1149
                XMLEntity child;
1150
                for (int i=0;i < num; i++) {
1151
                        child = this.getChild(i);
1152
                        if (child.getStringProperty(key).equals(value)) {
1153
                                return i;
1154
                        }
1155
                }
1156
                return -1;                
1157
        }
1158
                
1159
        /**
1160
         * Devuelve un iterador sobre los hijos que cumplen la condicion
1161
         * que el valor de su propiedad 'key' es igual a 'value'
1162
         * 
1163
         * El iterador no permite eliminacion
1164
         * 
1165
         * @param key nombre de la propidedad
1166
         * @param value valor de la propiedad
1167
         * @return
1168
         */
1169
        public Iterator findChildren(String key, String value) {
1170
                return new XMLEntityIterator(this,key,value);
1171
        }
1172
        
1173
        public String toString() {
1174
                StringWriter buffer = new StringWriter();
1175

    
1176
                Marshaller m;
1177
                try {
1178
                        m = new Marshaller(buffer);
1179
                } catch (IOException e4) {
1180
                        // TODO Auto-generated catch block
1181
                        e4.printStackTrace();
1182
                        return null;
1183
                }
1184
                m.setEncoding("ISO-8859-1");
1185

    
1186
                try {
1187
                        m.marshal(this.getXmlTag());                        
1188
                } catch (MarshalException e2) {
1189
                        // TODO Auto-generated catch block
1190
                        e2.printStackTrace();
1191
                        return null;
1192
                } catch (ValidationException e3) {
1193
                        // TODO Auto-generated catch block
1194
                        e3.printStackTrace();
1195
                        return null;
1196
                }
1197

    
1198
                return buffer.toString();                
1199
        }
1200
        
1201
        public static XMLEntity parse(String data) throws MarshalException, ValidationException {
1202
                StringReader reader = new StringReader(data);
1203

    
1204
                XmlTag tag;
1205
                tag = (XmlTag) XmlTag.unmarshal(reader);
1206
                return new XMLEntity(tag);
1207
        }
1208
        
1209
}
1210

    
1211
class XMLEntityIterator implements Iterator {
1212
        private int lastIndex;
1213
        private int lastHasNextIndex;
1214
        private XMLEntity entity;
1215
        private String key;
1216
        private String value;
1217
        
1218
        public XMLEntityIterator(XMLEntity entity,String key,String value) {
1219
                this.entity = entity;
1220
                this.key = key;
1221
                this.value = value;
1222
                this.lastIndex = -1;
1223
                this.lastHasNextIndex = -1;
1224
        }
1225
        public void remove() {
1226
                throw new UnsupportedOperationException();                
1227
        }
1228
        
1229
        public boolean hasNext() {
1230
                if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){
1231
                        return false;
1232
                }
1233
                int num = this.entity.getChildrenCount();
1234
                XMLEntity child;
1235
                for (int i=this.lastIndex+1;i < num; i++) {
1236
                        child = this.entity.getChild(i);
1237
                        if (child.contains(key) && child.getStringProperty(key).equals(value)) {
1238
                                this.lastHasNextIndex = i;
1239
                                return true;
1240
                        }
1241
                }                
1242
                return false;
1243
        }
1244
        
1245
        public Object next() {
1246
                if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){
1247
                        throw new NoSuchElementException();
1248
                }
1249
                
1250
                XMLEntity child;
1251
                
1252
                if (this.lastHasNextIndex > -1) {
1253
                        if (this.entity.getChildrenCount() > this.lastHasNextIndex) {
1254
                                child = this.entity.getChild(this.lastHasNextIndex);
1255
                                if (child.contains(key) && child.getStringProperty(key).equals(value)) {
1256
                                        this.lastIndex = this.lastHasNextIndex;
1257
                                        this.lastHasNextIndex = -1;
1258
                                        return child;
1259
                                }
1260
                        }
1261
                }
1262
                        
1263
                
1264
                int num = this.entity.getChildrenCount();
1265
                
1266
                for (int i=this.lastIndex+1;i < num; i++) {
1267
                        child = this.entity.getChild(i);
1268
                        if (child.contains(key) && child.getStringProperty(key).equals(value)) {
1269
                                this.lastIndex = i;
1270
                                this.lastHasNextIndex = -1;
1271
                                return child;
1272
                        }
1273
                }
1274
                this.lastHasNextIndex = -1;
1275
                throw new NoSuchElementException();
1276
        }
1277
}