001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: DTMNamedNodeMap.java 1225427 2011-12-29 04:33:32Z mrglavas $
020     */
021    package org.apache.xml.dtm.ref;
022    
023    import org.apache.xml.dtm.DTM;
024    
025    import org.w3c.dom.DOMException;
026    import org.w3c.dom.NamedNodeMap;
027    import org.w3c.dom.Node;
028    
029    /**
030     * DTMNamedNodeMap is a quickie (as opposed to quick) implementation of the DOM's
031     * NamedNodeMap interface, intended to support DTMProxy's getAttributes()
032     * call.
033     * <p>
034     * ***** Note: this does _not_ current attempt to cache any of the data;
035     * if you ask for attribute 27 and then 28, you'll have to rescan the first
036     * 27. It should probably at least keep track of the last one retrieved,
037     * and possibly buffer the whole array.
038     * <p>
039     * ***** Also note that there's no fastpath for the by-name query; we search
040     * linearly until we find it or fail to find it. Again, that could be
041     * optimized at some cost in object creation/storage.
042     * @xsl.usage internal
043     */
044    public class DTMNamedNodeMap implements NamedNodeMap
045    {
046    
047      /** The DTM for this node. */
048      DTM dtm;
049    
050      /** The DTM element handle. */
051      int element;
052    
053      /** The number of nodes in this map. */
054      short m_count = -1;
055    
056      /**
057       * Create a getAttributes NamedNodeMap for a given DTM element node
058       *
059       * @param dtm The DTM Reference, must be non-null.
060       * @param element The DTM element handle.
061       */
062      public DTMNamedNodeMap(DTM dtm, int element)
063      {
064        this.dtm = dtm;
065        this.element = element;
066      }
067    
068      /**
069       * Return the number of Attributes on this Element
070       *
071       * @return The number of nodes in this map.
072       */
073      public int getLength()
074      {
075    
076        if (m_count == -1)
077        {
078          short count = 0;
079    
080          for (int n = dtm.getFirstAttribute(element); n != -1;
081                  n = dtm.getNextAttribute(n))
082          {
083            ++count;
084          }
085    
086          m_count = count;
087        }
088    
089        return (int) m_count;
090      }
091    
092      /**
093       * Retrieves a node specified by name.
094       * @param name The <code>nodeName</code> of a node to retrieve.
095       * @return A <code>Node</code> (of any type) with the specified
096       *   <code>nodeName</code>, or <code>null</code> if it does not identify
097       *   any node in this map.
098       */
099      public Node getNamedItem(String name)
100      {
101    
102        for (int n = dtm.getFirstAttribute(element); n != DTM.NULL;
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 i The index of the requested item.
117       * @return The node at the <code>index</code>th position in the map, or
118       *   <code>null</code> if that is not a valid index.
119       */
120      public Node item(int i)
121      {
122    
123        int count = 0;
124    
125        for (int n = dtm.getFirstAttribute(element); n != -1;
126                n = dtm.getNextAttribute(n))
127        {
128          if (count == i)
129            return dtm.getNode(n);
130          else
131            ++count;
132        }
133    
134        return null;
135      }
136    
137      /**
138       * Adds a node using its <code>nodeName</code> attribute. If a node with
139       * that name is already present in this map, it is replaced by the new
140       * one.
141       * <br>As the <code>nodeName</code> attribute is used to derive the name
142       * which the node must be stored under, multiple nodes of certain types
143       * (those that have a "special" string value) cannot be stored as the
144       * names would clash. This is seen as preferable to allowing nodes to be
145       * aliased.
146       * @param newNode node to store in this map. The node will later be
147       *   accessible using the value of its <code>nodeName</code> attribute.
148       *
149       * @return If the new <code>Node</code> replaces an existing node the
150       *   replaced <code>Node</code> is returned, otherwise <code>null</code>
151       *   is returned.
152       * @exception DOMException
153       *   WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
154       *   different document than the one that created this map.
155       *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
156       *   <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
157       *   <code>Attr</code> that is already an attribute of another
158       *   <code>Element</code> object. The DOM user must explicitly clone
159       *   <code>Attr</code> nodes to re-use them in other elements.
160       */
161      public Node setNamedItem(Node newNode)
162      {
163        throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
164      }
165    
166      /**
167       * Removes a node specified by name. When this map contains the attributes
168       * attached to an element, if the removed attribute is known to have a
169       * default value, an attribute immediately appears containing the
170       * default value as well as the corresponding namespace URI, local name,
171       * and prefix when applicable.
172       * @param name The <code>nodeName</code> of the node to remove.
173       *
174       * @return The node removed from this map if a node with such a name
175       *   exists.
176       * @exception DOMException
177       *   NOT_FOUND_ERR: Raised if there is no node named <code>name</code> in
178       *   this map.
179       *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
180       */
181      public Node removeNamedItem(String name)
182      {
183        throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
184      }
185    
186      /**
187       * Retrieves a node specified by local name and namespace URI. HTML-only
188       * DOM implementations do not need to implement this method.
189       * @param namespaceURI The namespace URI of the node to retrieve.
190       * @param localName The local name of the node to retrieve.
191       *
192       * @return A <code>Node</code> (of any type) with the specified local
193       *   name and namespace URI, or <code>null</code> if they do not
194       *   identify any node in this map.
195       * @since DOM Level 2
196       */
197      public Node getNamedItemNS(String namespaceURI, String localName)
198      {
199           Node retNode = null;
200           for (int n = dtm.getFirstAttribute(element); n != DTM.NULL;
201                           n = dtm.getNextAttribute(n))
202           {
203             if (localName.equals(dtm.getLocalName(n)))
204             {
205               String nsURI = dtm.getNamespaceURI(n); 
206               if ((namespaceURI == null && nsURI == null)
207                      || (namespaceURI != null && namespaceURI.equals(nsURI)))
208               {
209                 retNode = dtm.getNode(n);
210                 break;
211               }
212             }
213           }
214           return retNode;
215      }
216    
217      /**
218       * Adds a node using its <code>namespaceURI</code> and
219       * <code>localName</code>. If a node with that namespace URI and that
220       * local name is already present in this map, it is replaced by the new
221       * one.
222       * <br>HTML-only DOM implementations do not need to implement this method.
223       * @param arg A node to store in this map. The node will later be
224       *   accessible using the value of its <code>namespaceURI</code> and
225       *   <code>localName</code> attributes.
226       *
227       * @return If the new <code>Node</code> replaces an existing node the
228       *   replaced <code>Node</code> is returned, otherwise <code>null</code>
229       *   is returned.
230       * @exception DOMException
231       *   WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
232       *   different document than the one that created this map.
233       *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
234       *   <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
235       *   <code>Attr</code> that is already an attribute of another
236       *   <code>Element</code> object. The DOM user must explicitly clone
237       *   <code>Attr</code> nodes to re-use them in other elements.
238       * @since DOM Level 2
239       */
240      public Node setNamedItemNS(Node arg) throws DOMException
241      {
242        throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
243      }
244    
245      /**
246       * Removes a node specified by local name and namespace URI. A removed
247       * attribute may be known to have a default value when this map contains
248       * the attributes attached to an element, as returned by the attributes
249       * attribute of the <code>Node</code> interface. If so, an attribute
250       * immediately appears containing the default value as well as the
251       * corresponding namespace URI, local name, and prefix when applicable.
252       * <br>HTML-only DOM implementations do not need to implement this method.
253       * 
254       * @param namespaceURI The namespace URI of the node to remove.
255       * @param localName The local name of the node to remove.
256       *
257       * @return The node removed from this map if a node with such a local
258       *   name and namespace URI exists.
259       * @exception DOMException
260       *   NOT_FOUND_ERR: Raised if there is no node with the specified
261       *   <code>namespaceURI</code> and <code>localName</code> in this map.
262       *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
263       * @since DOM Level 2
264       */
265      public Node removeNamedItemNS(String namespaceURI, String localName)
266              throws DOMException
267      {
268        throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
269      }
270    
271      /**
272       * Simple implementation of DOMException.
273       * @xsl.usage internal
274       */
275      public static class DTMException extends org.w3c.dom.DOMException
276      {
277              static final long serialVersionUID = -8290238117162437678L;
278        /**
279         * Constructs a DOM/DTM exception.
280         *
281         * @param code
282         * @param message
283         */
284        public DTMException(short code, String message)
285        {
286          super(code, message);
287        }
288    
289        /**
290         * Constructor DTMException
291         *
292         *
293         * @param code
294         */
295        public DTMException(short code)
296        {
297          super(code, "");
298        }
299      }
300    }