views:

2932

answers:

8

I have the following template String: "Hello [Name] Please find attached [Invoice Number] which is due on [Due Date]".

I also have String variables for name, invoice number and due date - what's the best way to replace the tokens in the template with the variables?

(Note that if a variable happens to contain a token it should NOT be replaced).

---------- EDIT----------------

With thanks to laginimaineb and Alan M here's my solution:

public static String replaceTokens(String text,
  Map<String, String> replacements) {
 Pattern pattern = Pattern.compile("\\[(.+?)\\]");
 Matcher matcher = pattern.matcher(text);
 StringBuffer buffer = new StringBuffer();
 while (matcher.find()) {
  String replacement = replacements.get(matcher.group(1));
  if (replacement != null) {
//   matcher.appendReplacement(buffer, replacement);
   // see comment 
   matcher.appendReplacement(buffer, "");
   buffer.append(replacement);
  }
 }
 matcher.appendTail(buffer);
 return buffer.toString();
}
+5  A: 

You could try using a templating library like Apache Velocity.

http://velocity.apache.org/

Here is an example:

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;

public class TemplateExample {
    public static void main(String args[]) throws Exception {
        Velocity.init();

        VelocityContext context = new VelocityContext();
        context.put("name", "Mark");
        context.put("invoiceNumber", "42123");
        context.put("dueDate", "June 6, 2009");

        String template = "Hello $name. Please find attached invoice" +
                          " $invoiceNumber which is due on $dueDate.";
        StringWriter writer = new StringWriter();
        Velocity.evaluate(context, writer, "TemplateName", template);

        System.out.println(writer);
    }
}

The output would be:

Hello Mark. Please find attached invoice 42123 which is due on June 6, 2009.
hallidave
I've used velocity in the past. Works great.
Hardwareguy
agree, why reinvent the wheel
objects
+2  A: 

It depends of where the actual data that you want to replace is located. You might have a Map like this:

Map<String, String> values = new HashMap<String, String>();

containing all the data that can be replaced. Then you can iterate over the map and change everything in the String as follows:

String s = "Your String with [Fields]";
for (Map.Entry<String, String> e : values) {
  s = s.replaceAll("[" + e.getKey() + "]", e.getValue());
}

You could also iterate over the String and find the elements in the map. But that is a little bit more complicated because you need to parse the String searching for the []. You could do it with a regular expression using Pattern and Matcher.

rmarimon
+2  A: 

String.format("Hello %s Please find attached %s which is due on %s", name, invoice, date)

Bruno Ranschaert
Thanks - but in my case the template string can be modified by the user, so I can't be sure of the order of tokens
Mark
+1  A: 

I really don't think you need to use a templating engine or anything like that for this. You can use the String.format method, like so:

String template = "Hello %s Please find attached %s which is due on %s";

String message = String.format(template, name, invoiceNumber, dueDate);
Paul Morie
+3  A: 

The most efficient way would be using a matcher to continually find the expressions and replace them, then append the text to a string builder:

Pattern pattern = Pattern.compile("\\[(.+?)\\]");
Matcher matcher = pattern.matcher(text);
HashMap<String,String> replacements = new HashMap<String,String>();
//populate the replacements map ...
StringBuilder builder = new StringBuilder();
int i = 0;
while (matcher.find()) {
    String replacement = replacements.get(matcher.group(1));
    builder.append(text.substring(i, matcher.start()));
    if (replacement == null)
        builder.append(matcher.group(0));
    else
        builder.append(replacement);
    i = matcher.end();
}
builder.append(text.substring(i, text.length()));
return builder.toString();
laginimaineb
This is how I would do it, except I would use Matcher's appendReplacement() and appendTail() methods to copy the unmatched text; there's no need to do that by hand.
Alan Moore
You're right, never noticed those methods exist :P
laginimaineb
+1  A: 

Unfortunately the comfortable method String.format mentioned above is only available starting with Java 1.5 (which should be pretty standard nowadays, but you never know). Instead of that you might also use Java's class MessageFormat for replacing the placeholders.

It supports placeholders in the form '{number}', so your message would look like "Hello {0} Please find attached {1} which is due on {2}". These Strings can easily be externalized using ResourceBundles (e. g. for localization with multiple locales). The replacing would be done using the static'format' method of class MessageFormat:

String msg = "Hello {0} Please find attached {1} which is due on {2}";
String[] values = {
  "John Doe", "invoice #123", "2009-06-30"
};
System.out.println(MessageFormat.format(msg, values));
A: 

In the past, I've solved this kind of problem with StringTemplate and Groovy Templates.

Ultimately, the decision of using a templating engine or not should be based on the following factors:

  • Will you have many of these templates in the application?
  • Do you need the ability to modify the templates without restarting the application?
  • Who will be maintaining these templates? A Java programmer or a business analyst involved on the project?
  • Will you need to the ability to put logic in your templates, like conditional text based on values in the variables?
  • Will you need the ability to include other templates in a template?

If any of the above applies to your project, I would consider using a templating engine, most of which provide this functionality, and more.

Francois Gravel