Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STGroupString does not honor delimeter stanza in a string definition #46

Closed
jarrodhroberson opened this issue Feb 8, 2013 · 10 comments
Closed
Assignees
Milestone

Comments

@jarrodhroberson
Copy link

When the following text is loaded into a String and used to create a STGroupString, the "delimiters" line is not honored and you get the default "<", ">" delimiters instead.

I am loading my templates from the Google App Engine Blob store and the easiest way to get them into ST is with STGroupString.

delimiters "$", "$"

li(it) ::= "<li>$it.fname$ $it.lname$</li>"
ul(ulid, list) ::= "<ul$if(ulid)$ id=\"$ulid$\"$endif$>$list:li()$</ul>"
div(divid, ulid, content) ::= "<div$if(divid)$ id=\"$divid$\"$endif$>$ul(list=content, ulid=ulid)$</div>"
@sharwell
Copy link
Member

sharwell commented Feb 8, 2013

I am not able to reproduce this problem. I used the following unit test which passed:

@Test public void testDelimitersClauseInGroupString() throws Exception {
    String templates =
            "delimiters \"$\", \"$\""+newline+
            "method(name) ::= <<"+newline+
            "$stat(name)$" +newline+
            ">>"+newline+
            "stat(name,value=\"99\") ::= \"x=$value$; // $name$\""+newline
            ;
    STGroup group = new STGroupString(templates);
    ST b = group.getInstanceOf("method");
    b.add("name", "foo");
    String expecting = "x=99; // foo";
    String result = b.render();
    assertEquals(expecting, result);
}

@ghost ghost assigned sharwell Feb 8, 2013
sharwell added a commit to sharwell/stringtemplate4 that referenced this issue Feb 8, 2013
@jarrodhroberson
Copy link
Author

You aren't doing what I am doing, I am reading the String from a "File". It doesn't work when you read the String from some resource. I can't use a regular file because Google App Engine doesn't support real "File" objects. So I have to read from a Blob into a String and then try and create the STGroupString, if fails miserably when doing it this way.

delimiters "$", "$"title(title) ::= "<title>$title$</title>"html() ::= <<<!-- $template_name$ --><html>    <head>        $title(title=head_title)$    </head><body>    <div$if(body_div_id)$ id="$body_div_id$"$endif$>        <ul$if(body_div_ul_id)$ id="$body_div_ul_id$"$endif$>            $body_div_daysofweek:{dow | <li>$dow$</li>}$        </ul>    </div></body></html>>>

test-html.stg 1:0: garbled template definition starting at 'delimiters'
test-html.stg 1:370: invalid character '>'
st.toString() = html()
Exception in thread "main" java.lang.IllegalArgumentException: no such attribute: template_name
at org.stringtemplate.v4.ST.add(ST.java:187)
at Main.materialize(Main.java:83)
at Main.main(Main.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1

Screenshot_2_8_13_5_03_PM

@sharwell
Copy link
Member

sharwell commented Feb 8, 2013

You misspelled delimiters.

@sharwell sharwell closed this as completed Feb 8, 2013
@jarrodhroberson
Copy link
Author

It doesn't work with delimiters spelled correctly, t

@sharwell sharwell reopened this Feb 8, 2013
@sharwell
Copy link
Member

sharwell commented Feb 8, 2013

What is the error in that case?

@jarrodhroberson
Copy link
Author

same error, I attached a screen shot of what my source template looks like in SubEthaEdit

Screenshot_2_8_13_5_03_PM

delimiters "$", "$"title(title) ::= "<title>$title$</title>"html() ::= <<<!-- $template_name$ --><html>    <head>        $title(title=head_title)$    </head><body>    <div$if(body_div_id)$ id="$body_div_id$"$endif$>        <ul$if(body_div_ul_id)$ id="$body_div_ul_id$"$endif$>            $body_div_daysofweek:{dow | <li>$dow$</li>}$        </ul>    </div></body></html>>>
test-html.stg 1:0: garbled template definition starting at 'delimiters'
test-html.stg 1:370: invalid character '>'
Exception in thread "main" java.lang.IllegalArgumentException: no such attribute: template_name
at org.stringtemplate.v4.ST.add(ST.java:187)
at Main.materialize(Main.java:83)
at Main.main(Main.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Here is the code I am using to read in the file to a String:

private static STGroupString templateFromFile(final File f)
{
    try
    {
        final BufferedReader br = new BufferedReader(new FileReader(f));
        final StringBuilder sb = new StringBuilder((int) f.length());
        while (br.ready())
        {
            sb.append(br.readLine()).append("\n");
        }
        if (VERBOSE) { System.out.println(sb.toString()); }
        return new STGroupString(f.getName(), sb.toString());
    }
    catch (final FileNotFoundException e)
    {
        throw new RuntimeException(e);
    }
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }
}

@sharwell
Copy link
Member

sharwell commented Feb 8, 2013

Are you sure you're using a version of ST4 that supports the delimiters clause?

You can try running my unit test code in templateFromFile, and let me know if that works.

@jarrodhroberson
Copy link
Author

I am using 4.0.2, which version supports delimiters?

    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>stringtemplate</artifactId>
        <version>4.0.2</version>
    </dependency>

@jarrodhroberson
Copy link
Author

Upgrading my pom.xml to

    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>ST4</artifactId>
        <version>4.0.7</version>
    </dependency>

fixed the delimiters issue, but I am still getting the following error, when the very first thing in my template is $template_name$

delimiters "$", "$"

title(title) ::= "<title>$title$</title>"

html() ::= <<
$template_name$
<html>
    <head>
        $title(title=head_title)$
    </head>
<body>
    <div$if(body_div_id)$ id="$body_div_id$"$endif$>
        <ul$if(body_div_ul_id)$ id="$body_div_ul_id$"$endif$>
            $body_div_daysofweek:{dow | <li>$dow$</li>}$
        </ul>
    </div>
</body>
</html>
>>

Exception in thread "main" java.lang.IllegalArgumentException: no such attribute: template_name
at org.stringtemplate.v4.ST.add(ST.java:223)
at Main.materialize(Main.java:83)
at Main.main(Main.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

@sharwell
Copy link
Member

sharwell commented Feb 8, 2013

Your html() template is declared to have no formal arguments. At first glance the template appears to require the following.

html(template_name, head_title, body_div_id, body_div_ul_id, body_div_daysofweek)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants