Skip to content

Commit

Permalink
#842 migrated to velocity 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
olenagerasimova committed Dec 11, 2018
1 parent 1d43de3 commit f5faabb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -143,8 +143,8 @@ SOFTWARE.
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
Expand Down
36 changes: 28 additions & 8 deletions src/main/java/org/takes/rs/RsVelocity.java
Expand Up @@ -36,9 +36,8 @@
import lombok.ToString;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.log.NullLogChute;
import org.takes.Response;
import org.takes.Scalar;
import org.takes.misc.Utf8InputStreamReader;
import org.takes.misc.Utf8OutputStreamWriter;
import org.takes.misc.Utf8String;
Expand Down Expand Up @@ -124,6 +123,17 @@ public RsVelocity(final InputStream template, final Map<CharSequence,
*/
public RsVelocity(final String folder,
final InputStream template, final Map<CharSequence, Object> params) {
this(folder, template, () -> RsVelocity.convert(params));
}

/**
* Ctor.
* @param folder Template folder
* @param template Template
* @param params Map of params
*/
public RsVelocity(final String folder,
final InputStream template, final Scalar<Map<String, Object>> params) {
super(
new Response() {
@Override
Expand All @@ -132,7 +142,7 @@ public Iterable<String> head() {
}
@Override
public InputStream body() throws IOException {
return RsVelocity.render(folder, template, params);
return RsVelocity.render(folder, template, params.get());
}
}
);
Expand All @@ -148,14 +158,10 @@ public InputStream body() throws IOException {
*/
private static InputStream render(final String folder,
final InputStream template,
final Map<CharSequence, Object> params) throws IOException {
final Map<String, Object> params) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Writer writer = new Utf8OutputStreamWriter(baos);
final VelocityEngine engine = new VelocityEngine();
engine.setProperty(
RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,
new NullLogChute()
);
engine.setProperty(
"file.resource.loader.path",
folder
Expand Down Expand Up @@ -185,6 +191,20 @@ private static Map<CharSequence, Object> asMap(
return map;
}

/**
* Converts Map of CharSequence, Object to Map of String, Object.
* @param params Parameters in Map of CharSequence, Object
* @return Map of String, Object.
*/
private static Map<String, Object> convert(
final Map<CharSequence, Object> params) {
final Map<String, Object> map = new HashMap<>(params.size());
for (final Map.Entry<CharSequence, Object> ent : params.entrySet()) {
map.put(ent.getKey().toString(), ent.getValue());
}
return map;
}

/**
* Pair of values.
*/
Expand Down

0 comments on commit f5faabb

Please sign in to comment.