-1

Heys guys!

I'm having difficulties replacing string which consists of special characters.

So I have a following string for example:

Dear *|customer_name|*,

thank you for your order *|order_id|*.

Please expect delivery *|delivery_date|*

What I would like to to is to replace those dynamic variables with values.

I've done this snippet of code but it doesn't replace them correctly:

Map<String, String> structMap = getContectMap();
    for (String key : structMap.keySet()) {
        if (bodyText.contains(key)) {
            bodyText.replaceAll(getVariableKey(key), structMap.get(key));
        }
    }

private String getVariableKey(Object key) {
    return "\\*|" + key + "|\\*";
}

This is the ouput I get:

Dear User|User|User,

thank you for your order 1236|1236|1236.

Please expect delivery 5.12.2017|5.12.2017|5.12.2017

Any ideas what I'm doing wrong?

*EDIT* Found a problem. I should escape pipe character (|) too, works now.

Igor
  • 253
  • 3
  • 5
  • 14

1 Answers1

1

I would like to suggest you to use template engine instead of trying to reinvent the wheel.

Template engines help you to generate documents based on a template and your data.

There are several good Java frameworks what you can use to generate documents, I suggest you to check the following two:

  • Apache Velocity: easy to use, has good documentation and community
  • Jasper Reports: you can generate TXT, PDF, DOC, EXCEL, HTML, etc documents with this tool, it has a very good template editor application.

EDIT

If you really want to wtite your own template engine then this can be a solution:

public static void main(String[] args) {
    String template = "Dear CUSTOMER_NAME, \n\n"
            + "Thank you for your order ID is ORDER_ID.\n"
            + "Please expect delivery DELIVERY_DATE.";


    Date deliveryDate = new Date();
    String deliveryDateString = new SimpleDateFormat("dd/MM/yyyy").format(deliveryDate);


    Map<String, String> keywords = new HashMap<>();
    keywords.put("CUSTOMER_NAME", "Zappee");
    keywords.put("ORDER_ID", "123456");
    keywords.put("DELIVERY_DATE", deliveryDateString);

    for (Map.Entry<String, String> keyword : keywords.entrySet()) {
        template = template.replaceAll(keyword.getKey(), keyword.getValue());
    }

    System.out.println(template);
}

Output:

Dear Zappee, 

Thank you for your order ID is 123456.
Please expect delivery 01/12/2017.
zappee
  • 20,148
  • 14
  • 73
  • 129
  • Thank you for your suggestions! I would use template engine but problem is that we are trying to use same variable names and format in different outgoing channels. This \*|var_name|\* format is used by Mailchimp/Mandrill and we want to use it for sending SMS too. It's easier for users to set up their own dynamic content when they have same format across different parts of program. – Igor Dec 01 '17 at 10:09
  • I updated my post. Please check it. – zappee Dec 01 '17 at 10:26
  • Thank you for your response. I see what you mean but that still doesn't solve my situation. When our customers are making their Mandrill email templates, they have to use dynamic variables in following format: \*|variable|\*. This is what Mandrill supports for generating dynamic content. We want to use same format for generating SMS text in our program, so they don't have two different approaches for generating similar content. Your suggestion would make my life much easier but unfortunately I can't put it in use. – Igor Dec 01 '17 at 11:40