File: XMLElement.java

package info (click to toggle)
libnanoxml2-java 2.2.3.dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 992 kB
  • sloc: java: 5,085; xml: 150; makefile: 89; sh: 59
file content (1042 lines) | stat: -rw-r--r-- 29,099 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
/* XMLElement.java                                                 NanoXML/Java
 *
 * $Revision: 1.5 $
 * $Date: 2002/02/06 18:50:12 $
 * $Name: RELEASE_2_2_1 $
 *
 * This file is part of NanoXML 2 for Java.
 * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software in
 *     a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *  3. This notice may not be removed or altered from any source distribution.
 */

package net.n3.nanoxml;


import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;


/**
 * XMLElement is an XML element. The standard NanoXML builder generates a
 * tree of such elements.
 *
 * @see net.n3.nanoxml.StdXMLBuilder
 *
 * @author Marc De Scheemaecker
 * @version $Name: RELEASE_2_2_1 $, $Revision: 1.5 $
 */
public class XMLElement implements IXMLElement, Serializable {

    /**
     * Necessary for serialization.
     */
    static final long serialVersionUID = -2383376380548624920L;


    /**
     * No line number defined.
     */
    public static final int NO_LINE = -1;


    /**
     * The parent element.
     */
    private IXMLElement parent;


    /**
     * The attributes of the element.
     */
    private Vector attributes;


    /**
     * The child elements.
     */
    private Vector children;


    /**
     * The name of the element.
     */
    private String name;


    /**
     * The full name of the element.
     */
    private String fullName;


    /**
     * The namespace URI.
     */
    private String namespace;


    /**
     * The content of the element.
     */
    private String content;


    /**
     * The system ID of the source data where this element is located.
     */
    private String systemID;


    /**
     * The line in the source data where this element starts.
     */
    private int lineNr;


    /**
     * Creates an empty element to be used for #PCDATA content.
     */
    public XMLElement() {
        this(null, null, null, NO_LINE);
    }


    /**
     * Creates an empty element.
     *
     * @param fullName the name of the element.
     */
    public XMLElement(String fullName) {
        this(fullName, null, null, NO_LINE);
    }


    /**
     * Creates an empty element.
     *
     * @param fullName the name of the element.
     * @param systemID the system ID of the XML data where the element starts.
     * @param lineNr   the line in the XML data where the element starts.
     */
    public XMLElement(String fullName,
                      String systemID,
                      int    lineNr) {
        this(fullName, null, systemID, lineNr);
    }


    /**
     * Creates an empty element.
     *
     * @param fullName  the full name of the element
     * @param namespace the namespace URI.
     */
    public XMLElement(String fullName,
                      String namespace) {
        this(fullName, namespace, null, NO_LINE);
    }


    /**
     * Creates an empty element.
     *
     * @param fullName  the full name of the element
     * @param namespace the namespace URI.
     * @param systemID  the system ID of the XML data where the element starts.
     * @param lineNr    the line in the XML data where the element starts.
     */
    public XMLElement(String fullName,
                      String namespace,
                      String systemID,
                      int    lineNr) {
        this.attributes = new Vector();
        this.children = new Vector(8);
        this.fullName = fullName;
        if (namespace == null) {
            this.name = fullName;
        } else {
            int index = fullName.indexOf(':');
            if (index >= 0) {
                this.name = fullName.substring(index + 1);
            } else {
                this.name = fullName;
            }
        }
        this.namespace = namespace;
        this.content = null;
        this.lineNr = lineNr;
        this.systemID = systemID;
        this.parent = null;
    }


    /**
     * Creates an element to be used for #PCDATA content.
     */
    public IXMLElement createPCDataElement() {
        return new XMLElement();
    }


    /**
     * Creates an empty element.
     *
     * @param fullName the name of the element.
     */
    public IXMLElement createElement(String fullName) {
        return new XMLElement(fullName);
    }


    /**
     * Creates an empty element.
     *
     * @param fullName the name of the element.
     * @param systemID the system ID of the XML data where the element starts.
     * @param lineNr   the line in the XML data where the element starts.
     */
    public IXMLElement createElement(String fullName,
                                     String systemID,
                                     int    lineNr) {
        return new XMLElement(fullName, systemID, lineNr);
    }


