KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > view > webmacro > WebMacroViewRenderer


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.webmacro;
50
51 import java.io.*;
52
53 import com.anthonyeden.lib.config.Configuration;
54 import com.anthonyeden.lib.config.ConfigurationException;
55 import com.anthonyeden.lib.util.IOUtilities;
56 import org.jpublish.JPublishRuntimeException;
57 import org.jpublish.RequestContext;
58 import org.jpublish.view.AbstractViewRenderer;
59 import org.jpublish.view.ContentSource;
60 import org.jpublish.view.ViewRenderException;
61 import org.webmacro.Context;
62 import org.webmacro.WM;
63 import org.webmacro.WebMacro;
64 import org.webmacro.engine.StreamTemplate;
65
66 /**
67  * ViewRenderer which uses the WebMacro template engine.
68  *
69  * @author Anthony Eden
70  */

71
72 public class WebMacroViewRenderer extends AbstractViewRenderer {
73
74     private WebMacro wm = null;
75
76     /**
77      * Initialize the ViewRenderer.
78      */

79
80     public void init() {
81         try {
82             wm = new WM();
83         } catch (Exception JavaDoc e) {
84             throw new JPublishRuntimeException("Error initializing WebMacro engine", e);
85         }
86     }
87
88     /**
89      * Render the view.
90      *
91      * @param context The RequestContext
92      * @param path The path to the template
93      * @param out The Writer to write the rendered view
94      * @throws IOException
95      * @throws ViewRenderException
96      */

97
98     public void render(RequestContext context, String JavaDoc path, Writer out)
99             throws IOException, ViewRenderException {
100         Reader in = null;
101         try {
102             ContentSource contentSource = siteContext.getContentSource(path);
103             if (contentSource == null) {
104                 throw new ViewRenderException("Content not found: " + path);
105             }
106
107             in = contentSource.getReader();
108
109             render(context, path, in, out);
110         } catch (ViewRenderException e) {
111             throw e;
112         } catch (IOException e) {
113             throw e;
114         } catch (Exception JavaDoc e) {
115             throw new ViewRenderException(e);
116         } finally {
117             IOUtilities.close(in);
118         }
119     }
120
121     /**
122      * Render the view.
123      *
124      * @param context The RequestContext
125      * @param path The path to the template
126      * @param out The OutputStream to write the rendered view
127      * @throws IOException
128      * @throws ViewRenderException
129      */

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

146
147     public void render(RequestContext context, String JavaDoc path, Reader in,
148             Writer out) throws IOException, ViewRenderException {
149         try {
150             Context wmContext = wm.getContext();
151             Object JavaDoc[] keys = keys = context.getKeys();
152             for (int i = 0; i < keys.length; i++) {
153                 wmContext.put(keys[i], context.get(keys[i].toString()));
154             }
155             
156             // need to specify encoding here?
157
StreamTemplate template = new StreamTemplate(wm.getBroker(), in);
158             template.setName(path);
159             Object JavaDoc result = template.evaluate(wmContext);
160             if (result == null) {
161                 throw new Exception JavaDoc("Error evaluating template.");
162             } else {
163                 out.write(result.toString());
164             }
165         } catch (ViewRenderException e) {
166             throw e;
167         } catch (IOException e) {
168             throw e;
169         } catch (Exception JavaDoc e) {
170             throw new ViewRenderException(e);
171         }
172     }
173
174     /**
175      * Render the view.
176      *
177      * @param context The RequestContext
178      * @param path The path to the content
179      * @param in The InputStream providing the raw content
180      * @param out The OutputStream to write the rendered view
181      * @throws IOException
182      * @throws ViewRenderException
183      */

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

196
197     public void loadConfiguration(Configuration configuration)
198             throws ConfigurationException {
199
200     }
201
202 }
203
Popular Tags