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: AttList.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xml.utils;
022    
023    import org.w3c.dom.Attr;
024    import org.w3c.dom.NamedNodeMap;
025    import org.w3c.dom.Node;
026    
027    import org.xml.sax.Attributes;
028    
029    /**
030     * Wraps a DOM attribute list in a SAX Attributes.
031     * @xsl.usage internal
032     */
033    public class AttList implements Attributes
034    {
035    
036      /** List of attribute nodes          */
037      NamedNodeMap m_attrs;
038    
039      /** Index of last attribute node          */
040      int m_lastIndex;
041    
042      // ARGHH!!  JAXP Uses Xerces without setting the namespace processing to ON!
043      // DOM2Helper m_dh = new DOM2Helper();
044    
045      /** Local reference to DOMHelper          */
046      DOMHelper m_dh;
047    
048    //  /**
049    //   * Constructor AttList
050    //   *
051    //   *
052    //   * @param attrs List of attributes this will contain
053    //   */
054    //  public AttList(NamedNodeMap attrs)
055    //  {
056    //
057    //    m_attrs = attrs;
058    //    m_lastIndex = m_attrs.getLength() - 1;
059    //    m_dh = new DOM2Helper();
060    //  }
061    
062      /**
063       * Constructor AttList
064       *
065       *
066       * @param attrs List of attributes this will contain
067       * @param dh DOMHelper 
068       */
069      public AttList(NamedNodeMap attrs, DOMHelper dh)
070      {
071    
072        m_attrs = attrs;
073        m_lastIndex = m_attrs.getLength() - 1;
074        m_dh = dh;
075      }
076    
077      /**
078       * Get the number of attribute nodes in the list 
079       *
080       *
081       * @return number of attribute nodes
082       */
083      public int getLength()
084      {
085        return m_attrs.getLength();
086      }
087    
088      /**
089       * Look up an attribute's Namespace URI by index.
090       *
091       * @param index The attribute index (zero-based).
092       * @return The Namespace URI, or the empty string if none
093       *         is available, or null if the index is out of
094       *         range.
095       */
096      public String getURI(int index)
097      {
098        String ns = m_dh.getNamespaceOfNode(((Attr) m_attrs.item(index)));
099        if(null == ns)
100          ns = "";
101        return ns;
102      }
103    
104      /**
105       * Look up an attribute's local name by index.
106       *
107       * @param index The attribute index (zero-based).
108       * @return The local name, or the empty string if Namespace
109       *         processing is not being performed, or null
110       *         if the index is out of range.
111       */
112      public String getLocalName(int index)
113      {
114        return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index)));
115      }
116    
117      /**
118       * Look up an attribute's qualified name by index.
119       *
120       *
121       * @param i The attribute index (zero-based).
122       *
123       * @return The attribute's qualified name
124       */
125      public String getQName(int i)
126      {
127        return ((Attr) m_attrs.item(i)).getName();
128      }
129    
130      /**
131       * Get the attribute's node type by index
132       *
133       *
134       * @param i The attribute index (zero-based)
135       *
136       * @return the attribute's node type
137       */
138      public String getType(int i)
139      {
140        return "CDATA";  // for the moment
141      }
142    
143      /**
144       * Get the attribute's node value by index
145       *
146       *
147       * @param i The attribute index (zero-based)
148       *
149       * @return the attribute's node value
150       */
151      public String getValue(int i)
152      {
153        return ((Attr) m_attrs.item(i)).getValue();
154      }
155    
156      /**
157       * Get the attribute's node type by name
158       *
159       *
160       * @param name Attribute name
161       *
162       * @return the attribute's node type
163       */
164      public String getType(String name)
165      {
166        return "CDATA";  // for the moment
167      }
168    
169      /**
170       * Look up an attribute's type by Namespace name.
171       *
172       * @param uri The Namespace URI, or the empty String if the
173       *        name has no Namespace URI.
174       * @param localName The local name of the attribute.
175       * @return The attribute type as a string, or null if the
176       *         attribute is not in the list or if Namespace
177       *         processing is not being performed.
178       */
179      public String getType(String uri, String localName)
180      {
181        return "CDATA";  // for the moment
182      }
183    
184      /**
185       * Look up an attribute's value by name.
186       *
187       *
188       * @param name The attribute node's name
189       *
190       * @return The attribute node's value
191       */
192      public String getValue(String name)
193      {
194        Attr attr = ((Attr) m_attrs.getNamedItem(name));
195        return (null != attr) 
196              ? attr.getValue() : null;
197      }
198    
199      /**
200       * Look up an attribute's value by Namespace name.
201       *
202       * @param uri The Namespace URI, or the empty String if the
203       *        name has no Namespace URI.
204       * @param localName The local name of the attribute.
205       * @return The attribute value as a string, or null if the
206       *         attribute is not in the list.
207       */
208      public String getValue(String uri, String localName)
209      {
210                    Node a=m_attrs.getNamedItemNS(uri,localName);
211                    return (a==null) ? null : a.getNodeValue();
212      }
213    
214      /**
215       * Look up the index of an attribute by Namespace name.
216       *
217       * @param uri The Namespace URI, or the empty string if
218       *        the name has no Namespace URI.
219       * @param localPart The attribute's local name.
220       * @return The index of the attribute, or -1 if it does not
221       *         appear in the list.
222       */
223      public int getIndex(String uri, String localPart)
224      {
225        for(int i=m_attrs.getLength()-1;i>=0;--i)
226        {
227          Node a=m_attrs.item(i);
228          String u=a.getNamespaceURI();
229          if( (u==null ? uri==null : u.equals(uri))
230              &&
231              a.getLocalName().equals(localPart) )
232            return i;
233        }
234        return -1;
235      }
236    
237      /**
238       * Look up the index of an attribute by raw XML 1.0 name.
239       *
240       * @param qName The qualified (prefixed) name.
241       * @return The index of the attribute, or -1 if it does not
242       *         appear in the list.
243       */
244      public int getIndex(String qName)
245      {
246        for(int i=m_attrs.getLength()-1;i>=0;--i)
247        {
248          Node a=m_attrs.item(i);
249          if(a.getNodeName().equals(qName) )
250            return i;
251        }
252        return -1;
253      }
254    }
255