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: AbstractDocumentType.java,v 1.11 2001/10/12 11:05:01 jstrachan Exp $
008 */
009
010package org.dom4j.tree;
011
012import java.io.IOException;
013import java.io.Writer;
014import java.util.HashMap;
015import java.util.List;
016import java.util.Map;
017import java.util.Iterator;
018import java.util.StringTokenizer;
019
020import org.dom4j.DocumentType;
021import org.dom4j.Element;
022import org.dom4j.Visitor;
023
024/** <p><code>AbstractDocumentType</code> is an abstract base class for 
025  * tree implementors to use for implementation inheritence.</p>
026  *
027  * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
028  * @version $Revision: 1.11 $
029  */
030public abstract class AbstractDocumentType extends AbstractNode implements DocumentType {
031
032    public AbstractDocumentType() {
033    }
034    
035    public short getNodeType() {
036        return DOCUMENT_TYPE_NODE;
037    }
038
039    public String getName() {
040        return getElementName();
041    }
042    
043    public void setName(String name) {
044        setElementName(name);
045    }
046    
047    public String getPath(Element context) {
048        // not available in XPath
049        return "";
050    }
051
052    public String getUniquePath(Element context) {
053        // not available in XPath
054        return "";
055    }
056
057    /** Returns the text format of the declarations if applicable, or the empty String */
058    public String getText() {
059        List list = getInternalDeclarations();
060        if ( list != null && list.size() > 0 ) {
061            StringBuffer buffer = new StringBuffer();
062            Iterator iter = list.iterator(); 
063            if ( iter.hasNext() ) {
064                Object decl = iter.next();
065                buffer.append( decl.toString() );
066                while ( iter.hasNext() ) {
067                    decl = iter.next();
068                    buffer.append( "\n" );
069                    buffer.append( decl.toString() );
070                }
071            }
072            return buffer.toString();
073        }
074        return "";
075    }
076    public String toString() {
077        return super.toString() + " [DocumentType: " + asXML() + "]";
078    }
079
080    public String asXML() {
081        StringBuffer buffer = new StringBuffer( "<!DOCTYPE " );
082        buffer.append( getElementName() );
083        
084        boolean hasPublicID = false;
085        String publicID = getPublicID();
086        
087        if ( publicID != null && publicID.length() > 0 ) {
088            buffer.append( " PUBLIC \"" );
089            buffer.append( publicID );
090            buffer.append( "\"" );
091            hasPublicID = true;
092        }
093        
094        String systemID = getSystemID();
095        if ( systemID != null && systemID.length() > 0 ) {
096            if (!hasPublicID) {
097                buffer.append(" SYSTEM");
098            }
099            buffer.append( " \"" );
100            buffer.append( systemID );
101            buffer.append( "\"" );
102        }
103        buffer.append(">");
104        return buffer.toString();
105    }
106    
107    public void write(Writer writer) throws IOException {
108        writer.write( "<!DOCTYPE " );
109        writer.write( getElementName() );
110        
111        boolean hasPublicID = false;
112        String publicID = getPublicID();
113        
114        if ( publicID != null && publicID.length() > 0 ) {
115            writer.write( " PUBLIC \"" );
116            writer.write( publicID );
117            writer.write( "\"" );
118            hasPublicID = true;
119        }
120        
121        String systemID = getSystemID();
122        if ( systemID != null && systemID.length() > 0 ) {
123            if (!hasPublicID) {
124                writer.write(" SYSTEM");
125            }
126            writer.write( " \"" );
127            writer.write( systemID );
128            writer.write( "\"" );
129        }
130        List list = getInternalDeclarations();
131        if ( list != null && list.size() > 0 ) {
132            writer.write( " [" );
133            for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
134                Object decl = iter.next();
135                writer.write( "\n  " );
136                writer.write( decl.toString() );
137            }
138            writer.write( "\n]" );
139        }
140        writer.write(">");
141    }
142    
143    public void accept(Visitor visitor) {
144        visitor.visit(this);
145    }
146}
147
148
149
150
151
152
153/*
154 * Redistribution and use of this software and associated documentation
155 * ("Software"), with or without modification, are permitted provided
156 * that the following conditions are met:
157 *
158 * 1. Redistributions of source code must retain copyright
159 *    statements and notices.  Redistributions must also contain a
160 *    copy of this document.
161 *
162 * 2. Redistributions in binary form must reproduce the
163 *    above copyright notice, this list of conditions and the
164 *    following disclaimer in the documentation and/or other
165 *    materials provided with the distribution.
166 *
167 * 3. The name "DOM4J" must not be used to endorse or promote
168 *    products derived from this Software without prior written
169 *    permission of MetaStuff, Ltd.  For written permission,
170 *    please contact dom4j-info@metastuff.com.
171 *
172 * 4. Products derived from this Software may not be called "DOM4J"
173 *    nor may "DOM4J" appear in their names without prior written
174 *    permission of MetaStuff, Ltd. DOM4J is a registered
175 *    trademark of MetaStuff, Ltd.
176 *
177 * 5. Due credit should be given to the DOM4J Project
178 *    (http://dom4j.org/).
179 *
180 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
181 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
182 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
183 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
184 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
185 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
186 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
187 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
188 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
189 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
190 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
191 * OF THE POSSIBILITY OF SUCH DAMAGE.
192 *
193 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
194 *
195 * $Id: AbstractDocumentType.java,v 1.11 2001/10/12 11:05:01 jstrachan Exp $
196 */