KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > component > VelocityTemplate


1 package org.javabb.component;
2
3 import java.io.StringWriter JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.apache.velocity.Template;
10 import org.apache.velocity.VelocityContext;
11 import org.apache.velocity.app.VelocityEngine;
12
13 import com.opensymphony.webwork.views.velocity.VelocityManager;
14
15 /*
16  * Copyright 2004 JavaFree.org
17  *
18  * Licensed under the Apache License, Version 2.0 (the "License");
19  * you may not use this file except in compliance with the License.
20  * You may obtain a copy of the License at
21  *
22  * http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS,
26  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  */

30
31 /**
32  * $Id: VelocityTemplate.java,v 1.3.4.1.6.2 2006/04/17 17:47:31 daltoncamargo Exp $
33  * @author: Dalton Camargo
34  */

35 public class VelocityTemplate {
36     protected static final Log log = LogFactory.getLog(VelocityTemplate.class);
37
38     /**
39      * Obtem o resultado gerado por um template velocity
40      * @param velValues - Parametros a serem inseridos no contexto
41      * @param template - Template a ser realizado o parser
42      * @return Template renderizado
43      */

44     public static String JavaDoc makeTemplate(Map JavaDoc velValues, String JavaDoc template) {
45         String JavaDoc htmlDoVelocity = "";
46         try {
47             VelocityManager vle = VelocityManager.getInstance();
48             VelocityEngine ve = vle.getVelocityEngine();
49             Template t = ve.getTemplate(template);
50             VelocityContext context = new VelocityContext();
51             Iterator JavaDoc itChaves = velValues.keySet().iterator();
52             while (itChaves.hasNext()) {
53                 String JavaDoc chave = (String JavaDoc) itChaves.next();
54                 context.put(chave, velValues.get(chave));
55             }
56             StringWriter JavaDoc writer = new StringWriter JavaDoc();
57             t.merge(context, writer);
58
59             htmlDoVelocity = writer.toString();
60
61         } catch (Exception JavaDoc e) {
62             log.error("Error at VelocityTemplate:" + e);
63             e.printStackTrace();
64         }
65
66         return htmlDoVelocity;
67     }
68
69 }
Popular Tags