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