jaxp/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/utils/AttList.java
changeset 45505 ca0e16b2d5d6
parent 45504 ea7475564d07
parent 45495 8f5dd0fb0a6d
child 45506 790c716da86b
equal deleted inserted replaced
45504:ea7475564d07 45505:ca0e16b2d5d6
     1 /*
       
     2  * reserved comment block
       
     3  * DO NOT REMOVE OR ALTER!
       
     4  */
       
     5 /*
       
     6  * Licensed to the Apache Software Foundation (ASF) under one or more
       
     7  * contributor license agreements.  See the NOTICE file distributed with
       
     8  * this work for additional information regarding copyright ownership.
       
     9  * The ASF licenses this file to You under the Apache License, Version 2.0
       
    10  * (the "License"); you may not use this file except in compliance with
       
    11  * the License.  You may obtain a copy of the License at
       
    12  *
       
    13  *      http://www.apache.org/licenses/LICENSE-2.0
       
    14  *
       
    15  * Unless required by applicable law or agreed to in writing, software
       
    16  * distributed under the License is distributed on an "AS IS" BASIS,
       
    17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    18  * See the License for the specific language governing permissions and
       
    19  * limitations under the License.
       
    20  */
       
    21 
       
    22 package com.sun.org.apache.xml.internal.serializer.utils;
       
    23 
       
    24 import org.w3c.dom.Attr;
       
    25 import org.w3c.dom.NamedNodeMap;
       
    26 import org.w3c.dom.Node;
       
    27 
       
    28 import org.xml.sax.Attributes;
       
    29 
       
    30 /**
       
    31  * Wraps a DOM attribute list in a SAX Attributes.
       
    32  *
       
    33  * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
       
    34  * It exists to cut the serializers dependancy on that package.
       
    35  * A minor changes from that package are:
       
    36  * DOMHelper reference changed to DOM2Helper, class is not "public"
       
    37  *
       
    38  * This class is not a public API, it is only public because it is
       
    39  * used in com.sun.org.apache.xml.internal.serializer.
       
    40  *
       
    41  * @xsl.usage internal
       
    42  */
       
    43 public final class AttList implements Attributes
       
    44 {
       
    45 
       
    46   /** List of attribute nodes          */
       
    47   NamedNodeMap m_attrs;
       
    48 
       
    49   /** Index of last attribute node          */
       
    50   int m_lastIndex;
       
    51 
       
    52   // ARGHH!!  JAXP Uses Xerces without setting the namespace processing to ON!
       
    53   // DOM2Helper m_dh = new DOM2Helper();
       
    54 
       
    55   /** Local reference to DOMHelper          */
       
    56   DOM2Helper m_dh;
       
    57 
       
    58 //  /**
       
    59 //   * Constructor AttList
       
    60 //   *
       
    61 //   *
       
    62 //   * @param attrs List of attributes this will contain
       
    63 //   */
       
    64 //  public AttList(NamedNodeMap attrs)
       
    65 //  {
       
    66 //
       
    67 //    m_attrs = attrs;
       
    68 //    m_lastIndex = m_attrs.getLength() - 1;
       
    69 //    m_dh = new DOM2Helper();
       
    70 //  }
       
    71 
       
    72   /**
       
    73    * Constructor AttList
       
    74    *
       
    75    *
       
    76    * @param attrs List of attributes this will contain
       
    77    * @param dh DOMHelper
       
    78    */
       
    79   public AttList(NamedNodeMap attrs, DOM2Helper dh)
       
    80   {
       
    81 
       
    82     m_attrs = attrs;
       
    83     m_lastIndex = m_attrs.getLength() - 1;
       
    84     m_dh = dh;
       
    85   }
       
    86 
       
    87   /**
       
    88    * Get the number of attribute nodes in the list
       
    89    *
       
    90    *
       
    91    * @return number of attribute nodes
       
    92    */
       
    93   public int getLength()
       
    94   {
       
    95     return m_attrs.getLength();
       
    96   }
       
    97 
       
    98   /**
       
    99    * Look up an attribute's Namespace URI by index.
       
   100    *
       
   101    * @param index The attribute index (zero-based).
       
   102    * @return The Namespace URI, or the empty string if none
       
   103    *         is available, or null if the index is out of
       
   104    *         range.
       
   105    */
       
   106   public String getURI(int index)
       
   107   {
       
   108     String ns = m_dh.getNamespaceOfNode(((Attr) m_attrs.item(index)));
       
   109     if(null == ns)
       
   110       ns = "";
       
   111     return ns;
       
   112   }
       
   113 
       
   114   /**
       
   115    * Look up an attribute's local name by index.
       
   116    *
       
   117    * @param index The attribute index (zero-based).
       
   118    * @return The local name, or the empty string if Namespace
       
   119    *         processing is not being performed, or null
       
   120    *         if the index is out of range.
       
   121    */
       
   122   public String getLocalName(int index)
       
   123   {
       
   124     return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index)));
       
   125   }
       
   126 
       
   127   /**
       
   128    * Look up an attribute's qualified name by index.
       
   129    *
       
   130    *
       
   131    * @param i The attribute index (zero-based).
       
   132    *
       
   133    * @return The attribute's qualified name
       
   134    */
       
   135   public String getQName(int i)
       
   136   {
       
   137     return ((Attr) m_attrs.item(i)).getName();
       
   138   }
       
   139 
       
   140   /**
       
   141    * Get the attribute's node type by index
       
   142    *
       
   143    *
       
   144    * @param i The attribute index (zero-based)
       
   145    *
       
   146    * @return the attribute's node type
       
   147    */
       
   148   public String getType(int i)
       
   149   {
       
   150     return "CDATA";  // for the moment
       
   151   }
       
   152 
       
   153   /**
       
   154    * Get the attribute's node value by index
       
   155    *
       
   156    *
       
   157    * @param i The attribute index (zero-based)
       
   158    *
       
   159    * @return the attribute's node value
       
   160    */
       
   161   public String getValue(int i)
       
   162   {
       
   163     return ((Attr) m_attrs.item(i)).getValue();
       
   164   }
       
   165 
       
   166   /**
       
   167    * Get the attribute's node type by name
       
   168    *
       
   169    *
       
   170    * @param name Attribute name
       
   171    *
       
   172    * @return the attribute's node type
       
   173    */
       
   174   public String getType(String name)
       
   175   {
       
   176     return "CDATA";  // for the moment
       
   177   }
       
   178 
       
   179   /**
       
   180    * Look up an attribute's type by Namespace name.
       
   181    *
       
   182    * @param uri The Namespace URI, or the empty String if the
       
   183    *        name has no Namespace URI.
       
   184    * @param localName The local name of the attribute.
       
   185    * @return The attribute type as a string, or null if the
       
   186    *         attribute is not in the list or if Namespace
       
   187    *         processing is not being performed.
       
   188    */
       
   189   public String getType(String uri, String localName)
       
   190   {
       
   191     return "CDATA";  // for the moment
       
   192   }
       
   193 
       
   194   /**
       
   195    * Look up an attribute's value by name.
       
   196    *
       
   197    *
       
   198    * @param name The attribute node's name
       
   199    *
       
   200    * @return The attribute node's value
       
   201    */
       
   202   public String getValue(String name)
       
   203   {
       
   204     Attr attr = ((Attr) m_attrs.getNamedItem(name));
       
   205     return (null != attr)
       
   206           ? attr.getValue() : null;
       
   207   }
       
   208 
       
   209   /**
       
   210    * Look up an attribute's value by Namespace name.
       
   211    *
       
   212    * @param uri The Namespace URI, or the empty String if the
       
   213    *        name has no Namespace URI.
       
   214    * @param localName The local name of the attribute.
       
   215    * @return The attribute value as a string, or null if the
       
   216    *         attribute is not in the list.
       
   217    */
       
   218   public String getValue(String uri, String localName)
       
   219   {
       
   220         Node a=m_attrs.getNamedItemNS(uri,localName);
       
   221         return (a==null) ? null : a.getNodeValue();
       
   222   }
       
   223 
       
   224   /**
       
   225    * Look up the index of an attribute by Namespace name.
       
   226    *
       
   227    * @param uri The Namespace URI, or the empty string if
       
   228    *        the name has no Namespace URI.
       
   229    * @param localPart The attribute's local name.
       
   230    * @return The index of the attribute, or -1 if it does not
       
   231    *         appear in the list.
       
   232    */
       
   233   public int getIndex(String uri, String localPart)
       
   234   {
       
   235     for(int i=m_attrs.getLength()-1;i>=0;--i)
       
   236     {
       
   237       Node a=m_attrs.item(i);
       
   238       String u=a.getNamespaceURI();
       
   239       if( (u==null ? uri==null : u.equals(uri))
       
   240       &&
       
   241       a.getLocalName().equals(localPart) )
       
   242     return i;
       
   243     }
       
   244     return -1;
       
   245   }
       
   246 
       
   247   /**
       
   248    * Look up the index of an attribute by raw XML 1.0 name.
       
   249    *
       
   250    * @param qName The qualified (prefixed) name.
       
   251    * @return The index of the attribute, or -1 if it does not
       
   252    *         appear in the list.
       
   253    */
       
   254   public int getIndex(String qName)
       
   255   {
       
   256     for(int i=m_attrs.getLength()-1;i>=0;--i)
       
   257     {
       
   258       Node a=m_attrs.item(i);
       
   259       if(a.getNodeName().equals(qName) )
       
   260     return i;
       
   261     }
       
   262     return -1;
       
   263   }
       
   264 }