KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > message > NamedNodeMapImpl


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.message;
18
19 import org.w3c.dom.Attr JavaDoc;
20 import org.w3c.dom.DOMException JavaDoc;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  * A W3C simple DOM NameNodeMap implementation
30  *
31  * @author Heejune Ahn (cityboy@tmax.co.kr)
32  */

33 public class NamedNodeMapImpl implements NamedNodeMap JavaDoc {
34
35
36   /** Nodes. */
37   protected Vector JavaDoc nodes;
38
39   static private Document JavaDoc doc = null;
40   static{
41     try {
42       org.w3c.dom.Document JavaDoc doc = org.apache.axis.utils.XMLUtils.newDocument();
43     } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
44       throw new org.apache.axis.InternalException(e);
45     }
46   }
47
48   public NamedNodeMapImpl()
49   {
50     nodes = new Vector JavaDoc();
51   }
52   /**
53    * Retrieves a node specified by name.
54    * @param name The <code>nodeName</code> of a node to retrieve.
55    * @return A <code>Node</code> (of any type) with the specified
56    * <code>nodeName</code>, or <code>null</code> if it does not identify
57    * any node in this map.
58    */

59   public Node JavaDoc getNamedItem(String JavaDoc name){
60     if(name == null ){
61       Thread.dumpStack();
62       throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
63     }
64
65    for(Iterator JavaDoc iter = nodes.iterator(); iter.hasNext();){
66      Attr JavaDoc attr = (Attr JavaDoc)iter.next();
67      if(name.equals(attr.getName())){
68        return attr;
69      }
70    }
71    return null;
72   }
73
74     /**
75      * Adds a node using its <code>nodeName</code> attribute. If a node with
76      * that name is already present in this map, it is replaced by the new
77      * one.
78      * <br>As the <code>nodeName</code> attribute is used to derive the name
79      * which the node must be stored under, multiple nodes of certain types
80      * (those that have a "special" string value) cannot be stored as the
81      * names would clash. This is seen as preferable to allowing nodes to be
82      * aliased.
83      * @param arg A node to store in this map. The node will later be
84      * accessible using the value of its <code>nodeName</code> attribute.
85      * @return If the new <code>Node</code> replaces an existing node the
86      * replaced <code>Node</code> is returned, otherwise <code>null</code>
87      * is returned.
88      * @exception DOMException
89      * WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
90      * different document than the one that created this map.
91      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
92      * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
93      * <code>Attr</code> that is already an attribute of another
94      * <code>Element</code> object. The DOM user must explicitly clone
95      * <code>Attr</code> nodes to re-use them in other elements.
96      * <br>HIERARCHY_REQUEST_ERR: Raised if an attempt is made to add a node
97      * doesn't belong in this NamedNodeMap. Examples would include trying
98      * to insert something other than an Attr node into an Element's map
99      * of attributes, or a non-Entity node into the DocumentType's map of
100      * Entities.
101      */

102     public Node JavaDoc setNamedItem(Node JavaDoc arg) throws DOMException JavaDoc
103     {
104
105          String JavaDoc name = arg.getNodeName();
106
107          if(name == null ){
108            Thread.dumpStack();
109            throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
110          }
111
112          for(int i = 0; i < nodes.size(); i++){
113            Attr JavaDoc attr = (Attr JavaDoc)nodes.get(i);
114            // search if we have already
115
if(name.equals(attr.getName())){
116              nodes.remove(i);
117              nodes.add(i, arg);
118              return attr; // return old one
119
}
120          }
121
122          // if cannot found
123
nodes.add(arg);
124          return null;
125     }
126
127     /**
128      * Removes a node specified by name. When this map contains the attributes
129      * attached to an element, if the removed attribute is known to have a
130      * default value, an attribute immediately appears containing the
131      * default value as well as the corresponding namespace URI, local name,
132      * and prefix when applicable.
133      * @param name The <code>nodeName</code> of the node to remove.
134      * @return The node removed from this map if a node with such a name
135      * exists.
136      * @exception DOMException
137      * NOT_FOUND_ERR: Raised if there is no node named <code>name</code> in
138      * this map.
139      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
140      */

141     public Node JavaDoc removeNamedItem(String JavaDoc name) throws DOMException JavaDoc
142     {
143        if(name == null ){
144          Thread.dumpStack();
145          throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
146        }
147        for(int i = 0; i < nodes.size(); i++){
148          Attr JavaDoc attr = (Attr JavaDoc)nodes.get(i);
149          // search if we have already
150
if(name.equals(attr.getLocalName())){
151            nodes.remove(i);
152            return attr; // return old one
153
}
154       }
155       return null;
156     }
157
158     /**
159      * Returns the <code>index</code>th item in the map. If <code>index</code>
160      * is greater than or equal to the number of nodes in this map, this
161      * returns <code>null</code>.
162      * @param index Index into this map.
163      * @return The node at the <code>index</code>th position in the map, or
164      * <code>null</code> if that is not a valid index.
165      */

166     public Node JavaDoc item(int index){
167       return (nodes != null && index < nodes.size()) ?
168                     (Node JavaDoc)(nodes.elementAt(index)) : null;
169     }
170
171     /**
172      * The number of nodes in this map. The range of valid child node indices
173      * is <code>0</code> to <code>length-1</code> inclusive.
174      */