    /**
     * Creates an empty element.
     *
     * @param fullName  the full name of the element
     * @param namespace the namespace URI.
     */
    public IXMLElement createElement(String fullName,
                                     String namespace) {
        return new XMLElement(fullName, namespace);
    }


    /**
     * Creates an empty element.
     *
     * @param fullName  the full name of the element
     * @param namespace the namespace URI.
     * @param systemID  the system ID of the XML data where the element starts.
     * @param lineNr    the line in the XML data where the element starts.
     */
    public IXMLElement createElement(String fullName,
                                     String namespace,
                                     String systemID,
                                     int    lineNr) {
        return new XMLElement(fullName, namespace, systemID, lineNr);
    }


    /**
     * Cleans up the object when it's destroyed.
     */
    protected void finalize() throws Throwable {
        this.attributes.clear();
        this.attributes = null;
        this.children = null;
        this.fullName = null;
        this.name = null;
        this.namespace = null;
        this.content = null;
        this.systemID = null;
        this.parent = null;
        super.finalize();
    }


    /**
     * Returns the parent element. This method returns null for the root
     * element.
     */
    public IXMLElement getParent() {
        return this.parent;
    }


    /**
     * Returns the full name (i.e. the name including an eventual namespace
     * prefix) of the element.
     *
     * @return the name, or null if the element only contains #PCDATA.
     */
    public String getFullName() {
        return this.fullName;
    }


    /**
     * Returns the name of the element.
     *
     * @return the name, or null if the element only contains #PCDATA.
     */
    public String getName() {
        return this.name;
    }


    /**
     * Returns the namespace of the element.
     *
     * @return the namespace, or null if no namespace is associated with the
     *         element.
     */
    public String getNamespace() {
        return this.namespace;
    }


    /**
     * Sets the full name. This method also sets the short name and clears the
     * namespace URI.
     *
     * @param name the non-null name.
     */
    public void setName(String name) {
        this.name = name;
        this.fullName = name;
        this.namespace = null;
    }


    /**
     * Sets the name.
     *
     * @param fullName  the non-null full name.
     * @param namespace the namespace URI, which may be null.
     */
    public void setName(String fullName,
                        String namespace) {
        int index = fullName.indexOf(':');
        if ((namespace == null) || (index < 0)) {
            this.name = fullName;
        } else {
            this.name = fullName.substring(index + 1);
        }
        this.fullName = fullName;
        this.namespace = namespace;
    }


    /**
     * Adds a child element.
     *
     * @param child the non-null child to add.
     */
    public void addChild(IXMLElement child) {
        if (child == null) {
            throw new IllegalArgumentException("child must not be null");
        }
        if ((child.getName() == null) && (! this.children.isEmpty())) {
            IXMLElement lastChild = (IXMLElement) this.children.lastElement();

            if (lastChild.getName() == null) {
                lastChild.setContent(lastChild.getContent()
                                     + child.getContent());
                return;
            }
        }
        ((XMLElement)child).parent = this;
        this.children.addElement(child);
    }


    /**
     * Inserts a child element.
     *
     * @param child the non-null child to add.
     * @param index where to put the child.
     */
    public void insertChild(IXMLElement child,
                            int         index) {
        if (child == null) {
            throw new IllegalArgumentException("child must not be null");
        }
        if ((child.getName() == null) && (! this.children.isEmpty())) {
            IXMLElement lastChild = (IXMLElement) this.children.lastElement();
            if (lastChild.getName() == null) {
                lastChild.setContent(lastChild.getContent()
                                     + child.getContent());
                return;
            }
        }
        ((XMLElement) child).parent = this;
        this.children.insertElementAt(child, index);
    }


    /**
     * Removes a child element.
     *
     * @param child the non-null child to remove.
     */
    public void removeChild(IXMLElement child) {
        if (child == null) {
            throw new IllegalArgumentException("child must not be null");
        }
        this.children.removeElement(child);
    }


    /**
     * Removes the child located at a certain index.
     *
     * @param index the index of the child, where the first child has index 0.
     */
    public void removeChildAtIndex(int index) {
        this.children.removeElementAt(index);
    }


    /**
     * Returns an enumeration of all child elements.
     *
     * @return the non-null enumeration
     */
    public Enumeration enumerateChildren() {
        return this.children.elements();
    }


    /**
     * Returns whether the element is a leaf element.
     *
     * @return true if the element has no children.
     */
    public boolean isLeaf() {
        return this.children.isEmpty();
    }


