开发者

How to create dynamic Template String

开发者 https://www.devze.com 2022-12-21 15:11 出处:网络
Is there is any API which replace template st开发者_运维技巧ring along with values using Spring or java.

Is there is any API which replace template st开发者_运维技巧ring along with values using Spring or java.

For example:

Dear %FIRST_NAME% %LAST_NAME%,
---- remaining text-----------

where parameters (FIRST_NAME, LAST_NAME) in the form of Map.


It's relatively simple to write code that will do this. If this is something you're going to be doing a lot however you might want to look into using an existing library like Velocity. It uses a different syntax for values however. See Getting Started.

If you do want to write this yourself try:

public static String replaceAll(String text, Map<String, String> params) {
  return replaceAll(text, params, '%', '%');
}

public static String replaceAll(String text, Map<String, String> params,
    char leading, char trailing) {
  String pattern = "";
  if (leading != 0) {
    pattern += leading;
  }
  pattern += "(\\w+)";
  if (trailing != 0) {
    pattern += trailing;
  }
  Pattern p = Pattern.compile(pattern);
  Matcher m = p.matcher(text);
  boolean result = m.find();
  if (result) {
    StringBuffer sb = new StringBuffer();
    do {
      String replacement = params.get(m.group(1));
      if (replacement == null) {
        replacement = m.group();
      }
      m.appendReplacement(sb, replacement);
      result = m.find();
    } while (result);
    m.appendTail(sb);
    return sb.toString();
  }
  return text;
}

For example:

String in = "Hi %FIRST_NAME% %LAST_NAME%.";
Map<String, String> params = new HashMap<String, String>();
params.put("FIRST_NAME", "John");
params.put("LAST_NAME", "Smith");
String out = replaceAll(in, params);
System.out.println(out);

Output:

Hi John Smith.


My favorite templating engine is Apache Velocity

Integrates nicely with Spring as well, theres an introductory article here


Check StringTemplate, http://www.stringtemplate.org/ and article here to get feel of it, http://www.codecapers.com/post/Generating-Structured-Text-with-StringTemplate.aspx


Usage:

replaceAll(string,
        "param1", "value 1",
        "param12", "value 12");

Implementation:

public static String replaceAll(String text, Object... replacements) {
    Assert.isTrue(replacements.length % 2 == 0, "Is not key - value pairs: " + replacements);

    Map< String, String> longToShortNamesMap = new TreeMap<>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.length() == o2.length() ? o1.compareTo(o2) : o2.length() - o1.length();
        }
    });
    for (int i = 0; i < replacements.length; i += 2) {
        longToShortNamesMap.put(Objects.toString(replacements[i]), Objects.toString(replacements[i+1]));
    }
    String result = text;
    for (String key : longToShortNamesMap.keySet()) {
        Assert.isTrue(text.contains(key), "Can not find key in the text " + key);
        result = result.replaceAll(key, longToShortNamesMap.get(key));
    }
    return result;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消