001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: DOMText.java,v 1.4 2001/06/20 18:59:23 jstrachan Exp $
008 */
009
010package org.dom4j.dom;
011
012import org.dom4j.Element;
013import org.dom4j.QName;
014import org.dom4j.Text;
015import org.dom4j.tree.DefaultText;
016
017import org.w3c.dom.Document;
018import org.w3c.dom.DOMException;
019import org.w3c.dom.NamedNodeMap;
020import org.w3c.dom.NodeList;
021
022/** <p><code>DOMText</code> implements a Text node which 
023  * supports the W3C DOM API.</p>
024  *
025  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
026  * @version $Revision: 1.4 $
027  */
028public class DOMText extends DefaultText implements org.w3c.dom.Text {
029
030    public DOMText(String text) {
031        super(text);
032    }
033
034    public DOMText(Element parent, String text) {
035        super(parent, text);
036    }
037
038
039    
040    // org.w3c.dom.Node interface
041    //-------------------------------------------------------------------------        
042    public String getNamespaceURI() {
043        return DOMNodeHelper.getNamespaceURI(this);
044    }
045
046    public String getPrefix() {
047        return DOMNodeHelper.getPrefix(this);
048    }
049    
050    public void setPrefix(String prefix) throws DOMException {
051        DOMNodeHelper.setPrefix(this, prefix);
052    }
053
054    public String getLocalName() {
055        return DOMNodeHelper.getLocalName(this);
056    }
057
058    public String getNodeName() {
059        return getName();
060    }
061    
062    //already part of API  
063    //
064    //public short getNodeType();
065    
066
067    
068    public String getNodeValue() throws DOMException {
069        return DOMNodeHelper.getNodeValue(this);
070    }
071    
072    public void setNodeValue(String nodeValue) throws DOMException {
073        DOMNodeHelper.setNodeValue(this, nodeValue);
074    }
075        
076
077    public org.w3c.dom.Node getParentNode() {
078        return DOMNodeHelper.getParentNode(this);
079    }
080    
081    public NodeList getChildNodes() {
082        return DOMNodeHelper.getChildNodes(this);
083    }
084
085    public org.w3c.dom.Node getFirstChild() {
086        return DOMNodeHelper.getFirstChild(this);
087    }
088
089    public org.w3c.dom.Node getLastChild() {
090        return DOMNodeHelper.getLastChild(this);
091    }
092
093    public org.w3c.dom.Node getPreviousSibling() {
094        return DOMNodeHelper.getPreviousSibling(this);
095    }
096
097    public org.w3c.dom.Node getNextSibling() {
098        return DOMNodeHelper.getNextSibling(this);
099    }
100
101    public NamedNodeMap getAttributes() {
102        return DOMNodeHelper.getAttributes(this);
103    }
104
105    public Document getOwnerDocument() {
106        return DOMNodeHelper.getOwnerDocument(this);
107    }
108
109    public org.w3c.dom.Node insertBefore(
110        org.w3c.dom.Node newChild, 
111        org.w3c.dom.Node refChild
112    ) throws DOMException {
113        return DOMNodeHelper.insertBefore(this, newChild, refChild);
114    }
115
116    public org.w3c.dom.Node replaceChild(
117        org.w3c.dom.Node newChild, 
118        org.w3c.dom.Node oldChild
119    ) throws DOMException {
120        return DOMNodeHelper.replaceChild(this, newChild, oldChild);
121    }
122
123    public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) throws DOMException {
124        return DOMNodeHelper.removeChild(this, oldChild);
125    }
126
127    public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild) throws DOMException {
128        return DOMNodeHelper.appendChild(this, newChild);
129    }
130
131    public boolean hasChildNodes() {
132        return DOMNodeHelper.hasChildNodes(this);
133    }
134
135    public org.w3c.dom.Node cloneNode(boolean deep) {
136        return DOMNodeHelper.cloneNode(this, deep);
137    }
138
139    public void normalize() {
140        DOMNodeHelper.normalize(this);
141    }
142
143    public boolean isSupported(String feature, String version) {
144        return DOMNodeHelper.isSupported(this, feature, version);
145    }
146
147    public boolean hasAttributes() {
148        return DOMNodeHelper.hasAttributes(this);
149    }
150    
151    // org.w3c.dom.CharacterData interface
152    //-------------------------------------------------------------------------        
153    public String getData() throws DOMException {
154        return DOMNodeHelper.getData(this);
155    }
156    
157    public void setData(String data) throws DOMException {
158        DOMNodeHelper.setData(this, data);
159    }
160
161    public int getLength() {
162        return DOMNodeHelper.getLength(this);
163    }
164
165    public String substringData( int offset, int count) throws DOMException {
166        return DOMNodeHelper.substringData(this, offset, count);
167    }
168
169    public void appendData(String arg) throws DOMException {
170        DOMNodeHelper.appendData(this, arg);
171    }
172
173    public void insertData(int offset, String arg) throws DOMException {
174        DOMNodeHelper.insertData(this, offset, arg);
175    }
176
177    public void deleteData(int offset, int count) throws DOMException {
178        DOMNodeHelper.deleteData(this, offset, count);
179    }
180
181    public void replaceData( 
182        int offset, int count, String arg 
183    ) throws DOMException {
184        DOMNodeHelper.replaceData(this, offset, count, arg);
185    }
186    
187    // org.w3c.dom.Text interface
188    //-------------------------------------------------------------------------            
189    public org.w3c.dom.Text splitText(int offset) throws DOMException {
190        if ( isReadOnly() ) {
191            throw new DOMException( 
192                DOMException.NO_MODIFICATION_ALLOWED_ERR,
193                "CharacterData node is read only: " + this
194            );
195        }
196        else {
197            String text = getText();
198            int length = (text != null) ? text.length() : 0;
199            if ( offset < 0 || offset >= length ) {
200                throw new DOMException( 
201                    DOMException.INDEX_SIZE_ERR, 
202                    "No text at offset: " + offset
203                );
204            }
205            else {
206                String start = text.substring(0, offset);
207                String rest = text.substring(offset);
208                setText(start);
209                Element parent = getParent();
210                Text newText = createText(rest);
211                if ( parent != null ) {
212                    parent.add( newText );
213                }
214                return DOMNodeHelper.asDOMText( newText );
215            }
216        }
217    }
218    
219    // Implementation methods
220    //-------------------------------------------------------------------------            
221    protected Text createText(String text) {
222        return new DOMText( text );
223    }
224}
225
226
227
228
229/*
230 * Redistribution and use of this software and associated documentation
231 * ("Software"), with or without modification, are permitted provided
232 * that the following conditions are met:
233 *
234 * 1. Redistributions of source code must retain copyright
235 *    statements and notices.  Redistributions must also contain a
236 *    copy of this document.
237 *
238 * 2. Redistributions in binary form must reproduce the
239 *    above copyright notice, this list of conditions and the
240 *    following disclaimer in the documentation and/or other
241 *    materials provided with the distribution.
242 *
243 * 3. The name "DOM4J" must not be used to endorse or promote
244 *    products derived from this Software without prior written
245 *    permission of MetaStuff, Ltd.  For written permission,
246 *    please contact dom4j-info@metastuff.com.
247 *
248 * 4. Products derived from this Software may not be called "DOM4J"
249 *    nor may "DOM4J" appear in their names without prior written
250 *    permission of MetaStuff, Ltd. DOM4J is a registered
251 *    trademark of MetaStuff, Ltd.
252 *
253 * 5. Due credit should be given to the DOM4J Project
254 *    (http://dom4j.org/).
255 *
256 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
257 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
258 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
259 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
260 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
261 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
262 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
263 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
265 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
266 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
267 * OF THE POSSIBILITY OF SUCH DAMAGE.
268 *
269 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
270 *
271 * $Id: DOMText.java,v 1.4 2001/06/20 18:59:23 jstrachan Exp $
272 */