KickJava   Java API By Example, From Geeks To Geeks.

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


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
32 // DTDParser imports
33
import com.wutka.dtd.DTD;
34 import com.wutka.dtd.DTDParser;
35
36 /**
37  * <p>
38  * <code>{@link Source}</code> provides an interface for all input
39  * means. It details the required contract that other
40  * portions of the Zeus XML data binding framework must
41  * use for processing of an arbitrary input.
42  * </p>
43  * <p>
44  * This implementation of <code>DTDSource</code> deals with
45  * input from Java I/O streams, such as <code>InputStream</code>s
46  * and <code>Reader</code>s. It allows passing in of these
47  * types of constructs and subsequent reading of them, through
48  * various constructors.
49  * </p>
50  *
51  * @author Brett McLaughlin
52  */

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

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

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

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

139     public StreamDTDSource(Reader JavaDoc reader) {
140         this(reader, null);
141     }
142     
143     /**
144      * <p>
145      * This will return the <code>DTDParser</code>'s
146      * <code>DTD</code> representation of the supplied
147      * XML DTD.
148      * </p>
149      *
150      * @return <code>DTD</code> - the <code>DTDParser</code>
151      * representation of the input source.
152      * @throws <code>IOException</code> - when construction of
153      * a <code>DTD</code> generates errors.
154      */

155     public DTD getDTD() throws IOException JavaDoc {
156         DTDParser dtdParser = new DTDParser(reader);
157         DTD dtd = dtdParser.parse(true);
158         
159         // Make sure a valid DTD was returned
160
Object JavaDoc items[] = dtd.getItems();
161         if (items.length == 0) {
162             throw new IOException JavaDoc("A viable DTD could not be obtained from " +
163                 "the supplied input source.");
164         }
165         
166         return dtd;
167     }
168 }
169
Popular Tags