DomElement.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /* DomElement.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 java.util.HashSet;
  33. import java.util.Set;
  34. import javax.xml.XMLConstants;
  35. import org.w3c.dom.Attr;
  36. import org.w3c.dom.DOMException;
  37. import org.w3c.dom.Element;
  38. import org.w3c.dom.NamedNodeMap;
  39. import org.w3c.dom.Node;
  40. import org.w3c.dom.TypeInfo;
  41. /**
  42. * <p> "Element" implementation.
  43. *
  44. * @author David Brownell
  45. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  46. */
  47. public class DomElement
  48. extends DomNsNode
  49. implements Element
  50. {
  51. /**
  52. * User-defined ID attributes.
  53. * Used by DomAttr.isId and DomDocument.getElementById
  54. */
  55. Set userIdAttrs;
  56. // Attributes are VERY expensive in DOM, and not just for
  57. // this implementation. Avoid creating them.
  58. private DomNamedNodeMap attributes;
  59. // xml:space cache
  60. String xmlSpace = "";
  61. /**
  62. * Constructs an Element node associated with the specified document.
  63. *
  64. * <p>This constructor should only be invoked by a Document as part
  65. * of its createElement functionality, or through a subclass which is
  66. * similarly used in a "Sub-DOM" style layer.
  67. *
  68. * @param owner The document with which this node is associated
  69. * @param namespaceURI Combined with the local part of the name,
  70. * this is used to uniquely identify a type of element
  71. * @param name Name of this element, which may include a prefix
  72. */
  73. protected DomElement(DomDocument owner, String namespaceURI, String name)
  74. {
  75. super(ELEMENT_NODE, owner, namespaceURI, name);
  76. }
  77. /**
  78. * <p>
  79. * Constructs an Element node associated with the specified document.
  80. * This constructor should only be invoked by a Document as part
  81. * of its createElement functionality, or through a subclass which is
  82. * similarly used in a "Sub-DOM" style layer.
  83. * </p>
  84. * <p>
  85. * With this constructor, the prefix and local part are given explicitly
  86. * rather than being computed. This allows them to be explicitly set to
  87. * {@code null} as required by {@link Document#createElement(String)}.
  88. * </p>
  89. *
  90. * @param owner The document with which this node is associated
  91. * @param namespaceURI Combined with the local part of the name,
  92. * this is used to uniquely identify a type of element
  93. * @param name Name of this element, which may include a prefix
  94. * @param prefix the namespace prefix of the name. May be {@code null}.
  95. * @param localName the local part of the name. May be {@code null}.
  96. */
  97. protected DomElement(DomDocument owner, String namespaceURI, String name,
  98. String prefix, String localName)
  99. {
  100. super(ELEMENT_NODE, owner, namespaceURI, name, prefix, localName);
  101. }
  102. /**
  103. * <b>DOM L1</b>
  104. * Returns the element's attributes
  105. */
  106. public NamedNodeMap getAttributes()
  107. {
  108. if (attributes == null)
  109. {
  110. attributes = new DomNamedNodeMap(this, Node.ATTRIBUTE_NODE);
  111. }
  112. return attributes;
  113. }
  114. /**
  115. * <b>DOM L2></b>
  116. * Returns true iff this is an element node with attributes.
  117. */
  118. public boolean hasAttributes()
  119. {
  120. return attributes != null && attributes.length != 0;
  121. }
  122. /**
  123. * Shallow clone of the element, except that associated
  124. * attributes are (deep) cloned.
  125. */
  126. public Object clone()
  127. {
  128. DomElement node = (DomElement) super.clone();
  129. if (attributes != null)
  130. {
  131. node.attributes = new DomNamedNodeMap(node, Node.ATTRIBUTE_NODE);
  132. for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
  133. {
  134. node.attributes.setNamedItem(ctx.cloneNode(true), true, true);
  135. }
  136. }
  137. return node;
  138. }
  139. void setOwner(DomDocument doc)
  140. {
  141. if (attributes != null)
  142. {
  143. for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
  144. {
  145. ctx.setOwner(doc);
  146. }
  147. }
  148. super.setOwner(doc);
  149. }
  150. /**
  151. * Marks this element, its children, and its associated attributes as
  152. * readonly.
  153. */
  154. public void makeReadonly()
  155. {
  156. super.makeReadonly();
  157. if (attributes != null)
  158. {
  159. attributes.makeReadonly();
  160. }
  161. }
  162. /**
  163. * <b>DOM L1</b>
  164. * Returns the element name (same as getNodeName).
  165. */
  166. final public String getTagName()
  167. {
  168. return getNodeName();
  169. }
  170. /**
  171. * <b>DOM L1</b>
  172. * Returns the value of the specified attribute, or an
  173. * empty string.
  174. */
  175. public String getAttribute(String name)
  176. {
  177. if ("xml:space" == name) // NB only works on interned string
  178. {
  179. // Use cached value
  180. return xmlSpace;
  181. }
  182. Attr attr = getAttributeNode(name);
  183. return (attr == null) ? "" : attr.getValue();
  184. }
  185. /**
  186. * <b>DOM L2</b>
  187. * Returns true if the element has an attribute with the
  188. * specified name (specified or DTD defaulted).
  189. */
  190. public boolean hasAttribute(String name)
  191. {
  192. return getAttributeNode(name) != null;
  193. }
  194. /**
  195. * <b>DOM L2</b>
  196. * Returns true if the element has an attribute with the
  197. * specified name (specified or DTD defaulted).
  198. */
  199. public boolean hasAttributeNS(String namespaceURI, String local)
  200. {
  201. return getAttributeNodeNS(namespaceURI, local) != null;
  202. }
  203. /**
  204. * <b>DOM L2</b>
  205. * Returns the value of the specified attribute, or an
  206. * empty string.
  207. */
  208. public String getAttributeNS(String namespaceURI, String local)
  209. {
  210. Attr attr = getAttributeNodeNS(namespaceURI, local);
  211. return (attr == null) ? "" : attr.getValue();
  212. }
  213. /**
  214. * <b>DOM L1</b>
  215. * Returns the appropriate attribute node; the name is the
  216. * nodeName property of the attribute.
  217. */
  218. public Attr getAttributeNode(String name)
  219. {
  220. return (attributes == null) ? null :
  221. (Attr) attributes.getNamedItem(name);
  222. }
  223. /**
  224. * <b>DOM L2</b>
  225. * Returns the appropriate attribute node; the name combines
  226. * the namespace name and the local part.
  227. */
  228. public Attr getAttributeNodeNS(String namespace, String localPart)
  229. {
  230. return (attributes == null) ? null :
  231. (Attr) attributes.getNamedItemNS(namespace, localPart);
  232. }
  233. /**
  234. * <b>DOM L1</b>
  235. * Modifies an existing attribute to have the specified value,
  236. * or creates a new one with that value. The name used is the
  237. * nodeName value.
  238. */
  239. public void setAttribute(String name, String value)
  240. {
  241. Attr attr = getAttributeNode(name);
  242. if (attr != null)
  243. {
  244. attr.setNodeValue(value);
  245. ((DomAttr) attr).setSpecified(true);
  246. return;
  247. }
  248. attr = owner.createAttribute(name);
  249. attr.setNodeValue(value);
  250. setAttributeNode(attr);
  251. }
  252. /**
  253. * <b>DOM L2</b>
  254. * Modifies an existing attribute to have the specified value,
  255. * or creates a new one with that value.
  256. */
  257. public void setAttributeNS(String uri, String aname, String value)
  258. {
  259. if (("xmlns".equals (aname) || aname.startsWith ("xmlns:"))
  260. && !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals (uri))
  261. {
  262. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  263. "setting xmlns attribute to illegal value", this, 0);
  264. }
  265. Attr attr = getAttributeNodeNS(uri, aname);
  266. if (attr != null)
  267. {
  268. attr.setNodeValue(value);
  269. return;
  270. }
  271. attr = owner.createAttributeNS(uri, aname);
  272. attr.setNodeValue(value);
  273. setAttributeNodeNS(attr);
  274. }
  275. /**
  276. * <b>DOM L1</b>
  277. * Stores the specified attribute, optionally overwriting any
  278. * existing one with that name.
  279. */
  280. public Attr setAttributeNode(Attr attr)
  281. {
  282. return (Attr) getAttributes().setNamedItem(attr);
  283. }
  284. /**
  285. * <b>DOM L2</b>
  286. * Stores the specified attribute, optionally overwriting any
  287. * existing one with that name.
  288. */
  289. public Attr setAttributeNodeNS(Attr attr)
  290. {
  291. return (Attr) getAttributes().setNamedItemNS(attr);
  292. }
  293. /**
  294. * <b>DOM L1</b>
  295. * Removes the appropriate attribute node.
  296. * If there is no such node, this is (bizarrely enough) a NOP so you
  297. * won't see exceptions if your code deletes non-existent attributes.
  298. *
  299. * <p>Note that since there is no portable way for DOM to record
  300. * DTD information, default values for attributes will never be
  301. * provided automatically.
  302. */
  303. public void removeAttribute(String name)
  304. {
  305. if (attributes == null)
  306. {
  307. return;
  308. }
  309. try
  310. {
  311. attributes.removeNamedItem(name);
  312. }
  313. catch (DomDOMException e)
  314. {
  315. if (e.code != DOMException.NOT_FOUND_ERR)
  316. {
  317. throw e;
  318. }
  319. }
  320. }
  321. /**
  322. * <b>DOM L1</b>
  323. * Removes the appropriate attribute node; the name is the
  324. * nodeName property of the attribute.
  325. *
  326. * <p>Note that since there is no portable way for DOM to record
  327. * DTD information, default values for attributes will never be
  328. * provided automatically.
  329. */
  330. public Attr removeAttributeNode(Attr node)
  331. {
  332. if (attributes == null)
  333. {
  334. throw new DomDOMException(DOMException.NOT_FOUND_ERR, null, node, 0);
  335. }
  336. return (Attr) attributes.removeNamedItem(node.getNodeName());
  337. }
  338. /**
  339. * <b>DOM L2</b>
  340. * Removes the appropriate attribute node; the name combines
  341. * the namespace name and the local part.
  342. *
  343. * <p>Note that since there is no portable way for DOM to record
  344. * DTD information, default values for attributes will never be
  345. * provided automatically.
  346. */
  347. public void removeAttributeNS(String namespace, String localPart)
  348. {
  349. if (attributes == null)
  350. {
  351. throw new DomDOMException(DOMException.NOT_FOUND_ERR, localPart, null, 0);
  352. }
  353. attributes.removeNamedItemNS (namespace, localPart);
  354. }
  355. // DOM Level 3 methods
  356. public String lookupPrefix(String namespaceURI)
  357. {
  358. if (namespaceURI == null)
  359. {
  360. return null;
  361. }
  362. String namespace = getNamespaceURI();
  363. if (namespace != null && namespace.equals(namespaceURI))
  364. {
  365. return getPrefix();
  366. }
  367. if (attributes != null)
  368. {
  369. for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
  370. {
  371. if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
  372. .equals(ctx.getNamespaceURI()))
  373. {
  374. String value = ctx.getNodeValue();
  375. if (value.equals(namespaceURI))
  376. {
  377. return ctx.getLocalName();
  378. }
  379. }
  380. }
  381. }
  382. return super.lookupPrefix(namespaceURI);
  383. }
  384. public boolean isDefaultNamespace(String namespaceURI)
  385. {
  386. String namespace = getNamespaceURI();
  387. if (namespace != null && namespace.equals(namespaceURI))
  388. {
  389. return getPrefix() == null;
  390. }
  391. if (attributes != null)
  392. {
  393. for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
  394. {
  395. if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
  396. .equals(ctx.getNamespaceURI()))
  397. {
  398. String qName = ctx.getNodeName();
  399. return (XMLConstants.XMLNS_ATTRIBUTE.equals(qName));
  400. }
  401. }
  402. }
  403. return super.isDefaultNamespace(namespaceURI);
  404. }
  405. public String lookupNamespaceURI(String prefix)
  406. {
  407. String namespace = getNamespaceURI();
  408. if (namespace != null && equal(prefix, getPrefix()))
  409. {
  410. return namespace;
  411. }
  412. if (attributes != null)
  413. {
  414. for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
  415. {
  416. if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
  417. .equals(ctx.getNamespaceURI()))
  418. {
  419. if (prefix == null)
  420. {
  421. if (XMLConstants.XMLNS_ATTRIBUTE.equals(ctx.getNodeName()))
  422. {
  423. return ctx.getNodeValue();
  424. }
  425. }
  426. else
  427. {
  428. if (prefix.equals(ctx.getLocalName()))
  429. {
  430. return ctx.getNodeValue();
  431. }
  432. }
  433. }
  434. }
  435. }
  436. return super.lookupNamespaceURI(prefix);
  437. }
  438. public String getBaseURI()
  439. {
  440. if (attributes != null)
  441. {
  442. Node xmlBase =
  443. attributes.getNamedItemNS(XMLConstants.XML_NS_URI, "base");
  444. if (xmlBase != null)
  445. {
  446. return xmlBase.getNodeValue();
  447. }
  448. }
  449. return super.getBaseURI();
  450. }
  451. public TypeInfo getSchemaTypeInfo()
  452. {
  453. // DTD implementation
  454. DomDoctype doctype = (DomDoctype) owner.getDoctype();
  455. if (doctype != null)
  456. {
  457. return doctype.getElementTypeInfo(getNodeName());
  458. }
  459. // TODO XML Schema implementation
  460. return null;
  461. }
  462. public void setIdAttribute(String name, boolean isId)
  463. {
  464. NamedNodeMap attrs = getAttributes();
  465. Attr attr = (Attr) attrs.getNamedItem(name);
  466. setIdAttributeNode(attr, isId);
  467. }
  468. public void setIdAttributeNode(Attr attr, boolean isId)
  469. {
  470. if (readonly)
  471. {
  472. throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
  473. }
  474. if (attr == null || attr.getOwnerElement() != this)
  475. {
  476. throw new DomDOMException(DOMException.NOT_FOUND_ERR);
  477. }
  478. if (isId)
  479. {
  480. if (userIdAttrs == null)
  481. {
  482. userIdAttrs = new HashSet();
  483. }
  484. userIdAttrs.add(attr);
  485. }
  486. else if (userIdAttrs != null)
  487. {
  488. userIdAttrs.remove(attr);
  489. if (userIdAttrs.isEmpty())
  490. {
  491. userIdAttrs = null;
  492. }
  493. }
  494. }
  495. public void setIdAttributeNS(String namespaceURI, String localName,
  496. boolean isId)
  497. {
  498. NamedNodeMap attrs = getAttributes();
  499. Attr attr = (Attr) attrs.getNamedItemNS(namespaceURI, localName);
  500. setIdAttributeNode(attr, isId);
  501. }
  502. public boolean isEqualNode(Node arg)
  503. {
  504. if (!super.isEqualNode(arg))
  505. return false;
  506. getAttributes();
  507. NamedNodeMap argAttrs = arg.getAttributes();
  508. int len = argAttrs.getLength();
  509. if (argAttrs == null || (len != attributes.length))
  510. return false;
  511. for (int i = 0; i < len; i++)
  512. {
  513. Node argCtx = argAttrs.item(i);
  514. // Don't compare namespace nodes
  515. if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
  516. .equals(argCtx.getNamespaceURI()))
  517. continue;
  518. // Find corresponding attribute node
  519. DomNode ctx = attributes.first;
  520. for (; ctx != null; ctx = ctx.next)
  521. {
  522. if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
  523. .equals(ctx.getNamespaceURI()))
  524. continue;
  525. if (!ctx.isEqualNode(argCtx))
  526. continue;
  527. break;
  528. }
  529. if (ctx == null)
  530. return false; // not found
  531. }
  532. return true;
  533. }
  534. }