KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > velocity > StandaloneVelocityPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.velocity;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.velocity.VelocityContext;
36 import org.apache.velocity.app.VelocityEngine;
37 import org.blojsom.blog.Blog;
38 import org.blojsom.plugin.Plugin;
39 import org.blojsom.plugin.PluginException;
40 import org.blojsom.util.BlojsomConstants;
41
42 import javax.servlet.ServletConfig JavaDoc;
43 import javax.servlet.ServletContext JavaDoc;
44 import java.io.StringWriter JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.Properties JavaDoc;
47
48 /**
49  * StandalongVelocityPlugin
50  *
51  * @author David Czarnecki
52  * @version $Id: StandaloneVelocityPlugin.java,v 1.3 2006/05/28 18:30:39 czarneckid Exp $
53  * @since blojsom 3.0
54  */

55 public abstract class StandaloneVelocityPlugin implements Plugin {
56
57     protected Log _logger = LogFactory.getLog(StandaloneVelocityPlugin.class);
58
59     protected Properties JavaDoc _velocityProperties;
60     protected ServletConfig JavaDoc _servletConfig;
61
62     /**
63      * Initialize this plugin. This method only called when the plugin is instantiated.
64      *
65      * @param servletConfig Servlet config object for the plugin to retrieve any initialization parameters
66      * @param blojsomConfiguration {@link org.blojsom.blog.BlojsomConfiguration} information
67      * @throws org.blojsom.plugin.PluginException
68      * If there is an error initializing the plugin
69      */

70     public void init() throws PluginException {
71     }
72
73     /**
74      * Set the Velocity properties
75      *
76      * @param velocityProperties Velocity properties
77      */

78     public void setVelocityProperties(Properties JavaDoc velocityProperties) {
79         _velocityProperties = velocityProperties;
80     }
81
82     /**
83      * Set the {@link ServletConfig}
84      *
85      * @param servletConfig {@link ServletConfig}
86      */

87     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
88         _servletConfig = servletConfig;
89     }
90
91     /**
92      * Merge a given template for the user with the appropriate context
93      *
94      * @param template Template
95      * @param blog {@link Blog} information
96      * @param context Context with objects for use in the template
97      * @return Merged template or <code>null</code> if there was an error setting properties, loading the template, or merging
98      * the template
99      */

100     protected String JavaDoc mergeTemplate(String JavaDoc template, Blog blog, Map JavaDoc context) {
101         ServletContext JavaDoc servletContext = _servletConfig.getServletContext();
102
103         // Create the Velocity Engine
104
VelocityEngine velocityEngine = new VelocityEngine();
105
106         try {
107             Properties JavaDoc updatedVelocityProperties = (Properties JavaDoc) _velocityProperties.clone();
108             updatedVelocityProperties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, servletContext.getRealPath("/WEB-INF/"
109                     + "blogs/" + blog.getBlogId() + "/templates/") + ", " + servletContext.getRealPath("/WEB-INF/templates/"));
110             velocityEngine.init(updatedVelocityProperties);
111         } catch (Exception JavaDoc e) {
112             if (_logger.isErrorEnabled()) {
113                 _logger.error(e);
114             }
115
116             return null;
117         }
118
119         StringWriter JavaDoc writer = new StringWriter JavaDoc();
120
121         // Setup the VelocityContext
122
VelocityContext velocityContext = new VelocityContext(context);
123
124         if (!velocityEngine.templateExists(template)) {
125             if (_logger.isErrorEnabled()) {
126                 _logger.error("Could not find template for user: " + template);
127             }
128
129             return null;
130         } else {
131             try {
132                 velocityEngine.mergeTemplate(template, BlojsomConstants.UTF8, velocityContext, writer);
133             } catch (Exception JavaDoc e) {
134                 if (_logger.isErrorEnabled()) {
135                     _logger.error(e);
136                 }
137
138                 return null;
139             }
140         }
141
142         if (_logger.isDebugEnabled()) {
143             _logger.debug("Merged template: " + template);
144         }
145
146         return writer.toString();
147     }
148 }
Popular Tags