aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/rhino1_7R1/xmlimplsrc/org/mozilla/javascript/xmlimpl/XmlNode.java
blob: 06955d09efdacb076e39442ff1fec8bff7390078 (plain) (blame)
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
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Rhino DOM-only E4X implementation.
 *
 * The Initial Developer of the Original Code is
 * David P. Caldwell.
 * Portions created by David P. Caldwell are Copyright (C)
 * 2007 David P. Caldwell. All Rights Reserved.
 *
 *
 * Contributor(s):
 *   David P. Caldwell <inonit@inonit.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU General Public License Version 2 or later (the "GPL"), in which
 * case the provisions of the GPL are applicable instead of those above. If
 * you wish to allow use of your version of this file only under the terms of
 * the GPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replacing
 * them with the notice and other provisions required by the GPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the GPL.
 *
 * ***** END LICENSE BLOCK ***** */

package org.mozilla.javascript.xmlimpl;

import java.util.*;

import org.w3c.dom.*;

import org.mozilla.javascript.*;

//    Disambiguate with org.mozilla.javascript
import org.w3c.dom.Node;

class XmlNode {
    private static final String XML_NAMESPACES_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";

    private static final String USER_DATA_XMLNODE_KEY = XmlNode.class.getName();

    private static final boolean DOM_LEVEL_3 = true;

    private static XmlNode getUserData(Node node) {
        if (DOM_LEVEL_3) {
            return (XmlNode)node.getUserData(USER_DATA_XMLNODE_KEY);
        }
        return null;
    }

    private static void setUserData(Node node, XmlNode wrap) {
        if (DOM_LEVEL_3) {
            node.setUserData(USER_DATA_XMLNODE_KEY, wrap, wrap.events);
        }
    }

    private static XmlNode createImpl(Node node) {
        if (node instanceof Document) throw new IllegalArgumentException();
        XmlNode rv = null;
        if (getUserData(node) == null) {
            rv = new XmlNode();
            rv.dom = node;
            setUserData(node, rv);
        } else {
            rv = getUserData(node);
        }
        return rv;
    }

    static XmlNode newElementWithText(XmlProcessor processor, XmlNode reference, XmlNode.QName qname, String value) {
        if (reference instanceof org.w3c.dom.Document) throw new IllegalArgumentException("Cannot use Document node as reference");
        Document document = null;
        if (reference != null) {
            document = reference.dom.getOwnerDocument();
        } else {
            document = processor.newDocument();
        }
        Node referenceDom = (reference != null) ? reference.dom : null;
        Element e = document.createElementNS(qname.getUri(), qname.qualify(referenceDom));
        if (value != null) {
            e.appendChild(document.createTextNode(value));
        }
        return XmlNode.createImpl(e);
    }

    static XmlNode createText(XmlProcessor processor, String value) {
        return createImpl( processor.newDocument().createTextNode(value) );
    }

    static XmlNode createElementFromNode(Node node) {
        if (node instanceof Document)
            node = ((Document) node).getDocumentElement();
        return createImpl(node);
    }

    static XmlNode createElement(XmlProcessor processor, String namespaceUri, String xml) throws org.xml.sax.SAXException {
        return createImpl( processor.toXml(namespaceUri, xml) );
    }

    static XmlNode createEmpty(XmlProcessor processor) {
        return createText(processor, "");
    }

    private static XmlNode copy(XmlNode other) {
        return createImpl( other.dom.cloneNode(true) );
    }

    private static final long serialVersionUID = 1L;

    private UserDataHandler events = new UserDataHandler() {
        public void handle(short operation, String key, Object data, Node src, Node dest) {
        }
    };

    private Node dom;

    private XML xml;

    private XmlNode() {
    }

    String debug() {
        XmlProcessor raw = new XmlProcessor();
        raw.setIgnoreComments(false);
        raw.setIgnoreProcessingInstructions(false);
        raw.setIgnoreWhitespace(false);
        raw.setPrettyPrinting(false);
        return raw.ecmaToXmlString(this.dom);
    }

    public String toString() {
        return "XmlNode: type=" + dom.getNodeType() + " dom=" + dom.toString();
    }

    XML getXml() {
        return xml;
    }

    void setXml(XML xml) {
        this.xml = xml;
    }

    int getChildCount() {
        return this.dom.getChildNodes().getLength();
    }

    XmlNode parent() {
        Node domParent = dom.getParentNode();
        if (domParent instanceof Document) return null;
        if (domParent == null) return null;
        return createImpl(domParent);
    }

