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.CharacterData;
  59. import org.w3c.dom.DOMException;
  60. import org.w3c.dom.Node;
  61. import org.w3c.dom.Text;
  62. /**
  63. * Text nodes hold the non-markup, non-Entity content of
  64. * an Element or Attribute.
  65. * <P>
  66. * When a document is first made available to the DOM, there is only
  67. * one Text object for each block of adjacent plain-text. Users (ie,
  68. * applications) may create multiple adjacent Texts during editing --
  69. * see {@link org.w3c.dom.Element#normalize} for discussion.
  70. * <P>
  71. * Note that CDATASection is a subclass of Text. This is conceptually
  72. * valid, since they're really just two different ways of quoting
  73. * characters when they're written out as part of an XML stream.
  74. *
  75. * @version $Id: TextImpl.java,v 1.22 2004/02/10 17:09:45 elena Exp $
  76. * @since PR-DOM-Level-1-19980818.
  77. */
  78. public class TextImpl
  79. extends CharacterDataImpl
  80. implements CharacterData, Text {
  81. //
  82. // Constants
  83. //
  84. /** Serialization version. */
  85. static final long serialVersionUID = -5294980852957403469L;
  86. //
  87. // Constructors
  88. //
  89. /** Default constructor */
  90. public TextImpl(){}
  91. /** Factory constructor. */
  92. public TextImpl(CoreDocumentImpl ownerDoc, String data) {
  93. super(ownerDoc, data);
  94. }
  95. /**
  96. * NON-DOM: resets node and sets specified values for the current node
  97. *
  98. * @param ownerDoc
  99. * @param data
  100. */
  101. public void setValues(CoreDocumentImpl ownerDoc, String data){
  102. flags=0;
  103. nextSibling = null;
  104. previousSibling=null;
  105. setOwnerDocument(ownerDoc);
  106. super.data = data;
  107. }
  108. //
  109. // Node methods
  110. //
  111. /**
  112. * A short integer indicating what type of node this is. The named
  113. * constants for this value are defined in the org.w3c.dom.Node interface.
  114. */
  115. public short getNodeType() {
  116. return Node.TEXT_NODE;
  117. }
  118. /** Returns the node name. */
  119. public String getNodeName() {
  120. return "#text";
  121. }
  122. /**
  123. * NON-DOM: Set whether this Text is ignorable whitespace.
  124. */
  125. public void setIgnorableWhitespace(boolean ignore) {
  126. if (needsSyncData()) {
  127. synchronizeData();
  128. }
  129. isIgnorableWhitespace(ignore);
  130. } // setIgnorableWhitespace(boolean)
  131. /**
  132. * DOM L3 Core CR - Experimental
  133. *
  134. * Returns whether this text node contains
  135. * element content whitespace</a>, often abusively called "ignorable whitespace".
  136. * The text node is determined to contain whitespace in element content
  137. * during the load of the document or if validation occurs while using
  138. * <code>Document.normalizeDocument()</code>.
  139. * @since DOM Level 3
  140. */
  141. public boolean isElementContentWhitespace() {
  142. // REVISIT: is this implemenation correct?
  143. if (needsSyncData()) {
  144. synchronizeData();
  145. }
  146. return internalIsIgnorableWhitespace();
  147. }
  148. /**
  149. * DOM Level 3 WD - Experimental.
  150. * Returns all text of <code>Text</code> nodes logically-adjacent text
  151. * nodes to this node, concatenated in document order.
  152. * @since DOM Level 3
  153. */
  154. public String getWholeText(){
  155. if (needsSyncData()) {
  156. synchronizeData();
  157. }
  158. if (nextSibling == null) {
  159. return data;
  160. }
  161. if (fBufferStr == null){
  162. fBufferStr = new StringBuffer();
  163. }
  164. else {
  165. fBufferStr.setLength(0);
  166. }
  167. if (data != null && data.length() != 0) {
  168. fBufferStr.append(data);
  169. }
  170. getWholeText(nextSibling, fBufferStr);
  171. return fBufferStr.toString();
  172. }
  173. /**
  174. * Concatenates the text of all logically-adjacent text nodes
  175. *
  176. * @param node
  177. * @param buffer
  178. * @return true - if execution was stopped because the type of node
  179. * other than EntityRef, Text, CDATA is encountered, otherwise
  180. * return false
  181. */
  182. private boolean getWholeText(Node node, StringBuffer buffer){
  183. String text;
  184. while (node != null) {
  185. short type = node.getNodeType();
  186. if (type == Node.ENTITY_REFERENCE_NODE) {
  187. if (getWholeText(node.getFirstChild(), buffer)){
  188. return true;
  189. }
  190. }
  191. else if (type == Node.TEXT_NODE ||
  192. type == Node.CDATA_SECTION_NODE) {
  193. ((NodeImpl)node).getTextContent(buffer);
  194. }
  195. else {
  196. return true;
  197. }
  198. node = node.getNextSibling();
  199. }
  200. return false;
  201. }
  202. /**
  203. * DOM Level 3 WD - Experimental.
  204. */
  205. public Text replaceWholeText(String content)
  206. throws DOMException{
  207. if (needsSyncData()) {
  208. synchronizeData();
  209. }
  210. // make sure we can make the replacement
  211. if (!canModify(nextSibling)) {
  212. throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
  213. DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null));
  214. }
  215. Node parent = this.getParentNode();
  216. if (content == null || content.length() == 0) {
  217. // remove current node
  218. if (parent !=null) { // check if node in the tree
  219. parent.removeChild(this);
  220. return null;
  221. }
  222. }
  223. Text currentNode = null;
  224. if (isReadOnly()){
  225. Text newNode = this.ownerDocument().createTextNode(content);
  226. if (parent !=null) { // check if node in the tree
  227. parent.insertBefore(newNode, this);
  228. parent.removeChild(this);
  229. currentNode = newNode;
  230. } else {
  231. return newNode;
  232. }
  233. } else {
  234. this.setData(content);
  235. currentNode = this;
  236. }
  237. Node sibling = currentNode.getNextSibling();
  238. while ( sibling !=null) {
  239. parent.removeChild(sibling);
  240. sibling = currentNode.getNextSibling();
  241. }
  242. return currentNode;
  243. }
  244. /**
  245. * If any EntityReference to be removed has descendants
  246. * that are not EntityReference, Text, or CDATASection
  247. * nodes, the replaceWholeText method must fail before
  248. * performing any modification of the document, raising a
  249. * DOMException with the code NO_MODIFICATION_ALLOWED_ERR.
  250. *
  251. * @param node
  252. * @return true - can replace text
  253. * false - can't replace exception must be raised
  254. */
  255. private boolean canModify(Node node){
  256. while (node != null) {
  257. short type = node.getNodeType();
  258. if (type == Node.ENTITY_REFERENCE_NODE) {
  259. if (!canModify(node.getFirstChild())){
  260. return false;
  261. }
  262. }
  263. else if (type != Node.TEXT_NODE &&
  264. type != Node.CDATA_SECTION_NODE) {
  265. return false;
  266. }
  267. node = node.getNextSibling();
  268. }
  269. return true;
  270. }
  271. /**
  272. * NON-DOM: Returns whether this Text is ignorable whitespace.
  273. */
  274. public boolean isIgnorableWhitespace() {
  275. if (needsSyncData()) {
  276. synchronizeData();
  277. }
  278. return internalIsIgnorableWhitespace();
  279. } // isIgnorableWhitespace():boolean
  280. //
  281. // Text methods
  282. //
  283. /**
  284. * Break a text node into two sibling nodes. (Note that if the
  285. * current node has no parent, they won't wind up as "siblings" --
  286. * they'll both be orphans.)
  287. *
  288. * @param offset The offset at which to split. If offset is at the
  289. * end of the available data, the second node will be empty.
  290. *
  291. * @return A reference to the new node (containing data after the
  292. * offset point). The original node will contain data up to that
  293. * point.
  294. *
  295. * @throws DOMException(INDEX_SIZE_ERR) if offset is <0 or >length.
  296. *
  297. * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is read-only.
  298. */
  299. public Text splitText(int offset)
  300. throws DOMException {
  301. if (isReadOnly()) {
  302. throw new DOMException(
  303. DOMException.NO_MODIFICATION_ALLOWED_ERR,
  304. DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null));
  305. }
  306. if (needsSyncData()) {
  307. synchronizeData();
  308. }
  309. if (offset < 0 || offset > data.length() ) {
  310. throw new DOMException(DOMException.INDEX_SIZE_ERR,
  311. DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null));
  312. }
  313. // split text into two separate nodes
  314. Text newText =
  315. getOwnerDocument().createTextNode(data.substring(offset));
  316. setNodeValue(data.substring(0, offset));
  317. // insert new text node
  318. Node parentNode = getParentNode();
  319. if (parentNode != null) {
  320. parentNode.insertBefore(newText, nextSibling);
  321. }
  322. return newText;
  323. } // splitText(int):Text
  324. /**
  325. * NON-DOM (used by DOMParser): Reset data for the node.
  326. */
  327. public void replaceData (String value){
  328. data = value;
  329. }
  330. /**
  331. * NON-DOM (used by DOMParser: Sets data to empty string.
  332. * Returns the value the data was set to.
  333. */
  334. public String removeData (){
  335. String olddata=data;
  336. data = "";
  337. return olddata;
  338. }
  339. } // class TextImpl