KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > source > StreamSource


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.source;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.Reader JavaDoc;
25
26 // Zeus imports
27
import org.enhydra.zeus.Source;
28
29 // JDOM imports
30
import org.jdom.Document;
31 import org.jdom.JDOMException;
32 import org.jdom.input.SAXBuilder;
33
34 /**
35  * <p>
36  * <code>{@link Source}</code> provides an interface for all input
37  * means. It details the required contract that other
38  * portions of the Zeus XML data binding framework must
39  * use for processing of an arbitrary input.
40  * </p>
41  * <p>
42  * This implementation of <code>Source</code> deals with
43  * input from Java I/O streams, such as <code>InputStream</code>s
44  * and <code>Reader</code>s. It allows passing in of these
45  * types of constructs and subsequent reading of them, through
46  * various constructors.
47  * </p>
48  *
49  * @author Brett McLaughlin
50  */

51 public class StreamSource extends BaseSource {
52     
53     /** <code>Reader</code> to read from */
54     private Reader JavaDoc reader;
55
56     /** <code>Document</code> constructed from input */
57     private Document document;
58
59     /**
60      * <p>
61      * This will take in an <code>InputStream</code>
62      * and read XML from that stream when asked,
63      * converting the read XML into a
64      * <code>{@link Document}</code> as needed. It
65      * also takes in a system ID for resolution of
66      * external references.
67      * </p>
68      *
69      * @param inputStream <code>InputStream</code> to read from.
70      * @param systemID <code>String</code> system ID for input
71      * document.
72      */

73     public StreamSource(InputStream JavaDoc inputStream, String JavaDoc systemID) {
74         if (inputStream == null) {
75             throw new IllegalArgumentException JavaDoc("A StreamSource cannot " +
76                 "have a null InputStream.");
77         }
78         
79         this.reader = new InputStreamReader JavaDoc(inputStream);
80         setSystemID(systemID);
81     }
82
83     /**
84      * <p>
85      * This will take in an <code>InputStream</code>
86      * and read XML from that stream when asked,
87      * converting the read XML into a
88      * <code>{@link Document}</code> as needed.
89      * It assumes that no system ID is present; for
90      * passing in a system ID, see
91      * <code>{@link #StreamSource(InputStream, String)}</code>.
92      * </p>
93      *
94      * @param inputStream <code>InputStream</code> to read from.
95      */

96     public StreamSource(InputStream JavaDoc inputStream) {
97         this(inputStream, null);
98     }
99     
100     /**
101      * <p>
102      * This will take in a <code>Reader</code>
103      * and read XML from that stream when asked,
104      * converting the read XML into a
105      * <code>{@link Document}</code> as needed. It
106      * also takes in a system ID for resolution of
107      * external references.
108      * </p>
109      *
110      * @param reader <code>Reader</code> to read from.
111      * @param systemID <code>String</code> system ID for input
112      * document.
113      */

114     public StreamSource(Reader JavaDoc reader, String JavaDoc systemID) {
115         if (reader == null) {
116             throw new IllegalArgumentException JavaDoc("A StreamSource cannot " +
117                 "have a null Reader.");
118         }
119         
120         this.reader = reader;
121         setSystemID(systemID);
122     }
123
124     /**
125      * <p>
126      * This will take in a <code>Reader</code>
127      * and read XML from that stream when asked,
128      * converting the read XML into a
129      * <code>{@link Document}</code> as needed.
130      * It assumes that no system ID is present; for
131      * passing in a system ID, see
132      * <code>{@link #StreamSource(Reader, String)}</code>.
133      * </p>
134      *
135      * @param reader <code>Reader</code> to read from.
136      */

137     public StreamSource(Reader JavaDoc reader) {
138         this(reader, null);
139     }
140     
141     /**
142      * <p>
143      * This will return a JDOM <code>Document</code> that
144      * represents the input source.
145      * </p>
146      *
147      * @return <code>Document</code> - the JDOM representation
148      * of the input source.
149      * @throws <code>IOException</code> - when construction of
150      * a <code>Document</code> generates errors.
151      */

152     public Document getDocument() throws IOException JavaDoc {
153         try {
154             SAXBuilder builder = new SAXBuilder();
155             if (entityResolver != null) {
156                 builder.setEntityResolver(entityResolver);
157             }
158             if (reader != null) {
159                 document = builder.build(reader);
160             } else {
161                 throw new IOException JavaDoc("No source to read XML from!");
162             }
163     
164             return document;
165         } catch (JDOMException e) {
166             throw new IOException JavaDoc(e.getMessage());
167         }
168     }
169 }
170
Popular Tags