| 1 |
/* |
| 2 |
* Copyright (C) 2018 Google, Inc. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
package com.google.escapevelocity; |
| 17 |
|
| 18 |
import com.google.escapevelocity.EvaluationContext.PlainEvaluationContext; |
| 19 |
import java.io.IOException; |
| 20 |
import java.io.Reader; |
| 21 |
import java.util.Map; |
| 22 |
|
| 23 |
/** |
| 24 |
* A template expressed in EscapeVelocity, a subset of the Velocity Template Language (VTL) from |
| 25 |
* Apache. The intent of this implementation is that if a template is accepted and successfully |
| 26 |
* produces output, that output will be identical to what Velocity would have produced for the same |
| 27 |
* template and input variables. |
| 28 |
* |
| 29 |
* @author [email protected] (Éamonn McManus) |
| 30 |
*/ |
| 31 |
// TODO(emcmanus): spell out exactly what Velocity features are unsupported. |
| 32 |
public class Template { |
| 33 |
private final Node root; |
| 34 |
|
| 35 |
/** |
| 36 |
* Caches {@link Method} objects for public methods accessed through references. The first time |
| 37 |
* we evaluate {@code $var.property} or {@code $var.method(...)} for a {@code $var} of a given |
| 38 |
* class and for a given property or method signature, we'll store the resultant {@link Method} |
| 39 |
* object. Every subsequent time we'll reuse that {@link Method}. The method lookup is quite slow |
| 40 |
* so caching is useful. The main downside is that we may potentially hold on to {@link Method} |
| 41 |
* objects that will never be used with this {@link Template} again. But in practice templates |
| 42 |
* tend to be used repeatedly with the same classes. |
| 43 |
*/ |
| 44 |
private final MethodFinder methodFinder = new MethodFinder(); |
| 45 |
|
| 46 |
/** |
| 47 |
* Used to resolve references to resources in the template, through {@code #parse} directives. |
| 48 |
* |
| 49 |
* <p>Here is an example that opens nested templates as resources relative to the calling class: |
| 50 |
* |
| 51 |
* <pre>{@code |
| 52 |
* ResourceOpener resourceOpener = resourceName -> { |
| 53 |
* InputStream inputStream = getClass().getResource(resourceName); |
| 54 |
* if (inputStream == null) { |
| 55 |
* throw new IOException("Unknown resource: " + resourceName); |
| 56 |
* } |
| 57 |
* return new BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8)); |
| 58 |
* }; |
| 59 |
* }</pre> |
| 60 |
*/ |
| 61 |
@FunctionalInterface |
| 62 |
public interface ResourceOpener { |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns a {@code Reader} that will be used to read the given resource, then closed. |
| 66 |
* |
| 67 |
* @param resourceName the name of the resource to be read. This will never be null. |
| 68 |
* @return a {@code Reader} for the resource. |
| 69 |
* @throws IOException if the resource cannot be opened. |
| 70 |
*/ |
| 71 |
Reader openResource(String resourceName) throws IOException; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Parses a VTL template from the given {@code Reader}. The template cannot reference other |
| 76 |
* templates (for example with {@code #parse}). For that, use |
| 77 |
* {@link #parseFrom(String, ResourceOpener)}. |
| 78 |
* |
| 79 |
* @param reader a Reader that will supply the text of the template. It will be closed on return |
| 80 |
* from this method. |
| 81 |
* @return an object representing the parsed template. |
| 82 |
* @throws IOException if there is an exception reading from {@code reader}, or if the template |
| 83 |
* references another template via {@code #parse}. |
| 84 |
*/ |
| 85 |
public static Template parseFrom(Reader reader) throws IOException { |
| 86 |
ResourceOpener resourceOpener = resourceName -> { |
| 87 |
if (resourceName == null) { |
| 88 |
return reader; |
| 89 |
} else { |
| 90 |
throw new IOException("No ResourceOpener has been configured to read " + resourceName); |
| 91 |
} |
| 92 |
}; |
| 93 |
try { |
| 94 |
return parseFrom((String) null, resourceOpener); |
| 95 |
} finally { |
| 96 |
reader.close(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Parse a VTL template of the given name using the given {@code ResourceOpener}. |
| 102 |
* |
| 103 |
* @param resourceName name of the resource. May be null. |
| 104 |
* @param resourceOpener used to open the initial resource and resources referenced by |
| 105 |
* {@code #parse} directives in the template. |
| 106 |
* @return an object representing the parsed template. |
| 107 |
* @throws IOException if there is an exception opening or reading from any resource. |
| 108 |
*/ |
| 109 |
public static Template parseFrom( |
| 110 |
String resourceName, ResourceOpener resourceOpener) throws IOException { |
| 111 |
try (Reader reader = resourceOpener.openResource(resourceName)) { |
| 112 |
return new Parser(reader, resourceName, resourceOpener).parse(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
Template(Node root) { |
| 117 |
this.root = root; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Evaluate the given template with the given initial set of variables. |
| 122 |
* |
| 123 |
* @param vars a map where the keys are variable names and the values are the corresponding |
| 124 |
* variable values. For example, if {@code "x"} maps to 23, then {@code $x} in the template |
| 125 |
* will expand to 23. |
| 126 |
* |
| 127 |
* @return the string result of evaluating the template. |
| 128 |
*/ |
| 129 |
public String evaluate(Map<String, ?> vars) { |
| 130 |
EvaluationContext evaluationContext = new PlainEvaluationContext(vars, methodFinder); |
| 131 |
return String.valueOf(root.evaluate(evaluationContext)); |
| 132 |
} |
| 133 |
} |