KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > TemplateUtils


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32
33 public class TemplateUtils {
34     
35     public static String JavaDoc getTemplatePath(Object JavaDoc object) {
36         return getTemplatePath(object, null);
37     }
38     
39     public static String JavaDoc getTemplatePath(Class JavaDoc clazz) {
40         return getTemplatePath(clazz, null);
41     }
42     
43     public static String JavaDoc getTemplatePath(Object JavaDoc object, String JavaDoc suffix) {
44         return getTemplatePath(object.getClass(), suffix);
45     }
46     
47     public static String JavaDoc getTemplatePath(Class JavaDoc clazz, String JavaDoc suffix) {
48         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
49         sb.append("classpath:/");
50         sb.append(clazz.getName().replace('.', '/'));
51         if (suffix != null) {
52             sb.append(suffix);
53         }
54         sb.append(".ftl");
55         return sb.toString();
56     }
57     
58     public static void render(FormContext context, String JavaDoc template,
59             String JavaDoc modelKey, Object JavaDoc object, PrintWriter JavaDoc writer) {
60         
61         HashMap JavaDoc model = new HashMap JavaDoc();
62         model.put(modelKey, object);
63         context.getTemplateRenderer().render(template, model, writer);
64     }
65         
66     public static String JavaDoc getInitScript(Element element) {
67         return getInitScript(element, element.getClass());
68     }
69     
70     public static String JavaDoc getInitScript(Element element, Class JavaDoc baseClass) {
71         StringWriter JavaDoc sw = new StringWriter JavaDoc();
72         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(sw);
73                 
74         FormContext context = element.getForm().getFormContext();
75         String JavaDoc template = getTemplatePath(baseClass, "_init");
76         Map JavaDoc model = Collections.singletonMap("element", element);
77         context.getTemplateRenderer().render(template, model, writer);
78         
79         return sw.toString();
80     }
81
82 }
83
Popular Tags