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.tree;
    9
    10  import java.io.IOException;
    11  import java.io.StringWriter;
    12  import java.io.Writer;
    13
    14  import org.dom4j.CDATA;
    15  import org.dom4j.Visitor;
    16
    17  /***
    18   * <p>
    19   * <code>AbstractCDATA</code> is an abstract base class for tree implementors
    20   * to use for implementation inheritence.
    21   * </p>
    22   *
    23   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    24   * @version $Revision: 1.12 $
    25   */
    26  public abstract class AbstractCDATA extends AbstractCharacterData implements
    27          CDATA {
    28      public AbstractCDATA() {
    29      }
    30
    31      public short getNodeType() {
    32          return CDATA_SECTION_NODE;
    33      }
    34
    35      public String toString() {
    36          return super.toString() + " [CDATA: \"" + getText() + "\"]";
    37      }
    38
    39      public String asXML() {
    40          StringWriter writer = new StringWriter();
    41
    42          try {
    43              write(writer);
    44          } catch (IOException e) {
    45              // will not happen since we are using a StringWriter!
    46          }
    47
    48          return writer.toString();
    49      }
    50
    51      public void write(Writer writer) throws IOException {
    52          writer.write("<![CDATA[");
    53
    54          if (getText() != null) {
    55              writer.write(getText());
    56          }
    57
    58          writer.write("]]>");
    59      }
    60
    61      public void accept(Visitor visitor) {
    62          visitor.visit(this);
    63      }
    64  }
    65
    66  /*
    67   * Redistribution and use of this software and associated documentation
    68   * ("Software"), with or without modification, are permitted provided that the
    69   * following conditions are met:
    70   *
    71   * 1. Redistributions of source code must retain copyright statements and
    72   * notices. Redistributions must also contain a copy of this document.
    73   *
    74   * 2. Redistributions in binary form must reproduce the above copyright notice,
    75   * this list of conditions and the following disclaimer in the documentation
    76   * and/or other materials provided with the distribution.
    77   *
    78   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    79   * from this Software without prior written permission of MetaStuff, Ltd. For
    80   * written permission, please contact dom4j-info@metastuff.com.
    81   *
    82   * 4. Products derived from this Software may not be called "DOM4J" nor may
    83   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    84   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    85   *
    86   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    87   *
    88   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    89   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    90   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    91   * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    92   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    93   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    94   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    95   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    96   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    97   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    98   * POSSIBILITY OF SUCH DAMAGE.
    99   *
    100  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    101  */