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