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: DTMNamedNodeMap.java,v 1.8 2004/02/16 23:06:11 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.dtm.ref;
  20. import com.sun.org.apache.xml.internal.dtm.DTM;
  21. import org.w3c.dom.DOMException;
  22. import org.w3c.dom.NamedNodeMap;
  23. import org.w3c.dom.Node;
  24. /**
  25. * DTMNamedNodeMap is a quickie (as opposed to quick) implementation of the DOM's
  26. * NamedNodeMap interface, intended to support DTMProxy's getAttributes()
  27. * call.
  28. * <p>
  29. * ***** Note: this does _not_ current attempt to cache any of the data;
  30. * if you ask for attribute 27 and then 28, you'll have to rescan the first
  31. * 27. It should probably at least keep track of the last one retrieved,
  32. * and possibly buffer the whole array.
  33. * <p>
  34. * ***** Also note that there's no fastpath for the by-name query; we search
  35. * linearly until we find it or fail to find it. Again, that could be
  36. * optimized at some cost in object creation/storage.
  37. * @xsl.usage internal
  38. */
  39. public class DTMNamedNodeMap implements NamedNodeMap
  40. {
  41. /** The DTM for this node. */
  42. DTM dtm;
  43. /** The DTM element handle. */
  44. int element;
  45. /** The number of nodes in this map. */
  46. short m_count = -1;
  47. /**
  48. * Create a getAttributes NamedNodeMap for a given DTM element node
  49. *
  50. * @param dtm The DTM Reference, must be non-null.
  51. * @param element The DTM element handle.
  52. */
  53. public DTMNamedNodeMap(DTM dtm, int element)
  54. {
  55. this.dtm = dtm;
  56. this.element = element;
  57. }
  58. /**
  59. * Return the number of Attributes on this Element
  60. *
  61. * @return The number of nodes in this map.
  62. */
  63. public int getLength()
  64. {
  65. if (m_count == -1)
  66. {
  67. short count = 0;
  68. for (int n = dtm.getFirstAttribute(element); n != -1;
  69. n = dtm.getNextAttribute(n))
  70. {
  71. ++count;
  72. }
  73. m_count = count;
  74. }
  75. return (int) m_count;
  76. }
  77. /**
  78. * Retrieves a node specified by name.
  79. * @param nameThe <code>nodeName</code> of a node to retrieve.
  80. *
  81. * @param name Name of the item being requested.
  82. * @return A <code>Node</code> (of any type) with the specified
  83. * <code>nodeName</code>, or <code>null</code> if it does not identify
  84. * any node in this map.
  85. */
  86. public Node getNamedItem(String name)
  87. {
  88. for (int n = dtm.getFirstAttribute(element); n != -1;
  89. n = dtm.getNextAttribute(n))
  90. {
  91. if (dtm.getNodeName(n).equals(name))
  92. return dtm.getNode(n);
  93. }
  94. return null;
  95. }
  96. /**
  97. * Returns the <code>index</code>th item in the map. If <code>index</code>
  98. * is greater than or equal to the number of nodes in this map, this
  99. * returns <code>null</code>.
  100. * @param indexIndex into this map.
  101. *
  102. * @param i The index of the requested item.
  103. * @return The node at the <code>index</code>th position in the map, or
  104. * <code>null</code> if that is not a valid index.
  105. */
  106. public Node item(int i)
  107. {
  108. int count = 0;
  109. for (int n = dtm.getFirstAttribute(element); n != -1;
  110. n = dtm.getNextAttribute(n))
  111. {
  112. if (count == i)
  113. return dtm.getNode(n);
  114. else
  115. ++count;
  116. }
  117. return null;
  118. }
  119. /**
  120. * Adds a node using its <code>nodeName</code> attribute. If a node with
  121. * that name is already present in this map, it is replaced by the new
  122. * one.
  123. * <br>As the <code>nodeName</code> attribute is used to derive the name
  124. * which the node must be stored under, multiple nodes of certain types
  125. * (those that have a "special" string value) cannot be stored as the
  126. * names would clash. This is seen as preferable to allowing nodes to be
  127. * aliased.
  128. * @param newNode node to store in this map. The node will later be
  129. * accessible using the value of its <code>nodeName</code> attribute.
  130. *
  131. * @return If the new <code>Node</code> replaces an existing node the
  132. * replaced <code>Node</code> is returned, otherwise <code>null</code>
  133. * is returned.
  134. * @exception DOMException
  135. * WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
  136. * different document than the one that created this map.
  137. * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
  138. * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
  139. * <code>Attr</code> that is already an attribute of another
  140. * <code>Element</code> object. The DOM user must explicitly clone
  141. * <code>Attr</code> nodes to re-use them in other elements.
  142. */
  143. public Node setNamedItem(Node newNode)
  144. {
  145. throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
  146. }
  147. /**
  148. * Removes a node specified by name. When this map contains the attributes
  149. * attached to an element, if the removed attribute is known to have a
  150. * default value, an attribute immediately appears containing the
  151. * default value as well as the corresponding namespace URI, local name,
  152. * and prefix when applicable.
  153. * @param name The <code>nodeName</code> of the node to remove.
  154. *
  155. * @return The node removed from this map if a node with such a name
  156. * exists.
  157. * @exception DOMException
  158. * NOT_FOUND_ERR: Raised if there is no node named <code>name</code> in
  159. * this map.
  160. * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
  161. */
  162. public Node removeNamedItem(String name)
  163. {
  164. throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
  165. }
  166. /**
  167. * Retrieves a node specified by local name and namespace URI. HTML-only
  168. * DOM implementations do not need to implement this method.
  169. * @param namespaceURI The namespace URI of the node to retrieve.
  170. * @param localName The local name of the node to retrieve.
  171. *
  172. * @return A <code>Node</code> (of any type) with the specified local
  173. * name and namespace URI, or <code>null</code> if they do not
  174. * identify any node in this map.
  175. * @since DOM Level 2
  176. */
  177. public Node getNamedItemNS(String namespaceURI, String localName)
  178. {
  179. throw new DTMException(DTMException.NOT_SUPPORTED_ERR);
  180. }
  181. /**
  182. * Adds a node using its <code>namespaceURI</code> and
  183. * <code>localName</code>. If a node with that namespace URI and that
  184. * local name is already present in this map, it is replaced by the new
  185. * one.
  186. * <br>HTML-only DOM implementations do not need to implement this method.
  187. * @param arg A node to store in this map. The node will later be
  188. * accessible using the value of its <code>namespaceURI</code> and
  189. * <code>localName</code> attributes.
  190. *
  191. * @return If the new <code>Node</code> replaces an existing node the
  192. * replaced <code>Node</code> is returned, otherwise <code>null</code>
  193. * is returned.
  194. * @exception DOMException
  195. * WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
  196. * different document than the one that created this map.
  197. * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
  198. * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
  199. * <code>Attr</code> that is already an attribute of another
  200. * <code>Element</code> object. The DOM user must explicitly clone
  201. * <code>Attr</code> nodes to re-use them in other elements.
  202. * @since DOM Level 2
  203. */
  204. public Node setNamedItemNS(Node arg) throws DOMException
  205. {
  206. throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
  207. }
  208. /**
  209. * Removes a node specified by local name and namespace URI. A removed
  210. * attribute may be known to have a default value when this map contains
  211. * the attributes attached to an element, as returned by the attributes
  212. * attribute of the <code>Node</code> interface. If so, an attribute
  213. * immediately appears containing the default value as well as the
  214. * corresponding namespace URI, local name, and prefix when applicable.
  215. * <br>HTML-only DOM implementations do not need to implement this method.
  216. *
  217. * @param namespaceURI The namespace URI of the node to remove.
  218. * @param localName The local name of the node to remove.
  219. *
  220. * @return The node removed from this map if a node with such a local
  221. * name and namespace URI exists.
  222. * @exception DOMException
  223. * NOT_FOUND_ERR: Raised if there is no node with the specified
  224. * <code>namespaceURI</code> and <code>localName</code> in this map.
  225. * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
  226. * @since DOM Level 2
  227. */
  228. public Node removeNamedItemNS(String namespaceURI, String localName)
  229. throws DOMException
  230. {
  231. throw new DTMException(DTMException.NO_MODIFICATION_ALLOWED_ERR);
  232. }
  233. /**
  234. * Simple implementation of DOMException.
  235. * @xsl.usage internal
  236. */
  237. public class DTMException extends org.w3c.dom.DOMException
  238. {
  239. /**
  240. * Constructs a DOM/DTM exception.
  241. *
  242. * @param code
  243. * @param message
  244. */
  245. public DTMException(short code, String message)
  246. {
  247. super(code, message);
  248. }
  249. /**
  250. * Constructor DTMException
  251. *
  252. *
  253. * @param code
  254. */
  255. public DTMException(short code)
  256. {
  257. super(code, "");
  258. }
  259. }
  260. }