1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16 */
17 package org.bejug.javacareers.common.templates;
18
19 import java.io.File;
20 import java.io.FilenameFilter;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.velocity.Template;
27 import org.apache.velocity.app.VelocityEngine;
28 import org.apache.velocity.exception.VelocityException;
29 import org.bejug.javacareers.common.util.FileUtilities;
30
31 /***
32 * @author bbr (last modified by $Author: shally $)
33 * @version $Revision: 1.16 $ - $Date: 2005/12/20 15:36:46 $
34 */
35 public class TemplateUtilities {
36
37 private VelocityEngine velocityEngine;
38
39 private static final Log LOG = LogFactory.getLog(TemplateUtilities.class);
40 private static final String PATH = "file.resource.loader.path";
41
42
43 /***
44 * default constructor.
45 */
46 public TemplateUtilities() {
47 }
48
49
50 /***
51 * gets a template by name.
52 * @param template a String indicating the name of the template.
53 * @return a TemplateHolder holding the wanted Template.
54 */
55 public TemplateHolder getTemplate(String template) {
56 LOG.info("Debug: Getting template: "+template);
57 TemplateHolder holder = new TemplateHolder();
58 try {
59 Template velTemplate = null;
60 velTemplate = velocityEngine.getTemplate(template);
61 velTemplate.touch();
62 LOG.info("Debug: Retrieved "+velTemplate.getName()+" from engine");
63 holder.setTemplate(velTemplate);
64 holder.setFilepath(getPath().getAbsolutePath()+File.separatorChar+template);
65 LOG.info("Debug: Set filepath to "+holder.getFilepath());
66 } catch (VelocityException e) {
67
68 LOG.info("Debug: Error getting template: "+e);
69 } catch (Exception e) {
70
71 LOG.info("Debug: Error getting template: "+e);
72 }
73 return holder;
74 }
75
76 /***
77 * gets a template based on the name and locale of the template.
78 * @param template the name of the template to match.
79 * @param locale the locale to match
80 * @return the TemplateHolder matching the given criteria.
81 */
82 public TemplateHolder getTemplate(String template,String locale) {
83 TemplateHolder holder = new TemplateHolder();
84 holder = getTemplate(template+"_"+locale+".vm");
85 holder.setLocale(locale);
86 return holder;
87 }
88
89 /***
90 * gets all the templates available.
91 * @return a List containing all velocity templates.
92 */
93 public List getAllTemplates() {
94 File path = getPath();
95 File[] files = path.listFiles(new FilenameFilter() {
96 public boolean accept(File dir, String file) {
97 if (FileUtilities.getFileExtension(file).equalsIgnoreCase("vm")) {
98 return true;
99 }
100 return false;
101 }
102
103 });
104 return loadHolders(files);
105 }
106
107 /***
108 * gets all templates that are written for a certain language
109 * @param language the language to match.
110 * @return a List containing all templates for a certain language.
111 */
112 public List getAllTemplatesByLanguage(final String language) {
113 File path = getPath();
114 File[] files = path.listFiles(new FilenameFilter() {
115 public boolean accept(File dir, String file) {
116 if (FileUtilities.getFileName(file).endsWith("_" + language)) {
117 return true;
118 }
119 return false;
120 }
121
122 });
123 return loadHolders(files);
124 }
125
126 /***
127 * loads all templates by name.
128 * @param template the name of the template.
129 * @return a List of templates who's names start with template.
130 */
131 public List getAllTemplatesByName(final String template) {
132 File path = getPath();
133 File[] files = path.listFiles(new FilenameFilter() {
134 public boolean accept(File dir, String file) {
135 if (FileUtilities.getFileName(file).startsWith(template)) {
136 return true;
137 }
138 return false;
139 }
140 });
141 return loadHolders(files);
142 }
143
144 /***
145 * loads the TemplateHolders.
146 * @param files holds the File-handles for the TemplateHolders.
147 * @return a List containing the TemplateHolders.
148 */
149 private List loadHolders(File [] files) {
150 List list = new ArrayList();
151 for (int a=0;a<files.length;a++) {
152 TemplateHolder holder = new TemplateHolder();
153 File file = files[a];
154 holder = getTemplate(file.getName());
155
156 int ix1 = file.getName().lastIndexOf('_');
157 int ix2 = file.getName().lastIndexOf('.');
158 if (ix1 > 0 && ix2 > ix1){
159 holder.setLocale(file.getName().substring(ix1+1,ix2));
160 }
161 list.add(holder);
162 }
163 return list;
164 }
165
166 /***
167 * @return the wanted VelocityEngine.
168 */
169 public VelocityEngine getVelocityEngine() {
170 return velocityEngine;
171 }
172
173 /***
174 * @param velocityEngine the engine to set.
175 */
176 public void setVelocityEngine(VelocityEngine velocityEngine) {
177 this.velocityEngine = velocityEngine;
178 }
179
180 /***
181 * @return a File-handle holding the velocityEngine's path.
182 */
183 private File getPath() {
184 String prop = (String) velocityEngine.getProperty(PATH);
185 File dir = new File(prop);
186 LOG.info("Debug: Using directory: " + dir);
187 return dir;
188 }
189 }
190
191 /***
192 * $Log: TemplateUtilities.java,v $
193 * Revision 1.16 2005/12/20 15:36:46 shally
194 * CheckStyle and PMD changes.
195 *
196 * Revision 1.15 2005/12/08 14:53:46 shally
197 * Opkuis voor checkstyle.
198 *
199 * Revision 1.14 2005/12/07 23:07:45 shally
200 * Adding editing for velocity templates, fixing some cosmetics
201 *
202 * Revision 1.13 2005/09/30 14:38:07 bavo_jcs
203 * Fixed URL
204 *
205 * Revision 1.12 2005/09/13 08:11:06 schauwvliege
206 * organize imports
207 *
208 * Revision 1.11 2005/08/20 13:01:21 stephan_janssen
209 * FileUtilities import refactoring
210 *
211 * Revision 1.10 2005/08/10 09:04:46 bavo_jcs
212 * Optimized imports according to checkstyle
213 *
214 * Revision 1.9 2005/08/09 12:59:53 bavo_jcs
215 * Optimized imports
216 *
217 * Revision 1.8 2005/08/08 09:38:22 bme_jcs
218 * resolved checkstyle errors
219 *
220 * Revision 1.7 2005/08/05 10:09:23 bavo_jcs
221 * Extracted File methods to static class
222 *
223 * Revision 1.6 2005/08/05 07:00:59 bme_jcs
224 * resolved checkstyle errors
225 *
226 * Revision 1.5 2005/07/18 15:42:31 bavo_jcs
227 * search fields alignment
228 *
229 * Revision 1.4 2005/07/15 15:58:30 bavo_jcs
230 * Templates I18N and writing
231 *
232 * Revision 1.3 2005/07/15 09:31:41 bavo_jcs
233 * Templates early update
234 *
235 * Revision 1.2 2005/07/14 15:49:40 bavo_jcs
236 * Templates early update
237 *
238 * Revision 1.1 2005/07/14 15:30:41 bavo_jcs
239 * Templates early update
240 *
241 */