File Source: VelocityRenderer.java
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.weblogger.ui.rendering.velocity;
20
21 import java.io.StringWriter;
22 import java.io.Writer;
23 import java.util.Map;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.weblogger.pojos.Template;
27 import org.apache.roller.weblogger.pojos.ThemeTemplate;
28 import org.apache.roller.weblogger.ui.rendering.Renderer;
29 import org.apache.roller.weblogger.ui.rendering.RenderingException;
30 import org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel;
31 import org.apache.velocity.VelocityContext;
32 import org.apache.velocity.context.Context;
33 import org.apache.velocity.exception.ParseErrorException;
34 import org.apache.velocity.exception.ResourceNotFoundException;
35
36
37 /**
38 * Renderer that renders using the Velocity template engine.
39 */
40 public class VelocityRenderer implements Renderer {
41
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init
*
* Postconditions:
* init'ed(log)
*/
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 parseException = null;
53
54
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)
*
* Preconditions:
* org/apache/roller/weblogger/ui/rendering/velocity/RollerVelocity.velocityEngine != null
* (soft) template != null
*
* Postconditions:
* init'ed(this.parseException)
* this.renderTemplate == template
* (soft) this.renderTemplate != null
* init'ed(this.velocityDecorator)
* init'ed(this.velocityTemplate)
*
* Test Vectors:
* org.apache.roller.weblogger.pojos.ThemeTemplate:getDecorator(...)@69: Addr_Set{null}, Inverse{null}
*/
55 public VelocityRenderer(Template template) throws Exception {
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 this is a ThemeTemplate than look for a decorator too
66 if(template instanceof ThemeTemplate) {
67 ThemeTemplate templ = (ThemeTemplate) template;
68
69 Template decorator = templ.getDecorator();
70 if(decorator != null) {
71 velocityDecorator = RollerVelocity.getTemplate(decorator.getId(), "UTF-8");
72 }
73 }
74
75 } catch(ResourceNotFoundException ex) {
76 // velocity couldn't find the resource so lets log a warning
77 log.warn("Error creating renderer for "+template.getId()+
78 " due to ["+ex.getMessage()+"]");
79
80 // then just rethrow so that the caller knows this instantiation failed
81 throw ex;
82
83 } catch(ParseErrorException ex) {
84 // in the case of a parsing error we want to render an
85 // error page instead so the user knows what was wrong
86 parseException = ex;
87
88 // need to lookup error page template
89 velocityTemplate = RollerVelocity.getTemplate("templates/error-page.vm");
90
91 } catch(Exception ex) {
92 // some kind of generic/unknown exception, dump it to the logs
93 log.error("Unknown exception creatting renderer for "+template.getId(), ex);
94
95 // throw if back to the caller
96 throw ex;
97 }
98 }
99
100
101 public void render(Map model, Writer out) throws RenderingException {
102
103 try {
/*
P/P * Method: void render(Map, Writer)
*
* Preconditions:
* init'ed(this.parseException)
* this.renderTemplate != null
* this.velocityTemplate != null
* (soft) log != null
* (soft) init'ed(this.velocityDecorator)
*
* Test Vectors:
* this.parseException: Addr_Set{null}, Inverse{null}
* this.velocityDecorator: Addr_Set{null}, Inverse{null}
*/
104 if(parseException != null) {
105
106 Context ctx = new VelocityContext(model);
107 ctx.put("exception", parseException);
108 ctx.put("exceptionSource", renderTemplate.getId());
109 ctx.put("utils", new UtilitiesModel());
110
111 // render output to Writer
112 velocityTemplate.merge(ctx, out);
113
114 // and we're done
115 return;
116 }
117
118 long startTime = System.currentTimeMillis();
119
120 // convert model to Velocity Context
121 Context ctx = new VelocityContext(model);
122
123 if(velocityDecorator != null) {
124
125 /**
126 * We only allow decorating once, so the process isn't
127 * fully recursive. This is just to keep it simple.
128 */
129
130 // render base template to a temporary StringWriter
131 StringWriter sw = new StringWriter();
132 velocityTemplate.merge(ctx, sw);
133
134 // put rendered template into context
135 ctx.put("decorator_body", sw.toString());
136
137 log.debug("Applying decorator "+velocityDecorator.getName());
138
139 // now render decorator to our output writer
140 velocityDecorator.merge(ctx, out);
141
142 } else {
143
144 // no decorator, so just merge template to our output writer
145 velocityTemplate.merge(ctx, out);
146 }
147
148 long endTime = System.currentTimeMillis();
149 long renderTime = (endTime - startTime)/1000;
150
151 log.debug("Rendered ["+renderTemplate.getId()+"] in "+renderTime+" secs");
152
153 } catch (Exception ex) {
154 // wrap and rethrow so caller can deal with it
155 throw new RenderingException("Error during rendering", ex);
156 }
157 }
158
159 }
SofCheck Inspector Build Version : 2.18479
| VelocityRenderer.java |
2009-Jan-02 14:24:52 |
| VelocityRenderer.class |
2009-Sep-04 03:12:45 |