KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serializer > utils > AttList


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: AttList.java,v 1.1.4.1 2005/09/08 11:03:08 suresh_emailid Exp $
18  */

19 package com.sun.org.apache.xml.internal.serializer.utils;
20
21 import org.w3c.dom.Attr JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24
25 import org.xml.sax.Attributes JavaDoc;
26
27 /**
28  * Wraps a DOM attribute list in a SAX Attributes.
29  *
30  * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
31  * It exists to cut the serializers dependancy on that package.
32  * A minor changes from that package are:
33  * DOMHelper reference changed to DOM2Helper, class is not "public"
34  *
35  * This class is not a public API, it is only public because it is
36  * used in com.sun.org.apache.xml.internal.serializer.
37  *
38  * @xsl.usage internal
39  */

40 public final class AttList implements Attributes JavaDoc
41 {
42
43   /** List of attribute nodes */
44   NamedNodeMap JavaDoc m_attrs;
45
46   /** Index of last attribute node */
47   int m_lastIndex;
48
49   // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON!
50
// DOM2Helper m_dh = new DOM2Helper();
51

52   /** Local reference to DOMHelper */
53   DOM2Helper m_dh;
54
55 // /**
56
// * Constructor AttList
57
// *
58
// *
59
// * @param attrs List of attributes this will contain
60
// */
61
// public AttList(NamedNodeMap attrs)
62
// {
63
//
64
// m_attrs = attrs;
65
// m_lastIndex = m_attrs.getLength() - 1;
66
// m_dh = new DOM2Helper();
67
// }
68

69   /**
70    * Constructor AttList
71    *
72    *
73    * @param attrs List of attributes this will contain
74    * @param dh DOMHelper
75    */

76   public AttList(NamedNodeMap JavaDoc attrs, DOM2Helper dh)
77   {
78     
79     m_attrs = attrs;
80     m_lastIndex = m_attrs.getLength() - 1;
81     m_dh = dh;
82   }
83
84   /**
85    * Get the number of attribute nodes in the list
86    *
87    *
88    * @return number of attribute nodes
89    */

90   public int getLength()
91   {
92     return m_attrs.getLength();
93   }
94
95   /**
96    * Look up an attribute's Namespace URI by index.
97    *
98    * @param index The attribute index (zero-based).
99    * @return The Namespace URI, or the empty string if none
100    * is available, or null if the index is out of
101    * range.
102    */

103   public String JavaDoc getURI(int index)
104   {
105     String JavaDoc ns = m_dh.getNamespaceOfNode(((Attr JavaDoc) m_attrs.item(index)));
106     if(null == ns)
107       ns = "";
108     return ns;
109   }
110
111   /**
112    * Look up an attribute's local name by index.
113    *
114    * @param index The attribute index (zero-based).
115    * @return The local name, or the empty string if Namespace
116    * processing is not being performed, or null
117    * if the index is out of range.
118    */

119   public String JavaDoc getLocalName(int index)
120   {
121     return m_dh.getLocalNameOfNode(((Attr JavaDoc) m_attrs.item(index)));
122   }
123
124   /**
125    * Look up an attribute's qualified name by index.
126    *
127    *
128    * @param i The attribute index (zero-based).
129    *
130    * @return The attribute's qualified name
131    */

132   public String JavaDoc getQName(int i)
133   {
134     return ((Attr JavaDoc) m_attrs.item(i)).getName();
135   }
136
137   /**
138    * Get the attribute's node type by index
139    *
140    *
141    * @param i The attribute index (zero-based)
142    *
143    * @return the attribute's node type
144    */

145   public String JavaDoc getType(int i)
146   {
147     return "CDATA"; // for the moment
148
}
149
150   /**
151    * Get the attribute's node value by index
152    *
153    *
154    * @param i The attribute index (zero-based)
155    *
156    * @return the attribute's node value
157    */

158   public String JavaDoc getValue(int i)
159   {
160     return ((Attr JavaDoc) m_attrs.item(i)).getValue();
161   }
162
163   /**
164    * Get the attribute's node type by name
165    *
166    *
167    * @param name Attribute name
168    *
169    * @return the attribute's node type
170    */

171   public String JavaDoc getType(String JavaDoc name)
172   {
173     return "CDATA"; // for the moment
174
}
175
176   /**
177    * Look up an attribute's type by Namespace name.
178    *
179    * @param uri The Namespace URI, or the empty String if the
180    * name has no Namespace URI.
181    * @param localName The local name of the attribute.
182    * @return The attribute type as a string, or null if the
183    * attribute is not in the list or if Namespace
184    * processing is not being performed.
185    */

186   public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName)
187   {
188     return "CDATA"; // for the moment
189
}
190
191   /**
192    * Look up an attribute's value by name.
193    *
194    *
195    * @param name The attribute node's name
196    *
197    * @return The attribute node's value
198    */

199   public String JavaDoc getValue(String JavaDoc name)
200   {
201     Attr JavaDoc attr = ((Attr JavaDoc) m_attrs.getNamedItem(name));
202     return (null != attr)
203           ? attr.getValue() : null;
204   }
205
206   /**
207    * Look up an attribute's value by Namespace name.
208    *
209    * @param uri The Namespace URI, or the empty String if the
210    * name has no Namespace URI.
211    * @param localName The local name of the attribute.
212    * @return The attribute value as a string, or null if the
213    * attribute is not in the list.
214    */

215   public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName)
216   {
217         Node JavaDoc a=m_attrs.getNamedItemNS(uri,localName);
218         return (a==null) ? null : a.getNodeValue();
219   }
220
221   /**
222    * Look up the index of an attribute by Namespace name.
223    *
224    * @param uri The Namespace URI, or the empty string if
225    * the name has no Namespace URI.
226    * @param localPart The attribute's local name.
227    * @return The index of the attribute, or -1 if it does not
228    * appear in the list.
229    */

230   public int getIndex(String JavaDoc uri, String JavaDoc localPart)
231   {
232     for(int i=m_attrs.getLength()-1;i>=0;--i)
233     {
234       Node JavaDoc a=m_attrs.item(i);
235       String JavaDoc u=a.getNamespaceURI();
236       if( (u==null ? uri==null : u.equals(uri))
237       &&
238       a.getLocalName().equals(localPart) )
239     return i;
240     }
241     return -1;
242   }
243
244   /**
245    * Look up the index of an attribute by raw XML 1.0 name.
246    *
247    * @param qName The qualified (prefixed) name.
248    * @return The index of the attribute, or -1 if it does not
249    * appear in the list.
250    */

251   public int getIndex(String JavaDoc qName)
252   {
253     for(int i=m_attrs.getLength()-1;i>=0;--i)
254     {
255       Node JavaDoc a=m_attrs.item(i);
256       if(a.getNodeName().equals(qName) )
257     return i;
258     }
259     return -1;
260   }
261 }
262
263
Popular Tags