Carsten Rhod Gregersen wrote:
> 
> Hi,
> 
> First, thank you for a really nice tool.
> 
> I'm using velocity and anakia to automatically generate
> some files based on a xml description.
> 
> I've run into some different problems.
> 
> I'm using the programs in a little different way than the
> examples, and that might be the source to all my problems.

Nah.  Different is good.  

> I'm using a single xml file in combination with several
> stylesheets (velocity templates) to generate about 6-8
> different "final" files (e.g. 6-8 autogenerated files pr. xml file)
> that all are different but based on the same data.
> 
> This can today only be acomplished by creating 6-8 links
> and using the extension to decide which style (template) to
> use. Has anyone done something like this before, and how
> did they do it (e.g. is there a smarter way) ??
> 
> I've read the source code of the anakia task, and it would seem
> quite easy to code something for my specific purpose, but
> if someone has done it before and submitted it I would rather
> work on that.

I've done exactly that when my needs were different.  The basic model
that Anakia uses is pretty straightforward : 

1) parse your XML into a JDOM Document

        SAXBuilder builder;
        Document root = null;

        try 
        {
            builder = new SAXBuilder( 
"org.apache.xerces.parsers.SAXParser" );
            root = builder.build( file );
        }
        catch( Exception )
        {
            System.out.println( ...  );
        }

2) Stuff the Document (or root Element) into the context

        context.put("root", root );

3) Render a template.

Anakia is a nice tool, but it is tailored for a specific purpose.  You
can extend it to make it do more (and contribute it back here if its
general and you like it...) or make a new tool, drawing on Anakia as an
example, to solve whatever problem you have.

> 
> Also I've found out that it seem that the macro's spans over multiple
> stylesheets (velocity templates). So if I have a macro in one
> template the same name can not be used in another. I'm not
> saying this is an error, but quite some time was invested before
> i found out that the macro's seemed to have a global definition.

Yes, that's the idea - that you can create a library of VMs
(Velocimacros) that anyone can share.  Of course, there are issues with
that, like when you *don't* want that to be the case.  If so, you can
turn on what is called 'local scope' :

velocimacro.permissions.allow.inline.local.scope = true

and that should keep the macro definition local to the template that
declared it.  Anakia can take a properties file, so you can put it
there, or in your own code either read a properties file, or do
something like

Velocity.setProperty(
"velocimacro.permissions.allow.inline.local.scope", Boolean.TRUE );

or if you don't like the string :

Velocity.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE );

geir

-- 
Geir Magnusson Jr.                           [EMAIL PROTECTED]
System and Software Consulting

Developing for the web?  See http://jakarta.apache.org/velocity/

Reply via email to