Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
VelocityPage |
|
| 1.2;1.2 |
1 | 1 | /** |
2 | * Copyright (c) 2012-2014, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.jcabi.velocity; | |
31 | ||
32 | import java.io.PrintWriter; | |
33 | import java.io.StringWriter; | |
34 | import java.util.Map; | |
35 | import javax.validation.constraints.NotNull; | |
36 | import org.apache.velocity.Template; | |
37 | import org.apache.velocity.VelocityContext; | |
38 | import org.apache.velocity.app.VelocityEngine; | |
39 | import org.apache.velocity.runtime.RuntimeConstants; | |
40 | import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; | |
41 | ||
42 | /** | |
43 | * Velocity page, builder/wrapper around Apache Velocity template. | |
44 | * | |
45 | * <p>The template should be in classpath: | |
46 | * | |
47 | * <pre> String text = new VelocityPage("com/foo/my-template.vm") | |
48 | * .set("name", "John Doe") | |
49 | * .toString();</pre> | |
50 | * | |
51 | * <p>At the moment all logging is forwarded to LOG4J. In Velocity 2.0 there | |
52 | * will be an adapter for SLF4J and we'll use it: {@code Slf4jLogChute}. | |
53 | * | |
54 | * <p>The class is mutable and thread-safe. | |
55 | * | |
56 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
57 | * @version $Id$ | |
58 | * @since 0.1.6 | |
59 | * @see <a href="http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html">Velocity User Guide</a> | |
60 | * @see <a href="http://velocity.apache.org/engine/releases/velocity-1.7/developer-guide.html">Velocity Developer Guide</a> | |
61 | * @see <a href="http://velocity.apache.org/engine/devel/apidocs/org/apache/velocity/slf4j/Slf4jLogChute.html">Slf4jLogChute</a> | |
62 | */ | |
63 | public final class VelocityPage { | |
64 | ||
65 | /** | |
66 | * The engine to use. | |
67 | */ | |
68 | 1 | private static final VelocityEngine ENGINE = VelocityPage.init(); |
69 | ||
70 | /** | |
71 | * Name of resource. | |
72 | */ | |
73 | private final transient String name; | |
74 | ||
75 | /** | |
76 | * The context. | |
77 | */ | |
78 | 1 | private final transient VelocityContext context = new VelocityContext(); |
79 | ||
80 | /** | |
81 | * Public ctor, with absolute resource name in classpath. | |
82 | * @param res Name of resource with template (absolute resource name in | |
83 | * classpath) | |
84 | */ | |
85 | 1 | public VelocityPage(@NotNull final String res) { |
86 | 1 | this.name = res; |
87 | 1 | } |
88 | ||
89 | /** | |
90 | * Set the name to the value specified. | |
91 | * @param prop Name of the property to set | |
92 | * @param value The value to use | |
93 | * @return This object | |
94 | */ | |
95 | public VelocityPage set(@NotNull final String prop, final Object value) { | |
96 | 1 | synchronized (this.context) { |
97 | 1 | this.context.put(prop, value); |
98 | 1 | } |
99 | 1 | return this; |
100 | } | |
101 | ||
102 | /** | |
103 | * Set all names in one go. | |
104 | * @param args Map of arguments | |
105 | * @return This object | |
106 | * @since 0.8 | |
107 | */ | |
108 | public VelocityPage set(@NotNull final Map<String, Object> args) { | |
109 | 0 | synchronized (this.context) { |
110 | 0 | for (final Map.Entry<String, Object> entry : args.entrySet()) { |
111 | 0 | this.context.put(entry.getKey(), entry.getValue()); |
112 | 0 | } |
113 | 0 | } |
114 | 0 | return this; |
115 | } | |
116 | ||
117 | /** | |
118 | * {@inheritDoc} | |
119 | */ | |
120 | @Override | |
121 | public String toString() { | |
122 | 1 | final Template template = |
123 | VelocityPage.ENGINE.getTemplate(this.name, "UTF-8"); | |
124 | 1 | final StringWriter writer = new StringWriter(); |
125 | 1 | template.merge(this.context, new PrintWriter(writer)); |
126 | 1 | return writer.toString(); |
127 | } | |
128 | ||
129 | /** | |
130 | * Create and initialize Velocity engine. | |
131 | * @return The engine to use | |
132 | */ | |
133 | private static VelocityEngine init() { | |
134 | 1 | final VelocityEngine engine = new VelocityEngine(); |
135 | 1 | engine.setProperty("resource.loader", "cp"); |
136 | 1 | engine.setProperty( |
137 | "cp.resource.loader.class", | |
138 | ClasspathResourceLoader.class.getName() | |
139 | ); | |
140 | 1 | engine.setProperty( |
141 | RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, | |
142 | "org.apache.velocity.runtime.log.Log4JLogChute" | |
143 | ); | |
144 | 1 | engine.setProperty( |
145 | "runtime.log.logsystem.log4j.logger", | |
146 | "org.apache.velocity" | |
147 | ); | |
148 | 1 | engine.init(); |
149 | 1 | return engine; |
150 | } | |
151 | ||
152 | } |