velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Lloyd-Newberry <>
Subject Re: wrapping XML document as JavaBeans?
Date Wed, 03 Oct 2001 15:52:33 GMT
--- Mike Williams <> wrote:
> Say I have the following XML document:
>    <bean>
>      <name>UserInfo</name>
>      <field>
>        <name>id</name>
>        <type>int</type>
>      </field>
>      <field>
>        <name>name</name>
>        <type>java.lang.String</type>
>      </field>
>    </bean>     
> I want to make the data therein available to a Velocity template.  I
> could
> do what Anakia does, and simply place a JDOM representation in the
> Context.  However, then I'd end up with template expressions like
>   $field.getChild("name").getText()
> when I'd much rather write:
>   $
> I could create explicit model JavaBean objects, like Torque does, but
> I
> might want to deal with lots of different XML documents, and don't
> want to
> have to generate a set of JavaBeans for each DTD.
> Are there any other options?  Would it be feasible to create a
> generic
> wrapper-ojbect that allows <field><name>id</name></field> to
> accessed as
> "$" ?

   I have some code that I have promised to "donate" to the project as
soon as I get some time to work on cleaning and documenting it that
does something similar in a non-XML way. I would love to "integrate"
some kind of XML solution with it but havn't had time (the one thing we
are all lacking ;}).

   The basic premise is an object that wraps a hash table. It enforces
that keys can only be strings. So you can (in velocity) do something

    $bean = $UTILS.METAOBJ.newObject( "Widget" )
    $bean.set( 'id', $UTILS.NUMBER.asInteger(5) )
    $bean.set( 'name', 'blue widget' )

    $beancollection = $UTILS.METAOBJ.newCollection()
    $beancollection.add( $bean )

    $bean = $UTILS.METAOBJ.newObject( "Widget" )
    $bean.set( 'id', $UTILS.NUMBER.asInteger(10) )
    $bean.set( 'name', 'green widget' )

I can then do a:

    #foreach( $bean in $beancollection )
        Bean "$" has an id of $

   An XML version of this would certainly be possible. I might even
play with whipping up a version that would "compile" some XML schema
into the format seen above. The benefit to that would be that I could
use the schema validator to validate the XML data format in a much more
"strict" way than writing a bunch of velocity code.

   Some of you ( probably Jon ;P ) are probably wondering what I am
using this for. The beauty of this is that I can write up some
"velocity" data files that represent the data model that we will be
using for an application and provide a very simple servlet to load them
into the context to render against display templates. This allows
developers to be working on layout at the same time I am working on the
servlet <--> data interface with strong business rules. I also can
seperate "presentation" data for objects out into these seperate files
and merge it with data from the result set which has been piped into a
collection of "metaobjects". I have also used this toolkit to implement
a code generator for our business objects.

   In hindsight I think I may have done things a little differently but
as the only developer on the project I needed a way to "enable" others
without programming skills to help themselves easily.

   It looks like it will be pretty hectic here until the end of October
but I will try to squeeze in some cleanup and documentation before
then. We will see. Then I need to figure out how to "contribute" it
since it really is not a part of velocity but a tool to be used with
it. (If people even are interested).

   On a seperate note, I can't express how Awsome all of the "core"
developers on the Velocity project are. They have put together a tool
that has saved me hundereds of hours and will continue to do so for as
far as I can see down the road. I wish to say again, Thanks a million!


Brian S. Lloyd-Newberry
Vertical Learning Curve Solutions

Do You Yahoo!?
Listen to your Yahoo! Mail messages from any phone.

View raw message