KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > io > DocumentInputSource


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.io;
9
10 import java.io.IOException JavaDoc;
11 import java.io.Reader JavaDoc;
12 import java.io.StringReader JavaDoc;
13 import java.io.StringWriter JavaDoc;
14
15 import org.dom4j.Document;
16
17 import org.xml.sax.InputSource JavaDoc;
18
19 /**
20  * <p>
21  * <code>DocumentInputSource</code> implements a SAX {@link InputSource}for a
22  * {@link Document}.
23  * </p>
24  *
25  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
26  * @version $Revision: 1.8 $
27  */

28 class DocumentInputSource extends InputSource JavaDoc {
29     /** The document source */
30     private Document document;
31
32     public DocumentInputSource() {
33     }
34
35     public DocumentInputSource(Document document) {
36         this.document = document;
37         setSystemId(document.getName());
38     }
39
40     // Properties
41
// -------------------------------------------------------------------------
42

43     /**
44      * DOCUMENT ME!
45      *
46      * @return the document which is being used as the SAX {@link InputSource}
47      */

48     public Document getDocument() {
49         return document;
50     }
51
52     /**
53      * Sets the document used as the SAX {@link InputSource}
54      *
55      * @param document
56      * DOCUMENT ME!
57      */

58     public void setDocument(Document document) {
59         this.document = document;
60         setSystemId(document.getName());
61     }
62
63     // Overloaded methods
64
// -------------------------------------------------------------------------
65

66     /**
67      * This method is not supported as this source is always a {@linkDocument}
68      * instance.
69      *
70      * @param characterStream
71      * DOCUMENT ME!
72      *
73      * @throws UnsupportedOperationException
74      * as this method is unsupported
75      */

76     public void setCharacterStream(Reader JavaDoc characterStream)
77             throws UnsupportedOperationException JavaDoc {
78         throw new UnsupportedOperationException JavaDoc();
79     }
80
81     /**
82      * Note this method is quite inefficent, it turns the in memory XML tree
83      * object model into a single block of text which can then be read by other
84      * XML parsers. Should only be used with care.
85      *
86      * @return DOCUMENT ME!
87      */

88     public Reader JavaDoc getCharacterStream() {
89         try {
90             StringWriter JavaDoc out = new StringWriter JavaDoc();
91             XMLWriter writer = new XMLWriter(out);
92             writer.write(document);
93             writer.flush();
94
95             return new StringReader JavaDoc(out.toString());
96         } catch (final IOException JavaDoc e) {
97             // this should never really happen
98
// but for completeness we'll return a Reader
99
// with the embedded exception inside it
100
return new Reader JavaDoc() {
101                 public int read(char[] ch, int offset, int length)
102                         throws IOException JavaDoc {
103                     throw e;
104                 }
105
106                 public void close() throws IOException JavaDoc {
107                 }
108             };
109         }
110     }
111 }
112
113 /*
114  * Redistribution and use of this software and associated documentation
115  * ("Software"), with or without modification, are permitted provided that the
116  * following conditions are met:
117  *
118  * 1. Redistributions of source code must retain copyright statements and
119  * notices. Redistributions must also contain a copy of this document.
120  *
121  * 2. Redistributions in binary form must reproduce the above copyright notice,
122  * this list of conditions and the following disclaimer in the documentation
123  * and/or other materials provided with the distribution.
124  *
125  * 3. The name "DOM4J" must not be used to endorse or promote products derived
126  * from this Software without prior written permission of MetaStuff, Ltd. For
127  * written permission, please contact dom4j-info@metastuff.com.
128  *
129  * 4. Products derived from this Software may not be called "DOM4J" nor may
130  * "DOM4J" appear in their names without prior written permission of MetaStuff,
131  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
132  *
133  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
134  *
135  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
136  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
137  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
138  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
139  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
140  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
141  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
142  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
143  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
144  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
145  * POSSIBILITY OF SUCH DAMAGE.
146  *
147  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
148  */

149
Popular Tags