1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 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 "Xerces" 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, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.dom;
  58. import org.w3c.dom.DOMException;
  59. import org.w3c.dom.Node;
  60. import org.w3c.dom.NodeList;
  61. /**
  62. * CharacterData is an abstract Node that can carry character data as its
  63. * Value. It provides shared behavior for Text, CData, and
  64. * possibly other node types. All offsets are 0-based.
  65. * <p>
  66. * Since ProcessingInstructionImpl inherits from this class to reuse the
  67. * setNodeValue method, this class isn't declared as implementing the interface
  68. * CharacterData. This is done by relevant subclasses (TexImpl, CommentImpl).
  69. * <p>
  70. * This class doesn't directly support mutation events, however, it notifies
  71. * the document when mutations are performed so that the document class do so.
  72. *
  73. * @version $Id: CharacterDataImpl.java,v 1.20 2003/05/08 19:52:40 elena Exp $
  74. * @since PR-DOM-Level-1-19980818.
  75. */
  76. public abstract class CharacterDataImpl
  77. extends ChildNode {
  78. //
  79. // Constants
  80. //
  81. /** Serialization version. */
  82. static final long serialVersionUID = 7931170150428474230L;
  83. //
  84. // Data
  85. //
  86. protected String data;
  87. /** Empty child nodes. */
  88. private static transient NodeList singletonNodeList = new NodeList() {
  89. public Node item(int index) { return null; }
  90. public int getLength() { return 0; }
  91. };
  92. //
  93. // Constructors
  94. //
  95. public CharacterDataImpl(){}
  96. /** Factory constructor. */
  97. protected CharacterDataImpl(CoreDocumentImpl ownerDocument, String data) {
  98. super(ownerDocument);
  99. this.data = data;
  100. }
  101. //
  102. // Node methods
  103. //
  104. /** Returns an empty node list. */
  105. public NodeList getChildNodes() {
  106. return singletonNodeList;
  107. }
  108. /*
  109. * returns the content of this node
  110. */
  111. public String getNodeValue() {
  112. if (needsSyncData()) {
  113. synchronizeData();
  114. }
  115. return data;
  116. }
  117. /** This function added so that we can distinguish whether
  118. * setNodeValue has been called from some other DOM functions.
  119. * or by the client.<p>
  120. * This is important, because we do one type of Range fix-up,
  121. * from the high-level functions in CharacterData, and another
  122. * type if the client simply calls setNodeValue(value).
  123. */
  124. protected void setNodeValueInternal(String value) {
  125. if (isReadOnly()) {
  126. String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
  127. throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
  128. }
  129. // revisit: may want to set the value in ownerDocument.
  130. // Default behavior, overridden in some subclasses
  131. if (needsSyncData()) {
  132. synchronizeData();
  133. }
  134. // keep old value for document notification
  135. String oldvalue = this.data;
  136. CoreDocumentImpl ownerDocument = ownerDocument();
  137. // notify document
  138. ownerDocument.modifyingCharacterData(this);
  139. this.data = value;
  140. // notify document
  141. ownerDocument.modifiedCharacterData(this, oldvalue, value);
  142. }
  143. /**
  144. * Sets the content, possibly firing related events,
  145. * and updating ranges (via notification to the document)
  146. */
  147. public void setNodeValue(String value) {
  148. setNodeValueInternal(value);
  149. // notify document
  150. ownerDocument().replacedText(this);
  151. }
  152. //
  153. // CharacterData methods
  154. //
  155. /**
  156. * Retrieve character data currently stored in this node.
  157. *
  158. * @throws DOMExcpetion(DOMSTRING_SIZE_ERR) In some implementations,
  159. * the stored data may exceed the permitted length of strings. If so,
  160. * getData() will throw this DOMException advising the user to
  161. * instead retrieve the data in chunks via the substring() operation.
  162. */
  163. public String getData() {
  164. if (needsSyncData()) {
  165. synchronizeData();
  166. }
  167. return data;
  168. }
  169. /**
  170. * Report number of characters currently stored in this node's
  171. * data. It may be 0, meaning that the value is an empty string.
  172. */
  173. public int getLength() {
  174. if (needsSyncData()) {
  175. synchronizeData();
  176. }
  177. return data.length();
  178. }
  179. /**
  180. * Concatenate additional characters onto the end of the data
  181. * stored in this node. Note that this, and insert(), are the paths
  182. * by which a DOM could wind up accumulating more data than the
  183. * language's strings can easily handle. (See above discussion.)
  184. *
  185. * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is readonly.
  186. */
  187. public void appendData(String data) {
  188. if (isReadOnly()) {
  189. String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
  190. throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
  191. }
  192. if (data == null) {
  193. return;
  194. }
  195. if (needsSyncData()) {
  196. synchronizeData();
  197. }
  198. setNodeValue(this.data + data);
  199. } // appendData(String)
  200. /**
  201. * Remove a range of characters from the node's value. Throws a
  202. * DOMException if the offset is beyond the end of the
  203. * string. However, a deletion _count_ that exceeds the available
  204. * data is accepted as a delete-to-end request.
  205. *
  206. * @throws DOMException(INDEX_SIZE_ERR) if offset is negative or
  207. * greater than length, or if count is negative.
  208. *
  209. * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is
  210. * readonly.
  211. */
  212. public void deleteData(int offset, int count)
  213. throws DOMException {
  214. if (isReadOnly()) {
  215. String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
  216. throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
  217. }
  218. if (count < 0) {
  219. String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
  220. throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
  221. }
  222. if (needsSyncData()) {
  223. synchronizeData();
  224. }
  225. int tailLength = Math.max(data.length() - count - offset, 0);
  226. try {
  227. String value = data.substring(0, offset) +
  228. (tailLength > 0
  229. ? data.substring(offset + count, offset + count + tailLength)
  230. : "");
  231. setNodeValueInternal(value);
  232. // notify document
  233. ownerDocument().deletedText(this, offset, count);
  234. }
  235. catch (StringIndexOutOfBoundsException e) {
  236. String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
  237. throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
  238. }
  239. } // deleteData(int,int)
  240. /**
  241. * Insert additional characters into the data stored in this node,
  242. * at the offset specified.
  243. *
  244. * @throws DOMException(INDEX_SIZE_ERR) if offset is negative or
  245. * greater than length.
  246. *
  247. * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is readonly.
  248. */
  249. public void insertData(int offset, String data)
  250. throws DOMException {
  251. if (isReadOnly()) {
  252. String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
  253. throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
  254. }
  255. if (needsSyncData()) {
  256. synchronizeData();
  257. }
  258. try {
  259. String value =
  260. new StringBuffer(this.data).insert(offset, data).toString();
  261. setNodeValueInternal(value);
  262. // notify document
  263. ownerDocument().insertedText(this, offset, data.length());
  264. }
  265. catch (StringIndexOutOfBoundsException e) {
  266. String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
  267. throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
  268. }
  269. } // insertData(int,int)
  270. /**
  271. * Replace a series of characters at the specified (zero-based)
  272. * offset with a new string, NOT necessarily of the same
  273. * length. Convenience method, equivalent to a delete followed by an
  274. * insert. Throws a DOMException if the specified offset is beyond
  275. * the end of the existing data.
  276. *
  277. * @param offset The offset at which to begin replacing.
  278. *
  279. * @param count The number of characters to remove,
  280. * interpreted as in the delete() method.
  281. *
  282. * @param data The new string to be inserted at offset in place of
  283. * the removed data. Note that the entire string will
  284. * be inserted -- the count parameter does not affect
  285. * insertion, and the new data may be longer or shorter
  286. * than the substring it replaces.
  287. *
  288. * @throws DOMException(INDEX_SIZE_ERR) if offset is negative or
  289. * greater than length, or if count is negative.
  290. *
  291. * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is
  292. * readonly.
  293. */
  294. public void replaceData(int offset, int count, String data)
  295. throws DOMException {
  296. // The read-only check is done by deleteData()
  297. // ***** This could be more efficient w/r/t Mutation Events,
  298. // specifically by aggregating DOMAttrModified and
  299. // DOMSubtreeModified. But mutation events are
  300. // underspecified; I don't feel compelled
  301. // to deal with it right now.
  302. deleteData(offset, count);
  303. insertData(offset, data);
  304. } // replaceData(int,int,String)
  305. /**
  306. * Store character data into this node.
  307. *
  308. * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is readonly.
  309. */
  310. public void setData(String value)
  311. throws DOMException {
  312. setNodeValue(value);
  313. }
  314. /**
  315. * Substring is more than a convenience function. In some
  316. * implementations of the DOM, where the stored data may exceed the
  317. * length that can be returned in a single string, the only way to
  318. * read it all is to extract it in chunks via this method.
  319. *
  320. * @param offset Zero-based offset of first character to retrieve.
  321. * @param count Number of characters to retrieve.
  322. *
  323. * If the sum of offset and count exceeds the length, all characters
  324. * to end of data are returned.
  325. *
  326. * @throws DOMException(INDEX_SIZE_ERR) if offset is negative or
  327. * greater than length, or if count is negative.
  328. *
  329. * @throws DOMException(WSTRING_SIZE_ERR) In some implementations,
  330. * count may exceed the permitted length of strings. If so,
  331. * substring() will throw this DOMException advising the user to
  332. * instead retrieve the data in smaller chunks.
  333. */
  334. public String substringData(int offset, int count)
  335. throws DOMException {
  336. if (needsSyncData()) {
  337. synchronizeData();
  338. }
  339. int length = data.length();
  340. if (count < 0 || offset < 0 || offset > length - 1) {
  341. String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
  342. throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
  343. }
  344. int tailIndex = Math.min(offset + count, length);
  345. return data.substring(offset, tailIndex);
  346. } // substringData(int,int):String
  347. } // class CharacterDataImpl