velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Revusky <>
Subject Re: my tree
Date Tue, 17 Jul 2001 15:25:29 GMT
Simon Christian wrote:
> Uday Kumar wrote:
> >
> > hi Tim
> >
> > i did write a code to print out a tree, but i am sure no one wud want to see
> > that, as it is such a lousy code. wrote it just to serve the purpose,
> > debugging it wud be a pain for new-comers. my problem is... i have a
> > vectorof objects and they are arranged in order. i have to print them out
> > one by one, but with the only condition that some of them might have
> > subcategories, i can call some methods on it(ex. hasSubCategories( ) ) and
> > then according to the result, i print out with indentations. as i cud not do
> > the indentations, i made tables and each time i changed the width and
> > printed it out...
> >
> > lemme give an example. actually, the categories would be like---
> >
> > main1
> >    test1
> >    test2
> >       test22
> >       test23
> >   test3
> > main2
> >
> > if this was my tree, the vector i have to recurse thru would be
> > [main1,test1,test2,test22,test23,test3,main2]. all the things are in order,
> > ready to be printed straightaway, but i have to identify whether each of
> > them has subcategories and then print it as above.
> >
> > now, i made u my problem more clear, tell me if this works in the SAME WAY u
> > did? if that is the case, i wud replace my code with yours. please let me
> > know
> Is it possible for the objects in your vector to report how many nodes
> there are in the tree between themselves and the 'root'? If so this is
> the easy way to tackle your problem. e.g. from your example, test22
> would have 2 nodes above it, hence indent it with two units. The
> simplest unit to use might be a non-breaking-space. Example vm code:
> #foreach( $node in $myVector )
> #foreach( $i in $node.countParentNodes() )&nbsp;&nbsp;&nbsp;#end
> $node.Name
> #end
> Given it's a brute force approach, but it would work ok (or am i missing
> something?)

Here is a snippet of template code that I wrote for a (free) forum app
that is downloadable from my site. The template syntax is freemarker but
it would surprise me if the basic approach doesn't work in velocity. The
<function> would correspond to a "velocimacro" or whatever they call it
and the <call ..> is the invocation syntax. The <list> directive and
<if> are #foreach and #if in Velocity. And of course, the article and
item objects are things exposed on the tree.

<function showSummary(item)>
    <if item.unique_id == article.unique_id>
    <B><FONT COLOR="red">
    <A HREF="${item.URL}">
        &quot;${item.title}&quot; by ${item.author_id}
    <if item.unique_id == article.unique_id>

<function showChildren(item)>
<list item.children as child>
    <call showSummary(child)>
    <call showChildren(child)>

It would be sort of interesting to see how hard it is to translate from
one syntax to the other. (I suspect not very.) 

Hope that helps.

Jonathan Revusky
available for Java/Delphi/Internet consulting
If you want to...
- make your .class files double-clickable with SmartJ
- do Delphi/Java mixed programming with easy-to-use JNI wrapper classes
- build robust web applications with the Niggle Application Framework
check out the Revusky Hacks Page:

View raw message