1
2
3
4
5
6
7
8
9
10 package org.dom4j.dom;
11
12 import org.dom4j.Element;
13 import org.dom4j.Text;
14 import org.dom4j.tree.DefaultText;
15 import org.w3c.dom.DOMException;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.NamedNodeMap;
18 import org.w3c.dom.NodeList;
19
20 /*** <p><code>DOMText</code> implements a Text node which
21 * supports the W3C DOM API.</p>
22 *
23 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
24 * @version $Revision: 1.10 $
25 */
26 public class DOMText extends DefaultText implements org.w3c.dom.Text {
27
28 public DOMText(String text) {
29 super(text);
30 }
31
32 public DOMText(Element parent, String text) {
33 super(parent, text);
34 }
35
36
37
38
39
40 public boolean supports(String feature, String version) {
41 return DOMNodeHelper.supports(this, feature, version);
42 }
43
44 public String getNamespaceURI() {
45 return DOMNodeHelper.getNamespaceURI(this);
46 }
47
48 public String getPrefix() {
49 return DOMNodeHelper.getPrefix(this);
50 }
51
52 public void setPrefix(String prefix) throws DOMException {
53 DOMNodeHelper.setPrefix(this, prefix);
54 }
55
56 public String getLocalName() {
57 return DOMNodeHelper.getLocalName(this);
58 }
59
60 public String getNodeName() {
61 return "#text";
62 }
63
64
65
66
67
68
69
70 public String getNodeValue() throws DOMException {
71 return DOMNodeHelper.getNodeValue(this);
72 }
73
74 public void setNodeValue(String nodeValue) throws DOMException {
75 DOMNodeHelper.setNodeValue(this, nodeValue);
76 }
77
78
79 public org.w3c.dom.Node getParentNode() {
80 return DOMNodeHelper.getParentNode(this);
81 }
82
83 public NodeList getChildNodes() {
84 return DOMNodeHelper.getChildNodes(this);
85 }
86
87 public org.w3c.dom.Node getFirstChild() {
88 return DOMNodeHelper.getFirstChild(this);
89 }
90
91 public org.w3c.dom.Node getLastChild() {
92 return DOMNodeHelper.getLastChild(this);
93 }
94
95 public org.w3c.dom.Node getPreviousSibling() {
96 return DOMNodeHelper.getPreviousSibling(this);
97 }
98
99 public org.w3c.dom.Node getNextSibling() {
100 return DOMNodeHelper.getNextSibling(this);
101 }
102
103 public NamedNodeMap getAttributes() {
104 return null;
105 }
106
107 public Document getOwnerDocument() {
108 return DOMNodeHelper.getOwnerDocument(this);
109 }
110
111 public org.w3c.dom.Node insertBefore(
112 org.w3c.dom.Node newChild,
113 org.w3c.dom.Node refChild
114 ) throws DOMException {
115 checkNewChildNode(newChild);
116 return DOMNodeHelper.insertBefore(this, newChild, refChild);
117 }
118
119 public org.w3c.dom.Node replaceChild(
120 org.w3c.dom.Node newChild,
121 org.w3c.dom.Node oldChild
122 ) throws DOMException {
123 checkNewChildNode(newChild);
124 return DOMNodeHelper.replaceChild(this, newChild, oldChild);
125 }
126
127 public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) throws DOMException {
128 return DOMNodeHelper.removeChild(this, oldChild);
129 }
130
131 public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild) throws DOMException {
132 checkNewChildNode(newChild);
133 return DOMNodeHelper.appendChild(this, newChild);
134 }
135
136 private void checkNewChildNode(org.w3c.dom.Node newChild) throws DOMException {
137 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
138 "Text nodes cannot have children");
139 }
140
141
142 public boolean hasChildNodes() {
143 return DOMNodeHelper.hasChildNodes(this);
144 }
145
146 public org.w3c.dom.Node cloneNode(boolean deep) {
147 return DOMNodeHelper.cloneNode(this, deep);
148 }
149
150 public void normalize() {
151 DOMNodeHelper.normalize(this);
152 }
153
154 public boolean isSupported(String feature, String version) {
155 return DOMNodeHelper.isSupported(this, feature, version);
156 }
157
158 public boolean hasAttributes() {
159 return DOMNodeHelper.hasAttributes(this);
160 }
161
162
163
164 public String getData() throws DOMException {
165 return DOMNodeHelper.getData(this);
166 }
167
168 public void setData(String data) throws DOMException {
169 DOMNodeHelper.setData(this, data);
170 }
171
172 public int getLength() {
173 return DOMNodeHelper.getLength(this);
174 }
175
176 public String substringData( int offset, int count) throws DOMException {
177 return DOMNodeHelper.substringData(this, offset, count);
178 }
179
180 public void appendData(String arg) throws DOMException {
181 DOMNodeHelper.appendData(this, arg);
182 }
183
184 public void insertData(int offset, String arg) throws DOMException {
185 DOMNodeHelper.insertData(this, offset, arg);
186 }
187
188 public void deleteData(int offset, int count) throws DOMException {
189 DOMNodeHelper.deleteData(this, offset, count);
190 }
191
192 public void replaceData(
193 int offset, int count, String arg
194 ) throws DOMException {
195 DOMNodeHelper.replaceData(this, offset, count, arg);
196 }
197
198
199
200 public org.w3c.dom.Text splitText(int offset) throws DOMException {
201 if ( isReadOnly() ) {
202 throw new DOMException(
203 DOMException.NO_MODIFICATION_ALLOWED_ERR,
204 "CharacterData node is read only: " + this
205 );
206 }
207 else {
208 String text = getText();
209 int length = (text != null) ? text.length() : 0;
210 if ( offset < 0 || offset >= length ) {
211 throw new DOMException(
212 DOMException.INDEX_SIZE_ERR,
213 "No text at offset: " + offset
214 );
215 }
216 else {
217 String start = text.substring(0, offset);
218 String rest = text.substring(offset);
219 setText(start);
220 Element parent = getParent();
221 Text newText = createText(rest);
222 if ( parent != null ) {
223 parent.add( newText );
224 }
225 return DOMNodeHelper.asDOMText( newText );
226 }
227 }
228 }
229
230
231
232 protected Text createText(String text) {
233 return new DOMText( text );
234 }
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283