geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject cvs commit: incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config StringValueParser.java
Date Sun, 24 Aug 2003 15:06:02 GMT
jdillon     2003/08/24 08:06:02

  Modified:    modules/twiddle project.xml
               modules/twiddle/src/java/org/apache/geronimo/twiddle/config
                        StringValueParser.java
  Log:
   o Hooked up JEXL for StringValueParser
  
  Revision  Changes    Path
  1.7       +1 -3      incubator-geronimo/modules/twiddle/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/project.xml,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- project.xml	24 Aug 2003 09:27:44 -0000	1.6
  +++ project.xml	24 Aug 2003 15:06:02 -0000	1.7
  @@ -126,7 +126,6 @@
         </properties>
       </dependency>
       
  -    <!-- Will need this soon, but not right now
       <dependency>
         <id>commons-jexl</id>
         <version>SNAPSHOT</version>
  @@ -135,7 +134,6 @@
           <runtime>true</runtime>
         </properties>
       </dependency>
  -    -->
       
       <dependency>
         <id>commons-cli</id>
  
  
  
  1.6       +74 -7     incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/StringValueParser.java
  
  Index: StringValueParser.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/StringValueParser.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- StringValueParser.java	24 Aug 2003 10:54:43 -0000	1.5
  +++ StringValueParser.java	24 Aug 2003 15:06:02 -0000	1.6
  @@ -56,6 +56,17 @@
   
   package org.apache.geronimo.twiddle.config;
   
  +import java.util.Map;
  +
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
  +
  +import org.apache.commons.jexl.JexlHelper;
  +import org.apache.commons.jexl.JexlContext;
  +import org.apache.commons.jexl.Expression;
  +import org.apache.commons.jexl.ExpressionFactory;
  +import org.apache.commons.jexl.resolver.FlatResolver;
  +
   import org.apache.geronimo.common.NullArgumentException;
   
   /**
  @@ -65,21 +76,62 @@
    */
   public class StringValueParser
   {
  +    private static final Log log = LogFactory.getLog(StringValueParser.class);
  +    
  +    protected JexlContext context;
  +    
  +    public StringValueParser(final Map vars)
  +    {
  +        context = JexlHelper.createContext();
  +        context.setVars(vars);
  +        
  +        if (log.isTraceEnabled()) {
  +            log.trace("Using variables: " + context.getVars());
  +        }
  +    }
  +    
       public StringValueParser()
       {
  +        this(System.getProperties());
       }
       
  -    protected String evaluate(final String expr)
  +    public Map getVariables()
  +    {
  +        return context.getVars();
  +    }
  +    
  +    private FlatResolver resolver = new FlatResolver(true);
  +    
  +    protected Expression createExpression(final String expression) throws Exception
       {
  -        //
  -        // TODO: Hook up JEXL here
  -        //
  +        Expression expr = ExpressionFactory.createExpression(expression);
  +        expr.addPreResolver(resolver);
  +        return expr;
  +    }
  +    
  +    public Object evaluate(final String expression) throws Exception
  +    {
  +        boolean trace = log.isTraceEnabled();
  +        if (trace) {
  +            log.trace("Evaluating expression: " + expression);
  +        }
           
  -        return System.getProperty(expr);
  +        Expression expr = createExpression(expression);
  +        Object obj = expr.evaluate(context);
  +        if (trace) {
  +            log.trace("Result: " + obj);
  +        }
  +        
  +        return obj;
       }
       
       public String parse(final String input)
       {
  +        boolean trace = log.isTraceEnabled();
  +        if (trace) {
  +            log.trace("Parsing input: " + input);
  +        }
  +        
           StringBuffer buff = new StringBuffer();
   
           int cur = 0;
  @@ -94,13 +146,28 @@
               }
   
               suffixLoc = input.indexOf("}", prefixLoc);
  +            if (suffixLoc < 0) {
  +                throw new RuntimeException("Missing '}': " + input);
  +            }
  +            
               String expr = input.substring(prefixLoc + 2, suffixLoc);
               buff.append(input.substring(cur, prefixLoc));
  -            buff.append(evaluate(expr));
  +            
  +            try {
  +                buff.append(evaluate(expr));
  +            }
  +            catch (Exception e) {
  +                throw new RuntimeException("Failed to evaluate: " + expr, e);
  +            }
  +            
               cur = suffixLoc + 1;
           }
           
           buff.append(input.substring(cur));
  +        
  +        if (trace) {
  +            log.trace("Parsed result: " + buff);
  +        }
           
           return buff.toString();
       }
  
  
  

Mime
View raw message