KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > AbstractStreamWriteableSource


1 /*
2  * Copyright 2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.source;
17
18 import org.apache.avalon.framework.component.ComponentException;
19 import org.apache.avalon.framework.component.ComponentManager;
20 import org.apache.avalon.framework.component.ComponentSelector;
21 import org.apache.cocoon.ProcessingException;
22 import org.apache.cocoon.serialization.Serializer;
23 import org.apache.cocoon.xml.AbstractXMLPipe;
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 /**
31  * This abstract class provides convenience methods to implement
32  * a stream based <code>org.apache.cocoon.environment.WriteableSource</code>.
33  * Implement getOutputStream() to obtain a valid implementation.
34  * <p>
35  * This base implementation creates a <code>ContentHandler</code> by using
36  * the sitemap 'xml' serializer to write SAX events to the stream returned by
37  * <code>getOutputStream()</code>.
38  *
39  * @deprecated Use the new Avalon Excalibur Source Resolving
40  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
41  * @version CVS $Id: AbstractStreamWriteableSource.java 30932 2004-07-29 17:35:38Z vgritsenko $
42  */

43 public abstract class AbstractStreamWriteableSource
44     extends AbstractStreamSource
45     implements org.apache.cocoon.environment.WriteableSource {
46
47     protected AbstractStreamWriteableSource(ComponentManager manager) {
48         super(manager);
49     }
50
51     /**
52      * Checks if the <code>OutputStream</code> under <code>handler</code> can be cancelled.
53      *
54      * @see #canCancel(OutputStream)
55      */

56     public boolean canCancel(ContentHandler JavaDoc handler) {
57         if (handler instanceof WritingPipe) {
58             WritingPipe pipe = (WritingPipe)handler;
59             if (pipe.getSource() == this) {
60                 return pipe.canCancel();
61             }
62         }
63
64         // Not a valid handler for this source
65
throw new IllegalArgumentException JavaDoc("The handler is not associated to this source");
66     }
67
68     /**
69      * Always return <code>false</code>. To be redefined by implementations that support
70      * <code>cancel()</code>.
71      */

72     public boolean canCancel(OutputStream JavaDoc stream) {
73         return false;
74     }
75
76     /**
77      * Cancels the <code>OutputStream</code> under <code>handler</code>.
78      *
79      * @see #cancel(OutputStream)
80      */

81     public void cancel(ContentHandler JavaDoc handler) throws Exception JavaDoc {
82         if (handler instanceof WritingPipe) {
83             WritingPipe pipe = (WritingPipe)handler;
84             if (pipe.getSource() == this) {
85                 pipe.cancel();
86                 return;
87             }
88         }
89
90         // Not a valid handler for this source
91
throw new IllegalArgumentException JavaDoc("The handler is not associated to this source");
92     }
93
94     /**
95      * Always throw <code>UnsupportedOperationException</code>. To be redefined by
96      * implementations that support <code>cancel()</code>.
97      */

98     public void cancel(OutputStream JavaDoc stream) throws Exception JavaDoc {
99         throw new UnsupportedOperationException JavaDoc("Cancel is not implemented on " +
100             this.getClass().getName());
101     }
102
103     /**
104      * Get a <code>ContentHandler</code> to write a SAX stream to this source. It
105      * uses either the 'xml' or 'html' serializer depending on the result of
106      * {@link #isHTMLContent()} to serialize events, and thus these serializers must
107      * exist in this source's component manager.
108      */

109     public ContentHandler JavaDoc getContentHandler() throws SAXException JavaDoc, ProcessingException {
110
111         Serializer serializer;
112         ComponentSelector selector;
113
114         String JavaDoc serializerName = this.isHTMLContent() ? "html" : "xml";
115
116         // Get the serializer
117
try {
118             selector =
119                 (ComponentSelector)this.manager.lookup(Serializer.ROLE + "Selector");
120             serializer = (Serializer)selector.select(serializerName);
121         } catch(ComponentException ce) {
122             throw new ProcessingException("Cannot get '" + serializerName + "' serializer");
123         }
124
125         try {
126             return new WritingPipe(getOutputStream(), selector, serializer);
127         } catch(IOException JavaDoc ioe) {
128             selector.release(serializer);
129             throw new ProcessingException("Cannot open stream for " + this.getSystemId(), ioe);
130         }
131     }
132
133     /**
134      * A pipe that closes the outputstream at the end of the document and handles cancel().
135      */

136     private class WritingPipe extends AbstractXMLPipe {
137
138         // The output stream
139
private OutputStream JavaDoc output;
140
141         // Serialier and its selector for proper release
142
private Serializer serializer;
143         private ComponentSelector selector;
144
145         public WritingPipe(OutputStream JavaDoc output, ComponentSelector selector, Serializer serializer)
146           throws IOException JavaDoc {
147             this.output = output;
148             this.selector = selector;
149             this.serializer = serializer;
150
151             // Connect this pipe, the serializer and the output stream
152
this.setConsumer(this.serializer);
153             this.serializer.setOutputStream(this.output);
154         }
155
156         public org.apache.cocoon.environment.WriteableSource getSource() {
157             return AbstractStreamWriteableSource.this;
158         }
159
160         /**
161          * Close the underlying stream
162          */

163         public void endDocument() throws SAXException JavaDoc {
164             super.endDocument();
165             try {
166                 close();
167             }
168             catch(Exception JavaDoc e) {
169                 throw new SAXException JavaDoc("Error while closing output stream", e);
170             }
171         }
172
173         public boolean canCancel() {
174             return this.output != null;
175         }
176
177         /**
178          * Cancel the wrapped output stream
179          */

180         public void cancel() throws Exception JavaDoc {
181             AbstractStreamWriteableSource.this.cancel(output);
182             close();
183         }
184
185         private void close() throws IOException JavaDoc {
186             if (this.serializer != null) {
187                 // Disconnect serializer;
188
this.recycle();
189                 // and release it
190
this.selector.release(this.serializer);
191                 this.serializer = null;
192             }
193
194             if (this.output != null) {
195                 this.output.close();
196                 this.output = null;
197             }
198         }
199
200         // Ensure all is closed properly
201
protected void finalize() throws Throwable JavaDoc {
202             close();
203             super.finalize();
204         }
205     }
206 }
207
Popular Tags