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