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