aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/escapevelocity/Template.java
blob: 0f4de8f8086714aacd17e4cfa7a321d088c9628f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (C) 2018 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.escapevelocity;

import com.google.escapevelocity.EvaluationContext.PlainEvaluationContext;
import java.io.IOException;
import java.io.Reader;
import java.util.Map;

/**
 * A template expressed in EscapeVelocity, a subset of the Velocity Template Language (VTL) from
 * Apache. The intent of this implementation is that if a template is accepted and successfully
 * produces output, that output will be identical to what Velocity would have produced for the same
 * template and input variables.
 *
 * @author emcmanus@google.com (Éamonn McManus)
 */
// TODO(emcmanus): spell out exactly what Velocity features are unsupported.
public class Template {
  private final Node root;

  /**
   * Used to resolve references to resources in the template, through {@code #parse} directives.
   *
   * <p>Here is an example that opens nested templates as resources relative to the calling class:
   *
   * <pre>{@code
   *   ResourceOpener resourceOpener = resourceName -> {
   *     InputStream inputStream = getClass().getResource(resourceName);
   *     if (inputStream == null) {
   *       throw new IOException("Unknown resource: " + resourceName);
   *     }
   *     return new BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8));
   *   };
   * }</pre>
   */
  @FunctionalInterface
  public interface ResourceOpener {

    /**
     * Returns a {@code Reader} that will be used to read the given resource, then closed.
     *
     * @param resourceName the name of the resource to be read. This will never be null.
     * @return a {@code Reader} for the resource.
     * @throws IOException if the resource cannot be opened.
     */
    Reader openResource(String resourceName) throws IOException;
  }

  /**
   * Parses a VTL template from the given {@code Reader}. The template cannot reference other
   * templates (for example with {@code #parse}). For that, use
   * {@link #parseFrom(String, ResourceOpener)}.
   *
   * @param reader a Reader that will supply the text of the template. It will be closed on return
   *     from this method.
   * @return an object representing the parsed template.
   * @throws IOException if there is an exception reading from {@code reader}, or if the template
   *     references another template via {@code #parse}.
   */
  public static Template parseFrom(Reader reader) throws IOException {
    ResourceOpener resourceOpener = resourceName -> {
      if (resourceName == null) {
        return reader;
      } else {
        throw new IOException("No ResourceOpener has been configured to read " + resourceName);
      }
    };
    try {
      return parseFrom((String) null, resourceOpener);
    } finally {
      reader.close();
    }
  }

  /**
   * Parse a VTL template of the given name using the given {@code ResourceOpener}.
   *
   * @param resourceName name of the resource. May be null.
   * @param resourceOpener used to open the initial resource and resources referenced by
   *     {@code #parse} directives in the template.
   * @return an object representing the parsed template.
   * @throws IOException if there is an exception opening or reading from any resource.
   */
  public static Template parseFrom(
      String resourceName, ResourceOpener resourceOpener) throws IOException {
    try (Reader reader = resourceOpener.openResource(resourceName)) {
      return new Parser(reader, resourceName, resourceOpener).parse();
    }
  }

  Template(Node root) {
    this.root = root;
  }

  /**
   * Evaluate the given template with the given initial set of variables.
   *
   * @param vars a map where the keys are variable names and the values are the corresponding
   *     variable values. For example, if {@code "x"} maps to 23, then {@code $x} in the template
   *     will expand to 23.
   *
   * @return the string result of evaluating the template.
   */
  public String evaluate(Map<String, ?> vars) {
    EvaluationContext evaluationContext = new PlainEvaluationContext(vars);
    return String.valueOf(root.evaluate(evaluationContext));
  }
}