velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Geir Magnusson Jr. <ge...@adeptra.com>
Subject Re: Running velocity in command line
Date Tue, 01 Apr 2003 19:12:20 GMT

On Monday, March 31, 2003, at 03:03 AM, Anthony Kong wrote:

> Hi, all,
>
> I would like to run velocity in a unix command line style.
>
>     e.g. java -cp $CLASSPATH org.apache.velocity.Whatever <parameters>
>
> instead of using ant. I think the easiest way to achieve this goal is 
> to
> modify the AnaikaTask to be a command line application. However it is 
> not
> strange-forward because there is dependency on other ant objects.
>
> I would like to know if someone has done something similar before, or 
> I just
> did not study the documentation well enough that I do not know how to 
> invoke
> velocity as such.

It really depends what you want Whatever to do.  I use velocity in all 
sorts of ways.  For example the following little program lets you 
specify a properties file and a template, and the output is the 
template rendered with the values from the properties file.

The only vaguely interesting thing about it is the custom 
VelocityContext, the class MyVC - I just did that to print out any 
references that didn't get matched.  You can use the default 
VelocityContext if you wish and just scan the velocity.log :)

But if you want to process XML, you need something like Anakia, or DVSL 
(which has a command-line mode) or....

Hope this helps.

geir


import java.io.FileInputStream;
import java.io.FileReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;

public class Foo
{
     public PayloadRender()
     {
     }

     public void render(Properties data, Reader template)
         throws Exception
         {
         /*
          *  create a new velocity engine and a VC
          */

          VelocityEngine ve = new VelocityEngine();

          ve.init();

          MyVC vc = new MyVC(data);

          StringWriter writer = new StringWriter();

          ve.evaluate(vc, writer, "", template);

          System.out.println(writer.toString());
         }

         /**
          *  Class to allow us to trap unresolved parameters in the 
input template
          */
         class MyVC implements Context
         {
             final Properties vcd;

             public MyVC(Properties vcData)
             {
                 vcd = vcData;
             }

                 public Object remove(Object o)
                 {
                     return null;
                 }

         public boolean containsKey(Object arg0)
         {
             String s = vcd.getProperty((String) arg0);

                 return s != null;
         }

         public Object get(String arg0)
         {
             String s = vcd.getProperty((String) arg0);

            if (s == null)
                 System.err.println("Render error = : no value for key " 
+ arg0);

                 return s;
         }

         public Object[] getKeys()
         {
             return null;
         }

         public Object put(String arg0, Object arg1)
         {
             return null;
         }

         }

     public static void main(String[] args)
         throws Exception
     {

         if (args.length < 2)
         {
             System.err.println("Error : requires 2 arguments ex : 
PayloadRender props.props input.xml > output.xml");
             return;
         }
                /*
                  *  now read in the properties
                  */

                 Properties p = new Properties();

                 FileInputStream fis = new FileInputStream(args[0]);

                 p.load(fis);

                 FileReader fr = new FileReader(args[1]);

                 PayloadRender pr = new PayloadRender();

                 pr.render(p, fr);
     }
}

-- 
Geir Magnusson Jr                                   203-956-2604(w)
Adeptra, Inc.                                       203-434-2093(m)
geirm@adeptra.com                                   203-247-1713(m)


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message