001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.velocity;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.velocity.VelocityContext;
035: import org.apache.velocity.app.VelocityEngine;
036: import org.blojsom.blog.Blog;
037: import org.blojsom.plugin.Plugin;
038: import org.blojsom.plugin.PluginException;
039: import org.blojsom.util.BlojsomConstants;
040:
041: import javax.servlet.ServletConfig;
042: import javax.servlet.ServletContext;
043: import java.io.StringWriter;
044: import java.util.Map;
045: import java.util.Properties;
046:
047: /**
048: * StandalongVelocityPlugin
049: *
050: * @author David Czarnecki
051: * @version $Id: StandaloneVelocityPlugin.java,v 1.4 2007/01/17 02:35:15 czarneckid Exp $
052: * @since blojsom 3.0
053: */
054: public abstract class StandaloneVelocityPlugin implements Plugin {
055:
056: protected Log _logger = LogFactory
057: .getLog(StandaloneVelocityPlugin.class);
058:
059: protected Properties _velocityProperties;
060: protected ServletConfig _servletConfig;
061:
062: /**
063: * Initialize this plugin. This method only called when the plugin is instantiated.
064: *
065: * @param servletConfig Servlet config object for the plugin to retrieve any initialization parameters
066: * @param blojsomConfiguration {@link org.blojsom.blog.BlojsomConfiguration} information
067: * @throws org.blojsom.plugin.PluginException
068: * If there is an error initializing the plugin
069: */
070: public void init() throws PluginException {
071: }
072:
073: /**
074: * Set the Velocity properties
075: *
076: * @param velocityProperties Velocity properties
077: */
078: public void setVelocityProperties(Properties velocityProperties) {
079: _velocityProperties = velocityProperties;
080: }
081:
082: /**
083: * Set the {@link ServletConfig}
084: *
085: * @param servletConfig {@link ServletConfig}
086: */
087: public void setServletConfig(ServletConfig servletConfig) {
088: _servletConfig = servletConfig;
089: }
090:
091: /**
092: * Merge a given template for the user with the appropriate context
093: *
094: * @param template Template
095: * @param blog {@link Blog} information
096: * @param context Context with objects for use in the template
097: * @return Merged template or <code>null</code> if there was an error setting properties, loading the template, or merging
098: * the template
099: */
100: protected String mergeTemplate(String template, Blog blog,
101: Map context) {
102: ServletContext servletContext = _servletConfig
103: .getServletContext();
104:
105: // Create the Velocity Engine
106: VelocityEngine velocityEngine = new VelocityEngine();
107:
108: try {
109: Properties updatedVelocityProperties = (Properties) _velocityProperties
110: .clone();
111: updatedVelocityProperties
112: .setProperty(
113: VelocityEngine.FILE_RESOURCE_LOADER_PATH,
114: servletContext.getRealPath("/WEB-INF/"
115: + "blogs/" + blog.getBlogId()
116: + "/templates/")
117: + ", "
118: + servletContext
119: .getRealPath("/WEB-INF/templates/"));
120: velocityEngine.init(updatedVelocityProperties);
121: } catch (Exception e) {
122: if (_logger.isErrorEnabled()) {
123: _logger.error(e);
124: }
125:
126: return null;
127: }
128:
129: StringWriter writer = new StringWriter();
130:
131: // Setup the VelocityContext
132: VelocityContext velocityContext = new VelocityContext(context);
133:
134: if (!velocityEngine.templateExists(template)) {
135: if (_logger.isErrorEnabled()) {
136: _logger.error("Could not find template for user: "
137: + template);
138: }
139:
140: return null;
141: } else {
142: try {
143: velocityEngine.mergeTemplate(template,
144: BlojsomConstants.UTF8, velocityContext, writer);
145: } catch (Exception e) {
146: if (_logger.isErrorEnabled()) {
147: _logger.error(e);
148: }
149:
150: return null;
151: }
152: }
153:
154: if (_logger.isDebugEnabled()) {
155: _logger.debug("Merged template: " + template);
156: }
157:
158: return writer.toString();
159: }
160: }
|