Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringTemplate invalid character '<' when reading XML Template

I am trying to create a simple XML-Template which so far only consists of:

<?xml version="1.0"?>

I read the file like this:

    STGroup group = new STGroupDir("templates");
    ST st = group.getInstanceOf("report");
    st.add("analysis", ana);
    String result = st.render();
    System.out.println(result);

And the result is several error messages:

report.st 1:1: invalid character '<'
report.st 1:1: invalid character '?'
report.st 1:19: invalid character '?'
report.st 1:20: invalid character '>'
report.st 1:2: no viable alternative at input 'xml'

I have seen other people reading HTML tempaltes that also use tags. So what am I doing wrong?

like image 896
er4z0r Avatar asked May 02 '12 13:05

er4z0r


3 Answers

Okay it seems I overlooked that you need to specify templates in a different snytax. Allthough this was not obvious from the examples I used:

My working template looks different now:

report (analysis) ::= <<
<?xml version="1.0"?>
>>

In addition I also changed the delimeters:

STGroup group = new STGroupDir("templates",'$','$');
like image 53
er4z0r Avatar answered Oct 30 '22 19:10

er4z0r


I've found that you can escape the angle bracket as well:

report(analysis) ::= <<
\<?xml version="1.0"?>
>>

Note the \ right before the <?xml -- additionally it's smart enough not to require another escape at the ?> closing bracket.

And I believe what Terrence Parr is suggesting about model/view separation is that the view never really has an opportunity to manipulate the underlying data structure (or model) passed to the template. This is accomplished by "applying" a template to the data or collection, rather than looping in the template over the data. Perhaps that is subtle, and perhaps practically (in the case of designers) a bit too pure.

like image 30
lucidquiet Avatar answered Oct 30 '22 19:10

lucidquiet


Alternatively, you can use STRawGroupDir if you don't want to use group file syntax. This class is similar to STGroupDir, but specifically for loading files like XML and HTML.

http://www.stringtemplate.org/api/org/stringtemplate/v4/STRawGroupDir.html

like image 43
metatheorem Avatar answered Oct 30 '22 18:10

metatheorem