View Javadoc

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;
15  import org.w3c.dom.Document;
16  import org.w3c.dom.NamedNodeMap;
17  import org.w3c.dom.NodeList;
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 {
29      public DOMCDATA(String text) {
30          super(text);
31      }
32  
33      public DOMCDATA(Element parent, String text) {
34          super(parent, text);
35      }
36  
37      // org.w3c.dom.Node interface
38      // -------------------------------------------------------------------------
39      public boolean supports(String feature, String version) {
40          return DOMNodeHelper.supports(this, feature, version);
41      }
42  
43      public String getNamespaceURI() {
44          return DOMNodeHelper.getNamespaceURI(this);
45      }
46  
47      public String getPrefix() {
48          return DOMNodeHelper.getPrefix(this);
49      }
50  
51      public void setPrefix(String prefix) throws DOMException {
52          DOMNodeHelper.setPrefix(this, prefix);
53      }
54  
55      public String getLocalName() {
56          return DOMNodeHelper.getLocalName(this);
57      }
58  
59      public String getNodeName() {
60          return "#cdata-section";
61      }
62  
63      // already part of API
64      //
65      // public short getNodeType();
66      public String getNodeValue() throws DOMException {
67          return DOMNodeHelper.getNodeValue(this);
68      }
69  
70      public void setNodeValue(String nodeValue) throws DOMException {
71          DOMNodeHelper.setNodeValue(this, nodeValue);
72      }
73  
74      public org.w3c.dom.Node getParentNode() {
75          return DOMNodeHelper.getParentNode(this);
76      }
77  
78      public NodeList getChildNodes() {
79          return DOMNodeHelper.getChildNodes(this);
80      }
81  
82      public org.w3c.dom.Node getFirstChild() {
83          return DOMNodeHelper.getFirstChild(this);
84      }
85  
86      public org.w3c.dom.Node getLastChild() {
87          return DOMNodeHelper.getLastChild(this);
88      }
89  
90      public org.w3c.dom.Node getPreviousSibling() {
91          return DOMNodeHelper.getPreviousSibling(this);
92      }
93  
94      public org.w3c.dom.Node getNextSibling() {
95          return DOMNodeHelper.getNextSibling(this);
96      }
97  
98      public NamedNodeMap getAttributes() {
99          return null;
100     }
101 
102     public Document getOwnerDocument() {
103         return DOMNodeHelper.getOwnerDocument(this);
104     }
105 
106     public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild,
107             org.w3c.dom.Node refChild) throws DOMException {
108         checkNewChildNode(newChild);
109 
110         return DOMNodeHelper.insertBefore(this, newChild, refChild);
111     }
112 
113     public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild,
114             org.w3c.dom.Node oldChild) throws DOMException {
115         checkNewChildNode(newChild);
116 
117         return DOMNodeHelper.replaceChild(this, newChild, oldChild);
118     }
119 
120     public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild)
121             throws DOMException {
122         return DOMNodeHelper.removeChild(this, oldChild);
123     }
124 
125     public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild)
126             throws DOMException {
127         checkNewChildNode(newChild);
128 
129         return DOMNodeHelper.appendChild(this, newChild);
130     }
131 
132     private void checkNewChildNode(org.w3c.dom.Node newChild)
133             throws DOMException {
134         throw new DOMException(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 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 feature, String 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 getData() throws DOMException {
161         return DOMNodeHelper.getData(this);
162     }
163 
164     public void setData(String data) throws DOMException {
165         DOMNodeHelper.setData(this, data);
166     }
167 
168     public int getLength() {
169         return DOMNodeHelper.getLength(this);
170     }
171 
172     public String substringData(int offset, int count) throws DOMException {
173         return DOMNodeHelper.substringData(this, offset, count);
174     }
175 
176     public void appendData(String arg) throws DOMException {
177         DOMNodeHelper.appendData(this, arg);
178     }
179 
180     public void insertData(int offset, String arg) throws DOMException {
181         DOMNodeHelper.insertData(this, offset, arg);
182     }
183 
184     public void deleteData(int offset, int count) throws DOMException {
185         DOMNodeHelper.deleteData(this, offset, count);
186     }
187 
188     public void replaceData(int offset, int count, String arg)
189             throws DOMException {
190         DOMNodeHelper.replaceData(this, offset, count, arg);
191     }
192 
193     // org.w3c.dom.Text interface
194     // -------------------------------------------------------------------------
195     public org.w3c.dom.Text splitText(int offset) throws DOMException {
196         if (isReadOnly()) {
197             throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
198                     "CharacterData node is read only: " + this);
199         } else {
200             String text = getText();
201             int length = (text != null) ? text.length() : 0;
202 
203             if ((offset < 0) || (offset >= length)) {
204                 throw new DOMException(DOMException.INDEX_SIZE_ERR,
205                         "No text at offset: " + offset);
206             } else {
207                 String start = text.substring(0, offset);
208                 String 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 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  */