哪些字符串替换 Java API(模板库)可用于进行条件和自定义字符串替换?
What String Replacement Java API's (Templating Library) are available to do conditional and custom string replacment?
我正在寻找一个开源 Java API 允许我根据自定义标签进行可配置的字符串替换。
示例模板:
Your did something<ACTION_DATETIME '" at "HH:mm AM" on "MM/dd/yyyy'> in[ {CITY}, {STATE}][ {ZIP5}]. Your's truly, [ {FIRST_INITIAL}][ {LAST_NAME}].
<ACTION_DATETIME '" at "MM/dd/yyyy" on "HH:mm AM'>
告诉我们要使用的日期以及所述日期的格式。
[ {CITY}, {STATE}]
告诉我们将城市和州放在此处,如果任一字段为空,则排除方括号之间的所有内容
示例结果:
Your did something at 1:32 PM on 10/13/2017 on in Mansfield, OH 44906. Your's truly, J Tully.
我已经有了一个使用正则表达式和正则字符串替换部分构建的解决方案,但是我希望有一个更强大的预构建解决方案。
我查看了 Commons Lang3 的 StrSubstitutor,虽然它处理简单和自定义的替换,但它似乎没有更多语法驱动的替换。
更新#1
目前停留在 Java 1.6.
我认为@TinkerTenorSoftwareGuy 关于模板库的建议是最佳选择。还有很多,我用Freemarker一点。
基本上你有一个模板:
You did ${action} at ${date} in ${city} ${state} ${zip}. Yours truly, ${firstName} ${lastName}.
以及包含数据的模型(java class):
class MyTemplate extends StringTemplate {
public MyTemplate(String action, Date date, /* etc */ ) { /* set the model state */ }
public String getTemplateFileLocation() { /* point to the template file */ }
public String process() { /* process the template and return the string */ }
public String getAction() { /* return the action as a string */ }
public String getDate() { /* return the formatted date as a string, i.e. */
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
return df.format(date);
}
public String getCity() { /* return the city as a string */ }
public String getState() { /* return the state as a string */ }
public String getZip() { /* return the zip code as a string */ }
public String getFirstName() { /* return the first name as a string */ }
public String getLastName() { /* return the last name as a string */ }
}
然后在您的代码中您可以实例化模板并对其进行处理。处理模板会将模板中 ${firstName}
的实例替换为模型中 getFirstName()
的 return 值(对于每个变量,依此类推):
StringTemplate template = new MyTemplate(action, date, city, state, zip, firstName, lastName);
String letter = template.process();
现在 letter
包含用模型中的值填充的模板。
有很多不同的模板库,但这是基本的想法。
我正在寻找一个开源 Java API 允许我根据自定义标签进行可配置的字符串替换。
示例模板:
Your did something<ACTION_DATETIME '" at "HH:mm AM" on "MM/dd/yyyy'> in[ {CITY}, {STATE}][ {ZIP5}]. Your's truly, [ {FIRST_INITIAL}][ {LAST_NAME}].
<ACTION_DATETIME '" at "MM/dd/yyyy" on "HH:mm AM'>
告诉我们要使用的日期以及所述日期的格式。
[ {CITY}, {STATE}]
告诉我们将城市和州放在此处,如果任一字段为空,则排除方括号之间的所有内容
示例结果:
Your did something at 1:32 PM on 10/13/2017 on in Mansfield, OH 44906. Your's truly, J Tully.
我已经有了一个使用正则表达式和正则字符串替换部分构建的解决方案,但是我希望有一个更强大的预构建解决方案。
我查看了 Commons Lang3 的 StrSubstitutor,虽然它处理简单和自定义的替换,但它似乎没有更多语法驱动的替换。
更新#1
目前停留在 Java 1.6.
我认为@TinkerTenorSoftwareGuy 关于模板库的建议是最佳选择。还有很多,我用Freemarker一点。
基本上你有一个模板:
You did ${action} at ${date} in ${city} ${state} ${zip}. Yours truly, ${firstName} ${lastName}.
以及包含数据的模型(java class):
class MyTemplate extends StringTemplate {
public MyTemplate(String action, Date date, /* etc */ ) { /* set the model state */ }
public String getTemplateFileLocation() { /* point to the template file */ }
public String process() { /* process the template and return the string */ }
public String getAction() { /* return the action as a string */ }
public String getDate() { /* return the formatted date as a string, i.e. */
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
return df.format(date);
}
public String getCity() { /* return the city as a string */ }
public String getState() { /* return the state as a string */ }
public String getZip() { /* return the zip code as a string */ }
public String getFirstName() { /* return the first name as a string */ }
public String getLastName() { /* return the last name as a string */ }
}
然后在您的代码中您可以实例化模板并对其进行处理。处理模板会将模板中 ${firstName}
的实例替换为模型中 getFirstName()
的 return 值(对于每个变量,依此类推):
StringTemplate template = new MyTemplate(action, date, city, state, zip, firstName, lastName);
String letter = template.process();
现在 letter
包含用模型中的值填充的模板。
有很多不同的模板库,但这是基本的想法。