001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.ui.core.tags;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.velocity.VelocityContext;
023: import org.apache.velocity.app.Velocity;
024: import org.apache.velocity.app.VelocityEngine;
025:
026: import java.io.StringWriter;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: import javax.servlet.jsp.JspException;
032: import javax.servlet.jsp.tagext.TagSupport;
033:
034: /**
035: * @author David M Johnson, Gregory Gerard
036: * @version 1.0
037: */
038: public abstract class VelocityTag extends HybridTag // TagSupport
039: {
040: private static Log mLogger = LogFactory.getFactory().getInstance(
041: VelocityTag.class);
042:
043: /*
044: private String title;
045: private String subtitle;
046: private List images;
047: private Map ingredients;
048: */
049:
050: /** Return path to Velocity template to render this tag */
051: public abstract String getTemplateClasspath();
052:
053: /** Prepare context for execution */
054: public abstract void prepareContext(VelocityContext ctx);
055:
056: public VelocityTag() {
057: }
058:
059: /**
060: * Release any resources we might have acquired.
061: */
062: public void release() {
063: super .release();
064: /*
065: title = null;
066: subtitle = null;
067: images = null;
068: ingredients = null;
069: */
070: }
071:
072: /**
073: * Evaluate any tags inside us. This will also allow us to have child tags
074: * send us messages.
075: * @return
076: * @throws JspException
077: */
078: public int doStartTag(java.io.PrintWriter pw) throws JspException {
079: return TagSupport.EVAL_BODY_INCLUDE;
080: }
081:
082: /**
083: * Check all all the public properties that must be set by now, either by
084: * tag arguments or tag children.
085: * @return
086: * @throws JspException
087: */
088: public int doEndTag(java.io.PrintWriter pw) throws JspException {
089: String myResource = getVelocityClasspathResource(getTemplateClasspath());
090:
091: try {
092: VelocityContext myVelocityContext = getVelocityContext();
093:
094: // ask concrete class to prepare context
095: prepareContext(myVelocityContext);
096:
097: StringWriter myStringWriter = new StringWriter();
098:
099: Velocity.mergeTemplate(myResource,
100: org.apache.velocity.runtime.RuntimeSingleton
101: .getString(Velocity.INPUT_ENCODING,
102: Velocity.ENCODING_DEFAULT),
103: myVelocityContext, myStringWriter);
104:
105: pw.println(myStringWriter);
106:
107: return EVAL_PAGE;
108: } catch (Exception e) {
109: mLogger.error("Unexpected exception", e);
110:
111: throw new JspException("Exception; TEMPLATE_CLASSPATH="
112: + getTemplateClasspath() + "; exception="
113: + e.getMessage());
114: }
115: }
116:
117: public String doStandaloneTest(Map inMap) throws Exception {
118: String resource = getVelocityClasspathResource(getTemplateClasspath());
119: VelocityContext context = getVelocityContext();
120: StringWriter w = new StringWriter();
121: Iterator iter = inMap.keySet().iterator();
122:
123: while (iter.hasNext()) {
124: Object o = iter.next();
125:
126: context.put(o.toString(), inMap.get(o));
127: }
128:
129: Velocity.mergeTemplate(resource,
130: org.apache.velocity.runtime.RuntimeSingleton.getString(
131: Velocity.INPUT_ENCODING,
132: Velocity.ENCODING_DEFAULT), context, w);
133:
134: return w.toString();
135: }
136:
137: protected static VelocityContext getVelocityContext()
138: throws java.lang.Exception {
139: Velocity.addProperty(Velocity.RESOURCE_LOADER, "classpath");
140: Velocity
141: .addProperty("classpath." + Velocity.RESOURCE_LOADER
142: + ".class",
143: "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
144: Velocity.setProperty(VelocityEngine.COUNTER_INITIAL_VALUE,
145: new Integer(0));
146: Velocity.init();
147:
148: return new VelocityContext();
149: }
150:
151: /**
152: * Velocity wants classpath resources to not have a leading slash. This is
153: * contrary to how one would normally load a resource off the classpath
154: * (e.g. SampleTag.class.getResourceAsStream("/extag/resources/test.html")
155: * @param inClasspathString
156: * @return
157: */
158: protected static String getVelocityClasspathResource(
159: String inClasspathString) {
160: if (inClasspathString.length() < 1) {
161: return inClasspathString;
162: } else {
163: if (inClasspathString.startsWith("/")) {
164: return inClasspathString.substring(1);
165: } else {
166: return inClasspathString;
167: }
168: }
169: }
170: }
|