KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
22 import java.util.Properties JavaDoc;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.roller.config.RollerConfig;
26 import org.apache.roller.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  */

39 public class RollerVelocity {
40     
41     public static final String JavaDoc VELOCITY_CONFIG = "/WEB-INF/velocity.properties";
42     
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 JavaDoc velocityProps = new Properties JavaDoc();
53         
54         try {
55             InputStream JavaDoc 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(RollerConfig.getBooleanProperty("rendering.legacyModels.enabled")) {
62                 String JavaDoc macroLibraries = (String JavaDoc) velocityProps.get("velocimacro.library");
63                 String JavaDoc oldLibraries = RollerConfig.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 JavaDoc e) {
78             throw new RuntimeException JavaDoc(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() {
90         return velocityEngine;
91     }
92     
93     
94     /**
95      * Convenience static method for looking up a template.
96      */

97     public static Template getTemplate(String JavaDoc name)
98             throws ResourceNotFoundException, ParseErrorException, Exception JavaDoc {
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 JavaDoc name, String JavaDoc encoding)
107             throws ResourceNotFoundException, ParseErrorException, Exception JavaDoc {
108         return velocityEngine.getTemplate(name, encoding);
109     }
110     
111 }
112
Popular Tags