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.Writer;
    12  import java.util.Iterator;
    13  import java.util.List;
    14
    15  import org.dom4j.DocumentType;
    16  import org.dom4j.Element;
    17  import org.dom4j.Visitor;
    18
    19  /***
    20   * <p>
    21   * <code>AbstractDocumentType</code> is an abstract base class for tree
    22   * implementors to use for implementation inheritence.
    23   * </p>
    24   *
    25   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    26   * @version $Revision: 1.17 $
    27   */
    28  public abstract class AbstractDocumentType extends AbstractNode implements
    29          DocumentType {
    30      public AbstractDocumentType() {
    31      }
    32
    33      public short getNodeType() {
    34          return DOCUMENT_TYPE_NODE;
    35      }
    36
    37      public String getName() {
    38          return getElementName();
    39      }
    40
    41      public void setName(String name) {
    42          setElementName(name);
    43      }
    44
    45      public String getPath(Element context) {
    46          // not available in XPath
    47          return "";
    48      }
    49
    50      public String getUniquePath(Element context) {
    51          // not available in XPath
    52          return "";
    53      }
    54
    55      /***
    56       * Returns the text format of the declarations if applicable, or the empty
    57       * String
    58       *
    59       * @return DOCUMENT ME!
    60       */
    61      public String getText() {
    62          List list = getInternalDeclarations();
    63
    64          if ((list != null) && (list.size() > 0)) {
    65              StringBuffer buffer = new StringBuffer();
    66              Iterator iter = list.iterator();
    67
    68              if (iter.hasNext()) {
    69                  Object decl = iter.next();
    70                  buffer.append(decl.toString());
    71
    72                  while (iter.hasNext()) {
    73                      decl = iter.next();
    74                      buffer.append("\n");
    75                      buffer.append(decl.toString());
    76                  }
    77              }
    78
    79              return buffer.toString();
    80          }
    81
    82          return "";
    83      }
    84
    85      public String toString() {
    86          return super.toString() + " [DocumentType: " + asXML() + "]";
    87      }
    88
    89      public String asXML() {
    90          StringBuffer buffer = new StringBuffer("<!DOCTYPE ");
    91          buffer.append(getElementName());
    92
    93          boolean hasPublicID = false;
    94          String publicID = getPublicID();
    95
    96          if ((publicID != null) && (publicID.length() > 0)) {
    97              buffer.append(" PUBLIC \"");
    98              buffer.append(publicID);
    99              buffer.append("\"");
    100             hasPublicID = true;
    101         }
    102
    103         String systemID = getSystemID();
    104
    105         if ((systemID != null) && (systemID.length() > 0)) {
    106             if (!hasPublicID) {
    107                 buffer.append(" SYSTEM");
    108             }
    109
    110             buffer.append(" \"");
    111             buffer.append(systemID);
    112             buffer.append("\"");
    113         }
    114
    115         buffer.append(">");
    116
    117         return buffer.toString();
    118     }
    119
    120     public void write(Writer writer) throws IOException {
    121         writer.write("<!DOCTYPE ");
    122         writer.write(getElementName());
    123
    124         boolean hasPublicID = false;
    125         String publicID = getPublicID();
    126
    127         if ((publicID != null) && (publicID.length() > 0)) {
    128             writer.write(" PUBLIC \"");
    129             writer.write(publicID);
    130             writer.write("\"");
    131             hasPublicID = true;
    132         }
    133
    134         String systemID = getSystemID();
    135
    136         if ((systemID != null) && (systemID.length() > 0)) {
    137             if (!hasPublicID) {
    138                 writer.write(" SYSTEM");
    139             }
    140
    141             writer.write(" \"");
    142             writer.write(systemID);
    143             writer.write("\"");
    144         }
    145
    146         List list = getInternalDeclarations();
    147
    148         if ((list != null) && (list.size() > 0)) {
    149             writer.write(" [");
    150
    151             for (Iterator iter = list.iterator(); iter.hasNext();) {
    152                 Object decl = iter.next();
    153                 writer.write("\n  ");
    154                 writer.write(decl.toString());
    155             }
    156
    157             writer.write("\n]");
    158         }
    159
    160         writer.write(">");
    161     }
    162
    163     public void accept(Visitor visitor) {
    164         visitor.visit(this);
    165     }
    166 }
    167
    168 /*
    169  * Redistribution and use of this software and associated documentation
    170  * ("Software"), with or without modification, are permitted provided that the
    171  * following conditions are met:
    172  *
    173  * 1. Redistributions of source code must retain copyright statements and
    174  * notices. Redistributions must also contain a copy of this document.
    175  *
    176  * 2. Redistributions in binary form must reproduce the above copyright notice,
    177  * this list of conditions and the following disclaimer in the documentation
    178  * and/or other materials provided with the distribution.
    179  *
    180  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    181  * from this Software without prior written permission of MetaStuff, Ltd. For
    182  * written permission, please contact dom4j-info@metastuff.com.
    183  *
    184  * 4. Products derived from this Software may not be called "DOM4J" nor may
    185  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    186  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    187  *
    188  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    189  *
    190  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    191  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    192  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    193  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    194  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    195  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    196  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    197  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    198  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    199  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    200  * POSSIBILITY OF SUCH DAMAGE.
    201  *
    202  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    203  */