Home | History | Annotate | Download | only in ref
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: DTMNamedNodeMap.java 468653 2006-10-28 07:07:05Z minchau $
     20  */
     21 package org.apache.xml.dtm.ref;
     22 
     23 import org.apache.xml.dtm.DTM;
     24 
     25 import org.w3c.dom.DOMException;
     26 import org.w3c.dom.NamedNodeMap;
     27 import org.w3c.dom.Node;
     28 
     29 /**
     30  * DTMNamedNodeMap is a quickie (as opposed to quick) implementation of the DOM's
     31  * NamedNodeMap interface, intended to support DTMProxy's getAttributes()
     32  * call.
     33  * <p>
     34  * ***** Note: this does _not_ current attempt to cache any of the data;
     35  * if you ask for attribute 27 and then 28, you'll have to rescan the first
     36  * 27. It should probably at least keep track of the last one retrieved,
     37  * and possibly buffer the whole array.
     38  * <p>
     39  * ***** Also note that there's no fastpath for the by-name query; we search
     40  * linearly until we find it or fail to find it. Again, that could be
     41  * optimized at some cost in object creation/storage.
     42  * @xsl.usage internal
     43  */
     44 public class DTMNamedNodeMap implements NamedNodeMap
     45 {
     46 
     47   /** The DTM for this node. */
     48   DTM dtm;
     49 
     50   /** The DTM element handle. */
     51   int element;
     52 
     53   /** The number of nodes in this map. */
     54   short m_count = -1;
     55 
     56   /**
     57    * Create a getAttributes NamedNodeMap for a given DTM element node
     58    *
     59    * @param dtm The DTM Reference, must be non-null.
     60    * @param element The DTM element handle.
     61    */
     62   public DTMNamedNodeMap(DTM dtm, int element)
     63   {
     64     this.dtm = dtm;
     65     this.element = element;
     66   }
     67 
     68   /**
     69    * Return the number of Attributes on this Element
     70    *
     71    * @return The number of nodes in this map.
     72    */
     73   public int getLength()
     74   {
     75 
     76     if (m_count == -1)
     77     {
     78       short count = 0;
     79 
     80       for (int n = dtm.getFirstAttribute(element); n != -1;
     81               n = dtm.getNextAttribute(n))
     82       {
     83         ++count;
     84       }
     85 
     86       m_count = count;
     87     }
     88 
     89     return (int) m_count;
     90   }
     91 
     92   /**
     93    * Retrieves a node specified by name.
     94    * @param name The <code>nodeName</code> of a node to retrieve.
     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 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 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 }
    301