001: /*
002: * Coefficient - facilitates project based collaboration
003: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004: * PO Box 395
005: * Pretoria 0001, RSA
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package za.org.coefficient.util.ejb;
021:
022: import za.org.coefficient.core.Constants;
023:
024: import org.apache.commons.lang.StringUtils;
025:
026: import org.apache.velocity.Template;
027: import org.apache.velocity.VelocityContext;
028: import org.apache.velocity.app.Velocity;
029: import org.apache.velocity.context.Context;
030:
031: import java.io.StringWriter;
032:
033: import java.text.SimpleDateFormat;
034:
035: import java.util.Map;
036: import java.util.Properties;
037:
038: /**
039: * DOCUMENT ME!
040: *
041: * @author $author$
042: * @version $Revision: 1.3 $
043: */
044: public class VelocityScreenUtil {
045: //~ Static fields/initializers =============================================
046:
047: public static final String TEMPLATES = "/templates/";
048:
049: static {
050: initVelocity();
051: }
052:
053: //~ Methods ================================================================
054:
055: /**
056: * This method will look for the template name on the classpath and if
057: * found merge it with the data provided in the map. The result is returned
058: * as a StringBuffer.
059: *
060: * @param templateName identifies the template that should
061: * be merged. If the template is not found or there is a parsing
062: * error in the template or there is a method invocation error
063: * while parsing the template then the return value will be null.
064: * It is assumed that this template will be found in the classpath
065: * in the same package space as the caller plus a level deeper
066: * called templates
067: * @param context is a map containing all the mappings that are to be
068: * merged into the template.
069: *
070: * @return StringBuffer containing the result of the merge, null if there
071: * were any exceptions thrown during the merge.
072: */
073: public static StringBuffer getProcessedScreen(String templateName,
074: Map context) {
075: String fullTemplateName = getCallingClassPackage() + TEMPLATES
076: + templateName;
077: StringBuffer retVal = null;
078: try {
079: Template currTemplate = Velocity
080: .getTemplate(fullTemplateName);
081: StringWriter sw = new StringWriter();
082: context.put("dateFormat", new SimpleDateFormat(
083: Constants.SYSTEM_DATE_FORMAT));
084: Context ctx = new VelocityContext(context);
085: currTemplate.merge(ctx, sw);
086: retVal = sw.getBuffer();
087: } catch (Exception e) {
088: e.printStackTrace();
089: }
090:
091: return retVal;
092: }
093:
094: public static StringBuffer getProcessedScreenFullyQualified(
095: String fullyQualifiedTemplateName, Map context) {
096: StringBuffer retVal = null;
097: try {
098: Template currTemplate = Velocity
099: .getTemplate(fullyQualifiedTemplateName);
100: StringWriter sw = new StringWriter();
101: context.put("dateFormat", new SimpleDateFormat(
102: Constants.SYSTEM_DATE_FORMAT));
103: Context ctx = new VelocityContext(context);
104: currTemplate.merge(ctx, sw);
105: retVal = sw.getBuffer();
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109:
110: return retVal;
111: }
112:
113: /**
114: * This method will look for the template name on the classpath and if
115: * found merge it with the data provided in the map. The result is returned
116: * as a StringBuffer.
117: *
118: * @param inString is a string containing velocity code that will be merged
119: * @param context is a map containing all the mappings that are to be
120: * merged into the template.
121: *
122: * @return StringBuffer containing the result of the merge, null if there
123: * were any exceptions thrown during the merge.
124: */
125: public static StringBuffer getProcessedString(String inString,
126: Map context) {
127: StringBuffer retVal = null;
128: try {
129: StringWriter sw = new StringWriter();
130: context.put("dateFormat", new SimpleDateFormat(
131: Constants.SYSTEM_DATE_FORMAT));
132: Context ctx = new VelocityContext(context);
133: Velocity.evaluate(ctx, sw, "temp", inString);
134: retVal = sw.getBuffer();
135: } catch (Exception e) {
136: e.printStackTrace();
137: }
138:
139: return retVal;
140: }
141:
142: public static String getCallingClassPackage() {
143: StackTraceElement[] ste = new Exception().getStackTrace();
144: if ((ste == null) || (ste.length < 3)) {
145: return null;
146: } else {
147: String temp = ste[2].getClassName();
148:
149: return StringUtils.replace(temp.substring(0, temp
150: .lastIndexOf(".")), ".", "/");
151: }
152: }
153:
154: private static void initVelocity() {
155: try {
156: Properties props = new Properties();
157: props.setProperty("resource.loader", "templateLoader");
158: props.setProperty("templateLoader.loader.description",
159: "Project Engine loader");
160: props
161: .setProperty(
162: "templateLoader.resource.loader.class",
163: "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
164: props.setProperty(
165: "velocimacro.permissions.allow.inline.local.scope",
166: "true");
167: Velocity.init(props);
168: } catch (Exception ex) {
169: ex.printStackTrace();
170:
171: // How to indicate the problem ??
172: }
173: }
174: }
|