KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > 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.12 2004/02/17 04:21:14 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.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  * @xsl.usage internal
30  */

31 public class AttList implements Attributes JavaDoc
32 {
33
34   /** List of attribute nodes */
35   NamedNodeMap JavaDoc m_attrs;
36
37   /** Index of last attribute node */
38   int m_lastIndex;
39
40   // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON!
41
// DOM2Helper m_dh = new DOM2Helper();
42

43   /** Local reference to DOMHelper */
44   DOMHelper m_dh;
45
46 // /**
47
// * Constructor AttList
48
// *
49
// *
50
// * @param attrs List of attributes this will contain
51
// */
52
// public AttList(NamedNodeMap attrs)
53
// {
54
//
55
// m_attrs = attrs;
56
// m_lastIndex = m_attrs.getLength() - 1;
57
// m_dh = new DOM2Helper();
58
// }
59

60   /**
61    * Constructor AttList
62    *
63    *
64    * @param attrs List of attributes this will contain
65    * @param dh DOMHelper
66    */

67   public AttList(NamedNodeMap JavaDoc attrs, DOMHelper dh)
68   {
69
70     m_attrs = attrs;
71     m_lastIndex = m_attrs.getLength() - 1;
72     m_dh = dh;
73   }
74
75   /**
76    * Get the number of attribute nodes in the list
77    *
78    *
79    * @return number of attribute nodes
80    */

81   public int getLength()
82   {
83     return m_attrs.getLength();
84   }
85
86   /**
87    * Look up an attribute's Namespace URI by index.
88    *
89    * @param index The attribute index (zero-based).
90    * @return The Namespace URI, or the empty string if none
91    * is available, or null if the index is out of
92    * range.
93    */

94   public String JavaDoc getURI(int index)
95   {
96     String JavaDoc ns = m_dh.getNamespaceOfNode(((Attr JavaDoc) m_attrs.item(index)));
97     if(null == ns)
98       ns = "";
99     return ns;
100   }
101
102   /**
103    * Look up an attribute's local name by index.
104    *
105    * @param index The attribute index (zero-based).
106    * @return The local name, or the empty string if Namespace
107    * processing is not being performed, or null
108    * if the index is out of range.
109    */

110   public String JavaDoc getLocalName(int index)
111   {
112     return m_dh.getLocalNameOfNode(((Attr JavaDoc) m_attrs.item(index)));
113   }
114
115   /**
116    * Look up an attribute's qualified name by index.
117    *
118    *
119    * @param index The attribute index (zero-based).
120    *
121    * @return The attribute's qualified name
122    */

123   public String JavaDoc getQName(int i)
124   {
125     return ((Attr JavaDoc) m_attrs.item(i)).getName();
126   }
127
128   /**
129    * Get the attribute's node type by index
130    *
131    *
132    * @param index The attribute index (zero-based)
133    *
134    * @return the attribute's node type
135    */

136   public String JavaDoc getType(int i)
137   {
138     return "CDATA"; // for the moment
139
}
140
141   /**
142    * Get the attribute's node value by index
143    *
144    *
145    * @param index The attribute index (zero-based)
146    *
147    * @return the attribute's node value
148    */

149   public String JavaDoc getValue(int i)
150   {
151     return ((Attr JavaDoc) m_attrs.item(i)).getValue();
152   }
153
154   /**
155    * Get the attribute's node type by name
156    *
157    *
158    * @param name Attribute name
159    *
160    * @return the attribute's node type
161    */

162   public String JavaDoc getType(String JavaDoc name)
163   {
164     return "CDATA"; // for the moment
165
}
166
167   /**
168    * Look up an attribute's type by Namespace name.
169    *
170    * @param uri The Namespace URI, or the empty String if the
171    * name has no Namespace URI.
172    * @param localName The local name of the attribute.
173    * @return The attribute type as a string, or null if the
174    * attribute is not in the list or if Namespace
175    * processing is not being performed.
176    */

177   public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName)
178   {
179     return "CDATA"; // for the moment
180
}
181
182   /**
183    * Look up an attribute's value by name.
184    *
185    *
186    * @param name The attribute node's name
187    *
188    * @return The attribute node's value
189    */

190   public String JavaDoc getValue(String JavaDoc name)
191   {
192     Attr JavaDoc attr = ((Attr JavaDoc) m_attrs.getNamedItem(name));
193     return (null != attr)
194           ? attr.getValue() : null;
195   }
196
197   /**
198    * Look up an attribute's value by Namespace name.
199    *
200    * @param uri The Namespace URI, or the empty String if the
201    * name has no Namespace URI.
202    * @param localName The local name of the attribute.
203    * @return The attribute value as a string, or null if the
204    * attribute is not in the list.
205    */

206   public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName)
207   {
208         Node JavaDoc a=m_attrs.getNamedItemNS(uri,localName);
209         return (a==null) ? null : a.getNodeValue();
210   }
211
212   /**
213    * Look up the index of an attribute by Namespace name.
214    *
215    * @param uri The Namespace URI, or the empty string if
216    * the name has no Namespace URI.
217    * @param localPart The attribute's local name.
218    * @return The index of the attribute, or -1 if it does not
219    * appear in the list.
220    */

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

242   public int getIndex(String JavaDoc qName)
243   {
244     for(int i=m_attrs.getLength()-1;i>=0;--i)
245     {
246       Node JavaDoc a=m_attrs.item(i);
247       if(a.getNodeName().equals(qName) )
248     return i;
249     }
250     return -1;
251   }
252 }
253
254
Popular Tags