01 /*
02 * Copyright 2007 Roberto Fasciolo
03 *
04 * This file is part of vsitegen.
05 *
06 * vsitegen is free software; you can redistribute it and/or modify
07 * it under the terms of the GNU General Public License as published by
08 * the Free Software Foundation; either version 2 of the License, or
09 * (at your option) any later version.
10 *
11 * vsitegen is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with vsitegen; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package net.robyf.vsitegen.engine;
21
22 import java.io.StringWriter;
23 import java.text.DateFormat;
24 import java.util.Date;
25 import java.util.Locale;
26 import java.util.Properties;
27
28 import net.robyf.vsitegen.model.Navigation;
29
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.app.VelocityEngine;
32
33 public final class TemplateEvaluator {
34
35 private final VelocityEngine velocity;
36
37 private final String projectName;
38
39 private final Navigation navigation;
40
41 public TemplateEvaluator(final String projectName,
42 final Navigation navigation,
43 final String templatePath) {
44 this.projectName = projectName;
45 this.navigation = navigation;
46
47 Properties velocityProperties = new Properties();
48
49 // Two resource loaders, one for ant task and one for unit tests.
50 velocityProperties.put("resource.loader", "file, class");
51
52 // File resource loader for ant task.
53 velocityProperties.put("file.resource.loader.class",
54 "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
55 velocityProperties.put("file.resource.loader.path", templatePath);
56
57 // Classpath resource loader for unit tests.
58 velocityProperties.put("class.resource.loader.class",
59 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
60
61 this.velocity = new VelocityEngine();
62 try {
63 this.velocity.init(velocityProperties);
64 } catch (Exception e) {
65 throw new EvaluatorException("Error initializing velocity", e);
66 }
67 }
68
69 public String evaluate(final String template, final String pageId, final String content) {
70 VelocityContext context = new VelocityContext();
71 context.put("projectName", this.projectName);
72 context.put("navigation", this.navigation);
73 context.put("pageId", pageId);
74 context.put("content", content);
75 context.put("generationTime", DateFormat.getDateTimeInstance(DateFormat.LONG,
76 DateFormat.FULL,
77 Locale.ENGLISH)
78 .format(new Date()));
79
80 StringWriter writer = new StringWriter();
81
82 try {
83 this.velocity.mergeTemplate(template, "ISO-8859-1", context, writer);
84 } catch (Exception e) {
85 throw new EvaluatorException("Error merging template", e);
86 }
87
88 return writer.toString();
89 }
90
91 }
|