Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / xml / dom / DomText.java
blob3ca17dc9b874bf82704d4f3da936e8044e2dd3af
1 /* DomText.java --
2 Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.dom;
40 import org.w3c.dom.DOMException;
41 import org.w3c.dom.Text;
43 /**
44 * <p> "Text" implementation. </p>
46 * @author David Brownell
47 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
49 public class DomText
50 extends DomCharacterData
51 implements Text
54 // NOTE: deleted unused per-instance "isIgnorable"
55 // support to reclaim its space.
57 /**
58 * Constructs a text node associated with the specified
59 * document and holding the specified data.
61 * <p>This constructor should only be invoked by a Document object
62 * as part of its createTextNode functionality, or through a subclass
63 * which is similarly used in a "Sub-DOM" style layer.
65 protected DomText(DomDocument owner, String value)
67 super(TEXT_NODE, owner, value);
70 protected DomText(DomDocument owner, char[] buf, int off, int len)
72 super(TEXT_NODE, owner, buf, off, len);
75 // Used by DomCDATA
76 DomText(short nodeType, DomDocument owner, String value)
78 super(nodeType, owner, value);
81 DomText(short nodeType, DomDocument owner, char[] buf, int off, int len)
83 super(nodeType, owner, buf, off, len);
86 /**
87 * <b>DOM L1</b>
88 * Returns the string "#text".
90 // can't be 'final' with CDATA subclassing
91 public String getNodeName()
93 return "#text";
96 /**
97 * <b>DOM L1</b>
98 * Splits this text node in two parts at the offset, returning
99 * the new text node (the sibling with the second part).
101 public Text splitText(int offset)
103 if (isReadonly())
105 throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
109 String text = getNodeValue();
110 String before = text.substring(0, offset);
111 String after = text.substring(offset);
112 Text next;
114 if (getNodeType() == TEXT_NODE)
116 next = owner.createTextNode(after);
118 else // CDATA_SECTION_NODE
120 next = owner.createCDATASection(after);
123 if (this.next != null)
125 parent.insertBefore(next, this.next);
127 else
129 parent.appendChild(next);
131 setNodeValue(before);
132 return next;
135 catch (IndexOutOfBoundsException x)
137 throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
141 // DOM Level 3
143 public boolean isElementContentWhitespace()
145 if (parent != null)
147 DomDoctype doctype = (DomDoctype) owner.getDoctype();
148 if (doctype != null)
150 DTDElementTypeInfo info =
151 doctype.getElementTypeInfo(parent.getNodeName());
152 if (info != null)
154 if (info.model == null && info.model.indexOf("#PCDATA") != -1)
156 return false;
158 return getNodeValue().trim().length() == 0;
162 return false;
165 public String getWholeText()
167 DomNode ref = this;
168 DomNode ctx;
169 for (ctx = previous; ctx != null &&
170 (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
171 ctx = ctx.previous)
173 ref = ctx;
175 StringBuffer buf = new StringBuffer(ref.getNodeValue());
176 for (ctx = ref.next; ctx != null &&
177 (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
178 ctx = ctx.next)
180 buf.append(ctx.getNodeValue());
182 return buf.toString ();
185 public Text replaceWholeText(String content)
186 throws DOMException
188 boolean isEmpty = (content == null || content.length () == 0);
189 if (!isEmpty)
191 setNodeValue(content);
194 DomNode ref = this;
195 DomNode ctx;
196 for (ctx = previous; ctx != null &&
197 (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
198 ctx = ctx.previous)
200 ref = ctx;
202 ctx = ref.next;
203 if ((isEmpty || ref != this) && parent != null)
205 parent.removeChild(ref);
207 for (; ctx != null &&
208 (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
209 ctx = ref)
211 ref = ctx.next;
212 if ((isEmpty || ctx != this) && parent != null)
214 parent.removeChild(ctx);
217 return (isEmpty) ? null : this;