001: /*
002: * $Id: VelocityTemplateEngine.java 560894 2007-07-30 09:05:32Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components.template;
022:
023: import com.opensymphony.xwork2.inject.Inject;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.struts2.ServletActionContext;
027: import org.apache.struts2.views.velocity.VelocityManager;
028: import org.apache.velocity.app.VelocityEngine;
029: import org.apache.velocity.context.Context;
030:
031: import javax.servlet.ServletContext;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import java.io.IOException;
035: import java.io.Writer;
036: import java.util.List;
037: import java.util.Map;
038:
039: /**
040: * Velocity based template engine.
041: */
042: public class VelocityTemplateEngine extends BaseTemplateEngine {
043: private static final Log LOG = LogFactory
044: .getLog(VelocityTemplateEngine.class);
045:
046: private VelocityManager velocityManager;
047:
048: @Inject
049: public void setVelocityManager(VelocityManager mgr) {
050: this .velocityManager = mgr;
051: }
052:
053: public void renderTemplate(TemplateRenderingContext templateContext)
054: throws Exception {
055: // get the various items required from the stack
056: Map actionContext = templateContext.getStack().getContext();
057: ServletContext servletContext = (ServletContext) actionContext
058: .get(ServletActionContext.SERVLET_CONTEXT);
059: HttpServletRequest req = (HttpServletRequest) actionContext
060: .get(ServletActionContext.HTTP_REQUEST);
061: HttpServletResponse res = (HttpServletResponse) actionContext
062: .get(ServletActionContext.HTTP_RESPONSE);
063:
064: // prepare velocity
065: velocityManager.init(servletContext);
066: VelocityEngine velocityEngine = velocityManager
067: .getVelocityEngine();
068:
069: // get the list of templates we can use
070: List<Template> templates = templateContext.getTemplate()
071: .getPossibleTemplates(this );
072:
073: // find the right template
074: org.apache.velocity.Template template = null;
075: String templateName = null;
076: Exception exception = null;
077: for (Template t : templates) {
078: templateName = getFinalTemplateName(t);
079: try {
080: // try to load, and if it works, stop at the first one
081: template = velocityEngine.getTemplate(templateName);
082: break;
083: } catch (IOException e) {
084: if (exception == null) {
085: exception = e;
086: }
087: }
088: }
089:
090: if (template == null) {
091: LOG.error("Could not load template "
092: + templateContext.getTemplate());
093: if (exception != null) {
094: throw exception;
095: } else {
096: return;
097: }
098: }
099:
100: if (LOG.isDebugEnabled()) {
101: LOG.debug("Rendering template " + templateName);
102: }
103:
104: Context context = velocityManager.createContext(templateContext
105: .getStack(), req, res);
106:
107: Writer outputWriter = templateContext.getWriter();
108: context.put("tag", templateContext.getTag());
109: context.put("parameters", templateContext.getParameters());
110:
111: template.merge(context, outputWriter);
112: }
113:
114: protected String getSuffix() {
115: return "vm";
116: }
117: }
|