dion        2002/06/12 08:38:05

  Modified:    src/java/org/apache/maven/jelly JellyAntProject.java
  Log:
  Switch to a jelly context
  
  Revision  Changes    Path
  1.4       +70 -38    
jakarta-turbine-maven/src/java/org/apache/maven/jelly/JellyAntProject.java
  
  Index: JellyAntProject.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jelly/JellyAntProject.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JellyAntProject.java      12 Jun 2002 09:41:32 -0000      1.3
  +++ JellyAntProject.java      12 Jun 2002 15:38:05 -0000      1.4
  @@ -2,21 +2,23 @@
   package org.apache.maven.jelly;
   
   import org.apache.maven.project.Project;
  -import org.apache.commons.jexl.Expression;
  -import org.apache.commons.jexl.ExpressionFactory;
  -import org.apache.commons.jexl.JexlContext;
  -import org.apache.commons.jexl.context.HashMapContext;
  +import org.apache.commons.jelly.JellyContext;
  +import org.apache.commons.jelly.expression.CompositeExpression;
  +import org.apache.commons.jelly.expression.Expression;
  +import org.apache.commons.jelly.expression.ExpressionFactory;
  +import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
  +
   import org.apache.commons.logging.LogFactory;
   import org.apache.commons.logging.Log;
   
   import java.util.Hashtable;
   
   /**
  - * An Ant {@link org.apache.tools.ant.Project project} that uses Jexl
  + * An Ant {@link org.apache.tools.ant.Project project} that uses Jelly
    * for it's variable context and resolution when 'normal' ant properties 
    * can't be found
    * 
  - * @task jexl expressions are not cached, and should be.
  + * @task jelly expressions are not cached, and should be.
    *
    * @version $Id$
    * @author <a href="mailto:[EMAIL PROTECTED]";>Bob McWhirter</a>
  @@ -26,48 +28,54 @@
   {
       /** logger */
       private static final Log log = LogFactory.getLog(JellyAntProject.class);
  -    /** the jexl context used to store variables */
  -    private HashMapContext jexlContext;
  -
  -    /** Create a new instance with a default {@link JexlContext jexl context} */
  +    /** the context used to store variables */
  +    private JellyContext context;
  +    /** factory for creating expressions in jelly */
  +    private ExpressionFactory factory = new JexlExpressionFactory();
  +    
  +    /** 
  +     * Create a new instance with a default {@link JexlContext jexl context}
  +     */
       public JellyAntProject()
       {
  -        jexlContext = new HashMapContext();
  +        this(new JellyContext());
       }
   
  -    /** Create a new instance with the provided {@link JexlContext jexl context}
  +    /** 
  +     * Create a new instance with the provided {@link JellyContext context}
        * for property resolution
  -     * @param jexlContext the context to use for resolution
  +     *
  +     * @param context the context to use for resolution
        */
  -    public JellyAntProject(JexlContext jexlContext)
  +    public JellyAntProject(JellyContext context)
       {
  -        setJexlContext( jexlContext );
  +        setContext(context);
       }
   
       /** 
        * Retrieve a property by name, checking the Ant properties first, and
  -     * then the jexl context
  +     * then the context
        *
        * @param name the name of the property to retrieve
        * @return the value of the property, or null if not found
        */
       public String getProperty(String name)
       {
  -        log.info("getting property: '" + name + "'");
  +        log.debug("getting property: '" + name + "'");
           String value = super.getProperty( name );
           if ( value == null )
           {
  -            log.info("Looking for property in context");
  +            log.debug("Looking for property in context");
               value = getMavenProperty( name );
           }
   
  -        log.info("Value is: '" + value + "'");
  +        log.debug("Value is: '" + value + "'");
           return value;
       }
   
       /** 
        * Retrieve a user property by name, checking the Ant properties first, and
  -     * then the jexl context
  +     * then the context
        *
        * @param name the name of the user property to retrieve
        * @return the value of the property, or null if not found
  @@ -89,7 +97,7 @@
        * uses {@link JellyAntProps} so that dynamic properties based on bean
        * properties can be accessed.
        *
  -     * @return a Hashtable of properties backed by the jexl context for
  +     * @return a Hashtable of properties backed by the context for
        * dynamic properties
        */
       public Hashtable getProperties()
  @@ -98,7 +106,7 @@
       }
   
       /**
  -     * Look up properties in the jexl context. These properties can be 'dynamic'
  +     * Look up properties in the context. These properties can be 'dynamic'
        * and for example use properties of beans stored in the context.
        * 
        * @param name the name of the property to retrieve
  @@ -106,37 +114,61 @@
        */
       public String getMavenProperty(String name)
       {
  -        String value = null;
  +        String propertyValue = null;
           try
           {
               // FIXME: Add caching of expressions here
  -            Expression expr = ExpressionFactory.createExpression( name );
  -            
  -            Object result = expr.evaluate( getJexlContext() );
  -
  -            if ( result != null )
  +            Object value = getContext().getVariable(name);
  +            if (value instanceof String)
  +            {
  +                log.debug(" jelly variable " + name + "=" + value);
  +                String valueString = (String) value;
  +                if (valueString.indexOf("${") != -1)
  +                {
  +                    value = evaluateExpression(valueString);
  +                }
  +            }
  +            log.debug(" evaluating expression gives: " + value);
  +            if ( value != null )
               {
  -                value = result.toString();
  +                propertyValue = value.toString();
  +            }
  +            else
  +            {
  +                propertyValue = evaluateExpression(name).toString();
               }
              
           }
           catch (Exception e)
           {
               e.printStackTrace();
  -            value = null;
  +            propertyValue = null;
           }
   
  -        return value;
  +        return propertyValue;
       }
   
       /**
  +     * Evaluate the given expression and return the result
  +     *
  +     * @param expr the expression to evaluate
  +     * @return the result of evaluation using Jelly
  +     * @throws Exception when an error occurs parsing or evaluating
  +     */
  +    public Object evaluateExpression(String expr) throws Exception
  +    {
  +        Expression expression = CompositeExpression.parse(expr, factory);
  +        return expression.evaluate(context);
  +    }
  +    
  +    /**
        * accept a maven project object and make it accessible via the jexl context
        *
        * @param project a configured {@link Project project}
        */
       public void setMavenProject(Project project)
       {
  -        jexlContext.put( "maven", project );
  +        getContext().setVariable("maven", project);
       }
   
       /**
  @@ -145,19 +177,19 @@
        *
        * @param jexlContext a context to copy variables from
        */
  -    public void setJexlContext(JexlContext jexlContext)
  +    public void setContext(JellyContext context)
       {
  -        this.jexlContext = new HashMapContext();
  -        this.jexlContext.putAll( jexlContext.getVars() );
  +        this.context = new JellyContext();
  +        this.context.setVariables(context.getVariables());
       }
   
       /**
        * provide access to the currently used context
        * @return a {@link JexlContext}
        */
  -    public JexlContext getJexlContext()
  +    public JellyContext getContext()
       {
  -        return jexlContext;
  +        return context;
       }
   }
   
  
  
  

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

Reply via email to