KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > util > PipelineUtil


1 /*
2  * Copyright 1999-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.flow.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.excalibur.io.IOUtil;
24 import org.apache.avalon.framework.activity.Disposable;
25 import org.apache.avalon.framework.context.Context;
26 import org.apache.avalon.framework.context.ContextException;
27 import org.apache.avalon.framework.context.Contextualizable;
28 import org.apache.avalon.framework.service.ServiceException;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.Serviceable;
31 import org.apache.cocoon.ProcessingException;
32 import org.apache.cocoon.components.ContextHelper;
33 import org.apache.cocoon.components.flow.FlowHelper;
34 import org.apache.cocoon.components.source.SourceUtil;
35 import org.apache.excalibur.source.Source;
36 import org.apache.excalibur.source.SourceResolver;
37 import org.w3c.dom.Document JavaDoc;
38 import org.xml.sax.ContentHandler JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 /**
42  * Utility class to process a pipeline to various destinations.
43  * This class must be setup from the flowscript before being used. This means that instances must
44  * be created with <code>cocoon.createObject(Packages.org.apache.cocoon.components.flow.util.PipelineUtil);
45  *
46  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
47  * @version CVS $Id: PipelineUtil.java 43561 2004-09-09 02:28:46Z vgritsenko $
48  */

49 public class PipelineUtil implements Contextualizable, Serviceable, Disposable {
50
51     private Context context;
52     private ServiceManager manager;
53     private SourceResolver resolver;
54
55     /* (non-Javadoc)
56      * @see org.apache.avalon.framework.activity.Disposable#dispose()
57      */

58     public void dispose() {
59         if (this.manager != null) {
60             this.manager.release(this.resolver);
61             this.manager = null;
62             this.resolver = null;
63         }
64     }
65
66     public void contextualize(Context context) throws ContextException {
67         this.context = context;
68
69     }
70
71     public void service(ServiceManager manager) throws ServiceException {
72         this.manager = manager;
73         this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
74     }
75
76     /**
77      * Check that this object has been correctly set up.
78      *
79      * @throws IllegalStateException if not already set up.
80      */

81     private void checkSetup() {
82         if (this.manager == null) {
83             throw new IllegalStateException JavaDoc("Instances of " + getClass().getName() +
84                                             " must be setup using either cocoon.createObject() or cocoon.setupObject().");
85         }
86     }
87
88     /**
89      * Process a pipeline to a stream.
90      *
91      * @param uri the pipeline URI
92      * @param viewData the view data object
93      * @param output the stream where pipeline result is output. Note: this stream is not closed.
94      * @throws IOException
95      */

96     public void processToStream(String JavaDoc uri, Object JavaDoc viewData, OutputStream JavaDoc output)
97     throws IOException JavaDoc {
98         checkSetup();
99
100         Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
101
102         // Keep the previous view data, if any (is it really necessary?), and set the new one
103
Object JavaDoc oldViewData = FlowHelper.getContextObject(objectModel);
104         FlowHelper.setContextObject(objectModel, FlowHelper.unwrap(viewData));
105
106         Source src = null;
107         InputStream JavaDoc input = null;
108         try {
109             src = this.resolver.resolveURI("cocoon:/" + uri);
110             input = src.getInputStream();
111             IOUtil.copy(input, output);
112         } finally {
113             if (input != null) {
114                 try {
115                     input.close();
116                 } catch (IOException JavaDoc ignored) {}
117             }
118
119             // Restore the previous view data
120
FlowHelper.setContextObject(objectModel, oldViewData);
121
122             if (src != null) {
123                 this.resolver.release(src);
124             }
125         }
126     }
127
128     /**
129      * Process a pipeline to a SAX <code>ContentHandler</code>
130      *
131      * @param uri the pipeline URI
132      * @param viewData the view data object
133      * @param handler where the pipeline should be streamed to.
134      */

135     public void processToSAX(String JavaDoc uri, Object JavaDoc viewData, ContentHandler JavaDoc handler)
136     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
137         checkSetup();
138
139         Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
140         Object JavaDoc oldViewData = FlowHelper.getContextObject(objectModel);
141         FlowHelper.setContextObject(objectModel, FlowHelper.unwrap(viewData));
142
143         Source src = null;
144         try {
145             src = this.resolver.resolveURI("cocoon:/" + uri);
146             SourceUtil.toSAX(src, handler);
147         } finally {
148             FlowHelper.setContextObject(objectModel, oldViewData);
149             if (src != null) {
150                 this.resolver.release(src);
151             }
152         }
153     }
154
155     /**
156      * Process a pipeline and gets is result as a DOM <code>Document</code>
157      *
158      * @param uri the pipeline URI
159      * @param viewData the view data object
160      * @return the document
161      */

162     public Document JavaDoc processToDOM(String JavaDoc uri, Object JavaDoc viewData) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
163         checkSetup();
164
165         Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
166         Object JavaDoc oldViewData = FlowHelper.getContextObject(objectModel);
167         FlowHelper.setContextObject(objectModel, FlowHelper.unwrap(viewData));
168
169         Source src = null;
170
171         try {
172             src = this.resolver.resolveURI("cocoon:/" + uri);
173             return SourceUtil.toDOM(src);
174         } finally {
175             FlowHelper.setContextObject(objectModel, oldViewData);
176             if (src != null) {
177                 this.resolver.release(src);
178             }
179         }
180     }
181 }
182
Popular Tags