You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ge...@apache.org on 2001/11/08 05:05:57 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/texen/util PropertiesUtil.java

geirm       01/11/07 20:05:57

  Modified:    src/java/org/apache/velocity/texen/ant Tag: VEL_1_2_BRANCH
                        TexenTask.java
               src/java/org/apache/velocity/texen/util Tag: VEL_1_2_BRANCH
                        PropertiesUtil.java
  Log:
  Backport of Stephane's patches - everyone seems to be in agreement that
  all is well with them.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.32.2.1  +57 -25    jakarta-velocity/src/java/org/apache/velocity/texen/ant/TexenTask.java
  
  Index: TexenTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/texen/ant/TexenTask.java,v
  retrieving revision 1.32
  retrieving revision 1.32.2.1
  diff -u -r1.32 -r1.32.2.1
  --- TexenTask.java	2001/09/26 21:40:42	1.32
  +++ TexenTask.java	2001/11/08 04:05:57	1.32.2.1
  @@ -54,6 +54,7 @@
    * <http://www.apache.org/>.
    */
   
  +import java.util.StringTokenizer;
   import java.util.Date;
   import java.util.Hashtable;
   import java.util.Iterator;
  @@ -79,19 +80,21 @@
   /**
    * An ant task for generating output by using Velocity
    *
  - * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
  + * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    * @author <a href="robertdonkin@mac.com">Robert Burrell Donkin</a>
  - * @version $Id: TexenTask.java,v 1.32 2001/09/26 21:40:42 jvanzyl Exp $
  + * @version $Id: TexenTask.java,v 1.32.2.1 2001/11/08 04:05:57 geirm Exp $
    */
   public class TexenTask 
       extends Task
   {
       /**
  -     * This message (telling users to consult the log) is appended to
  -     * rethrown exception messages.
  -     */
  -    private final static String MSG_CONSULT_LOG = 
  -        ". For more information consult the velocity log.";
  +     * This message fragment (telling users to consult the log or
  +     * invoke ant with the -debug flag) is appended to rethrown
  +     * exception messages.
  +     */
  +    private final static String ERR_MSG_FRAGMENT = 
  +        ". For more information consult the velocity log, or invoke ant " +
  +        "with the -debug flag.";
       
       /**
        * This is the control template that governs the output.
  @@ -159,6 +162,11 @@
       protected boolean useClasspath;
   
       /**
  +     * Path separator.
  +     */
  +    private String fileSeparator = System.getProperty("file.separator");
  +
  +    /**
        * [REQUIRED] Set the control template for the
        * generating process.
        */
  @@ -182,16 +190,22 @@
        * loader.
        */
       
  -    public void setTemplatePath(File templatePath)
  +    public void setTemplatePath(String templatePath) throws Exception
       {
  -         try 
  -         {
  -             this.templatePath = templatePath.getCanonicalPath();
  -         } 
  -         catch (java.io.IOException ioe) 
  -         {
  -             throw new BuildException(ioe);
  -         }
  +        StringBuffer resolvedPath = new StringBuffer();
  +        StringTokenizer st = new StringTokenizer(templatePath, ",");
  +        while ( st.hasMoreTokens() )
  +        {
  +            // resolve relative path from basedir and leave
  +            // absolute path untouched.
  +            File fullPath = project.resolveFile(st.nextToken());
  +            resolvedPath.append(fullPath.getCanonicalPath());
  +            if ( st.hasMoreTokens() )
  +            {
  +                resolvedPath.append(",");
  +            }
  +        }
  +         this.templatePath = resolvedPath.toString();
        }
   
       /**
  @@ -268,7 +282,10 @@
               
               try
               {
  -                source.load(new FileInputStream(sources[i]));
  +                // resolve relative path from basedir and leave
  +                // absolute path untouched.
  +                File fullPath = project.resolveFile(sources[i]);
  +                source.load(new FileInputStream(fullPath));
               }
               catch (Exception e)
               {
  @@ -348,7 +365,6 @@
       public void execute () 
           throws BuildException
       {
  -
           // Make sure the template path is set.
           if (templatePath == null && useClasspath == false)
           {
  @@ -380,12 +396,14 @@
               // Setup the Velocity Runtime.
               if (templatePath != null)
               {
  +            	log("Using templatePath: " + templatePath, project.MSG_VERBOSE);
                   Velocity.setProperty(
                       Velocity.FILE_RESOURCE_LOADER_PATH, templatePath);
               }
               
               if (useClasspath)
               {
  +            	log("Using classpath");
                   Velocity.addProperty(
                       Velocity.RESOURCE_LOADER, "classpath");
               
  @@ -411,7 +429,7 @@
               if (templatePath != null)
               {
                   generator.setTemplatePath(templatePath);
  -            }                
  +            }
               
               // Make sure the output directory exists, if it doesn't
               // then create it.
  @@ -419,10 +437,10 @@
               if (! file.exists())
               {
                   file.mkdirs();
  -            }                
  +            }
               
               String path = outputDirectory + File.separator + outputFile;
  -            System.out.println(path);
  +            log("Generating to file " + path, project.MSG_INFO);
               FileWriter writer = new FileWriter(path);
               
               // The generator and the output path should
  @@ -504,6 +522,7 @@
               writer.flush();
               writer.close();
               generator.shutdown();
  +            cleanup();
           }
           catch( BuildException e)
           {
  @@ -513,20 +532,20 @@
           {
               throw new BuildException(
                   "Exception thrown by '" + e.getReferenceName() + "." + 
  -                    e.getMethodName() +"'" + MSG_CONSULT_LOG,
  +                    e.getMethodName() +"'" + ERR_MSG_FRAGMENT,
                           e.getWrappedThrowable());
           }       
           catch( ParseErrorException e )
           {
  -            throw new BuildException("Velocity syntax error" + MSG_CONSULT_LOG ,e);
  +            throw new BuildException("Velocity syntax error" + ERR_MSG_FRAGMENT ,e);
           }        
           catch( ResourceNotFoundException e )
           {
  -            throw new BuildException("Resource not found" + MSG_CONSULT_LOG,e);
  +            throw new BuildException("Resource not found" + ERR_MSG_FRAGMENT,e);
           }
           catch( Exception e )
           {
  -            throw new BuildException("Generation failed" + MSG_CONSULT_LOG ,e);
  +            throw new BuildException("Generation failed" + ERR_MSG_FRAGMENT ,e);
           }
       }
   
  @@ -551,5 +570,18 @@
           throws Exception
       {
           context.put("now", new Date().toString());
  +    }
  +
  +    /**
  +     * A hook method called at the end of {@link #execute()} which can
  +     * be overridden to perform any necessary cleanup activities (such
  +     * as the release of database connections, etc.).  By default,
  +     * does nothing.
  +     *
  +     * @exception Exception Problem cleaning up.
  +     */
  +    protected void cleanup()
  +        throws Exception
  +    {
       }
   }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.8.2.1   +102 -45   jakarta-velocity/src/java/org/apache/velocity/texen/util/PropertiesUtil.java
  
  Index: PropertiesUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/texen/util/PropertiesUtil.java,v
  retrieving revision 1.8
  retrieving revision 1.8.2.1
  diff -u -r1.8 -r1.8.2.1
  --- PropertiesUtil.java	2001/08/30 14:14:15	1.8
  +++ PropertiesUtil.java	2001/11/08 04:05:57	1.8.2.1
  @@ -58,6 +58,7 @@
   import java.io.InputStream;
   import java.io.IOException;
   import java.util.Properties;
  +import java.util.StringTokenizer;
   import org.apache.velocity.texen.Generator;
   
   /**
  @@ -65,30 +66,71 @@
    * Usually this class is only used from a Velocity context.
    *
    * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
  - * @version $Id: PropertiesUtil.java,v 1.8 2001/08/30 14:14:15 jvanzyl Exp $ 
  + * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
  + * @version $Id: PropertiesUtil.java,v 1.8.2.1 2001/11/08 04:05:57 geirm Exp $ 
    */
   public class PropertiesUtil
   {
  +    /**
  +     * Load properties from either a file in the templatePath if there
  +     * is one or the classPath.
  +     *
  +     * @param propertiesFile the properties file to load through
  +     * either the templatePath or the classpath.
  +     * @return a properties instance filled with the properties found
  +     * in the file or an empty instance if no file was found.
  +     */
       public Properties load(String propertiesFile)
       {
  +        Properties properties = new Properties();
           String templatePath = Generator.getInstance().getTemplatePath();
  +        if (templatePath != null)
  +        {
  +            properties = loadFromTemplatePath(propertiesFile);
  +        }
  +        else
  +        {
  +            properties = loadFromClassPath(propertiesFile);
  +        }
  +    
  +        return properties;
  +        
  +    }
  +    
  +    /**
  +     * Load a properties file from the templatePath defined in the
  +     * generator. As the templatePath can contains multiple paths,
  +     * it will cycle through them to find the file. The first file
  +     * that can be successfully loaded is considered. (kind of
  +     * like the java classpath), it is done to clone the Velocity
  +     * process of loading templates.
  +     *
  +     * @param propertiesFile the properties file to load. It must be
  +     * a relative pathname.
  +     * @return a properties instance loaded with the properties from
  +     * the file. If no file can be found it returns an empty instance.
  +     */
  +    protected Properties loadFromTemplatePath(String propertiesFile)
  +    {
           Properties properties = new Properties();
  +        String templatePath = Generator.getInstance().getTemplatePath();
           
  -        if (templatePath != null)
  +        // We might have something like the following:
  +        //
  +        // #set ($dbprops = $properties.load("$generator.templatePath/path/props")
  +        //
  +        // as we have in Torque but we want people to start using
  +        //
  +        // #set ($dbprops = $properties.load("path/props")
  +        //
  +        // so that everything works from the filesystem or from
  +        // a JAR. So the actual Generator.getTemplatePath()
  +        // is not deprecated but it's use in templates
  +        // should be.
  +        StringTokenizer st = new StringTokenizer(templatePath, ",");
  +        while (st.hasMoreTokens())
           {
  -            // We might have something like the following:
  -            //
  -            // #set ($dbprops = $properties.load("$generator.templatePath/path/props")
  -            //
  -            // as we have in Torque but we want people to start using
  -            //
  -            // #set ($dbprops = $properties.load("path/props")
  -            //
  -            // so that everything works from the filesystem or from
  -            // a JAR. So the actual Generator.getTemplatePath()
  -            // is not deprecated but it's use in templates
  -            // should be.
  -            
  +            String templateDir = st.nextToken();
               try
               {
                   // If the properties file is being pulled from the
  @@ -102,47 +144,62 @@
                   // for the properties file to be found. We want (1)
                   // to work whether the generation is being run from
                   // the file system or from a JAR file.
  -                if (!propertiesFile.startsWith(templatePath))
  +                String fullPath = propertiesFile;
  +                
  +                // FIXME probably not that clever since there could be
  +                // a mix of file separators and the test will fail :-(
  +                if (!fullPath.startsWith(templateDir))
                   {
  -                    propertiesFile = templatePath + "/" + propertiesFile;
  +                    fullPath = templateDir + "/" + propertiesFile;
                   }
  -                
  -                properties.load(new FileInputStream(propertiesFile));
  +
  +                properties.load(new FileInputStream(fullPath));
  +                // first pick wins, we don't need to go further since
  +                // we found a valid file.
  +                break;
               }
               catch (Exception e)
               {
                   // do nothing
               }
  -        }
  -        else
  +        } 
  +        return properties;
  +    }
  +
  +    /**
  +     * Load a properties file from the classpath
  +     *
  +     * @param propertiesFile the properties file to load.
  +     * @return a properties instance loaded with the properties from
  +     * the file. If no file can be found it returns an empty instance.
  +     */ 
  +    protected Properties loadFromClassPath(String propertiesFile)
  +    {
  +        Properties properties = new Properties();
  +        ClassLoader classLoader = this.getClass().getClassLoader();
  +        
  +        try
           {
  -            ClassLoader classLoader = this.getClass().getClassLoader();
  -            
  -            try
  +            // This is a hack for now to make sure that properties
  +            // files referenced in the filesystem work in
  +            // a JAR file. We have to deprecate the use
  +            // of $generator.templatePath in templates first
  +            // and this hack will allow those same templates
  +            // that use $generator.templatePath to work in
  +            // JAR files.
  +            if (propertiesFile.startsWith("$generator"))
               {
  -                // This is a hack for now to make sure that properties
  -                // files referenced in the filesystem work in
  -                // a JAR file. We have to deprecate the use
  -                // of $generator.templatePath in templates first
  -                // and this hack will allow those same templates
  -                // that use $generator.templatePath to work in
  -                // JAR files.
  -                if (propertiesFile.startsWith("$generator"))
  -                {
  -                    propertiesFile = propertiesFile.substring(
  -                        "$generator.templatePath/".length());
  -                }
  -                
  -                InputStream inputStream = classLoader.getResourceAsStream(propertiesFile);
  -                properties.load(inputStream);
  +                propertiesFile = propertiesFile.substring(
  +                    "$generator.templatePath/".length());
               }
  -            catch (IOException ioe)
  -            {
  -                // do nothing
  -            }
  +            
  +            InputStream inputStream = classLoader.getResourceAsStream(propertiesFile);
  +            properties.load(inputStream);
           }
  -    
  +        catch (IOException ioe)
  +        {
  +            // do nothing
  +        }
           return properties;
  -        
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>