DomAttr.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* DomAttr.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 gnu.java.lang.CPStringBuilder;
  33. import org.w3c.dom.Attr;
  34. import org.w3c.dom.DOMException;
  35. import org.w3c.dom.Element;
  36. import org.w3c.dom.Node;
  37. import org.w3c.dom.TypeInfo;
  38. import org.w3c.dom.events.MutationEvent;
  39. /**
  40. * <p> "Attr" implementation. In DOM, attributes cost quite a lot of
  41. * memory because their values are complex structures rather than just
  42. * simple strings. To reduce your costs, avoid having more than one
  43. * child of an attribute; stick to a single Text node child, and ignore
  44. * even that by using the attribute's "nodeValue" property.</p>
  45. *
  46. * <p> As a bit of general advice, only look at attribute modification
  47. * events through the DOMAttrModified event (sent to the associated
  48. * element). Implementations are not guaranteed to report other events
  49. * in the same order, so you're very likely to write nonportable code if
  50. * you monitor events at the "children of Attr" level.</p>
  51. *
  52. * <p> At this writing, not all attribute modifications will cause the
  53. * DOMAttrModified event to be triggered ... only the ones using the string
  54. * methods (setNodeValue, setValue, and Element.setAttribute) to modify
  55. * those values. That is, if you manipulate those children directly,
  56. * elements won't get notified that attribute values have changed.
  57. * The natural fix for that will report other modifications, but won't
  58. * be able to expose "previous" attribute value; it'll need to be cached
  59. * or something (at which point why bother using child nodes). </p>
  60. *
  61. * <p><em>You are strongly advised not to use "children" of any attribute
  62. * nodes you work with.</em> </p>
  63. *
  64. * @author David Brownell
  65. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  66. */
  67. public class DomAttr
  68. extends DomNsNode
  69. implements Attr
  70. {
  71. private boolean specified;
  72. private String value; // string value cache
  73. /**
  74. * Constructs an Attr node associated with the specified document.
  75. * The "specified" flag is initialized to true, since this DOM has
  76. * no current "back door" mechanisms to manage default values so
  77. * that every value must effectively be "specified".
  78. *
  79. * <p>This constructor should only be invoked by a Document as part of
  80. * its createAttribute functionality, or through a subclass which is
  81. * similarly used in a "Sub-DOM" style layer.
  82. *
  83. * @param owner The document with which this node is associated
  84. * @param namespaceURI Combined with the local part of the name,
  85. * this is used to uniquely identify a type of attribute
  86. * @param name Name of this attribute, which may include a prefix
  87. */
  88. protected DomAttr(DomDocument owner, String namespaceURI, String name)
  89. {
  90. super(ATTRIBUTE_NODE, owner, namespaceURI, name);
  91. specified = true;
  92. length = 1;
  93. // XXX register self to get insertion/removal events
  94. // and character data change events and when they happen,
  95. // report self-mutation
  96. }
  97. /**
  98. * Constructs an Attr node associated with the specified document.
  99. * The "specified" flag is initialized to true, since this DOM has
  100. * no current "back door" mechanisms to manage default values so
  101. * that every value must effectively be "specified".
  102. *
  103. * <p>This constructor should only be invoked by a Document as part of
  104. * its createAttribute functionality, or through a subclass which is
  105. * similarly used in a "Sub-DOM" style layer.
  106. * <p>
  107. * With this constructor, the prefix and local part are given explicitly
  108. * rather than being computed. This allows them to be explicitly set to
  109. * {@code null} as required by {@link Document#createAttribute(String)}.
  110. * </p>
  111. *
  112. * @param owner The document with which this node is associated
  113. * @param namespaceURI Combined with the local part of the name,
  114. * this is used to uniquely identify a type of attribute
  115. * @param name Name of this attribute, which may include a prefix
  116. * @param prefix the namespace prefix of the name. May be {@code null}.
  117. * @param localName the local part of the name. May be {@code null}.
  118. */
  119. protected DomAttr(DomDocument owner, String namespaceURI, String name,
  120. String prefix, String localName)
  121. {
  122. super(ATTRIBUTE_NODE, owner, namespaceURI, name, prefix, localName);
  123. specified = true;
  124. length = 1;
  125. }
  126. /**
  127. * <b>DOM L1</b>
  128. * Returns the attribute name (same as getNodeName)
  129. */
  130. public final String getName()
  131. {
  132. return getNodeName();
  133. }
  134. /**
  135. * <b>DOM L1</b>
  136. * Returns true if a parser reported this was in the source text.
  137. */
  138. public final boolean getSpecified()
  139. {
  140. return specified;
  141. }
  142. /**
  143. * Records whether this attribute was in the source text.
  144. */
  145. public final void setSpecified(boolean value)
  146. {
  147. specified = value;
  148. }
  149. /**
  150. * <b>DOM L1</b>
  151. * Returns the attribute value, with character and entity
  152. * references substituted.
  153. * <em>NOTE: entity refs as children aren't currently handled.</em>
  154. */
  155. public String getNodeValue()
  156. {
  157. // If we have a simple node-value, use that
  158. if (first == null)
  159. {
  160. return (value == null) ? "" : value;
  161. }
  162. // Otherwise collect child node-values
  163. CPStringBuilder buf = new CPStringBuilder();
  164. for (DomNode ctx = first; ctx != null; ctx = ctx.next)
  165. {
  166. switch (ctx.nodeType)
  167. {
  168. case Node.TEXT_NODE:
  169. buf.append(ctx.getNodeValue());
  170. break;
  171. case Node.ENTITY_REFERENCE_NODE:
  172. // TODO
  173. break;
  174. }
  175. }
  176. return buf.toString();
  177. }
  178. /**
  179. * <b>DOM L1</b>
  180. * Assigns the value of the attribute; it will have one child,
  181. * which is a text node with the specified value (same as
  182. * setNodeValue).
  183. */
  184. public final void setValue(String value)
  185. {
  186. setNodeValue(value);
  187. }
  188. /**
  189. * <b>DOM L1</b>
  190. * Returns the value of the attribute as a non-null string; same
  191. * as getNodeValue.
  192. * <em>NOTE: entity refs as children aren't currently handled.</em>
  193. */
  194. public final String getValue()
  195. {
  196. return getNodeValue();
  197. }
  198. /**
  199. * <b>DOM L1</b>
  200. * Assigns the attribute value; using this API, no entity or
  201. * character references will exist.
  202. * Causes a DOMAttrModified mutation event to be sent.
  203. */
  204. public void setNodeValue(String value)
  205. {
  206. if (readonly)
  207. {
  208. throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
  209. }
  210. if (value == null)
  211. {
  212. value = "";
  213. }
  214. String oldValue = getNodeValue();
  215. while (last != null)
  216. {
  217. removeChild(last);
  218. }
  219. // don't create a new node just for this...
  220. /*
  221. Node text = owner.createTextNode(value);
  222. appendChild(text);
  223. */
  224. this.value = value;
  225. length = 1;
  226. specified = true;
  227. mutating(oldValue, value, MutationEvent.MODIFICATION);
  228. }
  229. public final Node getFirstChild()
  230. {
  231. // Create a child text node if necessary
  232. if (first == null)
  233. {
  234. length = 0;
  235. Node text = owner.createTextNode((value == null) ? "" : value);
  236. appendChild(text);
  237. }
  238. return first;
  239. }
  240. public final Node getLastChild()
  241. {
  242. // Create a child text node if necessary
  243. if (last == null)
  244. {
  245. length = 0;
  246. Node text = owner.createTextNode((value == null) ? "" : value);
  247. appendChild(text);
  248. }
  249. return last;
  250. }
  251. public Node item(int index)
  252. {
  253. // Create a child text node if necessary
  254. if (first == null)
  255. {
  256. length = 0;
  257. Node text = owner.createTextNode((value == null) ? "" : value);
  258. appendChild(text);
  259. }
  260. return super.item(index);
  261. }
  262. /**
  263. * <b>DOM L2</b>
  264. * Returns the element with which this attribute is associated.
  265. */
  266. public final Element getOwnerElement()
  267. {
  268. return (Element) parent;
  269. }
  270. public final Node getNextSibling()
  271. {
  272. return null;
  273. }
  274. public final Node getPreviousSibling()
  275. {
  276. return null;
  277. }
  278. public Node getParentNode()
  279. {
  280. return null;
  281. }
  282. /**
  283. * Records the element with which this attribute is associated.
  284. */
  285. public final void setOwnerElement(Element e)
  286. {
  287. if (parent != null)
  288. {
  289. throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR);
  290. }
  291. if (!(e instanceof DomElement))
  292. {
  293. throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR);
  294. }
  295. parent = (DomElement) e;
  296. depth = parent.depth + 1;
  297. }
  298. /**
  299. * The base URI of an Attr is always <code>null</code>.
  300. */
  301. public final String getBaseURI()
  302. {
  303. return null;
  304. }
  305. /**
  306. * Shallow clone of the attribute, breaking all ties with any
  307. * elements.
  308. */
  309. public Object clone()
  310. {
  311. DomAttr retval = (DomAttr) super.clone();
  312. retval.specified = true;
  313. return retval;
  314. }
  315. private void mutating(String oldValue, String newValue, short why)
  316. {
  317. if (!reportMutations || parent == null || equal(newValue, oldValue))
  318. {
  319. return;
  320. }
  321. // EVENT: DOMAttrModified, target = parent,
  322. // prev/new values provided, also attr name
  323. MutationEvent event;
  324. event = (MutationEvent) createEvent ("MutationEvents");
  325. event.initMutationEvent ("DOMAttrModified",
  326. true /* bubbles */, false /* nocancel */,
  327. null, oldValue, newValue, getNodeName (), why);
  328. parent.dispatchEvent (event);
  329. }
  330. // DOM Level 3 methods
  331. public TypeInfo getSchemaTypeInfo()
  332. {
  333. if (parent != null)
  334. {
  335. // DTD implementation
  336. DomDoctype doctype = (DomDoctype) parent.owner.getDoctype();
  337. if (doctype != null)
  338. {
  339. return doctype.getAttributeTypeInfo(parent.getNodeName(),
  340. getNodeName());
  341. }
  342. // TODO XML Schema implementation
  343. }
  344. return null;
  345. }
  346. public boolean isId()
  347. {
  348. if (parent != null)
  349. {
  350. DomDoctype doctype = (DomDoctype) parent.owner.getDoctype();
  351. if (doctype != null)
  352. {
  353. DTDAttributeTypeInfo info =
  354. doctype.getAttributeTypeInfo(parent.getNodeName(),
  355. getNodeName());
  356. if (info != null && "ID".equals(info.type))
  357. {
  358. return true;
  359. }
  360. }
  361. DomElement element = (DomElement) parent;
  362. if (element.userIdAttrs != null &&
  363. element.userIdAttrs.contains(this))
  364. {
  365. return true;
  366. }
  367. // TODO XML Schema implementation
  368. }
  369. return false;
  370. }
  371. }