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