package gnu.xml.dom;
import org.w3c.dom.DOMException;
import org.w3c.dom.Text;
public class DomText
extends DomCharacterData
implements Text
{
protected DomText(DomDocument owner, String value)
{
super(TEXT_NODE, owner, value);
}
protected DomText(DomDocument owner, char[] buf, int off, int len)
{
super(TEXT_NODE, owner, buf, off, len);
}
DomText(short nodeType, DomDocument owner, String value)
{
super(nodeType, owner, value);
}
DomText(short nodeType, DomDocument owner, char[] buf, int off, int len)
{
super(nodeType, owner, buf, off, len);
}
public String getNodeName()
{
return "#text";
}
public Text splitText(int offset)
{
if (isReadonly())
{
throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
}
try
{
String text = getNodeValue();
String before = text.substring(0, offset);
String after = text.substring(offset);
Text next;
if (getNodeType() == TEXT_NODE)
{
next = owner.createTextNode(after);
}
else {
next = owner.createCDATASection(after);
}
if (this.next != null)
{
parent.insertBefore(next, this.next);
}
else
{
parent.appendChild(next);
}
setNodeValue(before);
return next;
}
catch (IndexOutOfBoundsException x)
{
throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
}
}
public boolean isElementContentWhitespace()
{
if (parent != null)
{
DomDoctype doctype = (DomDoctype) owner.getDoctype();
if (doctype != null)
{
DTDElementTypeInfo info =
doctype.getElementTypeInfo(parent.getNodeName());
if (info != null)
{
if (info.model == null && info.model.indexOf("#PCDATA") != -1)
{
return false;
}
return getNodeValue().trim().length() == 0;
}
}
}
return false;
}
public String getWholeText()
{
DomNode ref = this;
DomNode ctx;
for (ctx = previous; ctx != null &&
(ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
ctx = ctx.previous)
{
ref = ctx;
}
StringBuffer buf = new StringBuffer(ref.getNodeValue());
for (ctx = ref.next; ctx != null &&
(ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
ctx = ctx.next)
{
buf.append(ctx.getNodeValue());
}
return buf.toString ();
}
public Text replaceWholeText(String content)
throws DOMException
{
boolean isEmpty = (content == null || content.length () == 0);
if (!isEmpty)
{
setNodeValue(content);
}
DomNode ref = this;
DomNode ctx;
for (ctx = previous; ctx != null &&
(ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
ctx = ctx.previous)
{
ref = ctx;
}
ctx = ref.next;
if ((isEmpty || ref != this) && parent != null)
{
parent.removeChild(ref);
}
for (; ctx != null &&
(ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
ctx = ref)
{
ref = ctx.next;
if ((isEmpty || ctx != this) && parent != null)
{
parent.removeChild(ctx);
}
}
return (isEmpty) ? null : this;
}
}