Class TemplateParser

java.lang.Object
cloud.opencode.base.sms.template.TemplateParser

public final class TemplateParser extends Object
Template Parser 模板解析器

Parses SMS templates with variable substitution.

解析带变量替换的短信模板。

Features | 主要功能:

  • Multiple variable patterns (${}, #{}, {{}}) - 多种变量模式
  • Strict and lenient parsing modes - 严格和宽松解析模式
  • Default value fallback - 默认值回退
  • Variable extraction - 变量提取

Usage Examples | 使用示例:

TemplateParser parser = TemplateParser.create();
String result = parser.parse("Hello ${name}!", Map.of("name", "World"));
Set<String> vars = parser.extractVariables("${a} and ${b}");

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(n) where n is template length - 时间复杂度: O(n),n 为模板长度
  • Space complexity: O(n) for result buffer - 空间复杂度: O(n) 结果缓冲区
Since:
JDK 25, opencode-base-sms V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static TemplateParser create()
      Create default parser 创建默认解析器
      Returns:
      the parser | 解析器
    • withPattern

      public static TemplateParser withPattern(Pattern pattern)
      Create parser with pattern 使用模式创建解析器
      Parameters:
      pattern - the variable pattern | 变量模式
      Returns:
      the parser | 解析器
    • hashPattern

      public static TemplateParser hashPattern()
      Create hash pattern parser (#{var}) 创建井号模式解析器
      Returns:
      the parser | 解析器
    • bracePattern

      public static TemplateParser bracePattern()
      Create brace pattern parser ({{var}}) 创建双大括号模式解析器
      Returns:
      the parser | 解析器
    • lenient

      public static TemplateParser lenient(String defaultValue)
      Create lenient parser 创建宽松模式解析器
      Parameters:
      defaultValue - the default value for missing variables | 缺失变量的默认值
      Returns:
      the parser | 解析器
    • extractVariables

      public List<String> extractVariables(String template)
      Parse template and extract variable names 解析模板并提取变量名
      Parameters:
      template - the template | 模板
      Returns:
      the variable names | 变量名列表
    • render

      public String render(String template, Map<String,String> variables)
      Render template with variables 使用变量渲染模板
      Parameters:
      template - the template | 模板
      variables - the variables | 变量
      Returns:
      the rendered content | 渲染后的内容
      Throws:
      SmsTemplateException - if strict mode and variable missing | 严格模式下变量缺失
    • validate

      public TemplateParser.ValidationResult validate(String template, Map<String,String> variables)
      Validate template has all required variables 验证模板包含所有必需变量
      Parameters:
      template - the template | 模板
      variables - the variables | 变量
      Returns:
      the validation result | 验证结果
    • countVariables

      public int countVariables(String template)
      Count variables in template 统计模板中的变量数
      Parameters:
      template - the template | 模板
      Returns:
      the count | 数量
    • hasVariables

      public boolean hasVariables(String template)
      Check if template has variables 检查模板是否包含变量
      Parameters:
      template - the template | 模板
      Returns:
      true if has variables | 如果包含变量返回true
    • escapeValue

      public static String escapeValue(String value)
      Escape special characters in value 转义值中的特殊字符
      Parameters:
      value - the value | 值
      Returns:
      the escaped value | 转义后的值