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: DocumentInputSource.java,v 1.2 2001/04/12 10:54:45 jstrachan Exp $
008 */
009
010package org.dom4j.io;
011
012import java.io.IOException;
013import java.io.Reader;
014import java.io.StringReader;
015import java.io.StringWriter;
016
017import org.dom4j.Document;
018
019import org.xml.sax.InputSource;
020
021
022/** <p><code>DocumentInputSource</code> implements a SAX {@link InputSource}
023  * for a {@link Document}.</p>
024  *
025  * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
026  * @version $Revision: 1.2 $
027  */
028class DocumentInputSource extends InputSource {
029
030    /** The document source */
031    private Document document;
032    
033
034    public DocumentInputSource() {
035    }
036
037    public DocumentInputSource(Document document) {
038        this.document = document;
039        setSystemId( document.getName() );
040    }
041
042
043    // Properties
044    //-------------------------------------------------------------------------                
045
046    /** @return the document which is being used as the SAX {@link InputSource}
047      */
048    public Document getDocument() {
049        return document;
050    }
051
052    /** Sets the document used as the SAX {@link InputSource}
053      */
054    public void setDocument(Document document) {
055        this.document = document;
056        setSystemId( document.getName() );
057    }
058
059
060    // Overloaded methods
061    //-------------------------------------------------------------------------                
062
063    /** This method is not supported as this source is always a 
064      * {@link Document} instance.
065      *
066      * @throws UnsupportedOperationException as this method is unsupported
067      */
068    public void setCharacterStream(Reader characterStream) 
069            throws UnsupportedOperationException {
070        throw new UnsupportedOperationException();
071    }
072
073    /** Note this method is quite inefficent, it turns the in memory XML tree
074      * object model into a single block of text which can then be read by
075      * other XML parsers. Should only be used with care.
076      */
077    public Reader getCharacterStream() {
078        try {
079            StringWriter out = new StringWriter();
080            XMLWriter writer = new XMLWriter( out );
081            writer.write( document );
082            writer.flush();
083            return new StringReader( out.toString() );
084        }
085        catch (final IOException e) {
086            // this should never really happen 
087            // but for completeness we'll return a Reader
088            // with the embedded exception inside it
089            return new Reader() {
090                public int read(char ch[], int offset, int length) 
091                    throws IOException {
092                    throw e;
093                }
094                public void close() throws IOException {
095                }
096            };
097        }
098    }
099}
100
101
102
103
104
105
106
107/*
108 * Redistribution and use of this software and associated documentation
109 * ("Software"), with or without modification, are permitted provided
110 * that the following conditions are met:
111 *
112 * 1. Redistributions of source code must retain copyright
113 *    statements and notices.  Redistributions must also contain a
114 *    copy of this document.
115 *
116 * 2. Redistributions in binary form must reproduce the
117 *    above copyright notice, this list of conditions and the
118 *    following disclaimer in the documentation and/or other
119 *    materials provided with the distribution.
120 *
121 * 3. The name "DOM4J" must not be used to endorse or promote
122 *    products derived from this Software without prior written
123 *    permission of MetaStuff, Ltd.  For written permission,
124 *    please contact dom4j-info@metastuff.com.
125 *
126 * 4. Products derived from this Software may not be called "DOM4J"
127 *    nor may "DOM4J" appear in their names without prior written
128 *    permission of MetaStuff, Ltd. DOM4J is a registered
129 *    trademark of MetaStuff, Ltd.
130 *
131 * 5. Due credit should be given to the DOM4J Project
132 *    (http://dom4j.org/).
133 *
134 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
135 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
136 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
137 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
138 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
139 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
140 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
141 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
142 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
143 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
144 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
145 * OF THE POSSIBILITY OF SUCH DAMAGE.
146 *
147 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
148 *
149 * $Id: DocumentInputSource.java,v 1.2 2001/04/12 10:54:45 jstrachan Exp $
150 */