KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > velocity > VelocityRenderer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.velocity;
20
21 import java.io.StringWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.pojos.Template;
27 import org.apache.roller.ui.rendering.Renderer;
28 import org.apache.roller.ui.rendering.RenderingException;
29 import org.apache.roller.ui.rendering.model.UtilitiesModel;
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.context.Context;
32 import org.apache.velocity.exception.MethodInvocationException;
33 import org.apache.velocity.exception.ParseErrorException;
34 import org.apache.velocity.exception.ResourceNotFoundException;
35
36
37 /**
38  * Renderer for Velocity templates.
39  */

40 public class VelocityRenderer implements Renderer {
41     
42     private static Log log = LogFactory.getLog(VelocityRenderer.class);
43     
44     // the original template we are supposed to render
45
private Template renderTemplate = null;
46     
47     // the velocity templates
48
private org.apache.velocity.Template velocityTemplate = null;
49     private org.apache.velocity.Template velocityDecorator = null;
50     
51     // a possible exception
52
private Exception JavaDoc parseException = null;
53     
54     
55     public VelocityRenderer(Template template) throws Exception JavaDoc {
56         
57         // the Template we are supposed to render
58
this.renderTemplate = template;
59         
60         try {
61             // make sure that we can locate the template
62
// if we can't then this will throw an exception
63
velocityTemplate = RollerVelocity.getTemplate(template.getId(), "UTF-8");
64             
65             // if there is a decorator then look that up too
66
Template decorator = renderTemplate.getDecorator();
67             if(decorator != null) {
68                 velocityDecorator = RollerVelocity.getTemplate(decorator.getId());
69             }
70             
71         } catch(ResourceNotFoundException ex) {
72             // velocity couldn't find the resource so lets log a warning
73
log.warn("Error creating renderer for "+template.getId()+
74                     " due to ["+ex.getMessage()+"]");
75             
76             // then just rethrow so that the caller knows this instantiation failed
77
throw ex;
78             
79         } catch(ParseErrorException ex) {
80             // in the case of a parsing error we want to render an
81
// error page instead so the user knows what was wrong
82
parseException = ex;
83             
84             // need to lookup error page template
85
velocityTemplate = RollerVelocity.getTemplate("templates/error-page.vm");
86             
87         } catch(Exception JavaDoc ex) {
88             // some kind of generic/unknown exception, dump it to the logs
89
log.error("Unknown exception creatting renderer for "+template.getId(), ex);
90             
91             // throw if back to the caller
92
throw ex;
93         }
94     }
95     
96     
97     public void render(Map JavaDoc model, Writer JavaDoc out) throws RenderingException {
98         
99         try {
100             if(parseException != null) {
101                 
102                 Context ctx = new VelocityContext(model);
103                 ctx.put("exception", parseException);
104                 ctx.put("exceptionSource", renderTemplate.getId());
105                 ctx.put("utils", new UtilitiesModel());
106                 
107                 // render output to Writer
108
velocityTemplate.merge(ctx, out);
109                 
110                 // and we're done
111
return;
112             }
113             
114             long startTime = System.currentTimeMillis();
115             
116             // convert model to Velocity Context
117
Context ctx = new VelocityContext(model);
118             
119             if(velocityDecorator != null) {
120                 
121                 /**
122                  * We only allow decorating once, so the process isn't
123                  * fully recursive. This is just to keep it simple.
124                  */

125                 
126                 // render base template to a temporary StringWriter
127
StringWriter JavaDoc sw = new StringWriter JavaDoc();
128                 velocityTemplate.merge(ctx, sw);
129                 
130                 // put rendered template into context
131
ctx.put("decorator_body", sw.toString());
132                 
133                 log.debug("Applying decorator "+velocityDecorator.getName());
134                 
135                 // now render decorator to our output writer
136
velocityDecorator.merge(ctx, out);
137                 
138             } else {
139                 
140                 // no decorator, so just merge template to our output writer
141
velocityTemplate.merge(ctx, out);
142             }
143             
144             long endTime = System.currentTimeMillis();
145             long renderTime = (endTime - startTime)/1000;
146             
147             log.debug("Rendered ["+renderTemplate.getId()+"] in "+renderTime+" secs");
148             
149         } catch (Exception JavaDoc ex) {
150             // wrap and rethrow so caller can deal with it
151
throw new RenderingException("Error during rendering", ex);
152         }
153     }
154
155 }
156
Popular Tags