3

I am a new bee to Java.

My intension is to use the template like sentences in Java program (no JSP or any web related pages)

Example:

String name = "Jon";

"#{ name } invited you";

      or 

 String user.name = "Jon";

 "#{ user.name } invited you";

If I pass this string to some method, I should get

"Jon invited you"

I've gone through some expression languages MVEL, OGNL, JSTL EL

In MVEL and OGNL, I have to write some set of code to achieve this but in some other way.

I can achieve this using JSTL EL only in JSP files not in java program.

Is there any way to achieve this?

Thanks in advance.

Jon

aioobe
  • 413,195
  • 112
  • 811
  • 826
Jon
  • 2,703
  • 3
  • 18
  • 14
  • Similar http://stackoverflow.com/questions/6734115/how-do-i-format-a-string-with-properties-from-a-bean – Qwerky Aug 22 '11 at 09:51
  • I found [StringTemplate](http://www.antlr.org/wiki/display/ST4/Using+StringTemplate+with+Java#UsingStringTemplatewithJava-install) library. It is simpler than MVEL to use. – Jon Aug 25 '11 at 05:45

1 Answers1

4

Is there any way to achieve this?

I'm not 100% sure I understand what you're after, but here are some pointers...

Have a look at the MessageFormat class in the API. You may also be interested in the Formatter class and/or the String.format method.

If you have some Properties and you want to search and replace substrings of the shape #{ property.key } you could also just do like this:

import java.util.Properties;
import java.util.regex.*;

class Test {

    public static String process(String template, Properties props) {
        Matcher m = Pattern.compile("#\\{(.*?)\\}").matcher(template);

        StringBuffer sb = new StringBuffer();
        while (m.find())
            m.appendReplacement(sb, props.getProperty(m.group(1).trim()));
        m.appendTail(sb);

        return sb.toString();
    }


    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("user.name", "Jon");
        props.put("user.email", "jon.doe@example.com");

        String template = "Name: #{ user.name }, email: #{ user.email }";

        // Prints "Name: Jon, email: jon.doe@example.com"
        System.out.println(process(template, props));
    }
}

If you have actual POJOs and not a Properties object, you could go through reflection, like this:

import java.util.regex.*;

class User {
    String name;
    String email;
}


class Test {

    public static String process(String template, User user) throws Exception {
        Matcher m = Pattern.compile("#\\{(.*?)\\}").matcher(template);

        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String fieldId = m.group(1).trim();
            Object val = User.class.getDeclaredField(fieldId).get(user);
            m.appendReplacement(sb, String.valueOf(val));
        }
        m.appendTail(sb);
        return sb.toString();
    }


    public static void main(String[] args) throws Exception {
        User user = new User();
        user.name = "Jon";
        user.email = "jon.doe@example.com";
        String template = "Name: #{ name }, email: #{ email }";

        System.out.println(process(template, user));
    }
}

... but it's getting ugly, and I suggest you consider digging deeper into some of the 3rd party libraries for solving this.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • He seems to be looking to dereference variables just like in a lot of scripting languages, which is really difficult, if not impossible in java. This property based mechanism is a good java style alternative. – Anya Shenanigans Aug 22 '11 at 08:57
  • @Petesh, ahaa.. updated the answer with an ugly reflection hack. – aioobe Aug 22 '11 at 09:03
  • @Petesh, you are right. I wanted to do this scripting languages way. It is possible in ruby. – Jon Aug 22 '11 at 09:32
  • @aioobe, many thanks for your reply. MVEL is works exactly in your way. – Jon Aug 22 '11 at 09:35