1. /*
  2. * $Id: DataNode.java,v 1.2 2001/04/30 21:49:04 edwingo Exp $
  3. *
  4. * The Apache Software License, Version 1.1
  5. *
  6. *
  7. * Copyright (c) 2000 The Apache Software Foundation. All rights
  8. * reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The end-user documentation included with the redistribution,
  23. * if any, must include the following acknowledgment:
  24. * "This product includes software developed by the
  25. * Apache Software Foundation (http://www.apache.org/)."
  26. * Alternately, this acknowledgment may appear in the software itself,
  27. * if and wherever such third-party acknowledgments normally appear.
  28. *
  29. * 4. The names "Crimson" and "Apache Software Foundation" must
  30. * not be used to endorse or promote products derived from this
  31. * software without prior written permission. For written
  32. * permission, please contact apache@apache.org.
  33. *
  34. * 5. Products derived from this software may not be called "Apache",
  35. * nor may "Apache" appear in their name, without prior written
  36. * permission of the Apache Software Foundation.
  37. *
  38. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  45. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  48. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This software consists of voluntary contributions made by many
  53. * individuals on behalf of the Apache Software Foundation and was
  54. * originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
  55. * http://www.sun.com. For more information on the Apache Software
  56. * Foundation, please see <http://www.apache.org/>.
  57. */
  58. package org.apache.crimson.tree;
  59. import java.io.Writer;
  60. import java.io.IOException;
  61. import org.w3c.dom.*;
  62. /**
  63. * Node representing XML character data, such as text (including
  64. * CDATA sections and comments).
  65. *
  66. * <P> At this time this uses an unsophisticated representation
  67. * which is not suited for complex editing operations.
  68. *
  69. * @author David Brownell
  70. * @version $Revision: 1.2 $
  71. */
  72. // public
  73. abstract class DataNode extends NodeBase implements CharacterData
  74. {
  75. // package private
  76. char data [];
  77. static NodeListImpl childNodes = new NodeListImpl ();
  78. /*
  79. * Constructs a data object with no text and unattached
  80. * to any document.
  81. */
  82. DataNode () { }
  83. /*
  84. * Constructs data node by copying text from the input buffer.
  85. */
  86. DataNode (char buf [], int offset, int len)
  87. {
  88. data = new char [len];
  89. System.arraycopy (buf, offset, data, 0, len);
  90. }
  91. /*
  92. * Constructs a data node by copying text from the string.
  93. */
  94. DataNode (String s)
  95. {
  96. if (s != null) {
  97. data = new char [s.length ()];
  98. s.getChars (0, data.length, data, 0);
  99. } else
  100. data = new char [0];
  101. }
  102. /**
  103. * Returns the text of the node. This may be modified by
  104. * the caller only if the length remains unchanged.
  105. */
  106. public char [] getText () { return data; }
  107. /**
  108. * Assigns the text of the node. The buffer is consumed; the
  109. * caller should make copies accordingly.
  110. */
  111. public void setText (char buf []) { data = buf; }
  112. /**
  113. * Returns the contents of this text as a String.
  114. */
  115. public String toString () {
  116. if (data != null) {
  117. return new String(data);
  118. } else {
  119. return null;
  120. }
  121. }
  122. // DOM support
  123. /** DOM: Returns the text data as a string. */
  124. public String getData () { return toString (); }
  125. /** DOM: Assigns the text data. */
  126. public void setData (String data) {
  127. if (isReadonly ())
  128. throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
  129. if (data == null) {
  130. setText (new char [0]);
  131. } else {
  132. setText (data.toCharArray ());
  133. }
  134. }
  135. /** DOM: Returns the length of the node's data. */
  136. public int getLength ()
  137. { return data == null ? 0 : data.length; }
  138. /**
  139. * DOM: Returns the specified substring of the data in this node.
  140. */
  141. public String substringData (int offset, int count)
  142. throws DOMException
  143. {
  144. if (offset < 0 || offset > data.length || count < 0)
  145. throw new DomEx (DOMException.INDEX_SIZE_ERR);
  146. count = Math.min (count, data.length - offset);
  147. return new String (data, offset, count);
  148. }
  149. /**
  150. * DOM: Appends the string to the existing stored data.
  151. */
  152. public void appendData (String newData)
  153. {
  154. if (isReadonly ())
  155. throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
  156. int length = newData.length ();
  157. char tmp [] = new char [length + data.length];
  158. System.arraycopy (data, 0, tmp, 0, data.length);
  159. newData.getChars (0, length, tmp, data.length);
  160. data = tmp;
  161. }
  162. /**
  163. * DOM: Inserts the given data into the existing stored data
  164. * at the specified offset.
  165. */
  166. public void insertData (int offset, String newData)
  167. throws DOMException
  168. {
  169. if (isReadonly ())
  170. throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
  171. if (offset < 0 || offset > data.length)
  172. throw new DomEx (DOMException.INDEX_SIZE_ERR);
  173. int length = newData.length ();
  174. char tmp [] = new char [length + data.length];
  175. System.arraycopy (data, 0, tmp, 0, offset);
  176. newData.getChars (0, length, tmp, offset);
  177. System.arraycopy (data, offset,
  178. tmp, offset + length,
  179. data.length - offset);
  180. data = tmp;
  181. }
  182. /**
  183. * DOM: Removes a range of characters from the text.
  184. */
  185. public void deleteData (int offset, int count)
  186. throws DOMException
  187. {
  188. char tmp [];
  189. if (isReadonly ())
  190. throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
  191. if (offset < 0 || offset >= data.length || count < 0)
  192. throw new DomEx (DOMException.INDEX_SIZE_ERR);
  193. count = Math.min (count, data.length - offset);
  194. tmp = new char [data.length - count];
  195. System.arraycopy (data, 0, tmp, 0, offset);
  196. System.arraycopy (data, offset + count, tmp, offset,
  197. tmp.length - offset);
  198. data = tmp;
  199. }
  200. /**
  201. * DOM: Replaces <em>count</em> characters starting at the specified
  202. * <em>offset</em> in the data with the characters from the
  203. * specified <em>arg</em>.
  204. */
  205. public void replaceData (int offset, int count, String arg)
  206. throws DOMException
  207. {
  208. if (isReadonly ())
  209. throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
  210. if (offset < 0 || offset >= data.length || count < 0)
  211. throw new DomEx (DOMException.INDEX_SIZE_ERR);
  212. if ((offset + count) >= data.length) {
  213. deleteData (offset, count);
  214. appendData (arg);
  215. } else if (arg.length () == count) {
  216. arg.getChars (0, (arg.length ()), data, offset);
  217. } else {
  218. char tmp [] = new char [data.length + (arg.length () - count)];
  219. System.arraycopy (data, 0, tmp, 0, offset);
  220. arg.getChars (0, (arg.length ()), tmp, offset);
  221. System.arraycopy (data, (offset + count), tmp, (offset +
  222. arg.length ()), data.length -(offset + count));
  223. data = tmp;
  224. }
  225. }
  226. /**
  227. * DOM: Returns the children of this node.
  228. */
  229. public NodeList getChildNodes () { return childNodes; }
  230. /**
  231. * DOM: Returns the node's character data.
  232. */
  233. public String getNodeValue () { return getData (); }
  234. /**
  235. * DOM: Assigns the node's character data.
  236. */
  237. public void setNodeValue (String value) { setData (value); }
  238. static final class NodeListImpl implements NodeList {
  239. public Node item (int i) { return null;}
  240. public int getLength () { return 0;}
  241. }
  242. }