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