[stringtemplate-interest] Tree of objects

Collin Fagan collin.fagan at gmail.com
Mon Jan 17 18:35:37 PST 2011


Ahh that's because trees are notorious for requiring recursive algorithms
for traversal.

Luckily StringTemplate supports recursion.

Here I have a simple template that takes a node object.

main(node) ::= <<

I am node: $node.index$
I have children:
    *$node.children:main()$*
>>

Here the template main actually contains a call to main. Crazy huh? So say I
have a node class that has a list of other node classes as children.

Node root = new Node();
root.getChildren().add(new Node());
root.getChildren().add(new Node());

Node child = new Node();
child.getChildren().add(new Node());
child.getChildren().add(new Node());

root.getChildren().add(child);

root.getChildren().add(new Node());
root.getChildren().add(new Node());

You can add as many nodes in whatever configurations you want (as long as
you don't make a loop) and the recursion should do all the magic.
Here it the output from the above template.

I am node: 0
I have children:
    I am node: 1
    I have children:
    I am node: 2
    I have children:
    I am node: 3
    I have children:
        I am node: 4
        I have children:
        I am node: 5
        I have children:

    I am node: 6
    I have children:
    I am node: 7
    I have children:


I hope this was enough information to help.

Collin


On Mon, Jan 17, 2011 at 9:16 AM, Peter Knego <peter at knego.net> wrote:

> Hi,
>
> I'm a newbie, so please bare with me. I read the docs and searched the
> mailinglist but could not find an appropriate answer.
>
> I have a tree of objects that represent a GUI hierarchy. Classes used
> are children of either ViewGroup or View. ViewGroup is a parent of
> View. ViewGroup is a container that holds a list other ViewGroups (and
> Views, since they are children of ViewGroup). Views are edge nodes
> that represent visual elements (buttons, etc..).
>
> This object tree is built dynamically at run-time. The depth of the
> tree can vary, usually it's 3-4 levels deep.
>
> I'd like to use ST to create HTML based on the object tree. What
> baffles me is how to support non-fixed depth of this tree, which
> happens because ViewGroup can contain other ViewGroups.
>
> Simplified example:
>
> class Div{
>    public List<Div> subDivs;
> }
>
> Please suggest how this can be solved? Is there any example you can
> point me to? I read that ST templates can be called recursively (for
> handling ViewGroups containg itself), but how do I pass the right
> object reference to it?
>
> Thanks for your help,
>
> Peter Knego
> _______________________________________________
> 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/20110117/b1901219/attachment.html 


More information about the stringtemplate-interest mailing list