    int getChildIndex() {
        if (this.isAttributeType()) return -1;
        if (parent() == null) return -1;
        org.w3c.dom.NodeList siblings = this.dom.getParentNode().getChildNodes();
        for (int i=0; i<siblings.getLength(); i++) {
            if (siblings.item(i) == dom) {
                return i;
            }
        }
        //    Either the parent is -1 or one of the this node's parent's children is this node.
        throw new RuntimeException("Unreachable.");
    }

    void removeChild(int index) {
        this.dom.removeChild( this.dom.getChildNodes().item(index) );
    }

    String toXmlString(XmlProcessor processor) {
        return processor.ecmaToXmlString(this.dom);
    }

    String ecmaValue() {
        //    TODO    See ECMA 357 Section 9.1
        if (isTextType()) {
            return ((org.w3c.dom.Text)dom).getData();
        } else if (isAttributeType()) {
            return ((org.w3c.dom.Attr)dom).getValue();
        } else if (isProcessingInstructionType()) {
            return ((org.w3c.dom.ProcessingInstruction)dom).getData();
        } else if (isCommentType()) {
            return ((org.w3c.dom.Comment)dom).getNodeValue();
        } else if (isElementType()) {
            throw new RuntimeException("Unimplemented ecmaValue() for elements.");
        } else {
            throw new RuntimeException("Unimplemented for node " + dom);
        }
    }

    void deleteMe() {
        if (dom instanceof Attr) {
            Attr attr = (Attr)this.dom;
            attr.getOwnerElement().getAttributes().removeNamedItemNS(attr.getNamespaceURI(), attr.getLocalName());
        } else {
            if (this.dom.getParentNode() != null) {
                this.dom.getParentNode().removeChild(this.dom);
            } else {
                //    This case can be exercised at least when executing the regression
                //    tests under https://bugzilla.mozilla.org/show_bug.cgi?id=354145
            }
        }
    }

    void normalize() {
        this.dom.normalize();
    }

    void insertChildAt(int index, XmlNode node) {
        Node parent = this.dom;
        Node child = parent.getOwnerDocument().importNode( node.dom, true );
        if (parent.getChildNodes().getLength() < index) {
            //    TODO    Check ECMA for what happens here
            throw new IllegalArgumentException("index=" + index + " length=" + parent.getChildNodes().getLength());
        }
        if (parent.getChildNodes().getLength() == index) {
            parent.appendChild(child);
        } else {
            parent.insertBefore(child, parent.getChildNodes().item(index));
        }
    }

    void insertChildrenAt(int index, XmlNode[] nodes) {
        for (int i=0; i<nodes.length; i++) {
            insertChildAt(index+i, nodes[i]);
        }
    }

    XmlNode getChild(int index) {
        Node child = dom.getChildNodes().item(index);
        return createImpl(child);
    }

    //    Helper method for XML.hasSimpleContent()
    boolean hasChildElement() {
        org.w3c.dom.NodeList nodes = this.dom.getChildNodes();
        for (int i=0; i<nodes.getLength(); i++) {
            if (nodes.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) return true;
        }
        return false;
    }

    boolean isSameNode(XmlNode other) {
        //    TODO    May need to be changed if we allow XmlNode to refer to several Node objects
        return this.dom == other.dom;
    }

    private String toUri(String ns) {
        return (ns == null) ? "" : ns;
    }

