Statistics
| Revision:

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

History | View | Annotate | Download (20.9 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 com.iver.utiles.xmlEntity.generate.Property;
44
import com.iver.utiles.xmlEntity.generate.XmlTag;
45

    
46

    
47
/**
48
 * Adaptador de las llamadas a sus m?todos sobre los generados por castor para
49
 * generar despu?s los XML.
50
 *
51
 * @author Vicente Caballero Navarro
52
 */
53
public class XMLEntity {
54
        private XmlTag xmltag;
55

    
56
        /**
57
         * Crea un nuevo XMLEntity.
58
         *
59
         * @param tag DOCUMENT ME!
60
         */
61
        public XMLEntity(XmlTag tag) {
62
                xmltag = tag;
63
        }
64

    
65
        /**
66
         * Crea un nuevo XMLEntity.
67
         */
68
        public XMLEntity() {
69
                xmltag = new XmlTag();
70
        }
71

    
72
        /**
73
         * A?ade una propiedad con un String como clave y un String como valor.
74
         *
75
         * @param key clave.
76
         * @param value valor.
77
         */
78
        public void putProperty(String key, String value) {
79
                if ((key == null)) {
80
                        return;
81
                }
82

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

    
89
        /**
90
         * A?ade una propiedad con un String como clave y un Object como valor.
91
         *
92
         * @param key clave.
93
         * @param value valor.
94
         */
95
        public void putProperty(String key, Object value) {
96
                String ret = "";
97

    
98
                if (key == null) {
99
                        return;
100
                }
101

    
102
                Class valueClass = value.getClass();
103

    
104
                if (valueClass.isArray()) {
105
                        Class compType = valueClass.getComponentType();
106

    
107
                        if (compType == byte.class) {
108
                                byte[] array = (byte[]) value;
109

    
110
                                if (!(array.length == 0)) {
111
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
112

    
113
                                        for (int i = 1; i < array.length; i++) {
114
                                                ret += (" ," +
115
                                                ("" + array[i]).replaceAll("[,]", "\\\\,"));
116
                                        }
117
                                }
118
                        } else if (compType == short.class) {
119
                                short[] array = (short[]) value;
120

    
121
                                if (!(array.length == 0)) {
122
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
123

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

    
132
                                if (!(array.length == 0)) {
133
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
134

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

    
143
                                if (!(array.length == 0)) {
144
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
145

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

    
154
                                if (!(array.length == 0)) {
155
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
156

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

    
165
                                if (!(array.length == 0)) {
166
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
167

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

    
176
                                if (!(array.length == 0)) {
177
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
178

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

    
187
                                if (!(array.length == 0)) {
188
                                        if (array[0] == null) {
189
                                                array[0] = "null";
190
                                        }
191

    
192
                                        ret += array[0].replaceAll("[,]", "\\\\,");
193

    
194
                                        for (int i = 1; i < array.length; i++) {
195
                                                if (array[i] == null) {
196
                                                        array[i] = "null";
197
                                                }
198

    
199
                                                array[i] = array[i].replaceAll("[,]", "\\\\,");
200
                                                ret += (" ," + array[i]);
201
                                        }
202
                                }
203
                        } else {
204
                                Object[] array = (Object[]) value;
205

    
206
                                if (!(array.length == 0)) {
207
                                        ret += ("" + array[0]).replaceAll("[,]", "\\\\,");
208

    
209
                                        for (int i = 1; i < array.length; i++) {
210
                                                array[i] = ("" + array[i]).replaceAll("[,]", "\\\\,");
211
                                                ret += (" ," + array[i]);
212
                                        }
213
                                }
214
                        }
215
                } else {
216
                        ret = value.toString();
217
                }
218

    
219
                Property p = new Property();
220
                p.setKey(key);
221
                p.setValue(ret);
222
                putProperty(p);
223
        }
224

    
225
        /**
226
         * A?ade una propiedad con un String como clave y un entero como valor.
227
         *
228
         * @param key clave.
229
         * @param value valor.
230
         */
231
        public void putProperty(String key, int value) {
232
                if (key == null) {
233
                        return;
234
                }
235

    
236
                Property p = new Property();
237
                p.setKey(key);
238
                p.setValue(new Integer(value).toString());
239
                putProperty(p);
240
        }
241

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

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

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

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

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

    
287
                Property p = new Property();
288
                p.setKey(key);
289
                p.setValue(new Float(value).toString());
290
                putProperty(p);
291
        }
292

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

    
304
                Property p = new Property();
305
                p.setKey(key);
306
                p.setValue(new Double(value).toString());
307
                putProperty(p);
308
        }
309

    
310
        /**
311
         * Devuelve el String que corresponda a la clave que se pasa como
312
         * par?metro.
313
         *
314
         * @param key clave
315
         *
316
         * @return valor.
317
         *
318
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
319
         *                    ning?n elemento con esa clave.
320
         */
321
        public String getStringProperty(String key) {
322
                Property[] properties = xmltag.getProperty();
323
                String res = null;
324
                boolean exists = false;
325

    
326
                for (int i = 0; i < properties.length; i++) {
327
                        if (properties[i].getKey().compareTo(key) == 0) {
328
                                res = properties[i].getValue();
329
                                exists = true;
330
                        }
331
                }
332

    
333
                if (exists) {
334
                        return res;
335
                }
336

    
337
                throw new NotExistInXMLEntity();
338
        }
339

    
340
        /**
341
         * Devuelve el double que corresponda a la clave que se pasa como
342
         * par?metro.
343
         *
344
         * @param key clave
345
         *
346
         * @return valor.
347
         *
348
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
349
         *                    ning?n elemento con esa clave.
350
         */
351
        public double getDoubleProperty(String key) {
352
                Property[] properties = xmltag.getProperty();
353
                double res = 0;
354
                boolean exists = false;
355

    
356
                for (int i = 0; i < properties.length; i++) {
357
                        if (properties[i].getKey().compareTo(key) == 0) {
358
                                res = Double.parseDouble(properties[i].getValue());
359
                                exists = true;
360
                        }
361
                }
362

    
363
                if (exists) {
364
                        return res;
365
                }
366

    
367
                throw new NotExistInXMLEntity();
368
        }
369

    
370
        /**
371
         * Devuelve el array de doubles que corresponda a la clave que se pasa como
372
         * par?metro.
373
         *
374
         * @param key clave
375
         *
376
         * @return valor.
377
         *
378
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
379
         *                    ning?n elemento con esa clave.
380
         */
381
        public double[] getDoubleArrayProperty(String key) {
382
                Property[] properties = xmltag.getProperty();
383
                String value = null;
384
                boolean exists = false;
385

    
386
                for (int i = 0; i < properties.length; i++) {
387
                        if (properties[i].getKey().compareTo(key) == 0) {
388
                                value = properties[i].getValue();
389
                                exists = true;
390
                        }
391
                }
392

    
393
                if (!exists) {
394
                        throw new NotExistInXMLEntity();
395
                }
396

    
397
                if (value.compareTo("") == 0) {
398
                        return new double[0];
399
                }
400

    
401
                String[] aux = (String[]) value.split(" ,");
402
                double[] ret = new double[aux.length];
403

    
404
                for (int i = 0; i < aux.length; i++) {
405
                        ret[i] = Double.parseDouble(aux[i].replaceAll("\\\\,", ","));
406
                }
407

    
408
                return ret;
409
        }
410

    
411
        /**
412
         * Devuelve el Object que corresponda a la clave que se pasa como
413
         * par?metro.
414
         *
415
         * @param key clave
416
         *
417
         * @return valor.
418
         *
419
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
420
         *                    ning?n elemento con esa clave.
421
         */
422
        public Object getObjectProperty(String key) {
423
                Property[] properties = xmltag.getProperty();
424
                Object res = null;
425
                boolean exists = false;
426

    
427
                for (int i = 0; i < properties.length; i++) {
428
                        if (properties[i].getKey().compareTo(key) == 0) {
429
                                res = properties[i].getValue();
430
                                exists = true;
431
                        }
432
                }
433

    
434
                if (exists) {
435
                        return res;
436
                }
437

    
438
                throw new NotExistInXMLEntity();
439
        }
440

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

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

    
464
                if (!exists) {
465
                        throw new NotExistInXMLEntity();
466
                }
467

    
468
                if (value.compareTo("") == 0) {
469
                        return new float[0];
470
                }
471

    
472
                String[] aux = (String[]) value.split(" ,");
473
                float[] ret = new float[aux.length];
474

    
475
                for (int i = 0; i < aux.length; i++) {
476
                        ret[i] = Float.parseFloat(aux[i].replaceAll("\\\\,", ","));
477
                }
478

    
479
                return ret;
480
        }
481

    
482
        /**
483
         * Devuelve el array de long que corresponda a la clave que se pasa como
484
         * par?metro.
485
         *
486
         * @param key clave
487
         *
488
         * @return valor.
489
         *
490
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
491
         *                    ning?n elemento con esa clave.
492
         */
493
        public long[] getLongArrayProperty(String key) {
494
                Property[] properties = xmltag.getProperty();
495
                String value = null;
496
                boolean exists = false;
497

    
498
                for (int i = 0; i < properties.length; i++) {
499
                        if (properties[i].getKey().compareTo(key) == 0) {
500
                                value = properties[i].getValue();
501
                                exists = true;
502
                        }
503
                }
504

    
505
                if (!exists) {
506
                        throw new NotExistInXMLEntity();
507
                }
508

    
509
                if (value.compareTo("") == 0) {
510
                        return new long[0];
511
                }
512

    
513
                String[] aux = (String[]) value.split(" ,");
514
                long[] ret = new long[aux.length];
515

    
516
                for (int i = 0; i < aux.length; i++) {
517
                        ret[i] = Long.parseLong(aux[i].replaceAll("\\\\,", ","));
518
                }
519

    
520
                return ret;
521
        }
522
        /**
523
         * Devuelve el array de bytes que corresponda a la clave que se pasa como
524
         * par?metro.
525
         *
526
         * @param key clave
527
         *
528
         * @return valor.
529
         *
530
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
531
         *                    ning?n elemento con esa clave.
532
         */
533
        public byte[] getByteArrayProperty(String key) {
534
                Property[] properties = xmltag.getProperty();
535
                String value = null;
536
                boolean exists = false;
537

    
538
                for (int i = 0; i < properties.length; i++) {
539
                        if (properties[i].getKey().compareTo(key) == 0) {
540
                                value = properties[i].getValue();
541
                                exists = true;
542
                        }
543
                }
544

    
545
                if (!exists) {
546
                        throw new NotExistInXMLEntity();
547
                }
548

    
549
                if (value.compareTo("") == 0) {
550
                        return new byte[0];
551
                }
552

    
553
                String[] aux = (String[]) value.split(" ,");
554
                byte[] ret = new byte[aux.length];
555

    
556
                for (int i = 0; i < aux.length; i++) {
557
                        ret[i] = Byte.parseByte(aux[i].replaceAll("\\\\,", ","));
558
                }
559

    
560
                return ret;
561
        }
562

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

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

    
586
                if (!exists) {
587
                        throw new NotExistInXMLEntity();
588
                }
589

    
590
                if (value.compareTo("") == 0) {
591
                        return new int[0];
592
                }
593

    
594
                String[] aux = (String[]) value.split(" ,");
595
                int[] ret = new int[aux.length];
596

    
597
                for (int i = 0; i < aux.length; i++) {
598
                        ret[i] = Integer.parseInt(aux[i].replaceAll("\\\\,", ","));
599
                }
600

    
601
                return ret;
602
        }
603

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

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

    
627
                if (!exists) {
628
                        throw new NotExistInXMLEntity();
629
                }
630

    
631
                if (value.compareTo("") == 0) {
632
                        return new boolean[0];
633
                }
634

    
635
                String[] aux = (String[]) value.split(" ,");
636
                boolean[] ret = new boolean[aux.length];
637

    
638
                for (int i = 0; i < aux.length; i++) {
639
                        ret[i] = Boolean.valueOf(aux[i].replaceAll("\\\\,", ","))
640
                                                        .booleanValue();
641
                }
642

    
643
                return ret;
644
        }
645

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

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

    
670
                if (!exists) {
671
                        throw new NotExistInXMLEntity();
672
                }
673

    
674
                if (value.compareTo("") == 0) {
675
                        return new String[0];
676
                }
677

    
678
                String[] aux = (String[]) value.split(" ,");
679

    
680
                for (int i = 0; i < aux.length; i++) {
681
                        aux[i] = aux[i].replaceAll("\\\\,", ",");
682
                }
683

    
684
                return aux;
685
        }
686

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

    
703
                for (int i = 0; i < properties.length; i++) {
704
                        if (properties[i].getKey().compareTo(key) == 0) {
705
                                res = (boolean) Boolean.valueOf(properties[i].getValue())
706
                                                                           .booleanValue();
707
                                exists = true;
708
                        }
709
                }
710

    
711
                if (exists) {
712
                        return res;
713
                }
714

    
715
                throw new NotExistInXMLEntity();
716
        }
717

    
718
        /**
719
         * Devuelve el entero que corresponda a la clave que se pasa como
720
         * par?metro.
721
         *
722
         * @param key clave
723
         *
724
         * @return valor.
725
         *
726
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
727
         *                    ning?n elemento con esa clave.
728
         */
729
        public int getIntProperty(String key) {
730
                Property[] properties = xmltag.getProperty();
731
                int res = 0;
732
                boolean exists = false;
733

    
734
                for (int i = 0; i < properties.length; i++) {
735
                        if (properties[i].getKey().compareTo(key) == 0) {
736
                                res = Integer.parseInt(properties[i].getValue());
737
                                exists=true;
738
                        }
739
                }
740

    
741
                if (exists) {
742
                        return res;
743
                }
744

    
745
                throw new NotExistInXMLEntity();
746
        }
747

    
748
        /**
749
         * Devuelve el long que corresponda a la clave que se pasa como par?metro.
750
         *
751
         * @param key clave
752
         *
753
         * @return valor.
754
         *
755
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
756
         *                    ning?n elemento con esa clave.
757
         */
758
        public long getLongProperty(String key) {
759
                Property[] properties = xmltag.getProperty();
760
                long res = 0;
761
                boolean exists = false;
762

    
763
                for (int i = 0; i < properties.length; i++) {
764
                        if (properties[i].getKey().compareTo(key) == 0) {
765
                                res = Long.valueOf(properties[i].getValue()).longValue();
766
                                exists = true;
767
                        }
768
                }
769

    
770
                if (exists) {
771
                        return res;
772
                }
773

    
774
                throw new NotExistInXMLEntity();
775
        }
776

    
777
        /**
778
         * Devuelve el float que corresponda a la clave que se pasa como par?metro.
779
         *
780
         * @param key clave
781
         *
782
         * @return valor.
783
         *
784
         * @throws NotExistInXMLEntity Lanza esta excepci?n si no se encuentra
785
         *                    ning?n elemento con esa clave.
786
         */
787
        public float getFloatProperty(String key) {
788
                Property[] properties = xmltag.getProperty();
789
                float res = 0;
790
                boolean exists = false;
791

    
792
                for (int i = 0; i < properties.length; i++) {
793
                        if (properties[i].getKey().compareTo(key) == 0) {
794
                                res = Float.valueOf(properties[i].getValue()).floatValue();
795
                                exists = true;
796
                        }
797
                }
798

    
799
                if (exists) {
800
                        return res;
801
                }
802

    
803
                throw new NotExistInXMLEntity();
804
        }
805

    
806
        /**
807
         * A?ade el nombre con clave name y valor el String que se pasa como
808
         * par?metro.
809
         *
810
         * @param name nombre.
811
         */
812
        public void setName(String name) {
813
                Property p = new Property();
814
                p.setKey("name");
815
                p.setValue(name);
816
                xmltag.addProperty(p);
817
        }
818

    
819
        /**
820
         * Devuelve el nombre.
821
         *
822
         * @return nombre.
823
         */
824
        public String getName() {
825
                Property p = getProperty("name");
826

    
827
                return (String) p.getValue();
828
        }
829

    
830
        /**
831
         * Devuelve la clase que implementa.
832
         *
833
         * @return clase.
834
         */
835
        public Class getImplementingClass() {
836
                Property p = getProperty("class");
837
                Object ob = p.getValue();
838

    
839
                return (Class) ob;
840
        }
841

    
842
        /**
843
         * A?ade la clase que implementa
844
         *
845
         * @param c
846
         */
847
        public void setImplementingClass(Class c) {
848
                Property p = new Property();
849
                p.setKey("class");
850
                p.setValue(c.toString());
851
                xmltag.addProperty(p);
852
        }
853

    
854
        /**
855
         * A?ade un hijo al XMLEntity.
856
         *
857
         * @param entity xml para a?adir.
858
         */
859
        public void addChild(XMLEntity entity) {
860
                xmltag.addXmlTag(entity.getXmlTag());
861
        }
862

    
863
        /**
864
         * Devuelve un hijo a partir de un indice.
865
         *
866
         * @param i indice.
867
         *
868
         * @return hijo.
869
         */
870
        public XMLEntity getChild(int i) {
871
                return new XMLEntity(xmltag.getXmlTag(i));
872
        }
873

    
874
        /**
875
         * Devuelve el n?mero de hijos que contiene el XMLEntity.
876
         *
877
         * @return n?mero de hijos.
878
         */
879
        public int getNumChild() {
880
                return xmltag.getXmlTagCount();
881
        }
882

    
883
        /**
884
         * Devuelve los xmlTag hijos del actual xmltag.
885
         *
886
         * @return hijo.
887
         */
888
        public XmlTag[] hijos() {
889
                return xmltag.getXmlTag();
890
        }
891

    
892
        /**
893
         * Devuelve el Property a partir de la clave.
894
         *
895
         * @param key clave.
896
         *
897
         * @return Property.
898
         */
899
        private Property getProperty(String key) {
900
                Property[] ps = xmltag.getProperty();
901

    
902
                for (int i = 0; i < ps.length; i++) {
903
                        if (ps[i].getKey().compareTo(key) == 0) {
904
                                return ps[i];
905
                        }
906
                }
907

    
908
                return null;
909
        }
910

    
911
        /**
912
         * Devuelve el xmltag.
913
         *
914
         * @return xmltag.
915
         */
916
        public XmlTag getXmlTag() {
917
                return xmltag;
918
        }
919

    
920
        /**
921
         * A?ade el property que se pasa como par?metro.
922
         *
923
         * @param p property.
924
         */
925
        private void putProperty(Property p) {
926
                for (int i = 0; i < xmltag.getPropertyCount(); i++) {
927
                        if (xmltag.getProperty(i).getKey().compareTo(p.getKey()) == 0) {
928
                                xmltag.getProperty(i).setValue(p.getValue());
929

    
930
                                return;
931
                        }
932
                }
933

    
934
                xmltag.addProperty(p);
935
        }
936
        public boolean contains(String key) {
937
                Property[] properties = xmltag.getProperty();
938
                boolean exists = false;
939

    
940
                for (int i = 0; i < properties.length; i++) {
941
                        if (properties[i].getKey().compareTo(key) == 0) {
942
                                exists = true;
943
                        }
944
                }
945
                return exists;
946
        }
947
        
948
        public int getPropertyCount(){
949
                return xmltag.getPropertyCount();
950
        }
951
        
952
        public String getPropertyValue(int index){
953
                return xmltag.getProperty(index).getValue();
954
        }
955
        
956
        public String getPropertyName(int index){
957
                return xmltag.getProperty(index).getKey();
958
        }
959
        
960
        /**
961
         * Removes a property of this XML-Entity.
962
         *
963
         * @param p property.
964
         */
965
        public void remove(String key) {
966
                Property[] properties = xmltag.getProperty();
967
                
968
                for (int i = 0; i < properties.length; i++) {
969
                        if (properties[i].getKey().compareTo(key) == 0) {
970
                                Property[] newProperties = new Property[properties.length-1];
971
                                int k = 0;
972
                                for (int j = 0; j < newProperties.length; j++) {
973
                                        if (j==i) 
974
                                                k = 1;
975
                                        newProperties[j] = properties[j+k];
976
                                }
977
                                xmltag.setProperty(newProperties);
978
                                return;
979
                        }
980
                }
981
        }
982
}