KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > nodes > NamedNodeMapImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xdm.nodes;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.modules.xml.spi.dom.ROException;
27 import org.w3c.dom.NamedNodeMap JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30 /**
31  * Read-only implementation of NamedNodeMap delegating to a Java <code>Map</code>.
32  * The underlaying map must use {@link #createKey} as its keys. Also keeps
33  * fidelity of the attribute order.
34  *
35  * @author Ayub Khan
36  */

37 public final class NamedNodeMapImpl implements NamedNodeMap JavaDoc {
38     
39     private List JavaDoc<Attribute> attributes;
40     
41     /** Read-only empty map. */
42     public static final NamedNodeMap JavaDoc EMPTY =
43             new NamedNodeMapImpl(new ArrayList JavaDoc(0));
44     
45     /**
46      * Creates new NamedNodeMapImpl
47      * @param peer a map to delegate to. It must not be modified after this contructor call!
48      */

49     public NamedNodeMapImpl(List JavaDoc<Attribute> attributes) {
50         if (attributes == null) throw new NullPointerException JavaDoc();
51         this.attributes = new ArrayList JavaDoc(attributes);
52     }
53     
54     public int getLength() {
55         return attributes.size();
56     }
57     
58     public org.w3c.dom.Node JavaDoc removeNamedItem(String JavaDoc str)
59     throws org.w3c.dom.DOMException JavaDoc {
60         throw new ROException();
61     }
62     
63     public org.w3c.dom.Node JavaDoc setNamedItemNS(org.w3c.dom.Node JavaDoc node)
64     throws org.w3c.dom.DOMException JavaDoc {
65         throw new ROException();
66     }
67     
68     public org.w3c.dom.Node JavaDoc setNamedItem(org.w3c.dom.Node JavaDoc node)
69     throws org.w3c.dom.DOMException JavaDoc {
70         throw new ROException();
71     }
72     
73     public org.w3c.dom.Node JavaDoc getNamedItemNS(String JavaDoc uri, String JavaDoc local) {
74         String JavaDoc key = (String JavaDoc)createKey(uri, local);
75         if(key == null) return null;
76         return getNode(key);
77     }
78     
79     public org.w3c.dom.Node JavaDoc item(int param) {
80         if(param < attributes.size())
81             return (org.w3c.dom.Node JavaDoc) attributes.get(param);
82         return null;
83     }
84     
85     public org.w3c.dom.Node JavaDoc getNamedItem(String JavaDoc str) {
86         String JavaDoc key = (String JavaDoc)createKey(str);
87         if(key == null) return null;
88         return getNode(key);
89     }
90     
91     public org.w3c.dom.Node JavaDoc removeNamedItemNS(String JavaDoc str, String JavaDoc str1)
92     throws org.w3c.dom.DOMException JavaDoc {
93         throw new ROException();
94     }
95         
96     private Node getNode(String JavaDoc key) {
97         assert(key != null);
98         for(Attribute attr: attributes) {
99             if(key.equals(attr.getName())) {
100                 return attr;
101             }
102         }
103         return null;
104     }
105     
106     /**
107      * Create proper key for map entry
108      */

109     public static Object JavaDoc createKey(String JavaDoc qname) {
110         return qname;
111     }
112     
113     /**
114      * Create proper key for map entry
115      */

116     public static Object JavaDoc createKey(String JavaDoc uri, String JavaDoc local) {
117         return uri + ":" + local; // NOI18N
118
}
119     
120 }
Popular Tags