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