KickJava   Java API By Example, From Geeks To Geeks.

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


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.CDATA;
11 import org.dom4j.Element;
12 import org.dom4j.tree.DefaultCDATA;
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>DOMCDATA</code> implements a CDATA Section which supports the W3C DOM
22  * API.
23  * </p>
24  *
25  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
26  * @version $Revision: 1.12 $
27  */

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

266
Popular Tags