KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > ejb > VelocityScreenUtil


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.util.ejb;
21
22 import za.org.coefficient.core.Constants;
23
24 import org.apache.commons.lang.StringUtils;
25
26 import org.apache.velocity.Template;
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.Velocity;
29 import org.apache.velocity.context.Context;
30
31 import java.io.StringWriter JavaDoc;
32
33 import java.text.SimpleDateFormat JavaDoc;
34
35 import java.util.Map JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 /**
39  * DOCUMENT ME!
40  *
41  * @author $author$
42  * @version $Revision: 1.3 $
43  */

44 public class VelocityScreenUtil {
45     //~ Static fields/initializers =============================================
46

47     public static final String JavaDoc TEMPLATES = "/templates/";
48
49     static {
50         initVelocity();
51     }
52
53     //~ Methods ================================================================
54

55     /**
56      * This method will look for the template name on the classpath and if
57      * found merge it with the data provided in the map. The result is returned
58      * as a StringBuffer.
59      *
60      * @param templateName identifies the template that should
61      * be merged. If the template is not found or there is a parsing
62      * error in the template or there is a method invocation error
63      * while parsing the template then the return value will be null.
64      * It is assumed that this template will be found in the classpath
65      * in the same package space as the caller plus a level deeper
66      * called templates
67      * @param context is a map containing all the mappings that are to be
68      * merged into the template.
69      *
70      * @return StringBuffer containing the result of the merge, null if there
71      * were any exceptions thrown during the merge.
72      */

73     public static StringBuffer JavaDoc getProcessedScreen(String JavaDoc templateName,
74         Map JavaDoc context) {
75         String JavaDoc fullTemplateName =
76             getCallingClassPackage() + TEMPLATES + templateName;
77         StringBuffer JavaDoc retVal = null;
78         try {
79             Template currTemplate = Velocity.getTemplate(fullTemplateName);
80             StringWriter JavaDoc sw = new StringWriter JavaDoc();
81             context.put("dateFormat",
82                         new SimpleDateFormat JavaDoc(Constants.SYSTEM_DATE_FORMAT));
83             Context ctx = new VelocityContext(context);
84             currTemplate.merge(ctx, sw);
85             retVal = sw.getBuffer();
86         } catch (Exception JavaDoc e) {
87             e.printStackTrace();
88         }
89
90         return retVal;
91     }
92
93     public static StringBuffer JavaDoc getProcessedScreenFullyQualified(
94         String JavaDoc fullyQualifiedTemplateName, Map JavaDoc context) {
95         StringBuffer JavaDoc retVal = null;
96         try {
97             Template currTemplate =
98                 Velocity.getTemplate(fullyQualifiedTemplateName);
99             StringWriter JavaDoc sw = new StringWriter JavaDoc();
100             context.put("dateFormat",
101                         new SimpleDateFormat JavaDoc(Constants.SYSTEM_DATE_FORMAT));
102             Context ctx = new VelocityContext(context);
103             currTemplate.merge(ctx, sw);
104             retVal = sw.getBuffer();
105         } catch (Exception JavaDoc e) {
106             e.printStackTrace();
107         }
108
109         return retVal;
110     }
111
112     /**
113      * This method will look for the template name on the classpath and if
114      * found merge it with the data provided in the map. The result is returned
115      * as a StringBuffer.
116      *
117      * @param inString is a string containing velocity code that will be merged
118      * @param context is a map containing all the mappings that are to be
119      * merged into the template.
120      *
121      * @return StringBuffer containing the result of the merge, null if there
122      * were any exceptions thrown during the merge.
123      */

124     public static StringBuffer JavaDoc getProcessedString(String JavaDoc inString, Map JavaDoc context) {
125         StringBuffer JavaDoc retVal = null;
126         try {
127             StringWriter JavaDoc sw = new StringWriter JavaDoc();
128             context.put("dateFormat",
129                         new SimpleDateFormat JavaDoc(Constants.SYSTEM_DATE_FORMAT));
130             Context ctx = new VelocityContext(context);
131             Velocity.evaluate(ctx, sw, "temp", inString);
132             retVal = sw.getBuffer();
133         } catch (Exception JavaDoc e) {
134             e.printStackTrace();
135         }
136
137         return retVal;
138     }
139
140     public static String JavaDoc getCallingClassPackage() {
141         StackTraceElement JavaDoc[] ste = new Exception JavaDoc().getStackTrace();
142         if ((ste == null) || (ste.length < 3)) {
143             return null;
144         } else {
145             String JavaDoc temp = ste[2].getClassName();
146
147             return StringUtils.replace(temp.substring(0, temp.lastIndexOf(".")),
148                 ".", "/");
149         }
150     }
151
152     private static void initVelocity() {
153         try {
154             Properties JavaDoc props = new Properties JavaDoc();
155             props.setProperty("resource.loader", "templateLoader");
156             props.setProperty("templateLoader.loader.description",
157                 "Project Engine loader");
158             props.setProperty("templateLoader.resource.loader.class",
159                 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
160             props.setProperty("velocimacro.permissions.allow.inline.local.scope",
161                 "true");
162             Velocity.init(props);
163         } catch (Exception JavaDoc ex) {
164             ex.printStackTrace();
165
166             // How to indicate the problem ??
167
}
168     }
169 }
170
Popular Tags