[stringtemplate-interest] What compiles to : Bytecode.INSTR_LOAD_ATTR ?

Collin Fagan collin.fagan at gmail.com
Sun Sep 4 10:09:29 PDT 2011


I have built some experimental code for setting what I call an
"AttributeProvider" on an STGroup.

ExtendedGroupFile group = new
ExtendedGroupFile("templates/SpringContextAttributeProviderTest.stg");
group.setAttributeProvider(new SpringContextAttributeProvider(context));
ST template = group.getInstanceOf("main");
System.out.println(template.render());

This code when combined with the following template.

main() ::= <<

First Name: <dataBean.firstName>
Last Name: <dataBean.lastName>

System Properties:
 java.runtime.name = <systemProperties.("java.runtime.name")>
 java.version = <systemProperties.("java.version")>

>>

Produces this output:

First Name: Peter
Last Name: Parker

System Properties:
 java.runtime.name = Java(TM) SE Runtime Environment
 java.version = 1.6.0_23

As you can see none of those attributes were declared in the template.
Everything used exists in the spring application context.

SpringContextAttributeProvider implements AttributeProvider

public interface AttributeProvider {

    public Object getAttribute(ST sourceTemplate, String attributeName);

}

This lets you expose objects from any source to a template. One might use
this as a way to build jsr223
(http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
) bindings for ST. I've even been considering building one that understands
Apache Wicket models so I can build 1 model that binds to components or
generates text via ST.

Is there any chance something like this could be implemented directly into
ST? I think it's the next step beyond model adapters.

Collin

On Sun, Sep 4, 2011 at 7:44 AM, Collin Fagan <collin.fagan at gmail.com> wrote:

> It got me on the right track. I now have a template that will get all it's
> attributes from a Spring ApplicationContext.
>
> So now I can do :
>
> main() ::= <<
>
> <springBean.value>
>
> >>
>
> Where springBean is the name of the bean in the ApplicationContext. So now
> I can dependency inject a template attribute.
>
> Next is jsr223 (scripting) support I think.
>
> Collin
>
>
> On Sun, Sep 4, 2011 at 5:05 AM, Udo Borkowski <Udo.Borkowski at gmx.de>wrote:
>
>> Hi Colin,
>> little typo:
>>
>>
>>
>> instead of
>>
>>         group.defineTemplate("main", "string", "{<string.q>}");
>>
>>
>> just write
>>
>>         group.defineTemplate("main", "{<string.q>}");
>>
>>
>>
>> and it will generate an INSTR_LOAD_ATTR
>>
>> Udo
>>
>>
>> P.S. The relevant ST4 code is located in
>> public void refAttr(Token templateToken, CommonTree id)
>>
>>
>>
>> On 04.09.2011, at 11:53, Udo Borkowski wrote:
>>
>> Hi Colin,
>>
>> instead of
>>
>>         group.defineTemplate("main", "string", "{<string.q>}");
>>
>>
>> just write
>>
>>         group.defineTemplate("main", "{<string.q>}");
>>
>>
>>
>> and it will generate an INSTR_LOAD_LOCAL
>>
>> Udo
>>
>>
>> On 03.09.2011, at 14:30, Collin Fagan wrote:
>>
>> Hi Everyone,
>>
>> In my (probably misguided) quest to inject attributes into the scope of a
>> template I've overridden the getAttribute method on Interpreter. This method
>> call gets invoked when the Interpreter runs into Bytecode.INSTR_LOAD_ATTR.
>> My problem is that my simple test never generates this bytecode. It only
>> generates Bytecode.INSTR_LOAD_LOCAL.
>>
>> Does anyone know what I have to do to generate Bytecode.INSTR_LOAD_ATTR?
>>
>>
>> Thanks,
>>
>> Collin
>>
>>
>> Here is my experimental code:
>>
>> /**
>>  *
>>  * @author collin
>>  */
>> public class Exp {
>>
>>     private class ExtInterpreter extends Interpreter {
>>
>>         public ExtInterpreter(STGroup group, Locale locale,
>>                 ErrorManager errMgr, boolean debug) {
>>             super(group, locale, errMgr, debug);
>>             // TODO Auto-generated constructor stub
>>         }
>>
>>         @Override
>>         public Object getAttribute(ST self, String name) {
>>             System.out.println(name);
>>             return super.getAttribute(self, name);
>>         }
>>     }
>>
>>     @Test
>>     public void testStaticMethodAddForString() {
>>         STGroup group = new STGroup();
>>         group.defineTemplate("main", "string", "{<string.q>}");
>>         ST mainTemplate = group.getInstanceOf("main");
>> //        mainTemplate.add("string", "this is my test string");
>>
>>         Interpreter interp = new
>> ExtInterpreter(group,Locale.getDefault(),group.errMgr,false);
>>         StringWriter stringWriter = new StringWriter();
>>         STWriter wr = new AutoIndentWriter(stringWriter);
>>         wr.setLineWidth(STWriter.NO_WRAP);
>>         interp.exec(wr, mainTemplate);
>>         System.out.print(stringWriter.toString());
>>     }
>> }
>>
>>
>>
>> _______________________________________________
>> stringtemplate-interest mailing list
>> stringtemplate-interest at antlr.org
>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110904/ee2368fe/attachment.html 


More information about the stringtemplate-interest mailing list