175     public int getLength(){
176       return (nodes != null) ? nodes.size() : 0;
177     }
178
179     /**
180      * Retrieves a node specified by local name and namespace URI.
181      * <br>Documents which do not support the "XML" feature will permit only
182      * the DOM Level 1 calls for creating/setting elements and attributes.
183      * Hence, if you specify a non-null namespace URI, these DOMs will never
184      * find a matching node.
185      * @param namespaceURI The namespace URI of the node to retrieve.
186      * @param localName The local name of the node to retrieve.
187      * @return A <code>Node</code> (of any type) with the specified local
188      * name and namespace URI, or <code>null</code> if they do not
189      * identify any node in this map.
190      * @since DOM Level 2
191      */

192     public Node JavaDoc getNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName){
193
194       if(namespaceURI == null) namespaceURI = "";
195       if(localName == null ){
196         Thread.dumpStack();
197         throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
198       }
199
200      for(Iterator JavaDoc iter = nodes.iterator(); iter.hasNext();){
201        Attr JavaDoc attr = (Attr JavaDoc)iter.next();
202        if(namespaceURI.equals(attr.getNamespaceURI()) &&
203           localName.equals(attr.getLocalName())){
204          return attr;
205        }
206      }
207      return null;
208     }
209
210     /**
211      * Adds a node using its <code>namespaceURI</code> and
212      * <code>localName</code>. If a node with that namespace URI and that
213      * local name is already present in this map, it is replaced by the new
214      * one.
215      * @param arg A node to store in this map. The node will later be
216      * accessible using the value of its <code>namespaceURI</code> and
217      * <code>localName</code> attributes.
218      * @return If the new <code>Node</code> replaces an existing node the
219      * replaced <code>Node</code> is returned, otherwise <code>null</code>
220      * is returned.
221      * @exception DOMException
222      * WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
223      * different document than the one that created this map.
224      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
225      * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
226      * <code>Attr</code> that is already an attribute of another
227      * <code>Element</code> object. The DOM user must explicitly clone
228      * <code>Attr</code> nodes to re-use them in other elements.
229      * <br>HIERARCHY_REQUEST_ERR: Raised if an attempt is made to add a node
230      * doesn't belong in this NamedNodeMap. Examples would include trying
231      * to insert something other than an Attr node into an Element's map
232      * of attributes, or a non-Entity node into the DocumentType's map of
233      * Entities.
234      * <br>NOT_SUPPORTED_ERR: Always thrown if the current document does not
235      * support the <code>"XML"</code> feature, since namespaces were
236      * defined by XML.
237      * @since DOM Level 2
238      */

239     public Node JavaDoc setNamedItemNS(Node JavaDoc arg) throws DOMException JavaDoc
240     {
241       String JavaDoc namespaceURI = arg.getNamespaceURI();
242       String JavaDoc localName = arg.getLocalName();
243
244       if(namespaceURI == null) namespaceURI = "";
245       if(localName == null ){
246         Thread.dumpStack();
247         throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
248       }
249
250       for(int i = 0; i < nodes.size(); i++){
251         Attr JavaDoc attr = (Attr JavaDoc)nodes.get(i);
252         // search if we have already
253
if(namespaceURI.equals(attr.getNamespaceURI()) &&
254            namespaceURI.equals(attr.getLocalName())){
255           nodes.remove(i);
256           nodes.add(i, arg);
257           return attr; // return old one
258
}
259       }
260
261       // if cannot found
262
nodes.add(arg);
263       return null;
264     }
265
266     /**
267      * Removes a node specified by local name and namespace URI. A removed
268      * attribute may be known to have a default value when this map contains
269      * the attributes attached to an element, as returned by the attributes
270      * attribute of the <code>Node</code> interface. If so, an attribute
271      * immediately appears containing the default value as well as the
272      * corresponding namespace URI, local name, and prefix when applicable.
273      * <br>Documents which do not support the "XML" feature will permit only
274      * the DOM Level 1 calls for creating/setting elements and attributes.
275      * Hence, if you specify a non-null namespace URI, these DOMs will never
276      * find a matching node.
277      * @param namespaceURI The namespace URI of the node to remove.
278      * @param localName The local name of the node to remove.
279      * @return The node removed from this map if a node with such a local
280      * name and namespace URI exists.
281      * @exception DOMException
282      * NOT_FOUND_ERR: Raised if there is no node with the specified
283      * <code>namespaceURI</code> and <code>localName</code> in this map.
284      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
285      * @since DOM Level 2
286      */

287     public Node JavaDoc removeNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName) throws DOMException JavaDoc{
288
289       if(namespaceURI == null) namespaceURI = "";
290       if(localName == null ){
291         Thread.dumpStack();
292         throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
293       }
294
295       for(int i = 0; i < nodes.size(); i++){
296         Attr JavaDoc attr = (Attr JavaDoc)nodes.get(i);
297         // search if we have already
298
if(namespaceURI.equals(attr.getNamespaceURI()) &&
299            localName.equals(attr.getLocalName())){
300           nodes.remove(i);
301           return attr; // return old one
302
}
303       }
304       return null;
305
306     }
307
308
309 }
310
Popular Tags