KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > dom > DOMText


1 /*
2  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
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 JavaDoc;
15 import org.w3c.dom.Document JavaDoc;
16 import org.w3c.dom.NamedNodeMap JavaDoc;
17 import org.w3c.dom.NodeList JavaDoc;
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 JavaDoc {
28     public DOMText(String JavaDoc text) {
29         super(text);
30     }
31
32     public DOMText(Element parent, String JavaDoc text) {
33         super(parent, text);
34     }
35
36     // org.w3c.dom.Node interface
37
// -------------------------------------------------------------------------
38
public boolean supports(String JavaDoc feature, String JavaDoc version) {
39         return DOMNodeHelper.supports(this, feature, version);
40     }
41
42     public String JavaDoc getNamespaceURI() {
43         return DOMNodeHelper.getNamespaceURI(this);
44     }
45
46     public String JavaDoc getPrefix() {
47         return DOMNodeHelper.getPrefix(this);
48     }
49
50     public void setPrefix(String JavaDoc prefix) throws DOMException JavaDoc {
51         DOMNodeHelper.setPrefix(this, prefix);
52     }
53
54     public String JavaDoc getLocalName() {
55         return DOMNodeHelper.getLocalName(this);
56     }
57
58     public String JavaDoc getNodeName() {
59         return "#text";
60     }
61
62     // already part of API
63
//
64
// public short getNodeType();
65
public String JavaDoc getNodeValue() throws DOMException JavaDoc {
66         return DOMNodeHelper.getNodeValue(this);
67     }
68
69     public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc {
70         DOMNodeHelper.setNodeValue(this, nodeValue);
71     }
72
73     public org.w3c.dom.Node JavaDoc getParentNode() {
74         return DOMNodeHelper.getParentNode(this);
75     }
76
77     public NodeList JavaDoc getChildNodes() {
78         return DOMNodeHelper.getChildNodes(this);
79     }
80
81     public org.w3c.dom.Node JavaDoc getFirstChild() {
82         return DOMNodeHelper.getFirstChild(this);
83     }
84
85     public org.w3c.dom.Node JavaDoc getLastChild() {
86         return DOMNodeHelper.getLastChild(this);
87     }
88
89     public org.w3c.dom.Node JavaDoc getPreviousSibling() {
90         return DOMNodeHelper.getPreviousSibling(this);
91     }
92
93     public org.w3c.dom.Node JavaDoc getNextSibling() {
94         return DOMNodeHelper.getNextSibling(this);
95     }
96
97     public NamedNodeMap JavaDoc getAttributes() {
98         return null;
99     }
100
101     public Document JavaDoc getOwnerDocument() {
102         return DOMNodeHelper.getOwnerDocument(this);
103     }
104
105     public org.w3c.dom.Node JavaDoc insertBefore(org.w3c.dom.Node JavaDoc newChild,
106             org.w3c.dom.Node JavaDoc refChild) throws DOMException JavaDoc {
107         checkNewChildNode(newChild);
108
109         return DOMNodeHelper.insertBefore(this, newChild, refChild);
110     }
111
112     public org.w3c.dom.Node JavaDoc replaceChild(org.w3c.dom.Node JavaDoc newChild,
113             org.w3c.dom.Node JavaDoc oldChild) throws DOMException JavaDoc {
114         checkNewChildNode(newChild);
115
116         return DOMNodeHelper.replaceChild(this, newChild, oldChild);
117     }
118
119     public org.w3c.dom.Node JavaDoc removeChild(org.w3c.dom.Node JavaDoc oldChild)
120             throws DOMException JavaDoc {
121         return DOMNodeHelper.removeChild(this, oldChild);
122     }
123
124     public org.w3c.dom.Node JavaDoc appendChild(org.w3c.dom.Node JavaDoc newChild)
125             throws DOMException JavaDoc {
126         checkNewChildNode(newChild);
127
128         return DOMNodeHelper.appendChild(this, newChild);
129     }
130
131     private void checkNewChildNode(org.w3c.dom.Node JavaDoc newChild)
132             throws DOMException JavaDoc {
133         throw new DOMException JavaDoc(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 JavaDoc 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 JavaDoc feature, String JavaDoc version) {
150         return DOMNodeHelper.isSupported(this, feature, version);
151     }
152
153     public boolean hasAttributes() {
154         return DOMNodeHelper.hasAttributes(this);
155     }
156
157     // org.w3c.dom.CharacterData interface
158
// -------------------------------------------------------------------------
159
public String JavaDoc getData() throws DOMException JavaDoc {
160         return DOMNodeHelper.getData(this);
161     }
162
163     public void setData(String JavaDoc data) throws DOMException JavaDoc {
164         DOMNodeHelper.setData(this, data);
165     }
166
167     public int getLength() {
168         return DOMNodeHelper.getLength(this);
169     }
170
171     public String JavaDoc substringData(int offset, int count) throws DOMException JavaDoc {
172         return DOMNodeHelper.substringData(this, offset, count);
173     }
174
175     public void appendData(String JavaDoc arg) throws DOMException JavaDoc {
176         DOMNodeHelper.appendData(this, arg);
177     }
178
179     public void insertData(int offset, String JavaDoc arg) throws DOMException JavaDoc {
180         DOMNodeHelper.insertData(this, offset, arg);
181     }
182
183     public void deleteData(int offset, int count) throws DOMException JavaDoc {
184         DOMNodeHelper.deleteData(this, offset, count);
185     }
186
187     public void replaceData(int offset, int count, String JavaDoc arg)
188             throws DOMException JavaDoc {
189         DOMNodeHelper.replaceData(this, offset, count, arg);
190     }
191
192     // org.w3c.dom.Text interface
193
// -------------------------------------------------------------------------
194
public org.w3c.dom.Text JavaDoc splitText(int offset) throws DOMException JavaDoc {
195         if (isReadOnly()) {
196             throw new DOMException JavaDoc(DOMException.NO_MODIFICATION_ALLOWED_ERR,
197                     "CharacterData node is read only: " + this);
198         } else {
199             String JavaDoc text = getText();
200             int length = (text != null) ? text.length() : 0;
201
202             if ((offset < 0) || (offset >= length)) {
203                 throw new DOMException JavaDoc(DOMException.INDEX_SIZE_ERR,
204                         "No text at offset: " + offset);
205             } else {
206                 String JavaDoc start = text.substring(0, offset);
207                 String JavaDoc 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     // Implementation methods
223
// -------------------------------------------------------------------------
224
protected Text createText(String JavaDoc text) {
225         return new DOMText(text);
226     }
227 }
228
229 /*
230  * Redistribution and use of this software and associated documentation
231  * ("Software"), with or without modification, are permitted provided that the
232  * following conditions are met:
233  *
234  * 1. Redistributions of source code must retain copyright statements and
235  * notices. Redistributions must also contain a copy of this document.
236  *
237  * 2. Redistributions in binary form must reproduce the above copyright notice,
238  * this list of conditions and the following disclaimer in the documentation
239  * and/or other materials provided with the distribution.
240  *
241  * 3. The name "DOM4J" must not be used to endorse or promote products derived
242  * from this Software without prior written permission of MetaStuff, Ltd. For
243  * written permission, please contact dom4j-info@metastuff.com.
244  *
245  * 4. Products derived from this Software may not be called "DOM4J" nor may
246  * "DOM4J" appear in their names without prior written permission of MetaStuff,
247  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
248  *
249  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
250  *
251  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
252  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
253  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
255  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
256  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
257  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
258  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
259  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261  * POSSIBILITY OF SUCH DAMAGE.
262  *
263  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
264  */

265
Popular Tags