001: package org.enhydra.xml;
002:
003: import org.w3c.dom.DOMException;
004: import org.w3c.dom.Node;
005: import org.w3c.dom.Text;
006:
007: /**
008: * @author Tweety
009: *
010: * A class representing a node in a meta-data tree, which implements
011: * the <a href="../../../../api/org/w3c/dom/Element.html">
012: *
013: * <p> Namespaces are ignored in this implementation. The terms "tag
014: * name" and "node name" are always considered to be synonymous.
015: *
016: * @version 1.0
017: */
018: public class TextImpl extends CharacterDataImpl implements Text {
019:
020: /**
021: * Constructs a <code>TextImpl</code> from the given node.
022: *
023: * @param node, as a <code>TextImpl</code>.
024: */
025: public TextImpl(TextImpl node) {
026: super ((NodeImpl) node);
027: }
028:
029: /**
030: * Constructs a <code>TextImpl</code> from the given node value.
031: *
032: * @param value, as a <code>String</code>.
033: */
034: public TextImpl(String value) {
035: nodeValue = value;
036: type = Node.TEXT_NODE;
037: }
038:
039: /**
040: * Constructs a <code>TextImpl</code> from a given node,
041: * as a <code>Node</code>
042: *
043: * @param node, as <code>Node</code>.
044: */
045: public TextImpl(Node node) {
046: super (node);
047: }
048:
049: /**
050: * Returns the node type.
051: *
052: * @return the <code>TEXT_NODE</code> node type.
053: */
054: public short getNodeType() {
055: return Node.TEXT_NODE;
056: }
057:
058: /**
059: * Returns the name ("#text") associated with this node.
060: *
061: * @return the name, as a <code>String</code>.
062: */
063: public String getNodeName() {
064: return "#text";
065: }
066:
067: /**
068: * Returns the trimed node value associated with this node.
069: *
070: * @return the node value, as a <code>String</code>.
071: */
072: public String getNodeValue() throws DOMException {
073: return nodeValue.trim();
074: }
075:
076: /**
077: * Method beginToString for this class writes the value
078: * of this node (text).
079: *
080: * @param sb string buffer to add resulting string.
081: * @param indent used in formating the output.
082: */
083: protected void beginToString(StringBuffer sb, Indent indent) {
084: sb.append(this .nodeValue.trim());
085: }
086:
087: /**
088: * Method endToString does nothing.
089: */
090: protected void endToString(StringBuffer sb, Indent indent) {
091: }
092:
093: /**
094: * @see org.w3c.dom.Text#splitText(int)
095: *
096: * Break a text node into two sibling nodes. (Note that if the
097: * current node has no parent, they won't wind up as "siblings" --
098: * they'll both be orphans.)
099: *
100: * @param offset The offset at which to split. If offset is at the
101: * end of the available data, the second node will be empty.
102: *
103: * @returns A reference to the new node (containing data after the
104: * offset point). The original node will contain data up to that
105: * point.
106: *
107: * @throws DOMException(INDEX_SIZE_ERR) if offset is <0 or >length.
108: *
109: * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is read-only.
110: */
111: public Text splitText(int offset) throws DOMException {
112:
113: if (offset < 0 || offset > nodeValue.length()) {
114: throw new DOMException(DOMException.INDEX_SIZE_ERR,
115: "Index out of bounds");
116: }
117:
118: // split text into two separate nodes
119: TextImpl newText = new TextImpl(nodeValue.substring(offset));
120: nodeValue = nodeValue.substring(0, offset);
121:
122: // insert new text node
123: Node parentNode = getParentNode();
124: if (parentNode != null) {
125: parentNode.insertBefore(newText, nextSibling);
126: }
127:
128: return newText;
129:
130: }
131:
132: /* (non-Javadoc)
133: * @see org.w3c.dom.Text#isElementContentWhitespace()
134: */
135: public boolean isElementContentWhitespace() {
136: // TODO Auto-generated method stub
137: return false;
138: }
139:
140: /* (non-Javadoc)
141: * @see org.w3c.dom.Text#getWholeText()
142: */
143: public String getWholeText() {
144: // TODO Auto-generated method stub
145: return null;
146: }
147:
148: /* (non-Javadoc)
149: * @see org.w3c.dom.Text#replaceWholeText(java.lang.String)
150: */
151: public Text replaceWholeText(String arg0) throws DOMException {
152: // TODO Auto-generated method stub
153: return null;
154: }
155:
156: }
|