[stringtemplate-interest] Re : Re : ST4: XMLModelAdaptor

Matthieu Wipliez mwipliez at yahoo.fr
Mon Jan 24 21:18:17 PST 2011


Ok now *that* was a crazy example :D
The thing is that as long as you can plug custom Java code in ST (be it with 
renderers or adaptors) you can do pretty much anything. Let's not forget that 
"with great 

power comes great responsibility" :-)

Matthieu

>
>De : Collin Fagan <collin.fagan at gmail.com>
>À : Matthieu Wipliez <mwipliez at yahoo.fr>
>Cc : stringtemplate-interest List <stringtemplate-interest at antlr.org>
>Envoyé le : Mar 25 janvier 2011, 13h 12min 21s
>Objet : Re: [stringtemplate-interest] Re : ST4: XMLModelAdaptor
>
>they do not modify the underlying XML model
>
>Yes and that is why I think xpath is much closer to a property  invocation chain 
>
>then a SQL query. In the end you can only do so much  with xpath. So what can 
>you do?
>
>    1. Walk the document 
>
>    2. Access elements and attributes
>    3. querying/filtering based on the content of elements and attributes
>
>    4. Simple math.The filtering and math, while admittedly the responsibility 
>of 
>
>the controller in an MVC architecture, is rather benign in my opinion. 
>
>
>Let's compare this to a something truly horrific. I guarantee the following 
>compiles and executes but I shall never release the code behind it for fear it 
>might spread like a contagion. 
>
>
>main(groovy) ::= <<
>
>A groovy string: <groovy.(" 'hi!' ")>
>
>Lets do Math! 5 + 7 = <groovy.(" 5 + 7 ")>
>
>Lists: <groovy.("[5, 6, 7, 8]"); separator=" and "> 
>
>Functions:
>    
>    4 factorial = <groovy.("
>        // factorial function
>        def fac(n) {
>            n == 0 ? 1 : n * fac(n - 1) 
>        }
>        
>        fac(4);
>    ")>
>
>
>>>
>
>produces the following output: 
>
>A groovy string: hi!
>
>Lets do Math! 5 + 7 = 12
>
>Lists: 5 and 6 and 7 and 8 
>
>Functions:
>
>    4 factorial = 24
>
>Oh yes, I did just build a GroovyModelAdapter and it embodies everything we all 

>despise about JSP and similar technologies. My point is that xpath, while 
>technically a grey area, is really only light grey compared to mixing in 
>something as dangerous as a full blown scripting language like Groovy. 
>
>
>But then again I wrote this so I might be bias. 
>
>Collin
>
>
>
>On Mon, Jan 24, 2011 at 9:46 PM, Matthieu Wipliez <mwipliez at yahoo.fr> wrote:
>
>Personally I love the idea of being able to produce textual code from XML with
>>StringTemplate, which is a lot easier to do than with, say, XSLT :-)
>>I think that the idea of using XPath as properties of the model is nice and
>>feels pretty natural.
>>
>>Also, this respects the push strategy IMO, since the XML model is computed
>>beforehand. This may even be safer than using POJO-based model, because with
>>your own model you are free to modify the model in methods, which is not the
>>case with XPath expressions (they do not modify the underlying XML model).
>>
>>
>>Matthieu
>>
>>
>>----- Message d'origine ----
>>> De : Terence Parr <parrt at cs.usfca.edu>
>>> À : stringtemplate-interest List <stringtemplate-interest at antlr.org>
>>> Envoyé le : Mar 25 janvier 2011, 9h 27min 53s
>>> Objet : Re: [stringtemplate-interest] ST4: XMLModelAdaptor
>>
>>>
>>> Ok, so this is Crazy cool:
>>>
>>> >> <root.("/bookstore/book"): { book  |
>>> >>     <book.title> (<book.title.lang>),  <book.author>, <book.year>
>>> >> }>
>>>
>>> The only  question is: are we somehow entangling the controller in the view?
>>>Think about  it, xpath is kind of like doing an SQL query, right? On the 
other
>>>hand, you  could argue that
>>>
>>>
>>> <employee.user.name.last>
>>>
>>> is also digging  pretty deeply into the model whereas normally we want the
>>>controller to pull the  data out of the model and push it into the template.
>>>
>>> I think that is the  conversation we need to have, though I like the idea of
>>>some XML integration for  marketing reasons. anybody have thoughts?
>>>
>>> Ter
>>>
>>> On Jan 22, 2011, at  1:21 PM, Collin Fagan wrote:
>>>
>>> > Absolutely! I use only the standard XML  stuff from the JDK so there are 
no
>>>extra dependencies. I'd be happy to do  whatever you need to make this
>>>"inclusion ready", unit tests, style standards,  whatever, just let me know.
>>>
>>> >
>>> > Collin
>>> >
>>> > On Sat,  Jan 22, 2011 at 2:52 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
>>> >  Wow.You are a twisted guy! I like it! :)  I wonder if some built-in XML
>>>adapters should be built-in. Should we discuss including in the standard
>>>distribution?
>>> > Ter
>>> > On Jan 22, 2011, at 12:01 PM, Collin Fagan  wrote:
>>> >
>>> >> Hi All,
>>> >>
>>> >> I've been playing  with the idea of a model adapter that understands 
XPath
>>>and xml documents.
>>>
>>> >> This is what I ended up with.
>>> >>
>>> >>  Example:
>>> >>
>>> >> Given an xml file with a structure like  this:
>>> >>
>>> >> <bookstore>
>>> >>      <book>
>>> >>         <title lang="eng">The  Adventures of Augie March</title>
>>> >>          <year>1953</year>
>>> >>          <author>Saul Bellow</author>
>>> >>      </book>
>>> >> ...
>>> >>
>>> >> You can write a template  like this:
>>> >>
>>> >> All Books: <root.("/bookstore/book"): {  book |
>>> >>     <book.title> (<book.title.lang>),  <book.author>, <book.year>
>>> >> }>
>>> >>
>>> >> As you can see the books are being selected with the  /bookstore/book 
>XPath
>>>expression. ST is able to iterate over everything that is  returned and we 
can
>>>access elements and attributes on each selected item via the  "." notation.
>>>
>>> >>
>>> >> Here is the full source code and example  for those intersted.
>>> >>
>>> >> http://www.box.net/shared/8tnrna4bi7
>>> >>
>>> >>  Enjoy,
>>> >>
>>> >> Collin
>>> >>
>>> >>  _______________________________________________
>>> >>  stringtemplate-interest mailing list
>>> >> stringtemplate-interest at antlr.org
>>> >> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>> >
>>> >
>>> > _______________________________________________
>>> >  stringtemplate-interest mailing list
>>> > stringtemplate-interest at antlr.org
>>> > http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>> >
>>> >
>>>
>>> _______________________________________________
>>> stringtemplate-interest  mailing list
>>> stringtemplate-interest at antlr.org
>>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>>
>>
>>
>>
>>_______________________________________________
>>stringtemplate-interest mailing list
>>stringtemplate-interest at antlr.org
>>http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>
>


      


More information about the stringtemplate-interest mailing list