    private void addNamespaces(Namespaces rv, Element element) {
        if (element == null) throw new RuntimeException("element must not be null");
        String myDefaultNamespace = toUri(element.lookupNamespaceURI(null));
        String parentDefaultNamespace = "";
        if (element.getParentNode() != null) {
            parentDefaultNamespace = toUri(element.getParentNode().lookupNamespaceURI(null));
        }
        if (!myDefaultNamespace.equals(parentDefaultNamespace) || !(element.getParentNode() instanceof Element) ) {
            rv.declare(Namespace.create("", myDefaultNamespace));
        }
        NamedNodeMap attributes = element.getAttributes();
        for (int i=0; i<attributes.getLength(); i++) {
            Attr attr = (Attr)attributes.item(i);
            if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) {
                rv.declare(Namespace.create(attr.getLocalName(), attr.getValue()));
            }
        }
    }

    private Namespaces getAllNamespaces() {
        Namespaces rv = new Namespaces();

        Node target = this.dom;
        if (target instanceof Attr) {
            target = ((Attr)target).getOwnerElement();
        }
        while(target != null) {
            if (target instanceof Element) {
                addNamespaces(rv, (Element)target);
            }
            target = target.getParentNode();
        }
        //    Fallback in case no namespace was declared
        rv.declare(Namespace.create("", ""));
        return rv;
    }

    Namespace[] getInScopeNamespaces() {
        Namespaces rv = getAllNamespaces();
        return rv.getNamespaces();
    }

    Namespace[] getNamespaceDeclarations() {
        //    ECMA357 13.4.4.24
        if (this.dom instanceof Element) {
            Namespaces rv = new Namespaces();
            addNamespaces( rv, (Element)this.dom );
            return rv.getNamespaces();
        } else {
            return new Namespace[0];
        }
    }

    Namespace getNamespaceDeclaration(String prefix) {
        if (prefix.equals("") && dom instanceof Attr) {
            //    Default namespaces do not apply to attributes; see XML Namespaces section 5.2
            return Namespace.create("", "");
        }
        Namespaces rv = getAllNamespaces();
        return rv.getNamespace(prefix);
    }

    Namespace getNamespaceDeclaration() {
        if (dom.getPrefix() == null) return getNamespaceDeclaration("");
        return getNamespaceDeclaration(dom.getPrefix());
    }

    private static class Namespaces {
        private HashMap map = new HashMap();
        private HashMap uriToPrefix = new HashMap();

        Namespaces() {
        }

        void declare(Namespace n) {
            if (map.get(n.prefix) == null) {
                map.put(n.prefix, n.uri);
            }
            //    TODO    I think this is analogous to the other way, but have not really thought it through ... should local scope
            //            matter more than outer scope?
            if (uriToPrefix.get(n.uri) == null) {
                uriToPrefix.put(n.uri, n.prefix);
            }
        }

        Namespace getNamespaceByUri(String uri) {
            if (uriToPrefix.get(uri) == null) return null;
            return Namespace.create(uri, (String)uriToPrefix.get(uri));
        }

        Namespace getNamespace(String prefix) {
            if (map.get(prefix) == null) return null;
            return Namespace.create(prefix, (String)map.get(prefix));
        }

        Namespace[] getNamespaces() {
            Iterator i = map.keySet().iterator();
            ArrayList rv = new ArrayList();
            while(i.hasNext()) {
                String prefix = (String)i.next();
                String uri = (String)map.get(prefix);
                Namespace n = Namespace.create(prefix, uri);
                if (!n.isEmpty()) {
                    rv.add( n );
                }
            }
            return (Namespace[])rv.toArray(new Namespace[0]);
        }
    }

    final XmlNode copy() {
        return copy( this );
    }

    //    Returns whether this node is capable of being a parent
    final boolean isParentType() {
        return isElementType();
    }

    final boolean isTextType() {
        return dom.getNodeType() == Node.TEXT_NODE || dom.getNodeType() == Node.CDATA_SECTION_NODE;
    }

    final boolean isAttributeType() {
        return dom.getNodeType() == Node.ATTRIBUTE_NODE;
    }

    final boolean isProcessingInstructionType() {
        return dom.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE;
    }

    final boolean isCommentType() {
        return dom.getNodeType() == Node.COMMENT_NODE;
    }

    final boolean isElementType() {
        return dom.getNodeType() == Node.ELEMENT_NODE;
    }

    final void renameNode(QName qname) {
        this.dom = dom.getOwnerDocument().renameNode(dom, qname.getUri(), qname.qualify(dom));
    }

    void invalidateNamespacePrefix() {
        if (!(dom instanceof Element)) throw new IllegalStateException();
        String prefix = this.dom.getPrefix();
        QName after = QName.create(this.dom.getNamespaceURI(), this.dom.getLocalName(), null);
        renameNode(after);
        NamedNodeMap attrs = this.dom.getAttributes();
        for (int i=0; i<attrs.getLength(); i++) {
            if (attrs.item(i).getPrefix().equals(prefix)) {
                createImpl( attrs.item(i) ).renameNode( QName.create(attrs.item(i).getNamespaceURI(), attrs.item(i).getLocalName(), null) );
            }
        }
    }

    private void declareNamespace(Element e, String prefix, String uri) {
        if (prefix.length() > 0) {
            e.setAttributeNS(XML_NAMESPACES_NAMESPACE_URI, "xmlns:" + prefix, uri);
        } else {
            e.setAttribute("xmlns", uri);
        }
    }

    void declareNamespace(String prefix, String uri) {
        if (!(dom instanceof Element)) throw new IllegalStateException();
        if (dom.lookupNamespaceURI(uri) != null && dom.lookupNamespaceURI(uri).equals(prefix)) {
            //    do nothing
        } else {
            Element e = (Element)dom;
            declareNamespace(e, prefix, uri);
        }
    }

    private Namespace getDefaultNamespace() {
        String prefix = "";
        String uri = (dom.lookupNamespaceURI(null) == null) ? "" : dom.lookupNamespaceURI(null);
        return Namespace.create(prefix, uri);
    }

    private String getExistingPrefixFor(Namespace namespace) {
        if (getDefaultNamespace().getUri().equals(namespace.getUri())) {
            return "";
        }
        return dom.lookupPrefix(namespace.getUri());
    }

    private Namespace getNodeNamespace() {
        String uri = dom.getNamespaceURI();
        String prefix = dom.getPrefix();
        if (uri == null) uri = "";
        if (prefix == null) prefix = "";
        return Namespace.create(prefix, uri);
    }

    Namespace getNamespace() {
        return getNodeNamespace();
    }

    void removeNamespace(Namespace namespace) {
        Namespace current = getNodeNamespace();

        //    Do not remove in-use namespace
        if (namespace.is(current)) return;
        NamedNodeMap attrs = this.dom.getAttributes();
        for (int i=0; i<attrs.getLength(); i++) {
            XmlNode attr = XmlNode.createImpl(attrs.item(i));
            if (namespace.is(attr.getNodeNamespace())) return;
        }

        //    TODO    I must confess I am not sure I understand the spec fully.  See ECMA357 13.4.4.31
        String existingPrefix = getExistingPrefixFor(namespace);
        if (existingPrefix != null) {
            if (namespace.isUnspecifiedPrefix()) {
                //    we should remove any namespace with this URI from scope; we do this by declaring a namespace with the same
                //    prefix as the existing prefix and setting its URI to the default namespace
                declareNamespace(existingPrefix, getDefaultNamespace().getUri());
            } else {
                if (existingPrefix.equals(namespace.getPrefix())) {
                    declareNamespace(existingPrefix, getDefaultNamespace().getUri());
                }
            }
        } else {
            //    the argument namespace is not declared in this scope, so do nothing.
        }
    }

    private void setProcessingInstructionName(String localName) {
        org.w3c.dom.ProcessingInstruction pi = (ProcessingInstruction)this.dom;
        //    We cannot set the node name; Document.renameNode() only supports elements and attributes.  So we replace it
        pi.getParentNode().replaceChild(
            pi,
            pi.getOwnerDocument().createProcessingInstruction(localName, pi.getData())
        );
    }

    final void setLocalName(String localName) {
        if (dom instanceof ProcessingInstruction) {
            setProcessingInstructionName(localName);
        } else {
            String prefix = dom.getPrefix();
            if (prefix == null) prefix = "";
            this.dom = dom.getOwnerDocument().renameNode(dom, dom.getNamespaceURI(), QName.qualify(prefix, localName));
        }
    }

    final QName getQname() {
        String uri = (dom.getNamespaceURI()) == null ? "" : dom.getNamespaceURI();
        String prefix = (dom.getPrefix() == null) ? "" : dom.getPrefix();
        return QName.create( uri, dom.getLocalName(), prefix );
    }

    void addMatchingChildren(XMLList result, XmlNode.Filter filter) {
        Node node = this.dom;
        NodeList children = node.getChildNodes();
        for(int i=0; i<children.getLength(); i++) {
            Node childnode = children.item(i);
            XmlNode child = XmlNode.createImpl(childnode);
            if (filter.accept(childnode)) {
                result.addToList(child);
            }
        }
    }

    XmlNode[] getMatchingChildren(Filter filter) {
        ArrayList rv = new ArrayList();
        NodeList nodes = this.dom.getChildNodes();
        for (int i=0; i<nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (filter.accept(node)) {
                rv.add( createImpl(node) );
            }
        }
        return (XmlNode[])rv.toArray(new XmlNode[0]);
    }

    XmlNode[] getAttributes() {
        NamedNodeMap attrs = this.dom.getAttributes();
        //    TODO    Or could make callers handle null?
        if (attrs == null) throw new IllegalStateException("Must be element.");
        XmlNode[] rv = new XmlNode[attrs.getLength()];
        for (int i=0; i<attrs.getLength(); i++) {
            rv[i] = createImpl( attrs.item(i) );
        }
        return rv;
    }

    String getAttributeValue() {
        return ((Attr)dom).getValue();
    }

    void setAttribute(QName name, String value) {
        if (!(dom instanceof Element)) throw new IllegalStateException("Can only set attribute on elements.");
        name.setAttribute( (Element)dom, value );
    }

    void replaceWith(XmlNode other) {
        Node replacement = other.dom;
        if (replacement.getOwnerDocument() != this.dom.getOwnerDocument()) {
            replacement = this.dom.getOwnerDocument().importNode(replacement, true);
        }
        this.dom.getParentNode().replaceChild(replacement, this.dom);
    }

    String ecmaToXMLString(XmlProcessor processor) {
        if (this.isElementType()) {
            Element copy = (Element)this.dom.cloneNode(true);
            Namespace[] inScope = this.getInScopeNamespaces();
            for (int i=0; i<inScope.length; i++) {
                declareNamespace(copy, inScope[i].getPrefix(), inScope[i].getUri());
            }
            return processor.ecmaToXmlString(copy);
        } else {
            return processor.ecmaToXmlString(dom);
        }
    }

    static class Namespace {
        static Namespace create(String prefix, String uri) {
            if (prefix == null) throw new IllegalArgumentException("Empty string represents default namespace prefix");
            if (uri == null) throw new IllegalArgumentException("Namespace may not lack a URI");
            Namespace rv = new Namespace();
            rv.prefix = prefix;
            rv.uri = uri;
            return rv;
        }

        static Namespace create(String uri) {
            Namespace rv = new Namespace();
            rv.uri = uri;
            return rv;
        }

        static final Namespace GLOBAL = create("", "");

        private String prefix;
        private String uri;

        private Namespace() {
        }

        public String toString() {
            if (prefix == null) return "XmlNode.Namespace [" + uri + "]";
            return "XmlNode.Namespace [" + prefix + "{" + uri + "}]";
        }

        boolean isUnspecifiedPrefix() {
            return prefix == null;
        }

        boolean is(Namespace other) {
            return this.prefix != null && other.prefix != null && this.prefix.equals(other.prefix) && this.uri.equals(other.uri);
        }

        boolean isEmpty() {
            return prefix != null && prefix.equals("") && uri.equals("");
        }

        boolean isDefault() {
            return prefix != null && prefix.equals("");
        }

        boolean isGlobal() {
            return uri != null && uri.equals("");
        }

        //    Called by QName
        //    TODO    Move functionality from QName lookupPrefix to here
        private void setPrefix(String prefix) {
            if (prefix == null) throw new IllegalArgumentException();
            this.prefix = prefix;
        }

        String getPrefix() {
            return prefix;
        }

        String getUri() {
            return uri;
        }
    }

    //    TODO    Where is this class used?  No longer using it in QName implementation
    static class QName {
        static QName create(Namespace namespace, String localName) {
            //    A null namespace indicates a wild-card match for any namespace
            //    A null localName indicates "*" from the point of view of ECMA357
            if (localName != null && localName.equals("*")) throw new RuntimeException("* is not valid localName");
            QName rv = new QName();
            rv.namespace = namespace;
            rv.localName = localName;
            return rv;
        }

        /** @deprecated */
        static QName create(String uri, String localName, String prefix) {
            return create(Namespace.create(prefix, uri), localName);
        }

        static String qualify(String prefix, String localName) {
            if (prefix == null) throw new IllegalArgumentException("prefix must not be null");
            if (prefix.length() > 0) return prefix + ":" + localName;
            return localName;
        }

        private Namespace namespace;
        private String localName;

        private QName() {
        }

        public String toString() {
            return "XmlNode.QName [" + localName + "," + namespace + "]";
        }

        private boolean equals(String one, String two) {
            if (one == null && two == null) return true;
            if (one == null || two == null) return false;
            return one.equals(two);
        }

        private boolean namespacesEqual(Namespace one, Namespace two) {
            if (one == null && two == null) return true;
            if (one == null || two == null) return false;
            return equals(one.getUri(), two.getUri());
        }

        final boolean isEqualTo(QName other) {
            if (!namespacesEqual(this.namespace, other.namespace)) return false;
            if (!equals(this.localName, other.localName)) return false;
            return true;
        }

        void lookupPrefix(org.w3c.dom.Node node) {
            if (node == null) throw new IllegalArgumentException("node must not be null");
            String prefix = node.lookupPrefix(namespace.getUri());
            if (prefix == null) {
                //    check to see if we match the default namespace
                String defaultNamespace = node.lookupNamespaceURI(null);
                if (defaultNamespace == null) defaultNamespace = "";
                String nodeNamespace = namespace.getUri();
                if (nodeNamespace.equals(defaultNamespace)) {
                    prefix = "";
                }
            }
            int i = 0;
            while(prefix == null) {
                String generatedPrefix = "e4x_" + i++;
                String generatedUri = node.lookupNamespaceURI(generatedPrefix);
                if (generatedUri == null) {
                    prefix = generatedPrefix;
                    org.w3c.dom.Node top = node;
                    while(top.getParentNode() != null && top.getParentNode() instanceof org.w3c.dom.Element) {
                        top = top.getParentNode();
                    }
                    ((org.w3c.dom.Element)top).setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace.getUri());
                }
            }
            namespace.setPrefix(prefix);
        }

        String qualify(org.w3c.dom.Node node) {
            if (namespace.getPrefix() == null) {
                if (node != null) {
                    lookupPrefix(node);
                } else {
                    if (namespace.getUri().equals("")) {
                        namespace.setPrefix("");
                    } else {
                        //    TODO    I am not sure this is right, but if we are creating a standalone node, I think we can set the
                        //            default namespace on the node itself and not worry about setting a prefix for that namespace.
                        namespace.setPrefix("");
                    }
                }
            }
            return qualify(namespace.getPrefix(), localName);
        }

        void setAttribute(org.w3c.dom.Element element, String value) {
            if (namespace.getPrefix() == null) lookupPrefix(element);
            element.setAttributeNS(namespace.getUri(), qualify(namespace.getPrefix(), localName), value);
        }

        /** @deprecated Use getNamespace() */
        String getUri() {
            return namespace.getUri();
        }

        /** @deprecated Use getNamespace() */
        String getPrefix() {
            return namespace.getPrefix();
        }

        Namespace getNamespace() {
            return namespace;
        }

        String getLocalName() {
            return localName;
        }
    }

    static class List {
        private java.util.Vector v;

        List() {
            v = new java.util.Vector();
        }

        private void _add(XmlNode n) {
            v.add(n);
        }

        XmlNode item(int index) {
            return (XmlNode)(v.get(index));
        }

        void remove(int index) {
            v.remove(index);
        }

        void add(List other) {
            for (int i=0; i<other.length(); i++) {
                _add(other.item(i));
            }
        }

        void add(List from, int startInclusive, int endExclusive) {
            for (int i=startInclusive; i<endExclusive; i++) {
                _add(from.item(i));
            }
        }

        void add(XmlNode node) {
            _add(node);
        }

        /** @deprecated */
        void add(XML xml) {
            _add(xml.getAnnotation());
        }

        /** @deprecated */
        void addToList(Object toAdd) {
            if (toAdd instanceof Undefined) {
                // Missing argument do nothing...
                return;
            }

            if (toAdd instanceof XMLList) {
                XMLList xmlSrc = (XMLList)toAdd;
                for (int i = 0; i < xmlSrc.length(); i++) {
                    this._add((xmlSrc.item(i)).getAnnotation());
                }
            } else if (toAdd instanceof XML) {
                this._add(((XML)(toAdd)).getAnnotation());
            } else if (toAdd instanceof XmlNode) {
                this._add((XmlNode)toAdd);
            }
        }

        int length() {
            return v.size();
        }
    }

    static abstract class Filter {
        static final Filter COMMENT = new Filter() {
            boolean accept(Node node) {
                return node.getNodeType() == Node.COMMENT_NODE;
            }
        };
        static final Filter TEXT = new Filter() {
            boolean accept(Node node) {
                return node.getNodeType() == Node.TEXT_NODE;
            }
        };
        static Filter PROCESSING_INSTRUCTION(final XMLName name) {
            return new Filter() {
                boolean accept(Node node) {
                    if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
                        ProcessingInstruction pi = (ProcessingInstruction)node;
                        return name.matchesLocalName(pi.getTarget());
                    }
                    return false;
                }
            };
        }
        static Filter ELEMENT = new Filter() {
            boolean accept(Node node) {
                return node.getNodeType() == Node.ELEMENT_NODE;
            }
        };
        static Filter TRUE = new Filter() {
            boolean accept(Node node) {
                return true;
            }
        };
        abstract boolean accept(Node node);
    }

    //    Support experimental Java interface
    org.w3c.dom.Node toDomNode() {
        return this.dom;
    }
}