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. import org.w3c.dom.Attr;
  21. import org.w3c.dom.NamedNodeMap;
  22. import org.w3c.dom.Node;
  23. import org.xml.sax.Attributes;
  24. /**
  25. * Wraps a DOM attribute list in a SAX Attributes.
  26. * @xsl.usage internal
  27. */
  28. public class AttList implements Attributes
  29. {
  30. /** List of attribute nodes */
  31. NamedNodeMap m_attrs;
  32. /** Index of last attribute node */
  33. int m_lastIndex;
  34. // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON!
  35. // DOM2Helper m_dh = new DOM2Helper();
  36. /** Local reference to DOMHelper */
  37. DOMHelper m_dh;
  38. // /**
  39. // * Constructor AttList
  40. // *
  41. // *
  42. // * @param attrs List of attributes this will contain
  43. // */
  44. // public AttList(NamedNodeMap attrs)
  45. // {
  46. //
  47. // m_attrs = attrs;
  48. // m_lastIndex = m_attrs.getLength() - 1;
  49. // m_dh = new DOM2Helper();
  50. // }
  51. /**
  52. * Constructor AttList
  53. *
  54. *
  55. * @param attrs List of attributes this will contain
  56. * @param dh DOMHelper
  57. */
  58. public AttList(NamedNodeMap attrs, DOMHelper dh)
  59. {
  60. m_attrs = attrs;
  61. m_lastIndex = m_attrs.getLength() - 1;
  62. m_dh = dh;
  63. }
  64. /**
  65. * Get the number of attribute nodes in the list
  66. *
  67. *
  68. * @return number of attribute nodes
  69. */
  70. public int getLength()
  71. {
  72. return m_attrs.getLength();
  73. }
  74. /**
  75. * Look up an attribute's Namespace URI by index.
  76. *
  77. * @param index The attribute index (zero-based).
  78. * @return The Namespace URI, or the empty string if none
  79. * is available, or null if the index is out of
  80. * range.
  81. */
  82. public String getURI(int index)
  83. {
  84. String ns = m_dh.getNamespaceOfNode(((Attr) m_attrs.item(index)));
  85. if(null == ns)
  86. ns = "";
  87. return ns;
  88. }
  89. /**
  90. * Look up an attribute's local name by index.
  91. *
  92. * @param index The attribute index (zero-based).
  93. * @return The local name, or the empty string if Namespace
  94. * processing is not being performed, or null
  95. * if the index is out of range.
  96. */
  97. public String getLocalName(int index)
  98. {
  99. return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index)));
  100. }
  101. /**
  102. * Look up an attribute's qualified name by index.
  103. *
  104. *
  105. * @param index The attribute index (zero-based).
  106. *
  107. * @return The attribute's qualified name
  108. */
  109. public String getQName(int i)
  110. {
  111. return ((Attr) m_attrs.item(i)).getName();
  112. }
  113. /**
  114. * Get the attribute's node type by index
  115. *
  116. *
  117. * @param index The attribute index (zero-based)
  118. *
  119. * @return the attribute's node type
  120. */
  121. public String getType(int i)
  122. {
  123. return "CDATA"; // for the moment
  124. }
  125. /**
  126. * Get the attribute's node value by index
  127. *
  128. *
  129. * @param index The attribute index (zero-based)
  130. *
  131. * @return the attribute's node value
  132. */
  133. public String getValue(int i)
  134. {
  135. return ((Attr) m_attrs.item(i)).getValue();
  136. }
  137. /**
  138. * Get the attribute's node type by name
  139. *
  140. *
  141. * @param name Attribute name
  142. *
  143. * @return the attribute's node type
  144. */
  145. public String getType(String name)
  146. {
  147. return "CDATA"; // for the moment
  148. }
  149. /**
  150. * Look up an attribute's type by Namespace name.
  151. *
  152. * @param uri The Namespace URI, or the empty String if the
  153. * name has no Namespace URI.
  154. * @param localName The local name of the attribute.
  155. * @return The attribute type as a string, or null if the
  156. * attribute is not in the list or if Namespace
  157. * processing is not being performed.
  158. */
  159. public String getType(String uri, String localName)
  160. {
  161. return "CDATA"; // for the moment
  162. }
  163. /**
  164. * Look up an attribute's value by name.
  165. *
  166. *
  167. * @param name The attribute node's name
  168. *
  169. * @return The attribute node's value
  170. */
  171. public String getValue(String name)
  172. {
  173. Attr attr = ((Attr) m_attrs.getNamedItem(name));
  174. return (null != attr)
  175. ? attr.getValue() : null;
  176. }
  177. /**
  178. * Look up an attribute's value by Namespace name.
  179. *
  180. * @param uri The Namespace URI, or the empty String if the
  181. * name has no Namespace URI.
  182. * @param localName The local name of the attribute.
  183. * @return The attribute value as a string, or null if the
  184. * attribute is not in the list.
  185. */
  186. public String getValue(String uri, String localName)
  187. {
  188. Node a=m_attrs.getNamedItemNS(uri,localName);
  189. return (a==null) ? null : a.getNodeValue();
  190. }
  191. /**
  192. * Look up the index of an attribute by Namespace name.
  193. *
  194. * @param uri The Namespace URI, or the empty string if
  195. * the name has no Namespace URI.
  196. * @param localPart The attribute's local name.
  197. * @return The index of the attribute, or -1 if it does not
  198. * appear in the list.
  199. */
  200. public int getIndex(String uri, String localPart)
  201. {
  202. for(int i=m_attrs.getLength()-1;i>=0;--i)
  203. {
  204. Node a=m_attrs.item(i);
  205. String u=a.getNamespaceURI();
  206. if( (u==null ? uri==null : u.equals(uri))
  207. &&
  208. a.getLocalName().equals(localPart) )
  209. return i;
  210. }
  211. return -1;
  212. }
  213. /**
  214. * Look up the index of an attribute by raw XML 1.0 name.
  215. *
  216. * @param qName The qualified (prefixed) name.
  217. * @return The index of the attribute, or -1 if it does not
  218. * appear in the list.
  219. */
  220. public int getIndex(String qName)
  221. {
  222. for(int i=m_attrs.getLength()-1;i>=0;--i)
  223. {
  224. Node a=m_attrs.item(i);
  225. if(a.getNodeName().equals(qName) )
  226. return i;
  227. }
  228. return -1;
  229. }
  230. }