001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: AttList.java,v 1.13 2005/01/23 00:52:41 mcnamara Exp $
018: */
019: package org.apache.xml.utils;
020:
021: import org.w3c.dom.Attr;
022: import org.w3c.dom.NamedNodeMap;
023: import org.w3c.dom.Node;
024:
025: import org.xml.sax.Attributes;
026:
027: /**
028: * Wraps a DOM attribute list in a SAX Attributes.
029: * @xsl.usage internal
030: */
031: public class AttList implements Attributes {
032:
033: /** List of attribute nodes */
034: NamedNodeMap m_attrs;
035:
036: /** Index of last attribute node */
037: int m_lastIndex;
038:
039: // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON!
040: // DOM2Helper m_dh = new DOM2Helper();
041:
042: /** Local reference to DOMHelper */
043: DOMHelper m_dh;
044:
045: // /**
046: // * Constructor AttList
047: // *
048: // *
049: // * @param attrs List of attributes this will contain
050: // */
051: // public AttList(NamedNodeMap attrs)
052: // {
053: //
054: // m_attrs = attrs;
055: // m_lastIndex = m_attrs.getLength() - 1;
056: // m_dh = new DOM2Helper();
057: // }
058:
059: /**
060: * Constructor AttList
061: *
062: *
063: * @param attrs List of attributes this will contain
064: * @param dh DOMHelper
065: */
066: public AttList(NamedNodeMap attrs, DOMHelper dh) {
067:
068: m_attrs = attrs;
069: m_lastIndex = m_attrs.getLength() - 1;
070: m_dh = dh;
071: }
072:
073: /**
074: * Get the number of attribute nodes in the list
075: *
076: *
077: * @return number of attribute nodes
078: */
079: public int getLength() {
080: return m_attrs.getLength();
081: }
082:
083: /**
084: * Look up an attribute's Namespace URI by index.
085: *
086: * @param index The attribute index (zero-based).
087: * @return The Namespace URI, or the empty string if none
088: * is available, or null if the index is out of
089: * range.
090: */
091: public String getURI(int index) {
092: String ns = m_dh
093: .getNamespaceOfNode(((Attr) m_attrs.item(index)));
094: if (null == ns)
095: ns = "";
096: return ns;
097: }
098:
099: /**
100: * Look up an attribute's local name by index.
101: *
102: * @param index The attribute index (zero-based).
103: * @return The local name, or the empty string if Namespace
104: * processing is not being performed, or null
105: * if the index is out of range.
106: */
107: public String getLocalName(int index) {
108: return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index)));
109: }
110:
111: /**
112: * Look up an attribute's qualified name by index.
113: *
114: *
115: * @param i The attribute index (zero-based).
116: *
117: * @return The attribute's qualified name
118: */
119: public String getQName(int i) {
120: return ((Attr) m_attrs.item(i)).getName();
121: }
122:
123: /**
124: * Get the attribute's node type by index
125: *
126: *
127: * @param i The attribute index (zero-based)
128: *
129: * @return the attribute's node type
130: */
131: public String getType(int i) {
132: return "CDATA"; // for the moment
133: }
134:
135: /**
136: * Get the attribute's node value by index
137: *
138: *
139: * @param i The attribute index (zero-based)
140: *
141: * @return the attribute's node value
142: */
143: public String getValue(int i) {
144: return ((Attr) m_attrs.item(i)).getValue();
145: }
146:
147: /**
148: * Get the attribute's node type by name
149: *
150: *
151: * @param name Attribute name
152: *
153: * @return the attribute's node type
154: */
155: public String getType(String name) {
156: return "CDATA"; // for the moment
157: }
158:
159: /**
160: * Look up an attribute's type by Namespace name.
161: *
162: * @param uri The Namespace URI, or the empty String if the
163: * name has no Namespace URI.
164: * @param localName The local name of the attribute.
165: * @return The attribute type as a string, or null if the
166: * attribute is not in the list or if Namespace
167: * processing is not being performed.
168: */
169: public String getType(String uri, String localName) {
170: return "CDATA"; // for the moment
171: }
172:
173: /**
174: * Look up an attribute's value by name.
175: *
176: *
177: * @param name The attribute node's name
178: *
179: * @return The attribute node's value
180: */
181: public String getValue(String name) {
182: Attr attr = ((Attr) m_attrs.getNamedItem(name));
183: return (null != attr) ? attr.getValue() : null;
184: }
185:
186: /**
187: * Look up an attribute's value by Namespace name.
188: *
189: * @param uri The Namespace URI, or the empty String if the
190: * name has no Namespace URI.
191: * @param localName The local name of the attribute.
192: * @return The attribute value as a string, or null if the
193: * attribute is not in the list.
194: */
195: public String getValue(String uri, String localName) {
196: Node a = m_attrs.getNamedItemNS(uri, localName);
197: return (a == null) ? null : a.getNodeValue();
198: }
199:
200: /**
201: * Look up the index of an attribute by Namespace name.
202: *
203: * @param uri The Namespace URI, or the empty string if
204: * the name has no Namespace URI.
205: * @param localPart The attribute's local name.
206: * @return The index of the attribute, or -1 if it does not
207: * appear in the list.
208: */
209: public int getIndex(String uri, String localPart) {
210: for (int i = m_attrs.getLength() - 1; i >= 0; --i) {
211: Node a = m_attrs.item(i);
212: String u = a.getNamespaceURI();
213: if ((u == null ? uri == null : u.equals(uri))
214: && a.getLocalName().equals(localPart))
215: return i;
216: }
217: return -1;
218: }
219:
220: /**
221: * Look up the index of an attribute by raw XML 1.0 name.
222: *
223: * @param qName The qualified (prefixed) name.
224: * @return The index of the attribute, or -1 if it does not
225: * appear in the list.
226: */
227: public int getIndex(String qName) {
228: for (int i = m_attrs.getLength() - 1; i >= 0; --i) {
229: Node a = m_attrs.item(i);
230: if (a.getNodeName().equals(qName))
231: return i;
232: }
233: return -1;
234: }
235: }
|