jvanzyl     00/11/16 15:06:41

  Modified:    src/java/org/apache/velocity/runtime Runtime.java
                        RuntimeConstants.java
               src/java/org/apache/velocity/runtime/defaults
                        velocity.properties
  Log:
  - updated the runtime to allow the setting of template stream source
    properties via a common name mechanism.
  
  Revision  Changes    Path
  1.46      +66 -12    
jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java
  
  Index: Runtime.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- Runtime.java      2000/11/16 20:42:25     1.45
  +++ Runtime.java      2000/11/16 23:06:40     1.46
  @@ -153,7 +153,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Bowden</a>
  - * @version $Id: Runtime.java,v 1.45 2000/11/16 20:42:25 geirm Exp $
  + * @version $Id: Runtime.java,v 1.46 2000/11/16 23:06:40 jvanzyl Exp $
    */
   public class Runtime implements RuntimeConstants
   {
  @@ -222,6 +222,30 @@
       private static StringBuffer pendingMessages = new StringBuffer();
   
       /**
  +     * This is a list of the template stream source
  +     * initializers, basically properties for a particular
  +     * template stream source. The order in this list
  +     * reflects numbering of the properties i.e.
  +     * template.loader.1.<property> = <value>
  +     * template.loader.2.<property> = <value>
  +     */
  +    private static List sourceInitializerList;
  +    
  +    /**
  +     * This is a map of public name of the template
  +     * stream source to it's initializer. This is so
  +     * that clients of velocity can set properties of
  +     * a template source stream with its public name.
  +     * So for example, a client could set the 
  +     * File.template.path property and this would
  +     * change the template.path property for the
  +     * file template stream source.
  +     */
  +    private static Map sourceInitializerMap;
  +
  +    private static boolean sourceInitializersAssembled = false;
  +
  +    /**
        * Get the default properties for the Velocity Runtime.
        * This would allow the retrieval and modification of
        * the base properties before initializing the Velocity
  @@ -235,6 +259,7 @@
               InputStream inputStream = classLoader.getResourceAsStream(
                   DEFAULT_RUNTIME_PROPERTIES);
               VelocityResources.setPropertiesInputStream( inputStream );
  +            assembleSourceInitializers();
               info ("Default Properties File: " + new 
File(DEFAULT_RUNTIME_PROPERTIES).getAbsolutePath());
           }
           catch (IOException ioe)
  @@ -390,12 +415,14 @@
       {
           if (!getString(EXTERNAL_INIT, "false" ).equalsIgnoreCase("true"))
           {
  -            List initializers = getTemplateLoaderInitializers();
  +            if(!sourceInitializersAssembled)
  +                assembleSourceInitializers();
  +            
               templateLoaders = new ArrayList();
               
  -            for (int i = 0; i < initializers.size(); i++)
  +            for (int i = 0; i < sourceInitializerList.size(); i++)
               {
  -                Map initializer = (Map) initializers.get(i);
  +                Map initializer = (Map) sourceInitializerList.get(i);
                   String loaderClass = (String) initializer.get("class");
                   templateLoader = TemplateFactory.getLoader(loaderClass);
                   templateLoader.init(initializer);
  @@ -411,9 +438,10 @@
        * will be passed in when initializing the
        * the template loader.
        */
  -    private static List getTemplateLoaderInitializers()
  +    private static void assembleSourceInitializers()
       {
  -        ArrayList loaderInitializers = new ArrayList();
  +        sourceInitializerList = new ArrayList();
  +        sourceInitializerMap = new Hashtable();
           
           for (int i = 0; i < 10; i++)
           {
  @@ -423,7 +451,7 @@
               if (!e.hasMoreElements())
                   continue;
               
  -            Hashtable loaderInitializer = new Hashtable();
  +            Hashtable sourceInitializer = new Hashtable();
               
               while (e.hasMoreElements())
               {
  @@ -431,15 +459,41 @@
                   String value = VelocityResources.getString(property);
                   
                   property = property.substring(loaderID.length() + 1);
  -                loaderInitializer.put(property, value);
  +                sourceInitializer.put(property, value);
  +                
  +                // Make a Map of the public names for the sources
  +                // to the sources property identifier so that external
  +                // clients can set source properties. For example:
  +                // File.template.path would get translated into
  +                // template.loader.1.template.path and the translated
  +                // name would be used to set the property.
  +                
  +                if (property.equals("public.name"))
  +                    sourceInitializerMap.put(value, sourceInitializer);
               }    
               
  -            loaderInitializers.add(loaderInitializer);
  +            sourceInitializerList.add(sourceInitializer);
  +            sourceInitializersAssembled = true;
           }
  -        
  -        return loaderInitializers;
       }
  -    
  +
  +    /**
  +     * Allow clients of Velocity to set a template stream
  +     * source property before the template source streams
  +     * are initialized. This would for example allow clients
  +     * to set the template path that would be used by the
  +     * file template stream source. Right now these properties
  +     * have to be set before the template stream source is
  +     * initialized. Maybe we should allow these properties
  +     * to be changed on the fly.
  +     */
  +    public static void setSourceProperty(String key, String value)
  +    {
  +        String publicName = key.substring(0, key.indexOf("."));
  +        String property = key.substring(key.indexOf(".") + 1);
  +        ((Map)sourceInitializerMap.get(publicName)).put(property, value);
  +    }
  +
       /**
        * Initializes the Velocity parser pool.
        * This still needs to be implemented.
  
  
  
  1.2       +3 -0      
jakarta-velocity/src/java/org/apache/velocity/runtime/RuntimeConstants.java
  
  Index: RuntimeConstants.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/RuntimeConstants.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RuntimeConstants.java     2000/11/16 16:24:20     1.1
  +++ RuntimeConstants.java     2000/11/16 23:06:40     1.2
  @@ -73,4 +73,7 @@
   
       /** External service initialization of the Velocity Runtime */
       public static final String EXTERNAL_INIT = "external.init";
  +
  +    public static final String FILE_TEMPLATE_PATH = "File.template.path";
  +    public static final String FILE_TEMPLATE_CACHE = "File.template.cache";
   }
  
  
  
  1.8       +2 -0      
jakarta-velocity/src/java/org/apache/velocity/runtime/defaults/velocity.properties
  
  Index: velocity.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/defaults/velocity.properties,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- velocity.properties       2000/11/16 01:53:24     1.7
  +++ velocity.properties       2000/11/16 23:06:40     1.8
  @@ -64,12 +64,14 @@
   # 
   #----------------------------------------------------------------------------
   
  +template.loader.1.public.name = File
   template.loader.1.description = Velocity File Template Loader
   template.loader.1.class = org.apache.velocity.runtime.loader.FileTemplateLoader
   template.loader.1.template.path = .
   template.loader.1.cache = false
   template.loader.1.modificationCheckInterval = 2
   
  +#template.loader.2.public.name = URL
   #template.loader.2.description = Velocity URL Template Loader
   #template.loader.2.class = org.apache.velocity.runtime.loader.URLTemplateLoader
   #template.loader.2.template.path = http://localhost/templates/
  
  
  

Reply via email to