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.2 2005/01/24 04:04:40 mcnamara Exp $
018: */
019: package org.apache.xml.serializer.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: *
030: * This class is a copy of the one in org.apache.xml.utils.
031: * It exists to cut the serializers dependancy on that package.
032: * A minor changes from that package are:
033: * DOMHelper reference changed to DOM2Helper, class is not "public"
034: *
035: * This class is not a public API, it is only public because it is
036: * used in org.apache.xml.serializer.
037: *
038: * @xsl.usage internal
039: */
040: public final class AttList implements Attributes {
041:
042: /** List of attribute nodes */
043: NamedNodeMap m_attrs;
044:
045: /** Index of last attribute node */
046: int m_lastIndex;
047:
048: // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON!
049: // DOM2Helper m_dh = new DOM2Helper();
050:
051: /** Local reference to DOMHelper */
052: DOM2Helper m_dh;
053:
054: // /**
055: // * Constructor AttList
056: // *
057: // *
058: // * @param attrs List of attributes this will contain
059: // */
060: // public AttList(NamedNodeMap attrs)
061: // {
062: //
063: // m_attrs = attrs;
064: // m_lastIndex = m_attrs.getLength() - 1;
065: // m_dh = new DOM2Helper();
066: // }
067:
068: /**
069: * Constructor AttList
070: *
071: *
072: * @param attrs List of attributes this will contain
073: * @param dh DOMHelper
074: */
075: public AttList(NamedNodeMap attrs, DOM2Helper dh) {
076:
077: m_attrs = attrs;
078: m_lastIndex = m_attrs.getLength() - 1;
079: m_dh = dh;
080: }
081:
082: /**
083: * Get the number of attribute nodes in the list
084: *
085: *
086: * @return number of attribute nodes
087: */
088: public int getLength() {
089: return m_attrs.getLength();
090: }
091:
092: /**
093: * Look up an attribute's Namespace URI by index.
094: *
095: * @param index The attribute index (zero-based).
096: * @return The Namespace URI, or the empty string if none
097: * is available, or null if the index is out of
098: * range.
099: */
100: public String getURI(int index) {
101: String ns = m_dh
102: .getNamespaceOfNode(((Attr) m_attrs.item(index)));
103: if (null == ns)
104: ns = "";
105: return ns;
106: }
107:
108: /**
109: * Look up an attribute's local name by index.
110: *
111: * @param index The attribute index (zero-based).
112: * @return The local name, or the empty string if Namespace
113: * processing is not being performed, or null
114: * if the index is out of range.
115: */
116: public String getLocalName(int index) {
117: return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index)));
118: }
119:
120: /**
121: * Look up an attribute's qualified name by index.
122: *
123: *
124: * @param i The attribute index (zero-based).
125: *
126: * @return The attribute's qualified name
127: */
128: public String getQName(int i) {
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: return "CDATA"; // for the moment
142: }
143:
144: /**
145: * Get the attribute's node value by index
146: *
147: *
148: * @param i The attribute index (zero-based)
149: *
150: * @return the attribute's node value
151: */
152: public String getValue(int i) {
153: return ((Attr) m_attrs.item(i)).getValue();
154: }
155:
156: /**
157: * Get the attribute's node type by name
158: *
159: *
160: * @param name Attribute name
161: *
162: * @return the attribute's node type
163: */
164: public String getType(String name) {
165: return "CDATA"; // for the moment
166: }
167:
168: /**
169: * Look up an attribute's type by Namespace name.
170: *
171: * @param uri The Namespace URI, or the empty String if the
172: * name has no Namespace URI.
173: * @param localName The local name of the attribute.
174: * @return The attribute type as a string, or null if the
175: * attribute is not in the list or if Namespace
176: * processing is not being performed.
177: */
178: public String getType(String uri, String localName) {
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 getValue(String name) {
191: Attr attr = ((Attr) m_attrs.getNamedItem(name));
192: return (null != attr) ? attr.getValue() : null;
193: }
194:
195: /**
196: * Look up an attribute's value by Namespace name.
197: *
198: * @param uri The Namespace URI, or the empty String if the
199: * name has no Namespace URI.
200: * @param localName The local name of the attribute.
201: * @return The attribute value as a string, or null if the
202: * attribute is not in the list.
203: */
204: public String getValue(String uri, String localName) {
205: Node a = m_attrs.getNamedItemNS(uri, localName);
206: return (a == null) ? null : a.getNodeValue();
207: }
208:
209: /**
210: * Look up the index of an attribute by Namespace name.
211: *
212: * @param uri The Namespace URI, or the empty string if
213: * the name has no Namespace URI.
214: * @param localPart The attribute's local name.
215: * @return The index of the attribute, or -1 if it does not
216: * appear in the list.
217: */
218: public int getIndex(String uri, String localPart) {
219: for (int i = m_attrs.getLength() - 1; i >= 0; --i) {
220: Node a = m_attrs.item(i);
221: String u = a.getNamespaceURI();
222: if ((u == null ? uri == null : u.equals(uri))
223: && a.getLocalName().equals(localPart))
224: return i;
225: }
226: return -1;
227: }
228:
229: /**
230: * Look up the index of an attribute by raw XML 1.0 name.
231: *
232: * @param qName The qualified (prefixed) name.
233: * @return The index of the attribute, or -1 if it does not
234: * appear in the list.
235: */
236: public int getIndex(String qName) {
237: for (int i = m_attrs.getLength() - 1; i >= 0; --i) {
238: Node a = m_attrs.item(i);
239: if (a.getNodeName().equals(qName))
240: return i;
241: }
242: return -1;
243: }
244: }
|