DomNamedNodeMap.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* DomNamedNodeMap.java --
  2. Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.dom;
  32. import org.w3c.dom.DOMException;
  33. import org.w3c.dom.NamedNodeMap;
  34. import org.w3c.dom.Node;
  35. /**
  36. * <p> "NamedNodeMap" implementation. </p>
  37. * Used mostly to hold element attributes, but sometimes also
  38. * to list notations or entities.
  39. *
  40. * @author David Brownell
  41. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  42. */
  43. public class DomNamedNodeMap
  44. implements NamedNodeMap
  45. {
  46. final DomNode owner;
  47. final short type;
  48. DomNode first;
  49. int length;
  50. boolean readonly;
  51. // package private
  52. DomNamedNodeMap(DomNode owner, short type)
  53. {
  54. this.owner = owner;
  55. this.type = type;
  56. }
  57. /**
  58. * Exposes the internal "readonly" flag. In DOM, all NamedNodeMap
  59. * objects found in a DocumentType object are read-only (after
  60. * they are fully constructed), and those holding attributes of
  61. * a readonly element will also be readonly.
  62. */
  63. public final boolean isReadonly()
  64. {
  65. return readonly;
  66. }
  67. /**
  68. * Sets the internal "readonly" flag so the node and its
  69. * children can't be changed.
  70. */
  71. public void makeReadonly()
  72. {
  73. readonly = true;
  74. for (DomNode ctx = first; ctx != null; ctx = ctx.next)
  75. {
  76. ctx.makeReadonly();
  77. }
  78. }
  79. /**
  80. * <b>DOM L1</b>
  81. * Returns the named item from the map, or null; names are just
  82. * the nodeName property.
  83. */
  84. public Node getNamedItem(String name)
  85. {
  86. for (DomNode ctx = first; ctx != null; ctx = ctx.next)
  87. {
  88. if (ctx.getNodeName().equals(name))
  89. {
  90. return ctx;
  91. }
  92. }
  93. return null;
  94. }
  95. /**
  96. * <b>DOM L2</b>
  97. * Returns the named item from the map, or null; names are the
  98. * localName and namespaceURI properties, ignoring any prefix.
  99. */
  100. public Node getNamedItemNS(String namespaceURI, String localName)
  101. {
  102. if ("".equals(namespaceURI))
  103. {
  104. namespaceURI = null;
  105. }
  106. for (DomNode ctx = first; ctx != null; ctx = ctx.next)
  107. {
  108. String name = ctx.getLocalName();
  109. if ((localName == null && name == null) ||
  110. (localName != null && localName.equals(name)))
  111. {
  112. String uri = ctx.getNamespaceURI();
  113. if ("".equals(uri))
  114. {
  115. uri = null;
  116. }
  117. if ((namespaceURI == null && uri == null) ||
  118. (namespaceURI != null && namespaceURI.equals(uri)))
  119. {
  120. return ctx;
  121. }
  122. }
  123. }
  124. return null;
  125. }
  126. /**
  127. * <b>DOM L1</b>
  128. * Stores the named item into the map, optionally overwriting
  129. * any existing node with that name. The name used is just
  130. * the nodeName attribute.
  131. */
  132. public Node setNamedItem(Node arg)
  133. {
  134. return setNamedItem(arg, false, false);
  135. }
  136. /**
  137. * <b>DOM L2</b>
  138. * Stores the named item into the map, optionally overwriting
  139. * any existing node with that fully qualified name. The name
  140. * used incorporates the localName and namespaceURI properties,
  141. * and ignores any prefix.
  142. */
  143. public Node setNamedItemNS(Node arg)
  144. {
  145. return setNamedItem(arg, true, false);
  146. }
  147. Node setNamedItem(Node arg, boolean ns, boolean cloning)
  148. {
  149. if (readonly)
  150. {
  151. throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
  152. }
  153. DomNode node = (DomNode) arg;
  154. if (!cloning && node.owner != owner.owner)
  155. {
  156. throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR);
  157. }
  158. if (node.nodeType != type)
  159. {
  160. throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR);
  161. }
  162. if (node.nodeType == Node.ATTRIBUTE_NODE)
  163. {
  164. DomNode element = node.parent;
  165. if (element != null && element != owner)
  166. {
  167. throw new DomDOMException(DOMException.INUSE_ATTRIBUTE_ERR);
  168. }
  169. node.parent = owner;
  170. node.depth = owner.depth + 1;
  171. }
  172. String nodeName = node.getNodeName();
  173. String localName = ns ? node.getLocalName() : null;
  174. String namespaceURI = ns ? node.getNamespaceURI() : null;
  175. if ("".equals(namespaceURI))
  176. {
  177. namespaceURI = null;
  178. }
  179. // maybe attribute ADDITION events (?)
  180. DomNode last = null;
  181. for (DomNode ctx = first; ctx != null; ctx = ctx.next)
  182. {
  183. boolean test = false;
  184. if (ns)
  185. {
  186. String tln = ctx.getLocalName();
  187. if (tln == null)
  188. {
  189. tln = ctx.getNodeName();
  190. }
  191. if (tln.equals(localName))
  192. {
  193. String tu = ctx.getNamespaceURI();
  194. if ((tu == null && namespaceURI == null) ||
  195. (tu != null && tu.equals(namespaceURI)))
  196. {
  197. test = true;
  198. }
  199. }
  200. }
  201. else
  202. {
  203. test = ctx.getNodeName().equals(nodeName);
  204. }
  205. if (test)
  206. {
  207. // replace
  208. node.previous = ctx.previous;
  209. node.next = ctx.next;
  210. if (ctx.previous != null)
  211. {
  212. ctx.previous.next = node;
  213. }
  214. if (ctx.next != null)
  215. {
  216. ctx.next.previous = node;
  217. }
  218. if (first == ctx)
  219. {
  220. first = node;
  221. }
  222. reparent(node, nodeName, ctx.index);
  223. ctx.parent = null;
  224. ctx.next = null;
  225. ctx.previous = null;
  226. ctx.setDepth(0);
  227. ctx.index = 0;
  228. return ctx;
  229. }
  230. last = ctx;
  231. }
  232. // append
  233. if (last != null)
  234. {
  235. last.next = node;
  236. node.previous = last;
  237. }
  238. else
  239. {
  240. first = node;
  241. }
  242. length++;
  243. reparent(node, nodeName, 0);
  244. return null;
  245. }
  246. void reparent(DomNode node, String nodeName, int i)
  247. {
  248. node.parent = owner;
  249. node.setDepth(owner.depth + 1);
  250. // index renumbering
  251. for (DomNode ctx = node; ctx != null; ctx = ctx.next)
  252. {
  253. ctx.index = i++;
  254. }
  255. // cache xml:space
  256. boolean xmlSpace = "xml:space".equals(nodeName);
  257. if (xmlSpace && owner instanceof DomElement)
  258. {
  259. ((DomElement) owner).xmlSpace = node.getNodeValue();
  260. }
  261. }
  262. /**
  263. * <b>DOM L1</b>
  264. * Removes the named item from the map, or reports an exception;
  265. * names are just the nodeName property.
  266. */
  267. public Node removeNamedItem(String name)
  268. {
  269. return removeNamedItem(null, name, false);
  270. }
  271. /**
  272. * <b>DOM L2</b>
  273. * Removes the named item from the map, or reports an exception;
  274. * names are the localName and namespaceURI properties.
  275. */
  276. public Node removeNamedItemNS(String namespaceURI, String localName)
  277. {
  278. return removeNamedItem(namespaceURI, localName, true);
  279. }
  280. Node removeNamedItem(String uri, String name, boolean ns)
  281. {
  282. if (readonly)
  283. {
  284. throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
  285. }
  286. // report attribute REMOVAL event?
  287. for (DomNode ctx = first; ctx != null; ctx = ctx.next)
  288. {
  289. boolean test = false;
  290. String nodeName = ctx.getNodeName();
  291. if (ns)
  292. {
  293. String tln = ctx.getLocalName();
  294. if (name != null && name.equals(tln))
  295. {
  296. String tu = ctx.getNamespaceURI();
  297. if ((tu == null && uri == null) ||
  298. (tu != null && tu.equals(uri)))
  299. {
  300. test = true;
  301. }
  302. }
  303. }
  304. else
  305. {
  306. test = nodeName.equals(name);
  307. }
  308. if (test)
  309. {
  310. // uncache xml:space
  311. boolean xmlSpace = "xml:space".equals(nodeName);
  312. if (xmlSpace && owner instanceof DomElement)
  313. {
  314. ((DomElement) owner).xmlSpace = "";
  315. }
  316. // is this a default attribute?
  317. if (ctx.nodeType == Node.ATTRIBUTE_NODE)
  318. {
  319. String def = getDefaultValue(ctx.getNodeName());
  320. if (def != null)
  321. {
  322. ctx.setNodeValue(def);
  323. ((DomAttr) ctx).setSpecified(false);
  324. return null;
  325. }
  326. }
  327. // remove
  328. if (ctx == first)
  329. {
  330. first = ctx.next;
  331. }
  332. if (ctx.previous != null)
  333. {
  334. ctx.previous.next = ctx.next;
  335. }
  336. if (ctx.next != null)
  337. {
  338. ctx.next.previous = ctx.previous;
  339. }
  340. length--;
  341. ctx.previous = null;
  342. ctx.next = null;
  343. ctx.parent = null;
  344. ctx.setDepth(0);
  345. ctx.index = 0;
  346. return ctx;
  347. }
  348. }
  349. throw new DomDOMException(DOMException.NOT_FOUND_ERR);
  350. }
  351. String getDefaultValue(String name)
  352. {
  353. DomDoctype doctype = (DomDoctype) owner.owner.getDoctype();
  354. if (doctype == null)
  355. {
  356. return null;
  357. }
  358. DTDAttributeTypeInfo info =
  359. doctype.getAttributeTypeInfo(owner.getNodeName(), name);
  360. if (info == null)
  361. {
  362. return null;
  363. }
  364. return info.value;
  365. }
  366. /**
  367. * <b>DOM L1</b>
  368. * Returns the indexed item from the map, or null.
  369. */
  370. public Node item(int index)
  371. {
  372. DomNode ctx = first;
  373. int count = 0;
  374. while (ctx != null && count < index)
  375. {
  376. ctx = ctx.next;
  377. count++;
  378. }
  379. return ctx;
  380. }
  381. /**
  382. * <b>DOM L1</b>
  383. * Returns the length of the map.
  384. */
  385. public int getLength()
  386. {
  387. return length;
  388. }
  389. }