    /**
     * Returns whether the element has children.
     *
     * @return true if the element has children.
     */
    public boolean hasChildren() {
        return (! this.children.isEmpty());
    }


    /**
     * Returns the number of children.
     *
     * @return the count.
     */
    public int getChildrenCount() {
        return this.children.size();
    }


    /**
     * Returns a vector containing all the child elements.
     *
     * @return the vector.
     */
    public Vector getChildren() {
        return this.children;
    }


    /**
     * Returns the child at a specific index.
     *
     * @param index the index of the child
     *
     * @return the non-null child
     *
     * @throws java.lang.ArrayIndexOutOfBoundsException
     *		if the index is out of bounds.
     */
    public IXMLElement getChildAtIndex(int index)
    throws ArrayIndexOutOfBoundsException {
        return (IXMLElement) this.children.elementAt(index);
    }
    

    /**
     * Searches a child element.
     *
     * @param name the full name of the child to search for.
     *
     * @return the child element, or null if no such child was found.
     */
    public IXMLElement getFirstChildNamed(String name) {
        Enumeration enu = this.children.elements();
        while (enu.hasMoreElements()) {
            IXMLElement child = (IXMLElement) enu.nextElement();
            String childName = child.getFullName();
            if ((childName != null) && childName.equals(name)) {
                return child;
            }
        }
        return null;
    }


    /**
     * Searches a child element.
     *
     * @param name      the name of the child to search for.
     * @param namespace the namespace, which may be null.
     *
     * @return the child element, or null if no such child was found.
     */
    public IXMLElement getFirstChildNamed(String name,
                                          String namespace) {
        Enumeration enu = this.children.elements();
        while (enu.hasMoreElements()) {
            IXMLElement child = (IXMLElement) enu.nextElement();
            String str = child.getName();
            boolean found = (str != null) && (str.equals(name));
            str = child.getNamespace();
            if (str == null) {
                found &= (name == null);
            } else {
                found &= str.equals(namespace);
            }
            if (found) {
                return child;
            }
        }
        return null;
    }


    /**
     * Returns a vector of all child elements named <I>name</I>.
     *
     * @param name the full name of the children to search for.
     *
     * @return the non-null vector of child elements.
     */
    public Vector getChildrenNamed(String name) {
        Vector result = new Vector(this.children.size());
        Enumeration enu = this.children.elements();
        while (enu.hasMoreElements()) {
            IXMLElement child = (IXMLElement) enu.nextElement();
            String childName = child.getFullName();
            if ((childName != null) && childName.equals(name)) {
                result.addElement(child);
            }
        }
        return result;
    }


    /**
     * Returns a vector of all child elements named <I>name</I>.
     *
     * @param name      the name of the children to search for.
     * @param namespace the namespace, which may be null.
     *
     * @return the non-null vector of child elements.
     */
    public Vector getChildrenNamed(String name,
                                   String namespace) {
        Vector result = new Vector(this.children.size());
        Enumeration enu = this.children.elements();
        while (enu.hasMoreElements()) {
            IXMLElement child = (IXMLElement) enu.nextElement();
            String str = child.getName();
            boolean found = (str != null) && (str.equals(name));
            str = child.getNamespace();
            if (str == null) {
                found &= (name == null);
            } else {
                found &= str.equals(namespace);
            }

            if (found) {
                result.addElement(child);
            }
        }
        return result;
    }


