commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r1206068 - in /commons/proper/jexl/trunk/src: main/java/org/apache/commons/jexl2/ main/java/org/apache/commons/jexl2/parser/ test/java/org/apache/commons/jexl2/
Date Fri, 25 Nov 2011 07:28:11 GMT
Author: henrib
Date: Fri Nov 25 07:28:09 2011
New Revision: 1206068

URL: http://svn.apache.org/viewvc?rev=1206068&view=rev
Log:
Fix for constant literals handling;
Added handling for semi-pathological var expressions (x[['z']]) 

Modified:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTStringLiteral.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/VarTest.java

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java?rev=1206068&r1=1206067&r2=1206068&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java Fri Nov
25 07:28:09 2011
@@ -128,6 +128,21 @@ final class Debugger implements ParserVi
     public String data() {
         return builder.toString();
     }
+    
+    
+    /**
+     * @return The rebuilt expression from the node
+     */
+    public String data(JexlNode node) {
+        start = 0;
+        end = 0;
+        if (node != null) {
+            builder.setLength(0);
+            this.cause = node;
+            node.jjtAccept(this, null);
+        }
+        return builder.toString();
+    }
 
     /**
      * @return The starting offset location of the cause in the expression

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java?rev=1206068&r1=1206067&r2=1206068&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java Fri Nov
25 07:28:09 2011
@@ -906,7 +906,12 @@ public class JexlEngine {
                     if (child instanceof ASTReference && child.jjtGetNumChildren()
== 1) {
                         JexlNode desc = child.jjtGetChild(0);
                         if (varf && desc.isConstant()) {
-                            var.add(desc.image);
+                            String image = desc.image;
+                            if (image == null) {
+                                var.add(new Debugger().data(desc));
+                            } else {
+                                var.add(image); 
+                            }
                         } else if (desc instanceof ASTIdentifier) {
                             if (((ASTIdentifier) desc).getRegister() < 0) {
                                 List<String> di = new ArrayList<String>(1);

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java?rev=1206068&r1=1206067&r2=1206068&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java
(original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java
Fri Nov 25 07:28:09 2011
@@ -40,6 +40,12 @@ public class ASTNumberLiteral extends Je
     public Number getLiteral() {
         return literal;
     }
+    
+    /** {@inheritDoc} */
+    @Override
+    protected boolean isConstant(boolean literal) {
+        return true;
+    }
 
     /** {@inheritDoc} */
     @Override

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTStringLiteral.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTStringLiteral.java?rev=1206068&r1=1206067&r2=1206068&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTStringLiteral.java
(original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTStringLiteral.java
Fri Nov 25 07:28:09 2011
@@ -33,7 +33,13 @@ public final class ASTStringLiteral exte
     public String getLiteral() {
         return image;
     }
-
+    
+    /** {@inheritDoc} */
+    @Override
+    protected boolean isConstant(boolean literal) {
+        return true;
+    }
+    
     /** {@inheritDoc} */
     @Override
     public Object jjtAccept(ParserVisitor visitor, Object data) {

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java?rev=1206068&r1=1206067&r2=1206068&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java
(original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java
Fri Nov 25 07:28:09 2011
@@ -69,11 +69,21 @@ public abstract class JexlNode extends S
         return isConstant(this instanceof JexlNode.Literal<?>);
     }
 
-    private boolean isConstant(boolean literal) {
+    protected boolean isConstant(boolean literal) {
         if (literal) {
             if (children != null) {
                 for (JexlNode child : children) {
-                    if (!child.isConstant()) {
+                    if (child instanceof ASTReference) {
+                        boolean is = child.isConstant(true);
+                        if (!is) {
+                            return false;
+                        }
+                    } else if (child instanceof ASTMapEntry) {
+                        boolean is = child.isConstant(true);
+                        if (!is) {
+                            return false;
+                        }
+                    } else if (!child.isConstant()) {
                         return false;
                     }
                 }

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/VarTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/VarTest.java?rev=1206068&r1=1206067&r2=1206068&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/VarTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/VarTest.java Fri Nov
25 07:28:09 2011
@@ -16,8 +16,11 @@
  */
 package org.apache.commons.jexl2;
 
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 import java.util.logging.Logger;
@@ -157,13 +160,31 @@ public class VarTest extends JexlTestCas
         if (lhs.size() != rhs.size()) {
             return false;
         }
-        for(List<String> ref : lhs) {
-            if (!rhs.contains(ref)) {
+        List<String> llhs = stringify(lhs);
+        List<String> lrhs = stringify(rhs);
+        for(int s = 0; s < llhs.size(); ++s) {
+            String l = llhs.get(s);
+            String r = lrhs.get(s);
+            if (!l.equals(r)) {
                 return false;
             }
         }
         return true;
     }
+    
+    List<String> stringify(Set<List<String>> sls) {
+        List<String> ls = new ArrayList<String>();
+        for(List<String> l : sls) {
+        StringBuilder strb = new StringBuilder();
+        for(String s : l) {
+            strb.append(s);
+            strb.append('|');
+        }
+            ls.add(strb.toString());
+        }
+        Collections.sort(ls);
+        return ls;
+    }
 
     public void testRefs() throws Exception {
         Script e;
@@ -227,4 +248,20 @@ public class VarTest extends JexlTestCas
         assertEquals(1, locals.length);
         assertEquals("z", locals[0]);
     }
+    
+    public void testLiteral() throws Exception {
+        Script e = JEXL.createScript("x.y[['z', 't']]");
+        Set<List<String>> vars = e.getVariables();
+        assertEquals(1, vars.size());
+        assertTrue(eq(mkref(new String[][]{{"x", "y", "[ 'z', 't' ]"}}), vars));
+        
+        e = JEXL.createScript("x.y[{'z': 't'}]");
+        vars = e.getVariables();
+        assertEquals(1, vars.size());
+        assertTrue(eq(mkref(new String[][]{{"x", "y", "{ 'z' : 't' }"}}), vars));
+        e = JEXL.createScript("x.y.'{ \\'z\\' : \\'t\\' }'");
+        vars = e.getVariables();
+        assertEquals(1, vars.size());
+        assertTrue(eq(mkref(new String[][]{{"x", "y", "{ 'z' : 't' }"}}), vars));
+    }
 }



Mime
View raw message