jstrachan    2002/10/02 04:03:38

  Modified:    jelly/src/java/org/apache/commons/jelly Jelly.java
               jelly/src/java/org/apache/commons/jelly/tags/util
                        UtilTagLibrary.java
               jelly    project.xml
  Added:       jelly/src/java/org/apache/commons/jelly/tags/util
                        PropertiesTag.java
  Log:
  Applied Jim Birchfield's patches to support both a jelly.properties file when 
running Jelly from the command line as well as a new <properties> tag to load 
properties from a file or URI in Jelly script.
  
  Revision  Changes    Path
  1.15      +78 -8     
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java
  
  Index: Jelly.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Jelly.java        9 Aug 2002 19:11:56 -0000       1.14
  +++ Jelly.java        2 Oct 2002 11:03:38 -0000       1.15
  @@ -65,10 +65,13 @@
   import java.io.File;
   import java.io.FileWriter;
   import java.io.InputStream;
  +import java.io.FileInputStream;
   import java.io.OutputStreamWriter;
   import java.io.Writer;
   import java.net.MalformedURLException;
   import java.net.URL;
  +import java.util.Enumeration;
  +import java.util.Properties;
   
   import org.apache.commons.jelly.parser.XMLParser;
   import org.apache.commons.logging.Log;
  @@ -96,10 +99,11 @@
       /** The URL of the root context for other scripts */
       private URL rootContext;
       
  -    
  +    /** Whether we have loaded the properties yet */
  +    private boolean loadedProperties = false;
  +        
       public Jelly() {
       }
  -    
       public static void main(String[] args) throws Exception {
   
           try
  @@ -155,11 +159,18 @@
               }
           }
       }
  +
  +    
       
       /**
        * Compiles the script
        */
       public Script compileScript() throws Exception {
  +        if (! loadedProperties) {
  +            loadedProperties = true;
  +            loadJellyProperties();
  +        }
  +        
           XMLParser parser = new XMLParser();
           parser.setContext(getJellyContext());
           Script script = parser.parse(getUrl().openStream());
  @@ -169,7 +180,7 @@
           }
           return script;
       }
  -    
  +
       
       // Properties
       //-------------------------------------------------------------------------     
           
  @@ -234,5 +245,64 @@
               return file.toURL();
           }
           return new URL(name);
  +    }
  +
  +    /**
  +     * Attempts to load jelly.properties from the current directory,
  +     * the users home directory or from the classpath
  +     */
  +    protected void loadJellyProperties() {
  +        InputStream is = null;
  +    
  +        String userDir = System.getProperty("user.home");
  +        File f = new File(userDir + File.separator + "jelly.properties");
  +        try {
  +            if (f.exists()) {
  +                is = new FileInputStream(f);
  +                loadProperties(is);
  +            }
  +        }
  +        catch (Exception e) {
  +            log.error( "Caught exception while loading: " + f.getName() + ". 
Reason: " + e, e );
  +        }
  +    
  +        f = new File("jelly.properties");
  +        try {
  +            if (f.exists()) {
  +                is = new FileInputStream(f);
  +                loadProperties(is);
  +            }
  +        }
  +        catch (Exception e) {
  +            log.error( "Caught exception while loading: " + f.getName() + ". 
Reason: " + e, e );
  +        }
  +        
  +        
  +        is = getClass().getClassLoader().getResourceAsStream("jelly.properties");
  +        if (is != null) {
  +            try {
  +                loadProperties(is);
  +            }
  +            catch (Exception e) {
  +                log.error( "Caught exception while loading jelly.properties from 
the classpath. Reason: " + e, e );
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Loads the properties from the given input stream 
  +     */    
  +    protected void loadProperties(InputStream is) throws Exception {
  +        JellyContext context = getJellyContext();
  +        Properties props = new Properties();
  +        props.load(is);
  +        Enumeration enum = props.propertyNames();
  +        while (enum.hasMoreElements()) {
  +            String key = (String) enum.nextElement();
  +            String value = props.getProperty(key);
  +            
  +            // @todo we should parse the value in case its an Expression
  +            context.setVariable(key, value);
  +        }
       }
   }
  
  
  
  1.2       +6 -5      
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java
  
  Index: UtilTagLibrary.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UtilTagLibrary.java       16 Jul 2002 05:43:32 -0000      1.1
  +++ UtilTagLibrary.java       2 Oct 2002 11:03:38 -0000       1.2
  @@ -72,5 +72,6 @@
   {
       public UtilTagLibrary() {
           registerTag("tokenize", TokenizeTag.class);
  +        registerTag("properties", PropertiesTag.class);
       }
   }
  
  
  
  1.1                  
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/PropertiesTag.java
  
  Index: PropertiesTag.java
  ===================================================================
  /*
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.jelly.tags.util;
  
  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.MissingAttributeException;
  
  import java.util.StringTokenizer;
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.Properties;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.InputStream;
  
  /**
   * A tag which loads a properties file from a given file name or URI
   * which are loaded into the current context.
   * 
   * @author Jim Birchfield
   * @version $Revision: 1.8 $
   */
  public class PropertiesTag extends TagSupport {
      private String file;
      private String uri;
  
      public PropertiesTag() {
      }
  
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(final XMLOutput output) throws Exception {
          if (file == null && uri == null) {
              throw new JellyException("This tag must define a 'file' or 'uri' 
attribute");
          }
          InputStream is = null;
          if (file != null) {
              File f = new File(file);
              if (!f.exists()) {
                  throw new JellyException("file: " + file + " does not exist!");
              }
              is = new FileInputStream(f);
          }
          else {
              is = context.getResourceAsStream(uri);
              if (is == null) {
                  throw new JellyException( "Could not find: " + uri );
              }
          }
          Properties props = new Properties();
          props.load(is);
          Enumeration enum = props.propertyNames();
          while (enum.hasMoreElements()) {
              String key = (String) enum.nextElement();
              String value = props.getProperty(key);
              
              // @todo we should parse the value in case its an Expression
              context.setVariable(key, value);
          }
  
      }
  
      // Properties
      //------------------------------------------------------------------------- 
      
      /**
       * Sets the file name to be used to load the properties file.
       */
      public void setFile(String file) {
          this.file = file;
      }
  
      /**
       * Sets the URI of the properties file to use. This can be a full URL or a 
relative URI
       * or an absolute URI to the root context of this JellyContext.
       */
      public void setUri(String uri) {
          this.uri = uri;
      }
  
  }
  
  
  
  1.76      +8 -0      jakarta-commons-sandbox/jelly/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/project.xml,v
  retrieving revision 1.75
  retrieving revision 1.76
  diff -u -r1.75 -r1.76
  --- project.xml       30 Sep 2002 08:05:45 -0000      1.75
  +++ project.xml       2 Oct 2002 11:03:38 -0000       1.76
  @@ -150,6 +150,14 @@
                <role>Developer</role>
         </roles>
       </contributor>    
  +    <contributor>
  +      <name>Jim Birchfield</name>
  +      <email>[EMAIL PROTECTED]</email>
  +      <organization>Genscape, Inc.</organization>
  +      <roles>
  +             <role>Developer</role>
  +      </roles>
  +    </contributor>    
     </contributors>
   
     <dependencies>
  
  
  

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

Reply via email to