View Javadoc

1   /*
2    * Copyright 2001-2004 (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    * $Id: DOMText.java,v 1.10 2004/06/25 08:03:35 maartenc Exp $
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      // org.w3c.dom.Node interface
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      //already part of API
65      //
66      //public short getNodeType();
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     // org.w3c.dom.CharacterData interface
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     // org.w3c.dom.Text interface
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     // Implementation methods
231     //-------------------------------------------------------------------------
232     protected Text createText(String text) {
233         return new DOMText( text );
234     }
235 }
236 
237 
238 
239 
240 /*
241  * Redistribution and use of this software and associated documentation
242  * ("Software"), with or without modification, are permitted provided
243  * that the following conditions are met:
244  *
245  * 1. Redistributions of source code must retain copyright
246  *    statements and notices.  Redistributions must also contain a
247  *    copy of this document.
248  *
249  * 2. Redistributions in binary form must reproduce the
250  *    above copyright notice, this list of conditions and the
251  *    following disclaimer in the documentation and/or other
252  *    materials provided with the distribution.
253  *
254  * 3. The name "DOM4J" must not be used to endorse or promote
255  *    products derived from this Software without prior written
256  *    permission of MetaStuff, Ltd.  For written permission,
257  *    please contact dom4j-info@metastuff.com.
258  *
259  * 4. Products derived from this Software may not be called "DOM4J"
260  *    nor may "DOM4J" appear in their names without prior written
261  *    permission of MetaStuff, Ltd. DOM4J is a registered
262  *    trademark of MetaStuff, Ltd.
263  *
264  * 5. Due credit should be given to the DOM4J Project - 
265  *    http://www.dom4j.org
266  *
267  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
268  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
269  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
270  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
271  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
272  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
273  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
274  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
275  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
276  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
277  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
278  * OF THE POSSIBILITY OF SUCH DAMAGE.
279  *
280  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
281  *
282  * $Id: DOMText.java,v 1.10 2004/06/25 08:03:35 maartenc Exp $
283  */