DomText.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* DomText.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.DOMException;
  34. import org.w3c.dom.Text;
  35. /**
  36. * <p> "Text" implementation. </p>
  37. *
  38. * @author David Brownell
  39. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  40. */
  41. public class DomText
  42. extends DomCharacterData
  43. implements Text
  44. {
  45. // NOTE: deleted unused per-instance "isIgnorable"
  46. // support to reclaim its space.
  47. /**
  48. * Constructs a text node associated with the specified
  49. * document and holding the specified data.
  50. *
  51. * <p>This constructor should only be invoked by a Document object
  52. * as part of its createTextNode functionality, or through a subclass
  53. * which is similarly used in a "Sub-DOM" style layer.
  54. */
  55. protected DomText(DomDocument owner, String value)
  56. {
  57. super(TEXT_NODE, owner, value);
  58. }
  59. protected DomText(DomDocument owner, char[] buf, int off, int len)
  60. {
  61. super(TEXT_NODE, owner, buf, off, len);
  62. }
  63. // Used by DomCDATA
  64. DomText(short nodeType, DomDocument owner, String value)
  65. {
  66. super(nodeType, owner, value);
  67. }
  68. DomText(short nodeType, DomDocument owner, char[] buf, int off, int len)
  69. {
  70. super(nodeType, owner, buf, off, len);
  71. }
  72. /**
  73. * <b>DOM L1</b>
  74. * Returns the string "#text".
  75. */
  76. // can't be 'final' with CDATA subclassing
  77. public String getNodeName()
  78. {
  79. return "#text";
  80. }
  81. /**
  82. * <b>DOM L1</b>
  83. * Splits this text node in two parts at the offset, returning
  84. * the new text node (the sibling with the second part).
  85. */
  86. public Text splitText(int offset)
  87. {
  88. if (isReadonly())
  89. {
  90. throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
  91. }
  92. try
  93. {
  94. String text = getNodeValue();
  95. String before = text.substring(0, offset);
  96. String after = text.substring(offset);
  97. Text next;
  98. if (getNodeType() == TEXT_NODE)
  99. {
  100. next = owner.createTextNode(after);
  101. }
  102. else // CDATA_SECTION_NODE
  103. {
  104. next = owner.createCDATASection(after);
  105. }
  106. if (this.next != null)
  107. {
  108. parent.insertBefore(next, this.next);
  109. }
  110. else
  111. {
  112. parent.appendChild(next);
  113. }
  114. setNodeValue(before);
  115. return next;
  116. }
  117. catch (IndexOutOfBoundsException x)
  118. {
  119. throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
  120. }
  121. }
  122. // DOM Level 3
  123. public boolean isElementContentWhitespace()
  124. {
  125. if (parent != null)
  126. {
  127. DomDoctype doctype = (DomDoctype) owner.getDoctype();
  128. if (doctype != null)
  129. {
  130. DTDElementTypeInfo info =
  131. doctype.getElementTypeInfo(parent.getNodeName());
  132. if (info != null)
  133. {
  134. if (info.model == null && info.model.indexOf("#PCDATA") != -1)
  135. {
  136. return false;
  137. }
  138. return getNodeValue().trim().length() == 0;
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144. public String getWholeText()
  145. {
  146. DomNode ref = this;
  147. DomNode ctx;
  148. for (ctx = previous; ctx != null &&
  149. (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
  150. ctx = ctx.previous)
  151. {
  152. ref = ctx;
  153. }
  154. CPStringBuilder buf = new CPStringBuilder(ref.getNodeValue());
  155. for (ctx = ref.next; ctx != null &&
  156. (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
  157. ctx = ctx.next)
  158. {
  159. buf.append(ctx.getNodeValue());
  160. }
  161. return buf.toString ();
  162. }
  163. public Text replaceWholeText(String content)
  164. throws DOMException
  165. {
  166. boolean isEmpty = (content == null || content.length () == 0);
  167. if (!isEmpty)
  168. {
  169. setNodeValue(content);
  170. }
  171. DomNode ref = this;
  172. DomNode ctx;
  173. for (ctx = previous; ctx != null &&
  174. (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
  175. ctx = ctx.previous)
  176. {
  177. ref = ctx;
  178. }
  179. ctx = ref.next;
  180. if ((isEmpty || ref != this) && parent != null)
  181. {
  182. parent.removeChild(ref);
  183. }
  184. for (; ctx != null &&
  185. (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
  186. ctx = ref)
  187. {
  188. ref = ctx.next;
  189. if ((isEmpty || ctx != this) && parent != null)
  190. {
  191. parent.removeChild(ctx);
  192. }
  193. }
  194. return (isEmpty) ? null : this;
  195. }
  196. }