Class TemplateParser
java.lang.Object
cloud.opencode.base.sms.template.TemplateParser
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordValidation Result 验证结果 -
Method Summary
Modifier and TypeMethodDescriptionstatic TemplateParserCreate brace pattern parser ({{var}}) 创建双大括号模式解析器intcountVariables(String template) Count variables in template 统计模板中的变量数static TemplateParsercreate()Create default parser 创建默认解析器static StringescapeValue(String value) Escape special characters in value 转义值中的特殊字符extractVariables(String template) Parse template and extract variable names 解析模板并提取变量名static TemplateParserCreate hash pattern parser (#{var}) 创建井号模式解析器booleanhasVariables(String template) Check if template has variables 检查模板是否包含变量static TemplateParserCreate lenient parser 创建宽松模式解析器Render template with variables 使用变量渲染模板Validate template has all required variables 验证模板包含所有必需变量static TemplateParserwithPattern(Pattern pattern) Create parser with pattern 使用模式创建解析器
-
Method Details
-
create
-
withPattern
Create parser with pattern 使用模式创建解析器- Parameters:
pattern- the variable pattern | 变量模式- Returns:
- the parser | 解析器
-
hashPattern
Create hash pattern parser (#{var}) 创建井号模式解析器- Returns:
- the parser | 解析器
-
bracePattern
Create brace pattern parser ({{var}}) 创建双大括号模式解析器- Returns:
- the parser | 解析器
-
lenient
Create lenient parser 创建宽松模式解析器- Parameters:
defaultValue- the default value for missing variables | 缺失变量的默认值- Returns:
- the parser | 解析器
-
extractVariables
-
render
-
validate
Validate template has all required variables 验证模板包含所有必需变量- Parameters:
template- the template | 模板variables- the variables | 变量- Returns:
- the validation result | 验证结果
-
countVariables
Count variables in template 统计模板中的变量数- Parameters:
template- the template | 模板- Returns:
- the count | 数量
-
hasVariables
Check if template has variables 检查模板是否包含变量- Parameters:
template- the template | 模板- Returns:
- true if has variables | 如果包含变量返回true
-
escapeValue
-