KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > result > StreamResult


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.result;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24 import java.io.Writer JavaDoc;
25
26 // Zeus imports
27
import org.enhydra.zeus.Result;
28
29 /**
30  * <p>
31  * <code>{@link Result}</code> provides an interface for all output
32  * means. It details the required contract that other
33  * portions of the Zeus XML data binding framework must
34  * use for processing to an arbitrary output.
35  * </p>
36  * <p>
37  * This implementation of <code>Result</code> deals with
38  * output to Java I/O streams, such as <code>OutputStream</code>s
39  * and <code>Writer</code>s. It allows passing in of these
40  * types of constructs and subsequent output to them, through
41  * various constructors.
42  * </p>
43  *
44  * @author Brett McLaughlin
45  */

46 public class StreamResult extends BaseResult {
47
48     /** <code>Writer</code> to write to */
49     private Writer JavaDoc writer;
50
51     /**
52      * <p>
53      * This will take in an <code>OutputStream</code>
54      * and output XML to that stream when asked. It
55      * also takes in a system ID for resolution of
56      * external references.
57      * </p>
58      *
59      * @param outputStream <code>OutputStream</code> to write to.
60      * @param systemID <code>String</code> system ID for output
61      * document.
62      */

63     public StreamResult(OutputStream JavaDoc outputStream, String JavaDoc systemID) {
64         if (outputStream == null) {
65             throw new IllegalArgumentException JavaDoc("A StreamResult cannot have " +
66                 "a null OutputStream.");
67         }
68         
69         this.writer = new OutputStreamWriter JavaDoc(outputStream);
70         setSystemID(systemID);
71     }
72
73     /**
74      * <p>
75      * This will take in an <code>OutputStream</code>
76      * and output XML to that stream when asked
77      * It assumes that no system ID is present; for
78      * passing in a system ID, see
79      * <code>{@link #StreamResult(OutputStream, String)}</code>.
80      * </p>
81      *
82      * @param outputStream <code>OutputStream</code> to write to.
83      */

84     public StreamResult(OutputStream JavaDoc outputStream) {
85         this(outputStream, null);
86     }
87     
88     /**
89      * <p>
90      * This will take in a <code>Reader</code>
91      * and write XML to that stream when asked. It
92      * also takes in a system ID for resolution of
93      * external references.
94      * </p>
95      *
96      * @param writer <code>Writer</code> to write to.
97      * @param systemID <code>String</code> system ID for input
98      * document.
99      */

100     public StreamResult(Writer JavaDoc writer, String JavaDoc systemID) {
101         if (writer == null) {
102             throw new IllegalArgumentException JavaDoc("A StreamResult cannot have " +
103                 "a null Writer.");
104         }
105         
106         this.writer = writer;
107         setSystemID(systemID);
108     }
109
110     /**
111      * <p>
112      * This will take in a <code>Writer</code>
113      * and write XML to that stream when asked.
114      * It assumes that no system ID is present; for
115      * passing in a system ID, see
116      * <code>{@link #StreamResult(Writer, String)}</code>.
117      * </p>
118      *
119      * @param reader <code>Writer</code> to write to.
120      */

121     public StreamResult(Writer JavaDoc writer) {
122         this(writer, null);
123     }
124
125     /**
126      * <p>
127      * This will write a character stream to the output
128      * facility associated with this <code>Result</code>.
129      * No additional formatting is added, so line feeds,
130      * tabs, or other special characters should be handled
131      * by the code performing output.
132      * </p>
133      *
134      * @param output <code>String</code> to output.
135      * @throws <code>IOException</code> - when errors in output occur.
136      */

137     public void write(String JavaDoc output) throws IOException JavaDoc {
138         if (writer != null) {
139             writer.write(output);
140             writer.flush();
141         } else {
142             throw new IOException JavaDoc("No output source to write to!");
143         }
144     }
145     
146     /**
147      * <p>
148      * This will expose the <code>Writer</code> associated with this
149      * <code>StreamResult</code> for direct manipulation.
150      * </p>
151      *
152      * @return <code>Writer</code> - the output method for this
153      * <code>Result</code>.
154      */

155     public Writer JavaDoc getWriter() throws IOException JavaDoc {
156         if (writer != null) {
157             return writer;
158         } else {
159             throw new IOException JavaDoc("No output source supplied.");
160         }
161     }
162 }
163
Popular Tags