1 /*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5 /*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * 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 * $Id: AttList.java,v 1.2.4.1 2005/09/15 08:15:35 suresh_emailid Exp $
22 */
23 package com.sun.org.apache.xml.internal.utils;
24
25 import org.w3c.dom.Attr;
26 import org.w3c.dom.NamedNodeMap;
27 import org.w3c.dom.Node;
28
29 import org.xml.sax.Attributes;
30
31 /**
32 * Wraps a DOM attribute list in a SAX Attributes.
33 * @xsl.usage internal
34 */
35 public class AttList implements Attributes
36 {
37
38 /** List of attribute nodes */
39 NamedNodeMap m_attrs;
40
41 /** Index of last attribute node */
42 int m_lastIndex;
43
44 // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON!
45 // DOM2Helper m_dh = new DOM2Helper();
46
47 /** Local reference to DOMHelper */
48 DOMHelper m_dh;
49
50 // /**
51 // * Constructor AttList
52 // *
53 // *
54 // * @param attrs List of attributes this will contain
55 // */
56 // public AttList(NamedNodeMap attrs)
57 // {
58 //
59 // m_attrs = attrs;
60 // m_lastIndex = m_attrs.getLength() - 1;
61 // m_dh = new DOM2Helper();
62 // }
63
64 /**
65 * Constructor AttList
66 *
67 *
68 * @param attrs List of attributes this will contain
69 * @param dh DOMHelper
70 */
71 public AttList(NamedNodeMap attrs, DOMHelper dh)
72 {
73
74 m_attrs = attrs;
75 m_lastIndex = m_attrs.getLength() - 1;
76 m_dh = dh;
77 }
78
79 /**
80 * Get the number of attribute nodes in the list
81 *
82 *
83 * @return number of attribute nodes
84 */
85 public int getLength()
86 {
87 return m_attrs.getLength();
88 }
89
90 /**
91 * Look up an attribute's Namespace URI by index.
92 *
93 * @param index The attribute index (zero-based).
94 * @return The Namespace URI, or the empty string if none
95 * is available, or null if the index is out of
96 * range.
97 */
98 public String getURI(int index)
99 {
100 String ns = m_dh.getNamespaceOfNode(((Attr) m_attrs.item(index)));
101 if(null == ns)
102 ns = "";
103 return ns;
104 }
105
106 /**
107 * Look up an attribute's local name by index.
108 *
109 * @param index The attribute index (zero-based).
110 * @return The local name, or the empty string if Namespace
111 * processing is not being performed, or null
112 * if the index is out of range.
113 */
114 public String getLocalName(int index)
115 {
116 return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index)));
117 }
118
119 /**
120 * Look up an attribute's qualified name by index.
121 *
122 *
123 * @param i The attribute index (zero-based).
124 *
125 * @return The attribute's qualified name
126 */
127 public String getQName(int i)
128 {
129 return ((Attr) m_attrs.item(i)).getName();
130 }
131
132 /**
133 * Get the attribute's node type by index
134 *
135 *
136 * @param i The attribute index (zero-based)
137 *
138 * @return the attribute's node type
139 */
140 public String getType(int i)
141 {
142 return "CDATA"; // for the moment
143 }
144
145 /**
146 * Get the attribute's node value by index
147 *
148 *
149 * @param i The attribute index (zero-based)
150 *
151 * @return the attribute's node value
152 */
153 public String getValue(int i)
154 {
155 return ((Attr) m_attrs.item(i)).getValue();
156 }
157
158 /**
159 * Get the attribute's node type by name
160 *
161 *
162 * @param name Attribute name
163 *
164 * @return the attribute's node type
165 */
166 public String getType(String name)
167 {
168 return "CDATA"; // for the moment
169 }
170
171 /**
172 * Look up an attribute's type by Namespace name.
173 *
174 * @param uri The Namespace URI, or the empty String if the
175 * name has no Namespace URI.
176 * @param localName The local name of the attribute.
177 * @return The attribute type as a string, or null if the
178 * attribute is not in the list or if Namespace
179 * processing is not being performed.
180 */
181 public String getType(String uri, String localName)
182 {
183 return "CDATA"; // for the moment
184 }
185
186 /**
187 * Look up an attribute's value by name.
188 *
189 *
190 * @param name The attribute node's name
191 *
192 * @return The attribute node's value
193 */
194 public String getValue(String name)
195 {
196 Attr attr = ((Attr) m_attrs.getNamedItem(name));
197 return (null != attr)
198 ? attr.getValue() : null;
199 }
200
201 /**
202 * Look up an attribute's value by Namespace name.
203 *
204 * @param uri The Namespace URI, or the empty String if the
205 * name has no Namespace URI.
206 * @param localName The local name of the attribute.
207 * @return The attribute value as a string, or null if the
208 * attribute is not in the list.
209 */
210 public String getValue(String uri, String localName)
211 {
212 Node a=m_attrs.getNamedItemNS(uri,localName);
213 return (a==null) ? null : a.getNodeValue();
214 }
215
216 /**
217 * Look up the index of an attribute by Namespace name.
218 *
219 * @param uri The Namespace URI, or the empty string if
220 * the name has no Namespace URI.
221 * @param localPart The attribute's local name.
222 * @return The index of the attribute, or -1 if it does not
223 * appear in the list.
224 */
225 public int getIndex(String uri, String localPart)
226 {
227 for(int i=m_attrs.getLength()-1;i>=0;--i)
228 {
229 Node a=m_attrs.item(i);
230 String u=a.getNamespaceURI();
231 if( (u==null ? uri==null : u.equals(uri))
232 &&
233 a.getLocalName().equals(localPart) )
234 return i;
235 }
236 return -1;
237 }
238
239 /**
240 * Look up the index of an attribute by raw XML 1.0 name.
241 *
242 * @param qName The qualified (prefixed) name.
243 * @return The index of the attribute, or -1 if it does not
244 * appear in the list.
245 */
246 public int getIndex(String qName)
247 {
248 for(int i=m_attrs.getLength()-1;i>=0;--i)
249 {
250 Node a=m_attrs.item(i);
251 if(a.getNodeName().equals(qName) )
252 return i;
253 }
254 return -1;
255 }
256 }