KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > util > Template


1 /**
2  * Nov 22, 2004
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.util;
19
20 import java.io.IOException JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.Velocity;
29 import org.apache.velocity.exception.MethodInvocationException;
30 import org.apache.velocity.exception.ResourceNotFoundException;
31
32 /**
33  * A template is a string (sometimes of great length) that contains
34  * placeholders that can be substituted with values. A template is
35  * retrieved by name.
36  * <p>
37  * This class uses Velocity for its templating capability, but its
38  * interface is designed in such a way that the rest of the system need
39  * not be aware of this. This allows us to replace Velocity with another
40  * templating system if the need arises.
41  *
42  * @author jonni
43  * @author hgani
44  * @version $Id: Template.java,v 1.23 2006/08/16 12:45:13 hgani Exp $
45  */

46 public final class Template {
47   ///////////////////////////////
48
////////// Constants //////////
49
///////////////////////////////
50
/**
51    * Velocity config file
52    */

53   private static final String JavaDoc CONFIG = "velocity.properties";
54
55   /// Velocity templates for generating HTML/JS code. ///
56
public static final String JavaDoc CALENDAR = "uitags/calendar/calendar.vm";
57
58   public static final String JavaDoc CALENDAR_UPDATE_DATE =
59       "uitags/calendar/updateDate.vm";
60
61   public static final String JavaDoc CALENDAR_LIST_MONTHS =
62       "uitags/calendar/listMonths.vm";
63
64   public static final String JavaDoc CALENDAR_LIST_YEARS =
65       "uitags/calendar/listYears.vm";
66
67   public static final String JavaDoc FORM_GUIDE = "uitags/formGuide/formGuide.vm";
68
69   public static final String JavaDoc OPTION_TRANSFER =
70       "uitags/optionTransfer/optionTransfer.vm";
71
72   public static final String JavaDoc OPTION_TRANSFER_RETURN =
73       "uitags/optionTransfer/return.vm";
74
75   public static final String JavaDoc OPTION_TRANSFER_RETURN_ALL =
76       "uitags/optionTransfer/returnAll.vm";
77
78   public static final String JavaDoc OPTION_TRANSFER_TRANSFER =
79       "uitags/optionTransfer/transfer.vm";
80
81   public static final String JavaDoc OPTION_TRANSFER_TRANSFER_ALL =
82       "uitags/optionTransfer/transferAll.vm";
83
84   public static final String JavaDoc PANEL = "uitags/panel/panel.vm";
85
86   public static final String JavaDoc PANEL_ANCHOR = "uitags/panel/anchor.vm";
87
88   public static final String JavaDoc PANEL_DRAG = "uitags/panel/drag.vm";
89
90   public static final String JavaDoc PANEL_HIDE = "uitags/panel/hide.vm";
91
92   public static final String JavaDoc PANEL_SHOW = "uitags/panel/show.vm";
93
94   public static final String JavaDoc PANEL_STICK = "uitags/panel/stick.vm";
95
96   public static final String JavaDoc SEARCH = "uitags/search/search.vm";
97
98   public static final String JavaDoc SELECT = "uitags/select/select.vm";
99
100   public static final String JavaDoc SHIFT = "uitags/shift/shift.vm";
101
102   public static final String JavaDoc SORT = "uitags/sort/sort.vm";
103
104
105
106   ////////////////////////////
107
////////// Fields //////////
108
////////////////////////////
109

110   /**
111    * Holds values that will go to the placeholders
112    */

113   private Map JavaDoc params = new HashMap JavaDoc();
114   /**
115    * The file containing the template requested by the client
116    */

117   private String JavaDoc file;
118
119
120
121   ///////////////////////////////////////////
122
////////// Static initialization //////////
123
///////////////////////////////////////////
124

125   static {
126     init();
127   }
128
129   /**
130    * Loads Velocity config file and uses it to initialize Velocity.
131    *
132    * @throws PropertiesLoadingException if failed to load the config file
133    * @throws RuntimeException if Velocity throws exception during initialization
134    */

135   private static void init() {
136     Properties JavaDoc props = loadProperties(CONFIG);
137     try {
138       Velocity.init(props);
139     }
140     catch (Exception JavaDoc e) {
141       // Velocity.init() throws an Exception object, all we can do is wrap it
142
// in a RuntimeException.
143
throw new RuntimeException JavaDoc(e);
144     }
145   }
146
147   /**
148    * Loads Velocity config file.
149    *
150    * @param file name of the config file
151    * @return loaded configuration entries
152    * @throws PropertiesLoadingException if failed to load the config file
153    */

154   private static Properties JavaDoc loadProperties(String JavaDoc file) {
155     Properties JavaDoc props = new Properties JavaDoc();
156     try {
157       props.load(Template.class.getResourceAsStream(file));
158     }
159     catch (IOException JavaDoc e) {
160       throw new PropertiesLoadingException(e, file);
161     }
162     return props;
163   }
164
165
166
167   //////////////////////////////////
168
////////// Construction //////////
169
//////////////////////////////////
170

171   /**
172    * Factory method for creating a template for the given name.
173    *
174    * @param name which template to create?
175    * @return the template for the given name
176    */

177   public static Template forName(String JavaDoc name) {
178     // The "name" is mapped to a file name.
179
return new Template(name);
180   }
181
182   /**
183    * Non-instantiable by client, called by the factory method.
184    *
185    * @param file the name of the file containing the requested template
186    */

187   private Template(String JavaDoc file) {
188     this.file = file;
189   }
190
191
192
193   ////////////////////////////////////////
194
////////// For use by clients //////////
195
////////////////////////////////////////
196

197   /**
198    * Binds the specified value to a placeholder with the specified name.
199    * The actual value substitution is not performed until {@link #eval()}
200    * or {@link #evalToString()} is invoked.
201    *
202    * @param key the name of the placeholder to which the value is to be inserted
203    * @param value the value that goes to the placeholder
204    */

205   public void map(String JavaDoc key, Object JavaDoc value) {
206     if (key == null) {
207       throw new NullPointerException JavaDoc(
208           "Key of template parameter can't be null.");
209     }
210     this.params.put(key, value);
211   }
212
213   /**
214    * Performs the actual substitution of placeholders with values that
215    * have been previously supplied to {@link #map(String, Object)}.
216    *
217    * @return substitution result
218    * @throws RuntimeException if failed to perform value substitution
219    */

220   public StringWriter JavaDoc eval() {
221     map("velocityAdapter", new VelocityAdapter());
222
223     StringWriter JavaDoc result = new StringWriter JavaDoc();
224     try {
225       Velocity.mergeTemplate(
226           this.file, "ISO8859-1", new VelocityContext(this.params), result);
227     }
228     catch (ResourceNotFoundException e) {
229       throw new RuntimeException JavaDoc(e);
230     }
231     catch (MethodInvocationException e) {
232       throw new RuntimeException JavaDoc(e);
233     }
234     catch (Exception JavaDoc e) {
235       throw new RuntimeException JavaDoc(e);
236     }
237
238     return result;
239   }
240
241   /**
242    * Same as {@link #eval()}, only return value is different.
243    *
244    * @return substitution result
245    * @throws RuntimeException if failed to perform value substitution
246    */

247   public String JavaDoc evalToString() {
248     return eval().toString();
249   }
250 }
251
Popular Tags