[stringtemplate-interest] [Java] unrendering a template back to an object

Vivek Ramaswamy vramaswamy at gmail.com
Sun Feb 19 17:21:54 PST 2012


Hi Interest List,

Apologies if this is a newbie question but how do I take a string that
I know is derived from a particular template and render it back to an
object of the type that originally populated it?  If this solution (or
known availability of one) is posted somewhere , I'd greatly
appreciate an address.

Details below.

Kind regards,

VR

[1]  I have a class named Person:
public class Person {
 String firstName;
 String lastName;
 public void getFirstName(){
   return firstName;
 }
 public String getFirstName() {
   return firstName;
 }
}

[2]  I have a template person.stg:
html(p):==<<
<html>
  <body>
     <dl>
            <dt>first name <dd>^p.firstName$</dd></dt>
            <dt>last name <dd>^p.lastName$</dd></dt>
     </dl>
  </body>
</html>
>>

[3]  I then render the template:
		Person person = new Person();
		person.setFirstName("Vivek");
		person.setLastName("Ramaswamy");
		STGroup g = new STGroupFile("templates/person/person.stg","^","$");
		ST st = g.getInstanceOf("html");
                st.add("p",person);
                String renderedTemplate = st.render();

[4]  My question, does StringTemplate give me the ability to take the
String output, 'renderedTemplate', and unrender it back into a Person
object?  In effect unmarshall it.  It seems like this might be
possible if I provide the original template and a class definition.
I'm guessing this is _not_ possible but I thought I'd check before
building something that does this for my specific situation.


More information about the stringtemplate-interest mailing list