    /**
     * Searches an attribute.
     *
     * @param fullName the non-null full name of the attribute.
     *
     * @return the attribute, or null if the attribute does not exist.
     */
    private XMLAttribute findAttribute(String fullName) {
        Enumeration enu = this.attributes.elements();
        while (enu.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) enu.nextElement();
            if (attr.getFullName().equals(fullName)) {
                return attr;
            }
        }
        return null;
    }


    /**
     * Searches an attribute.
     *
     * @param name the non-null short name of the attribute.
     * @param namespace the name space, which may be null.
     *
     * @return the attribute, or null if the attribute does not exist.
     */
    private XMLAttribute findAttribute(String name,
                                       String namespace) {
        Enumeration enu = this.attributes.elements();
        while (enu.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) enu.nextElement();
            boolean found = attr.getName().equals(name);
            if (namespace == null) {
                found &= (attr.getNamespace() == null);
            } else {
                found &= namespace.equals(attr.getNamespace());
            }

            if (found) {
                return attr;
            }
        }
        return null;
    }


    /**
     * Returns the number of attributes.
     */
    public int getAttributeCount() {
        return this.attributes.size();
    }


    /**
     * @deprecated As of NanoXML/Java 2.1, replaced by
     *             {@link #getAttribute(java.lang.String,java.lang.String)}
     * Returns the value of an attribute.
     *
     * @param name the non-null name of the attribute.
     *
     * @return the value, or null if the attribute does not exist.
     */
    public String getAttribute(String name) {
        return this.getAttribute(name, null);
    }


    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null full name of the attribute.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public String getAttribute(String name,
                               String defaultValue) {
        XMLAttribute attr = this.findAttribute(name);
        if (attr == null) {
            return defaultValue;
        } else {
            return attr.getValue();
        }
    }


    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI, which may be null.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public String getAttribute(String name,
                               String namespace,
                               String defaultValue) {
        XMLAttribute attr = this.findAttribute(name, namespace);
        if (attr == null) {
            return defaultValue;
        } else {
            return attr.getValue();
        }
    }


    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null full name of the attribute.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public int getAttribute(String name,
                            int    defaultValue) {
        String value = this.getAttribute(name, Integer.toString(defaultValue));
        return Integer.parseInt(value);
    }


    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI, which may be null.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public int getAttribute(String name,
                            String namespace,
                            int    defaultValue) {
        String value = this.getAttribute(name, namespace,
                                         Integer.toString(defaultValue));
        return Integer.parseInt(value);
    }


    /**
     * Returns the type of an attribute.
     *
     * @param name the non-null full name of the attribute.
     *
     * @return the type, or null if the attribute does not exist.
     */
    public String getAttributeType(String name) {
        XMLAttribute attr = this.findAttribute(name);
        if (attr == null) {
            return null;
        } else {
            return attr.getType();
        }
    }


    /**
     * Returns the namespace of an attribute.
     *
     * @param name the non-null full name of the attribute.
     *
     * @return the namespace, or null if there is none associated.
     */
    public String getAttributeNamespace(String name) {
        XMLAttribute attr = this.findAttribute(name);
        if (attr == null) {
            return null;
        } else {
            return attr.getNamespace();
        }
    }


    /**
     * Returns the type of an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI, which may be null.
     *
     * @return the type, or null if the attribute does not exist.
     */
    public String getAttributeType(String name,
                                   String namespace) {
        XMLAttribute attr = this.findAttribute(name, namespace);
        if (attr == null) {
            return null;
        } else {
            return attr.getType();
        }
    }


    /**
     * Sets an attribute.
     *
     * @param name the non-null full name of the attribute.
     * @param value the non-null value of the attribute.
     */
    public void setAttribute(String name,
                             String value) {
        XMLAttribute attr = this.findAttribute(name);
        if (attr == null) {
            attr = new XMLAttribute(name, name, null, value, "CDATA");
            this.attributes.addElement(attr);
        } else {
            attr.setValue(value);
        }
    }


    /**
     * Sets an attribute.
     *
     * @param fullName the non-null full name of the attribute.
     * @param namespace the namespace URI of the attribute, which may be null.
     * @param value the non-null value of the attribute.
     */
    public void setAttribute(String fullName,
                             String namespace,
                             String value) {
        int index = fullName.indexOf(':');
        String name = fullName.substring(index + 1);
        XMLAttribute attr = this.findAttribute(name, namespace);
        if (attr == null) {
            attr = new XMLAttribute(fullName, name, namespace, value, "CDATA");
            this.attributes.addElement(attr);
        } else {
            attr.setValue(value);
        }
    }


    /**
     * Removes an attribute.
     *
     * @param name the non-null name of the attribute.
     */
    public void removeAttribute(String name) {
        for (int i = 0; i < this.attributes.size(); i++) {
            XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i);
            if (attr.getFullName().equals(name)) {
                this.attributes.removeElementAt(i);
                return;
            }
        }
    }


    /**
     * Removes an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI of the attribute, which may be null.
     */
    public void removeAttribute(String name,
                                String namespace) {
        for (int i = 0; i < this.attributes.size(); i++) {
            XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i);
            boolean found = attr.getName().equals(name);
            if (namespace == null) {
                found &= (attr.getNamespace() == null);
            } else {
                found &= attr.getNamespace().equals(namespace);
            }

            if (found) {
                this.attributes.removeElementAt(i);
                return;
            }
        }
    }


    /**
     * Returns an enumeration of all attribute names.
     *
     * @return the non-null enumeration.
     */
    public Enumeration enumerateAttributeNames() {
        Vector result = new Vector();
        Enumeration enu = this.attributes.elements();
        while (enu.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) enu.nextElement();
            result.addElement(attr.getFullName());
        }
        return result.elements();
    }


    /**
     * Returns whether an attribute exists.
     *
     * @return true if the attribute exists.
     */
    public boolean hasAttribute(String name) {
        return this.findAttribute(name) != null;
    }


    /**
     * Returns whether an attribute exists.
     *
     * @return true if the attribute exists.
     */
    public boolean hasAttribute(String name,
                                String namespace) {
        return this.findAttribute(name, namespace) != null;
    }


    /**
     * Returns all attributes as a Properties object.
     *
     * @return the non-null set.
     */
    public Properties getAttributes() {
        Properties result = new Properties();
        Enumeration enu = this.attributes.elements();
        while (enu.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) enu.nextElement();
            result.put(attr.getFullName(), attr.getValue());
        }
        return result;
    }


    /**
     * Returns all attributes in a specific namespace as a Properties object.
     *
     * @param namespace the namespace URI of the attributes, which may be null.
     *
     * @return the non-null set.
     */
    public Properties getAttributesInNamespace(String namespace) {
        Properties result = new Properties();
        Enumeration enu = this.attributes.elements();
        while (enu.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) enu.nextElement();
            if (namespace == null) {
                if (attr.getNamespace() == null) {
                    result.put(attr.getName(), attr.getValue());
                }
            } else {
                if (namespace.equals(attr.getNamespace())) {
                    result.put(attr.getName(), attr.getValue());
                }
            }
        }
        return result;
    }


    /**
     * Returns the system ID of the data where the element started.
     *
     * @return the system ID, or null if unknown.
     *
     * @see #getLineNr
     */
    public String getSystemID() {
        return this.systemID;
    }


    /**
     * Returns the line number in the data where the element started.
     *
     * @return the line number, or NO_LINE if unknown.
     *
     * @see #NO_LINE
     * @see #getSystemID
     */
    public int getLineNr() {
        return this.lineNr;
    }


    /**
     * Return the #PCDATA content of the element. If the element has a
     * combination of #PCDATA content and child elements, the #PCDATA
     * sections can be retrieved as unnamed child objects. In this case,
     * this method returns null.
     *
     * @return the content.
     */
    public String getContent() {
        return this.content;
    }


    /**
     * Sets the #PCDATA content. It is an error to call this method with a
     * non-null value if there are child objects.
     *
     * @param content the (possibly null) content.
     */
    public void setContent(String content) {
        this.content = content;
    }


    /**
     * Returns true if the element equals another element.
     *
     * @param rawElement the element to compare to
     */
    public boolean equals(Object rawElement) {
        try {
            return this.equalsXMLElement((IXMLElement) rawElement);
        } catch (ClassCastException e) {
            return false;
        }
    }


    /**
     * Returns true if the element equals another element.
     *
     * @param rawElement the element to compare to
     */
    public boolean equalsXMLElement(IXMLElement elt) {
        if (! this.name.equals(elt.getName())) {
            return false;
        }
        if (this.attributes.size() != elt.getAttributeCount()) {
            return false;
        }
        Enumeration enu = this.attributes.elements();
        while (enu.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) enu.nextElement();
            if (! elt.hasAttribute(attr.getName(), attr.getNamespace())) {
                return false;
            }
            String value = elt.getAttribute(attr.getName(),
                                            attr.getNamespace(),
                                            null);
            if (! attr.getValue().equals(value)) {
                return false;
            }
            String type = elt.getAttributeType(attr.getName(),
                                               attr.getNamespace());
            if (! attr.getType().equals(type)) {
                return false;
            }
        }
        if (this.children.size() != elt.getChildrenCount()) {
            return false;
        }
        for (int i = 0; i < this.children.size(); i++) {
            IXMLElement child1 = this.getChildAtIndex(i);
            IXMLElement child2 = elt.getChildAtIndex(i);

            if (! child1.equalsXMLElement(child2)) {
                return false;
            }
        }
        return true;
    }

}