[stringtemplate-interest] Fun with ST4

Collin Fagan collin.fagan at gmail.com
Sat Jan 22 11:37:23 PST 2011


*Can the new model adapter thing work for you?*

Yeah that's my point. I know you've kept calling arbitrary methods out of ST
.. but here I go an have to add them because I have no choice.

public class DocletStyleAdapter implements ModelAdaptor{

        @Override
        public Object getProperty(ST st, Object target, Object paramObject,
String paramString)
                throws STNoSuchPropertyException {
            try {
                return
target.getClass().getMethod(paramString).invoke(target);
            } catch (Exception e) {
                throw new STNoSuchPropertyException(e, paramString);
            }
        }
    }


On Sat, Jan 22, 2011 at 1:29 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

>
> On Jan 16, 2011, at 12:54 PM, Collin Fagan wrote:
>
> ST4 has been working well for me. My most complicated template files worked
> without a hitch,
>
>
> hooray!
>
> I guess I never used any of the shortcuts that got axed. I've been doing
> some experimenting with the model adapters and here is my feedback.
>
> *Task 1: ST + Doclet API*
> Status: Success, but ...
>
> The 'but' involves the fact that the names of the the methods in the Doclet
> API are unadorned like 'classes()' and 'methods()' etc. That left me one
> choice, and I know you aren't going to like it. I had to just lookup the
> method reflectively. While this makes working with the API really easy... it
> could also be used to invoke any method which is bad. :(
>
>
> yep. Can the new model adapter thing work for you?
>
> *Task 2: ST + XML/XPath*
> Status: Almost a Success
>
> So I thought it would be interesting to try to build a model adapter that
> takes an XPath expression as a "property". The expression is then used to
> get the right data our of the XML document.
>
> main(doc) ::= <<
>      I found the books: <doc./bookstore/book/title; separator=",">
> >>
>
> I get this output:
>
> t.stg 2:40: invalid character '/'
> I found the books: Harry Potter,Learning XML
>
>
> interesting. yeah, technically it shouldn't allow subdirectory names but I
> think I was allowing you to look up templates as keys of maps or something.
>
>
> I'm guessing that you never anticipated a property 'name' containing
> slashes. The funny thing is it only complains if the property starts with a
> slash.
>
>
> ha!
>
> The template: <doc.bookstore/book/title; separator=","> works with no
> invalid character output.
>
>
> hmm... should we make this illegal war added to the documentation is a
> feature? ;)
>
> Other XPath syntax that contains @ or [] breaks ST which is unsurprising
> since none of those are valid charactes for a Java identifier.
>
> In renderers one can pass whatever symbols they like to the format
> argument. I'm guessing this is because they are surrounded by quotes. Would
> it be reasonabale to ask for some optional delimiter on property names?
>
> Something like: <doc."//bookstore/book/title"; separator=",">
>
> or: <doc.{//bookstore/book/title}; separator=","> or something like that.
>
>
> <doc.(key)> uses key as a variable that holds the name of the field.  would
> that work?
>
> *Task 3: *JSR 223 Bindings Proof Of Concept.
> Status: Success
>
> So JSR 223 is the scripting API for Java. This is a common framework that
> allows Java objects to be exposed for consumption by JVM scripting
> languages. JRuby, Groovy, JPython all have compatible bindings implemented.
> Some other template engines (boooo, hisss) also provide bindings for JSR
> 223. Since it's so close to the ST API I thought I see what could be done.
>
> While I don't have the Factory lookup stuff all in place the implementation
> of the main interface "ScriptEngine" is pretty straight forward.
>
> This allows me to execute the following code:
>
> STScriptEngine engine = new STScriptEngine();
> engine.put("attributeName", "world");
> System.out.println(engine.eval("Hello <attributeName>"));
>
> Mine is just a wrapper class that just delegates to ST. I'm sure a more
> serious effort would result in code that pokes at the insides of ST.
>
>
> Check out the new feature, format, that I added; top of this page
>
> http://www.antlr.org/wiki/display/ST/Differences+between+v3+and+v4
>
> Anyway ST4 has been great, really great.
>
>
>
> hooray!
>
> Ter
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110122/4b0adece/attachment-0001.html 


More information about the stringtemplate-interest mailing list