KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > dtm > ref > DTMNamedNodeMap


1 /*
2  * Copyright 1999-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  * $Id: DTMNamedNodeMap.java,v 1.8 2004/02/16 23:06:11 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.dtm.ref;
20
21 import com.sun.org.apache.xml.internal.dtm.DTM;
22
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.NamedNodeMap JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26
27 /**
28  * DTMNamedNodeMap is a quickie (as opposed to quick) implementation of the DOM's
29  * NamedNodeMap interface, intended to support DTMProxy's getAttributes()
30  * call.
31  * <p>
32  * ***** Note: this does _not_ current attempt to cache any of the data;
33  * if you ask for attribute 27 and then 28, you'll have to rescan the first
34  * 27. It should probably at least keep track of the last one retrieved,
35  * and possibly buffer the whole array.
36  * <p>
37  * ***** Also note that there's no fastpath for the by-name query; we search
38  * linearly until we find it or fail to find it. Again, that could be
39  * optimized at some cost in object creation/storage.
40  * @xsl.usage internal
41  */

42 public class DTMNamedNodeMap implements NamedNodeMap JavaDoc
43 {
44
45   /** The DTM for this node. */
46   DTM dtm;
47
48   /** The DTM element handle. */
49   int element;
50
51   /** The number of nodes in this map. */
52   short m_count = -1;
53
54   /**
55    * Create a getAttributes NamedNodeMap for a given DTM element node
56    *
57    * @param dtm The DTM Reference, must be non-null.
58    * @param element The DTM element handle.
59    */

60   public DTMNamedNodeMap(DTM dtm, int element)
61   {
62     this.dtm = dtm;
63     this.element = element;
64   }
65
66   /**
67    * Return the number of Attributes on this Element
68    *
69    * @return The number of nodes in this map.
70    */

71   public int getLength()
72   {
73
74     if (m_count == -1)
75     {
76       short count = 0;
77
78       for (int n = dtm.getFirstAttribute(element); n != -1;
79               n = dtm.getNextAttribute(n))
80       {
81         ++count;
82       }
83
84       m_count = count;
85     }
86
87     return (int) m_count;
88   }
89
90   /**
91    * Retrieves a node specified by name.
92    * @param nameThe <code>nodeName</code> of a node to retrieve.
93    *
94    * @param name Name of the item being requested.
95    * @return A <code>Node</code> (of any type) with the specified
96    * <code>nodeName</code>, or <code>null</code> if it does not identify
97    * any node in this map.
98    */

99   public Node JavaDoc getNamedItem(String JavaDoc name)
100   {
101
102     for (int n = dtm.getFirstAttribute(element); n != -1;
103             n = dtm.getNextAttribute(n))
104     {
105       if (dtm.getNodeName(n).equals(name))
106         return dtm.getNode(n);
107     }
108
109     return null;
110   }
111
112   /**
113    * Returns the <code>index</code>th item in the map. If <code>index</code>
114    * is greater than or equal to the number of nodes in this map, this
115    * returns <code>null</code>.
116    * @param indexIndex into this map.
117    *
118    * @param i The index of the requested item.
119    * @return The node at the <code>index</code>th position in the map, or
120    * <code>null</code> if that is not a valid index.
121    */

122   public Node JavaDoc item(int i)
123   {
124
125     int count = 0;
126
127     for (int n = dtm.getFirstAttribute(element); n != -1;
128             n = dtm.getNextAttribute(n))
129     {
130       if (count == i)
131         return dtm.getNode(n);
132       else
133         ++count;
134     }
135
136     return null;
137   }
138
139   /**
140    * Adds a node using its <code>nodeName</code> attribute. If a node with
141    * that name is already present in this map, it is replaced by the new
142    * one.
143    * <br>As the <code>nodeName</code> attribute is used to derive the name
144    * which the node must be stored under, multiple nodes of certain types
145    * (those that have a "special" string value) cannot be stored as the
146    * names would clash. This is seen as preferable to allowing nodes to be
147    * aliased.
148    * @param newNode node to store in this map. The node will later be
149    * accessible using the value of its <code>nodeName</code> attribute.
150    *
151    * @return If the new <code>Node</code> replaces an existing node the
152    * replaced <code>Node</code> is returned, otherwise <code>null</code>
153    * is returned.
154    * @exception DOMException
155    * WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
156    * different document than the one that created this map.
157    * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
158    * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
159    * <code>Attr</code> that is already an attribute of another
160    * <code>Element</code> object. The DOM user must explicitly clone
161    * <code>Attr</code> nodes to re-use them in other elements.
162    */

163   public Node JavaDoc setNamedItem(Node JavaDoc newNode)
164   {
165     throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
166   }
167
168   /**
169    * Removes a node specified by name. When this map contains the attributes
170    * attached to an element, if the removed attribute is known to have a
171    * default value, an attribute immediately appears containing the
172    * default value as well as the corresponding namespace URI, local name,
173    * and prefix when applicable.
174    * @param name The <code>nodeName</code> of the node to remove.
175    *
176    * @return The node removed from this map if a node with such a name
177    * exists.
178    * @exception DOMException
179    * NOT_FOUND_ERR: Raised if there is no node named <code>name</code> in
180    * this map.
181    * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
182    */

183   public Node JavaDoc removeNamedItem(String JavaDoc name)
184   {
185     throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
186   }
187
188   /**
189    * Retrieves a node specified by local name and namespace URI. HTML-only
190    * DOM implementations do not need to implement this method.
191    * @param namespaceURI The namespace URI of the node to retrieve.
192    * @param localName The local name of the node to retrieve.
193    *
194    * @return A <code>Node</code> (of any type) with the specified local
195    * name and namespace URI, or <code>null</code> if they do not
196    * identify any node in this map.
197    * @since DOM Level 2
198    */

199   public Node JavaDoc getNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName)
200   {
201     throw new DTMException(DTMException.NOT_SUPPORTED_ERR);
202   }
203
204   /**
205    * Adds a node using its <code>namespaceURI</code> and
206    * <code>localName</code>. If a node with that namespace URI and that
207    * local name is already present in this map, it is replaced by the new
208    * one.
209    * <br>HTML-only DOM implementations do not need to implement this method.
210    * @param arg A node to store in this map. The node will later be
211    * accessible using the value of its <code>namespaceURI</code> and
212    * <code>localName</code> attributes.
213    *
214    * @return If the new <code>Node</code> replaces an existing node the
215    * replaced <code>Node</code> is returned, otherwise <code>null</code>
216    * is returned.
217    * @exception DOMException
218    * WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
219    * different document than the one that created this map.
220    * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
221    * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
222    * <code>Attr</code> that is already an attribute of another
223    * <code>Element</code> object. The DOM user must explicitly clone
224    * <code>Attr</code> nodes to re-use them in other elements.
225    * @since DOM Level 2
226    */

227   public Node JavaDoc setNamedItemNS(Node JavaDoc arg) throws DOMException JavaDoc
228   {
229     throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
230   }
231
232   /**
233    * Removes a node specified by local name and namespace URI. A removed
234    * attribute may be known to have a default value when this map contains
235    * the attributes attached to an element, as returned by the attributes
236    * attribute of the <code>Node</code> interface. If so, an attribute
237    * immediately appears containing the default value as well as the
238    * corresponding namespace URI, local name, and prefix when applicable.
239    * <br>HTML-only DOM implementations do not need to implement this method.
240    *
241    * @param namespaceURI The namespace URI of the node to remove.
242    * @param localName The local name of the node to remove.
243    *
244    * @return The node removed from this map if a node with such a local
245    * name and namespace URI exists.
246    * @exception DOMException
247    * NOT_FOUND_ERR: Raised if there is no node with the specified
248    * <code>namespaceURI</code> and <code>localName</code> in this map.
249    * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
250    * @since DOM Level 2
251    */

252   public Node JavaDoc removeNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName)
253           throws DOMException JavaDoc
254   {
255     throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
256   }
257
258   /**
259    * Simple implementation of DOMException.
260    * @xsl.usage internal
261    */

262   public class DTMException extends org.w3c.dom.DOMException JavaDoc
263   {
264
265     /**
266      * Constructs a DOM/DTM exception.
267      *
268      * @param code
269      * @param message
270      */

271     public DTMException(short code, String JavaDoc message)
272     {
273       super(code, message);
274     }
275
276     /**
277      * Constructor DTMException
278      *
279      *
280      * @param code
281      */

282     public DTMException(short code)
283     {
284       super(code, "");
285     }
286   }
287 }
288
Popular Tags