commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/expression TestExpressions.java
Date Thu, 13 Jun 2002 09:27:14 GMT
jstrachan    2002/06/13 02:27:14

  Modified:    jelly/src/test/org/apache/commons/jelly test_args.jelly
               jelly/src/java/org/apache/commons/jelly/expression/jexl
                        JexlExpression.java JexlExpressionFactory.java
               jelly/src/test/org/apache/commons/jelly/ant
                        filescanner.jelly
               jelly/src/test/org/apache/commons/jelly/expression
                        TestExpressions.java
  Log:
  Added optional support for Ant style variable names. 
  
  So ${foo.bar} will be evaluated first with Jexl. If there is a "foo" variable it will return
the "bar" value.
  If this value is null, then it will try find the variable "foo.bar".
  
  This change means that Jelly can process existing Ant build.xml files, particularly the
Maven ones. So its a backwards compatability thing really.
  
  Geir, this functionality could move back into Jexl at some point if you like. We can happily
remove it from Jelly and put it into Jexl if you like.
  
  Also added test case for <whitespace> tag
  
  Revision  Changes    Path
  1.3       +4 -1      jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test_args.jelly
  
  Index: test_args.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test_args.jelly,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- test_args.jelly	3 Jun 2002 05:14:29 -0000	1.2
  +++ test_args.jelly	13 Jun 2002 09:27:14 -0000	1.3
  @@ -3,5 +3,8 @@
   <!-- displays the current command line arguments -->
   
   <j:jelly xmlns:j="jelly:core">
  -  <j:forEach var="arg" items="${args}"><j:expr value="${arg}"/> </j:forEach>
  +  <j:forEach var="arg" items="${args}">
  +  	<!-- use the <whitespace> tag to have careful control over whitespace -->
  +  	<j:whitespace><j:expr value="${arg}"/> </j:whitespace>
  +  </j:forEach>
   </j:jelly>
  
  
  
  1.6       +5 -17     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/jexl/JexlExpression.java
  
  Index: JexlExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/jexl/JexlExpression.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JexlExpression.java	5 Jun 2002 07:00:58 -0000	1.5
  +++ JexlExpression.java	13 Jun 2002 09:27:14 -0000	1.6
  @@ -67,17 +67,12 @@
   import java.util.Collection;
   
   import org.apache.commons.jelly.JellyContext;
  -
   import org.apache.commons.jelly.expression.ExpressionSupport;
   
   import org.apache.commons.jexl.Expression;
  -
   import org.apache.commons.jexl.JexlContext;
   
  -import org.apache.commons.jexl.context.HashMapContext;
  -
   import org.apache.commons.logging.Log;
  -
   import org.apache.commons.logging.LogFactory;
   
   /** 
  @@ -92,25 +87,18 @@
   public class JexlExpression extends ExpressionSupport {
   
       /** The Log to which logging calls will be made. */
  -
       private static final Log log = LogFactory.getLog(JexlExpression.class);
   
       /** The Jexl expression object */
  -
       private Expression expression;
   
       public JexlExpression(Expression expression) {
  -
           this.expression = expression;
  -
       }
   
       // Expression interface
  -
       //------------------------------------------------------------------------- 
  -
       public Object evaluate(JellyContext context) {
  -
           try {
   
               JexlContext jexlContext = new JellyJexlContext( context );
  
  
  
  1.6       +65 -7     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/jexl/JexlExpressionFactory.java
  
  Index: JexlExpressionFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/jexl/JexlExpressionFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JexlExpressionFactory.java	6 Jun 2002 07:13:41 -0000	1.5
  +++ JexlExpressionFactory.java	13 Jun 2002 09:27:14 -0000	1.6
  @@ -62,12 +62,16 @@
   
   package org.apache.commons.jelly.expression.jexl;
   
  +import org.apache.commons.jelly.JellyContext;
   import org.apache.commons.jelly.expression.Expression;
  +import org.apache.commons.jelly.expression.ExpressionSupport;
   import org.apache.commons.jelly.expression.ExpressionFactory;
   
   /** 
    * Represents a factory of <a href="http://jakarta.apache.org/commons/jexl.html">Jexl</a>

    * expression which fully supports the Expression Language in JSTL and JSP.
  + * In addition this ExpressionFactory can also support Ant style variable
  + * names, where '.' is used inside variable names.
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
  @@ -75,12 +79,66 @@
   
   public class JexlExpressionFactory implements ExpressionFactory {
   
  +    /** whether we should allow Ant-style expresssions, using dots as part of variable
name */
  +    private boolean supportAntVariables = true;
  +    
       // ExpressionFactory interface
       //------------------------------------------------------------------------- 
  -    public Expression createExpression(String text) throws Exception {
  -        return new JexlExpression(
  +    public Expression createExpression(final String text) throws Exception {
  +        final Expression jexlExpression = new JexlExpression(
               org.apache.commons.jexl.ExpressionFactory.createExpression(text)
           );
  +        
  +        if ( isSupportAntVariables() && isValidAntVariableName(text) ) {
  +            return new ExpressionSupport() {
  +                public Object evaluate(JellyContext context) {
  +                    Object answer = jexlExpression.evaluate(context);
  +                    if ( answer == null ) {
  +                        answer = context.getVariable(text);
  +                    }
  +                    return answer;
  +                }
  +            };
  +        }
  +        return jexlExpression;
  +    }
  +    
  +    // Properties
  +    //------------------------------------------------------------------------- 
  +
  +    /** 
  +     * @return whether we should allow Ant-style expresssions, using dots as 
  +     * part of variable name 
  +     */
  +    public boolean isSupportAntVariables() {
  +        return supportAntVariables;
  +    }
  +    
  +    /** 
  +     * Sets whether we should allow Ant-style expresssions, using dots as 
  +     * part of variable name 
  +     */
  +    public void setSupportAntVariables(boolean supportAntVariables) {
  +        this.supportAntVariables = supportAntVariables;
  +    }
  +
  +    // Implementation methods
  +    //------------------------------------------------------------------------- 
  +
  +    /**
  +     * @return true if the given string is a valid Ant variable name,
  +     * typically thats alphanumeric text with '.' etc.
  +     */
  +    protected boolean isValidAntVariableName(String text) {
  +        char[] chars = text.toCharArray();            
  +        for (int i = 0, size = chars.length; i < size; i++ ) {
  +            char ch = chars[i];
  +            // could maybe be a bit more restrictive...
  +            if ( Character.isWhitespace(ch) || ch == '[' ) {
  +                return false;
  +            }
  +        }
  +        return true;
       }
   
   }
  
  
  
  1.3       +1 -1      jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/ant/filescanner.jelly
  
  Index: filescanner.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/ant/filescanner.jelly,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- filescanner.jelly	3 Jun 2002 04:31:55 -0000	1.2
  +++ filescanner.jelly	13 Jun 2002 09:27:14 -0000	1.3
  @@ -2,7 +2,7 @@
   <j:jelly xmlns:j="jelly:core" xmlns="jelly:ant">
   
     <fileScanner var="scanner">
  -    <fileset dir="src/taglibs" includes="**/build.xml"/>
  +    <fileset dir="src/test" includes="**/*.jelly"/>
     </fileScanner>
       
     Iterating through build files
  
  
  
  1.2       +6 -0      jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/expression/TestExpressions.java
  
  Index: TestExpressions.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/expression/TestExpressions.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestExpressions.java	6 Jun 2002 07:13:41 -0000	1.1
  +++ TestExpressions.java	13 Jun 2002 09:27:14 -0000	1.2
  @@ -111,6 +111,12 @@
           assertExpression("${topping}-${type}", "cheese-deepPan");
       }
       
  +    public void testAntExpresssions() throws Exception {
  +        context.setVariable("maven.home.foo", "cheese");
  +
  +        assertExpression("${maven.home.foo}", "cheese");
  +    }
  +    
       protected void assertExpression(String expressionText, Object expectedValue) throws
Exception {
           Expression expression = CompositeExpression.parse(expressionText, factory);
           assertTrue( "Created a valid expression for: " + expressionText, expression !=
null );
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message