1 package org.enhydra.xml;
2
3 import org.w3c.dom.DOMException;
4 import org.w3c.dom.Node;
5 import org.w3c.dom.Text;
6
7 /***
8 * @author Tweety
9 *
10 * A class representing a node in a meta-data tree, which implements
11 * the <a href="../../../../api/org/w3c/dom/Element.html">
12 *
13 * <p> Namespaces are ignored in this implementation. The terms "tag
14 * name" and "node name" are always considered to be synonymous.
15 *
16 * @version 1.0
17 */
18 public class TextImpl extends CharacterDataImpl implements Text {
19
20
21 /***
22 * Constructs a <code>TextImpl</code> from the given node.
23 *
24 * @param node, as a <code>TextImpl</code>.
25 */
26 public TextImpl(TextImpl node) {
27 super((NodeImpl)node);
28 }
29
30
31 /***
32 * Constructs a <code>TextImpl</code> from the given node value.
33 *
34 * @param value, as a <code>String</code>.
35 */
36 public TextImpl(String value) {
37 nodeValue = value;
38 type = Node.TEXT_NODE;
39 }
40
41
42 /***
43 * Constructs a <code>TextImpl</code> from a given node,
44 * as a <code>Node</code>
45 *
46 * @param node, as <code>Node</code>.
47 */
48 public TextImpl(Node node) {
49 super(node);
50 }
51
52
53 /***
54 * Returns the node type.
55 *
56 * @return the <code>TEXT_NODE</code> node type.
57 */
58 public short getNodeType() {
59 return Node.TEXT_NODE;
60 }
61
62 /***
63 * Returns the name ("#text") associated with this node.
64 *
65 * @return the name, as a <code>String</code>.
66 */
67 public String getNodeName() {
68 return "#text";
69 }
70
71
72 /***
73 * Returns the trimed node value associated with this node.
74 *
75 * @return the node value, as a <code>String</code>.
76 */
77 public String getNodeValue() throws DOMException {
78 return nodeValue.trim();
79 }
80
81
82 /***
83 * Method beginToString for this class writes the value
84 * of this node (text).
85 *
86 * @param sb string buffer to add resulting string.
87 * @param indent used in formating the output.
88 */
89 protected void beginToString(StringBuffer sb, Indent indent) {
90 sb.append(this.nodeValue.trim());
91 }
92
93 /***
94 * Method endToString does nothing.
95 */
96 protected void endToString(StringBuffer sb, Indent indent) {
97 }
98
99
100 /***
101 * @see org.w3c.dom.Text#splitText(int)
102 *
103 * Break a text node into two sibling nodes. (Note that if the
104 * current node has no parent, they won't wind up as "siblings" --
105 * they'll both be orphans.)
106 *
107 * @param offset The offset at which to split. If offset is at the
108 * end of the available data, the second node will be empty.
109 *
110 * @return A reference to the new node (containing data after the
111 * offset point). The original node will contain data up to that
112 * point.
113 *
114 * @throws DOMException(INDEX_SIZE_ERR) if offset is <0 or >length.
115 *
116 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is read-only.
117 */
118 public Text splitText(int offset)
119 throws DOMException {
120
121 if (offset < 0 || offset > nodeValue.length() ) {
122 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
123 }
124
125 // split text into two separate nodes
126 TextImpl newText = new TextImpl(nodeValue.substring(offset));
127 nodeValue = nodeValue.substring(0, offset);
128
129 // insert new text node
130 Node parentNode = getParentNode();
131 if (parentNode != null) {
132 parentNode.insertBefore(newText, nextSibling);
133 }
134
135 return newText;
136
137 }
138
139 }
This page automatically generated by Maven