1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. import org.w3c.dom.*;
  59. import org.xml.sax.*;
  60. import org.apache.xpath.DOMHelper;
  61. import org.apache.xpath.DOM2Helper;
  62. /**
  63. * <meta name="usage" content="internal"/>
  64. * Wraps a DOM attribute list in a SAX Attributes.
  65. */
  66. public class AttList implements Attributes
  67. {
  68. /** List of attribute nodes */
  69. NamedNodeMap m_attrs;
  70. /** Index of last attribute node */
  71. int m_lastIndex;
  72. // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON!
  73. // DOM2Helper m_dh = new DOM2Helper();
  74. /** Local reference to DOMHelper */
  75. DOMHelper m_dh;
  76. // /**
  77. // * Constructor AttList
  78. // *
  79. // *
  80. // * @param attrs List of attributes this will contain
  81. // */
  82. // public AttList(NamedNodeMap attrs)
  83. // {
  84. //
  85. // m_attrs = attrs;
  86. // m_lastIndex = m_attrs.getLength() - 1;
  87. // m_dh = new DOM2Helper();
  88. // }
  89. /**
  90. * Constructor AttList
  91. *
  92. *
  93. * @param attrs List of attributes this will contain
  94. * @param dh DOMHelper
  95. */
  96. public AttList(NamedNodeMap attrs, DOMHelper dh)
  97. {
  98. m_attrs = attrs;
  99. m_lastIndex = m_attrs.getLength() - 1;
  100. m_dh = dh;
  101. }
  102. /**
  103. * Get the number of attribute nodes in the list
  104. *
  105. *
  106. * @return number of attribute nodes
  107. */
  108. public int getLength()
  109. {
  110. return m_attrs.getLength();
  111. }
  112. /**
  113. * Look up an attribute's Namespace URI by index.
  114. *
  115. * @param index The attribute index (zero-based).
  116. * @return The Namespace URI, or the empty string if none
  117. * is available, or null if the index is out of
  118. * range.
  119. */
  120. public String getURI(int index)
  121. {
  122. String ns = m_dh.getNamespaceOfNode(((Attr) m_attrs.item(index)));
  123. if(null == ns)
  124. ns = "";
  125. return ns;
  126. }
  127. /**
  128. * Look up an attribute's local name by index.
  129. *
  130. * @param index The attribute index (zero-based).
  131. * @return The local name, or the empty string if Namespace
  132. * processing is not being performed, or null
  133. * if the index is out of range.
  134. */
  135. public String getLocalName(int index)
  136. {
  137. return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index)));
  138. }
  139. /**
  140. * Look up an attribute's qualified name by index.
  141. *
  142. *
  143. * @param index The attribute index (zero-based).
  144. *
  145. * @return The attribute's qualified name
  146. */
  147. public String getQName(int i)
  148. {
  149. return ((Attr) m_attrs.item(i)).getName();
  150. }
  151. /**
  152. * Get the attribute's node type by index
  153. *
  154. *
  155. * @param index The attribute index (zero-based)
  156. *
  157. * @return the attribute's node type
  158. */
  159. public String getType(int i)
  160. {
  161. return "CDATA"; // for the moment
  162. }
  163. /**
  164. * Get the attribute's node value by index
  165. *
  166. *
  167. * @param index The attribute index (zero-based)
  168. *
  169. * @return the attribute's node value
  170. */
  171. public String getValue(int i)
  172. {
  173. return ((Attr) m_attrs.item(i)).getValue();
  174. }
  175. /**
  176. * Get the attribute's node type by name
  177. *
  178. *
  179. * @param name Attribute name
  180. *
  181. * @return the attribute's node type
  182. */
  183. public String getType(String name)
  184. {
  185. return "CDATA"; // for the moment
  186. }
  187. /**
  188. * Look up an attribute's type by Namespace name.
  189. *
  190. * @param uri The Namespace URI, or the empty String if the
  191. * name has no Namespace URI.
  192. * @param localName The local name of the attribute.
  193. * @return The attribute type as a string, or null if the
  194. * attribute is not in the list or if Namespace
  195. * processing is not being performed.
  196. */
  197. public String getType(String uri, String localName)
  198. {
  199. return "CDATA"; // for the moment
  200. }
  201. /**
  202. * Look up an attribute's value by name.
  203. *
  204. *
  205. * @param name The attribute node's name
  206. *
  207. * @return The attribute node's value
  208. */
  209. public String getValue(String name)
  210. {
  211. Attr attr = ((Attr) m_attrs.getNamedItem(name));
  212. return (null != attr)
  213. ? attr.getValue() : null;
  214. }
  215. /**
  216. * Look up an attribute's value by Namespace name.
  217. *
  218. * @param uri The Namespace URI, or the empty String if the
  219. * name has no Namespace URI.
  220. * @param localName The local name of the attribute.
  221. * @return The attribute value as a string, or null if the
  222. * attribute is not in the list.
  223. */
  224. public String getValue(String uri, String localName)
  225. {
  226. Node a=m_attrs.getNamedItemNS(uri,localName);
  227. return (a==null) ? null : a.getNodeValue();
  228. }
  229. /**
  230. * Look up the index of an attribute by Namespace name.
  231. *
  232. * @param uri The Namespace URI, or the empty string if
  233. * the name has no Namespace URI.
  234. * @param localPart The attribute's local name.
  235. * @return The index of the attribute, or -1 if it does not
  236. * appear in the list.
  237. */
  238. public int getIndex(String uri, String localPart)
  239. {
  240. for(int i=m_attrs.getLength()-1;i>=0;--i)
  241. {
  242. Node a=m_attrs.item(i);
  243. String u=a.getNamespaceURI();
  244. if( (u==null ? uri==null : u.equals(uri))
  245. &&
  246. a.getLocalName().equals(localPart) )
  247. return i;
  248. }
  249. return -1;
  250. }
  251. /**
  252. * Look up the index of an attribute by raw XML 1.0 name.
  253. *
  254. * @param qName The qualified (prefixed) name.
  255. * @return The index of the attribute, or -1 if it does not
  256. * appear in the list.
  257. */
  258. public int getIndex(String qName)
  259. {
  260. for(int i=m_attrs.getLength()-1;i>=0;--i)
  261. {
  262. Node a=m_attrs.item(i);
  263. if(a.getNodeName().equals(qName) )
  264. return i;
  265. }
  266. return -1;
  267. }
  268. }