Class ExpressionTemplate

java.lang.Object
cloud.opencode.base.expression.ExpressionTemplate

public final class ExpressionTemplate extends Object
Expression Template Engine 表达式模板引擎

Template engine that mixes literal text with ${expression} placeholders. Each placeholder is evaluated using the expression engine and the result is converted to a string and substituted inline.

将字面文本与 ${expression} 占位符混合的模板引擎。 每个占位符使用表达式引擎求值,结果转换为字符串并内联替换。

Features | 主要功能:

  • Delimiter syntax: ${expression} (like JavaScript/Kotlin template strings) - 分隔符语法: ${expression}(类似JavaScript/Kotlin模板字符串)
  • Escape support: invalid input: '{@code \${} for literal {@code ${}} - 转义支持: {@code \${} 表示字面量 {@code ${}}</li> <li>Variable binding via Map or EvaluationContext - 通过Map或EvaluationContext绑定变量</li> <li>Full expression engine support inside placeholders - 占位符内支持完整的表达式引擎</li> <li>Nested brace handling within expressions - 表达式内的嵌套花括号处理</li> <li>Error reporting with position information for unclosed delimiters - 未关闭分隔符的位置信息错误报告</li> </ul> <p><strong>Usage Examples | 使用示例:</strong></p> <pre>{@code // Simple variable substitution String result = ExpressionTemplate.render( "Hello, ${name}!", Map.of("name", "World") ); // result = "Hello, World!" // Expression evaluation within template String result = ExpressionTemplate.render( "Total: ${price * quantity}", Map.of("price", 9.99, "quantity", 3) ); // result = "Total: 29.97" // Multiple placeholders String result = ExpressionTemplate.render( "${firstName} ${lastName} is ${age} years old", Map.of("firstName", "John", "lastName", "Doe", "age", 30) ); // result = "John Doe is 30 years old" // Escaped placeholder String result = ExpressionTemplate.render( "Use \\${variable} for templates", Map.of() ); // result = "Use ${variable} for templates" // With EvaluationContext StandardContext ctx = new StandardContext(); ctx.setVariable("user", myUser); String result = ExpressionTemplate.render( "Welcome, ${user.name}!", ctx ); }</pre> <p><strong>Security | 安全性:</strong></p> <ul> <li>Thread-safe: Yes, stateless utility with no shared mutable state - 线程安全: 是,无共享可变状态的无状态工具</li> <li>Null-safe: No, null template or variables throw NullPointerException - 空值安全: 否,null模板或变量抛出NullPointerException</li> <li>Expressions within placeholders are subject to the same security policies as direct expression evaluation - 占位符内的表达式受与直接表达式求值相同的安全策略约束</li> </ul> <p><strong>Performance | 性能:</strong></p> <ul> <li>Single-pass template parsing - 单次遍历模板解析</li> <li>StringBuilder-based rendering with no regex overhead - 基于StringBuilder的渲染,无正则开销</li> <li>Expression results are cached by the underlying expression engine - 表达式结果由底层表达式引擎缓存</li> </ul> @author Leon Soo <a href="https://leonsoo.com">www.LeonSoo.com</a> @see <a href="https://opencode.cloud">OpenCode.cloud</a> @since JDK 25, opencode-base-expression V1.0.3'
  • Method Details

    • render

      public static String render(String template, Map<String,Object> variables)
      Render a template with variable bindings from a Map 使用Map中的变量绑定渲染模板
      Parameters:
      template - the template string containing ${expression} placeholders | 包含 ${expression} 占位符的模板字符串
      variables - the variable map for expression evaluation | 用于表达式求值的变量映射
      Returns:
      the rendered string with all placeholders resolved | 所有占位符已解析的渲染字符串
      Throws:
      NullPointerException - if template or variables is null | 如果模板或变量为null
      OpenExpressionException - if a placeholder is unclosed or expression evaluation fails | 如果占位符未关闭或表达式求值失败
    • render

      public static String render(String template, EvaluationContext context)