KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > view > pipeline > PipelineViewRenderer


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.view.pipeline;
50
51 import java.io.*;
52 import java.util.ArrayList JavaDoc;
53 import java.util.Collection JavaDoc;
54 import java.util.Iterator JavaDoc;
55
56 import com.anthonyeden.lib.config.Configuration;
57 import com.anthonyeden.lib.config.ConfigurationException;
58 import org.apache.commons.logging.Log;
59 import org.apache.commons.logging.LogFactory;
60 import org.jpublish.RequestContext;
61 import org.jpublish.view.AbstractViewRenderer;
62 import org.jpublish.view.ViewRenderException;
63 import org.jpublish.view.ViewRenderer;
64
65 /**
66  * ViewRenderer which passes data through a pipeline of renderers.
67  *
68  * @author Anthony Eden
69  * @since 3.0
70  */

71
72 public class PipelineViewRenderer extends AbstractViewRenderer {
73
74     private static Log log = LogFactory.getLog(PipelineViewRenderer.class);
75
76     private Collection JavaDoc pipeline = new ArrayList JavaDoc();
77
78     /**
79      * Render the view.
80      *
81      * @param context The RequestContext
82      * @param path The path to the template
83      * @param out The Writer to write the rendered view
84      * @throws IOException
85      * @throws ViewRenderException
86      */

87
88     public void render(RequestContext context, String JavaDoc path, Writer out)
89             throws IOException, ViewRenderException {
90
91         log.debug("Executing pipeline");
92
93         boolean first = true;
94         String JavaDoc buffer = null;
95         Iterator JavaDoc viewRenderers = pipeline.iterator();
96         while (viewRenderers.hasNext()) {
97             ViewRenderer viewRenderer = (ViewRenderer) viewRenderers.next();
98             if (log.isDebugEnabled()) {
99                 log.debug("Renderer: " + viewRenderer.getClass());
100             }
101             Writer pipelineOut = new StringWriter();
102             if (first) {
103                 first = false;
104                 viewRenderer.render(context, path, pipelineOut);
105                 buffer = pipelineOut.toString();
106             } else {
107                 StringReader pipelineIn = new StringReader(buffer);
108                 if (viewRenderers.hasNext()) {
109                     viewRenderer.render(context, path, pipelineIn,
110                             pipelineOut);
111                     buffer = pipelineOut.toString();
112                 } else {
113                     viewRenderer.render(context, path, new StringReader(buffer),
114                             out);
115                 }
116             }
117         }
118     }
119
120     /**
121      * Render the view.
122      *
123      * @param context The RequestContext
124      * @param path The path to the template
125      * @param out The OutputStream to write the rendered view
126      * @throws IOException
127      * @throws ViewRenderException
128      */

129
130     public void render(RequestContext context, String JavaDoc path, OutputStream out)
131             throws IOException, ViewRenderException {
132         render(context, path, new OutputStreamWriter(out));
133     }
134
135     /**
136      * Render the view.
137      *
138      * @param context The RequestContext
139      * @param path The path to the content
140      * @param in The Reader providing the raw content
141      * @param out The Writer to write the rendered view
142      * @throws IOException
143      * @throws ViewRenderException
144      */

145
146     public void render(RequestContext context, String JavaDoc path, Reader in,
147             Writer out) throws IOException, ViewRenderException {
148         log.debug("Executing pipeline");
149
150         boolean first = true;
151         String JavaDoc buffer = null;
152         Iterator JavaDoc viewRenderers = pipeline.iterator();
153         while (viewRenderers.hasNext()) {
154             ViewRenderer viewRenderer = (ViewRenderer) viewRenderers.next();
155             if (log.isDebugEnabled()) {
156                 log.debug("Renderer: " + viewRenderer.getClass());
157             }
158             Writer pipelineOut = new StringWriter();
159             if (first) {
160                 first = false;
161                 viewRenderer.render(context, path, in, pipelineOut);
162                 buffer = pipelineOut.toString();
163             } else {
164                 StringReader pipelineIn = new StringReader(buffer);
165                 if (viewRenderers.hasNext()) {
166                     viewRenderer.render(context, path, pipelineIn,
167                             pipelineOut);
168                     buffer = pipelineOut.toString();
169                 } else {
170                     viewRenderer.render(context, path, new StringReader(buffer),
171                             out);
172                 }
173             }
174         }
175     }
176
177     /**
178      * Render the view.
179      *
180      * @param context The RequestContext
181      * @param path The path to the content
182      * @param in The InputStream providing the raw content
183      * @param out The OutputStream to write the rendered view
184      * @throws IOException
185      * @throws ViewRenderException
186      */

187
188     public void render(RequestContext context, String JavaDoc path, InputStream in,
189             OutputStream out) throws IOException, ViewRenderException {
190         render(context, path, new InputStreamReader(in),
191                 new OutputStreamWriter(out));
192     }
193
194     /**
195      * Load the configuration for the view.
196      *
197      * @param configuration The configuration object
198      */

199
200     public void loadConfiguration(Configuration configuration)
201             throws ConfigurationException {
202         pipeline.clear();
203
204         Iterator JavaDoc viewRendererElements =
205                 configuration.getChildren("view-renderer").iterator();
206         while (viewRendererElements.hasNext()) {
207             Configuration viewRendererElement =
208                     (Configuration) viewRendererElements.next();
209             String JavaDoc viewRendererName = viewRendererElement.getAttribute("name");
210             if (viewRendererName == null) {
211                 // use the default
212
if (log.isDebugEnabled()) {
213                     log.debug("Added default view renderer to pipeline");
214                 }
215                 pipeline.add(siteContext.getViewRenderer());
216             } else {
217                 // use the named renderer
218
if (log.isDebugEnabled()) {
219                     log.debug("Added " + viewRendererName + " to pipeline");
220                 }
221                 pipeline.add(siteContext.getViewRenderer(viewRendererName));
222             }
223         }
224     }
225
226 }
227
Popular Tags