File Source: RollerVelocity.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.InputStream;
22 import java.util.Properties;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.roller.weblogger.config.WebloggerConfig;
26 import org.apache.roller.weblogger.ui.core.RollerContext;
27 import org.apache.velocity.Template;
28 import org.apache.velocity.app.VelocityEngine;
29 import org.apache.velocity.exception.ParseErrorException;
30 import org.apache.velocity.exception.ResourceNotFoundException;
31
32
33 /**
34 * Represents the VelocityEngine used by Roller.
35 *
36 * We construct our own instance of VelocityEngine, initialize it, and provide
37 * access to the instance via the Singleton getInstance() method.
38 */
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.velocity.RollerVelocity()
*/
39 public class RollerVelocity {
40
41 public static final String VELOCITY_CONFIG = "/WEB-INF/velocity.properties";
42
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.velocity.RollerVelocity__static_init
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@43 != null
* org.apache.roller.weblogger.ui.core.RollerContext:getServletContext(...)@55 != null
*
* Postconditions:
* ","._tainted == 0
* "Velocity engine props = "._tainted == 0
* (soft) log != null
* velocityEngine == &new VelocityEngine(RollerVelocity__static_init#4)
* new VelocityEngine(RollerVelocity__static_init#4) num objects == 1
*
* Test Vectors:
* org.apache.roller.weblogger.config.WebloggerConfig:getBooleanProperty(...)@61: {0}, {1}
*/
43 private static Log log = LogFactory.getLog(RollerVelocity.class);
44
45 private static VelocityEngine velocityEngine = null;
46
47
48 static {
49 log.info("Initializing Velocity Rendering Engine");
50
51 // initialize the Velocity engine
52 Properties velocityProps = new Properties();
53
54 try {
55 InputStream instream =
56 RollerContext.getServletContext().getResourceAsStream(VELOCITY_CONFIG);
57
58 velocityProps.load(instream);
59
60 // need to dynamically add old macro libraries if they are enabled
61 if(WebloggerConfig.getBooleanProperty("rendering.legacyModels.enabled")) {
62 String macroLibraries = (String) velocityProps.get("velocimacro.library");
63 String oldLibraries = WebloggerConfig.getProperty("velocity.oldMacroLibraries");
64
65 // set the new value
66 velocityProps.setProperty("velocimacro.library", oldLibraries+","+macroLibraries);
67 }
68
69 log.debug("Velocity engine props = "+velocityProps);
70
71 // construct the VelocityEngine
72 velocityEngine = new VelocityEngine();
73
74 // init velocity with our properties
75 velocityEngine.init(velocityProps);
76
77 } catch (Exception e) {
78 throw new RuntimeException(e);
79 }
80 }
81
82
83 /**
84 * Access to the VelocityEngine.
85 *
86 * This shouldn't ever be needed, but it's here just in case someone
87 * really needs to do something special.
88 */
89 public static VelocityEngine getEngine() {
/*
P/P * Method: VelocityEngine getEngine()
*
* Preconditions:
* init'ed(velocityEngine)
*
* Postconditions:
* return_value == velocityEngine
* init'ed(return_value)
*/
90 return velocityEngine;
91 }
92
93
94 /**
95 * Convenience static method for looking up a template.
96 */
97 public static Template getTemplate(String name)
98 throws ResourceNotFoundException, ParseErrorException, Exception {
/*
P/P * Method: Template getTemplate(String)
*
* Preconditions:
* velocityEngine != null
*
* Postconditions:
* init'ed(return_value)
*/
99 return velocityEngine.getTemplate(name);
100 }
101
102
103 /**
104 * Convenience static method for looking up a template.
105 */
106 public static Template getTemplate(String name, String encoding)
107 throws ResourceNotFoundException, ParseErrorException, Exception {
/*
P/P * Method: Template getTemplate(String, String)
*
* Preconditions:
* velocityEngine != null
*
* Postconditions:
* init'ed(return_value)
*/
108 return velocityEngine.getTemplate(name, encoding);
109 }
110
111 }
SofCheck Inspector Build Version : 2.18479
| RollerVelocity.java |
2009-Jan-02 14:24:58 |
| RollerVelocity.class |
2009-Sep-04 03:12:45 |