geirm       02/02/05 20:47:12

  Modified:    src/java/org/apache/tools/dvsl DVSL.java DVSLTask.java
  Log:
  Added support for configuration of the VelocityEngine through a client-supplied
  Map that holds Velocity config keys/values.
  
  Thanks to Peter Sojan <[EMAIL PROTECTED]> for the suggestion and testing. :)
  
  Revision  Changes    Path
  1.5       +99 -1     jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSL.java
  
  Index: DVSL.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSL.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DVSL.java 27 Jan 2002 22:20:53 -0000      1.4
  +++ DVSL.java 6 Feb 2002 04:47:12 -0000       1.5
  @@ -104,6 +104,8 @@
       private Transformer transformer;
       private boolean ready = false;
   
  +    private Map velConfig = null;
  +
       private TemplateHandler templateHandler = new TemplateHandler();
   
       public DVSL()
  @@ -112,6 +114,18 @@
   
       /**
        *  <p>
  +     *  lets the user pass a java.util.Properties containing
  +     *  properties for the configuration of the VelocityEngine
  +     *  used by DVSL
  +     *  </p>
  +     */
  +    public void setVelocityConfig( Map map )
  +    {
  +        velConfig = map;
  +    }
  +
  +    /**
  +     *  <p>
        *  Sets the user context.  The user context is
        *  a Velocity Context containing user-supplied
        *  objects and data that are to be made available
  @@ -263,7 +277,16 @@
            */
   
           ve = new VelocityEngine();
  -      
  +
  +        /*
  +         * if there are user properties, set those first - carefully
  +         */
  +
  +        if ( velConfig != null )
  +        {
  +            configureVelocityEngine( ve, velConfig );
  +        }
  +
           /*
            *  register our template() directive
            */
  @@ -311,6 +334,81 @@
            */
   
           transformer = new Transformer( ve, templateHandler,  baseContext );
  +    }
  +
  +    /**
  +     *  <p>
  +     *   Adds the allowed properties from the Properties to the
  +     *   velocity engine
  +     *  </p>
  +     *  <p>
  +     *  So far, we support, in RuntimeConstant parlance :
  +     *  </p>
  +     *  <ul>
  +     *  <li>VM_LIBRARY</li>
  +     *  <li>FILE_RESOURCE_LOADER_PATH</li>
  +     *  <li>RUNTIME_LOG</li>
  +     *  <li>RUNTIME_LOG_LOGSYSTEM</li>
  +     *  <li>RUNTIME_LOG_LOGSYSTEM_CLASS</li>
  +     *  </ul>
  +     *
  +     *  <p>
  +     *  If you are going to use this, ensure you do it *before* setting
  +     *  the stylesheet, as that creates the VelocityEngine
  +     *  </p>
  +     */
  +    private void configureVelocityEngine( VelocityEngine ve, Map map )
  +    {
  +        if ( ve == null || map == null)
  +        {
  +            return;
  +        }
  +
  +        /*
  +         * for now,  keep it simple
  +         */
  +
  +        Object val = map.get(VelocityEngine.VM_LIBRARY );
  +
  +        if (val instanceof String)
  +        {
  +            ve.setProperty( VelocityEngine.VM_LIBRARY, (String) val);
  +        }
  +
  +        /*
  +         *  assumes that for now, we are using the file loader
  +         */
  +        val = map.get(VelocityEngine.FILE_RESOURCE_LOADER_PATH );
  +
  +        if ( val instanceof String)
  +        {
  +            ve.setProperty( VelocityEngine.FILE_RESOURCE_LOADER_PATH, (String) val);
  +        }
  +
  +        /*
  +         *  No real assumptions - pass what you want
  +         */
  +        val = map.get(VelocityEngine.RUNTIME_LOG);
  +
  +        if ( val instanceof String)
  +        {
  +            ve.setProperty( VelocityEngine.RUNTIME_LOG,  val);
  +        }
  +
  +        val = map.get(VelocityEngine.RUNTIME_LOG_LOGSYSTEM);
  +
  +        if ( val != null )
  +        {
  +            ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM,  val);
  +        }
  +
  +        val = map.get(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS);
  +
  +        if ( val instanceof String)
  +        {
  +            ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,  val);
  +        }
  +
       }
   
       /**
  
  
  
  1.2       +42 -1     
jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSLTask.java
  
  Index: DVSLTask.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSLTask.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DVSLTask.java     18 Dec 2001 21:23:15 -0000      1.1
  +++ DVSLTask.java     6 Feb 2002 04:47:12 -0000       1.2
  @@ -54,6 +54,7 @@
   package org.apache.tools.dvsl;
   
   import java.util.Properties;
  +import java.util.Map;
   
   import java.io.Writer;
   import java.io.FileWriter;
  @@ -86,6 +87,7 @@
       protected String stylesheet;
       protected String toolboxProps;
       protected String extension = ".html";
  +    protected String velPropClass = null;
   
       /*
        * I wish they called this sourcedir in Style.  grrr...
  @@ -115,6 +117,18 @@
           toolboxProps = toolbox;
       }
   
  +    /**
  +     *  <p>
  +     *  Allows the user to specify a class that implements
  +     *  java.util.Properties that will have user properties
  +     *  to be used when setting up DVSL
  +     *  </p>
  +     */
  +    public void setVelocityConfigClass( String classname )
  +    {
  +        velPropClass = classname;
  +    }
  +
       public void execute () throws BuildException
       {
           /*
  @@ -157,11 +171,38 @@
           throws BuildException
       {
           /*
  -         *  make a DVSL and set the stylesheet
  +         *  make a DVSL
            */
        
           DVSL dvsl = new DVSL();
   
  +        /*
  +         *  now, if the user gave us a velPropClass, we create an instance
  +         *  of that to use to config the VelocityEngine inside te DVSL
  +         */
  +
  +        if( velPropClass != null )
  +        {
  +            Object ob = null;
  +
  +            try
  +            {
  +                 ob = Class.forName( velPropClass).newInstance();
  +            }
  +            catch( Exception ex )
  +            {
  +                throw new BuildException("Error instantiating VelocityPropClass : " 
+ ex );
  +            }
  +
  +            if ( ob instanceof Map )
  +            {
  +                dvsl.setVelocityConfig( (Map) ob );
  +            }
  +        }
  +
  +        /*
  +         * now the stylesheet
  +         */
           try
           {
               dvsl.setStylesheet( stylesheet );
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to