cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amicha...@apache.org
Subject svn commit: r989871 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java test/java/org/apache/cxf/jaxrs/ext/search/FiqlParserTest.java
Date Thu, 26 Aug 2010 18:54:52 GMT
Author: amichalec
Date: Thu Aug 26 18:54:52 2010
New Revision: 989871

URL: http://svn.apache.org/viewvc?rev=989871&view=rev
Log:
Fixed brackets handling in FIQL parser

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/FiqlParserTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java?rev=989871&r1=989870&r2=989871&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
Thu Aug 26 18:54:52 2010
@@ -101,27 +101,48 @@ public class FiqlParser<T> {
      * @throws FiqlParseException when expression does not follow FIQL grammar
      */
     public SearchCondition<T> parse(String fiqlExpression) throws FiqlParseException
{
-        ASTNode<T> ast = parseAndsOrsParens(fiqlExpression);
+        ASTNode<T> ast = parseAndsOrsBrackets(fiqlExpression);
         // System.out.println(ast);
         return ast.build();
     }
 
-    private ASTNode<T> parseAndsOrsParens(String expr) throws FiqlParseException {
-        String s1 = "([\\p{ASCII}&&[^;,()]]+|\\([\\p{ASCII}]+\\))([;,])?";
-        Pattern p = Pattern.compile(s1);
-        Matcher m = p.matcher(expr);
+    private ASTNode<T> parseAndsOrsBrackets(String expr) throws FiqlParseException
{
         List<String> subexpressions = new ArrayList<String>();
         List<String> operators = new ArrayList<String>();
-        int lastEnd = -1;
-        while (m.find()) {
-            subexpressions.add(m.group(1));
-            operators.add(m.group(2));
-            if (lastEnd != -1 && lastEnd != m.start()) {
-                throw new FiqlParseException(String
-                    .format("Unexpected characters \"%s\" starting at position %d", expr.substring(lastEnd,
m
-                        .start()), lastEnd));
+        int level = 0;
+        int lastIdx = 0;
+        int idx = 0;
+        for (idx = 0; idx < expr.length(); idx++) {
+            char c = expr.charAt(idx);
+            if (c == '(') {
+                level++;
+            } else if (c == ')') {
+                level--;
+                if (level < 0) {
+                    throw new FiqlParseException(String.format("Unexpected closing bracket
at position %d",
+                                                               idx));
+                }
             }
-            lastEnd = m.end();
+            String cs = Character.toString(c);
+            boolean isOperator = AND.equals(cs) || OR.equals(cs);
+            if (level == 0 && isOperator) {
+                String s1 = expr.substring(lastIdx, idx);
+                String s2 = expr.substring(idx, idx + 1);
+                subexpressions.add(s1);
+                operators.add(s2);
+                lastIdx = idx + 1;
+            }
+            boolean isEnd = (idx == expr.length() - 1);
+            if (isEnd) {
+                String s1 = expr.substring(lastIdx, idx + 1);
+                subexpressions.add(s1);
+                operators.add(null);
+                lastIdx = idx + 1;
+            }
+        }
+        if (level != 0) {
+            throw new FiqlParseException(String
+                .format("Unmatched opening and closing brackets in expression: %s", expr));
         }
         if (operators.get(operators.size() - 1) != null) {
             String op = operators.get(operators.size() - 1);
@@ -143,7 +164,7 @@ public class FiqlParser<T> {
                 String subex = subexpressions.get(from);
                 ASTNode<T> node = null;
                 if (subex.startsWith("(")) {
-                    node = parseAndsOrsParens(subex.substring(1, subex.length() - 1));
+                    node = parseAndsOrsBrackets(subex.substring(1, subex.length() - 1));
                 } else {
                     node = parseComparison(subex);
                 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/FiqlParserTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/FiqlParserTest.java?rev=989871&r1=989870&r2=989871&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/FiqlParserTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/FiqlParserTest.java
Thu Aug 26 18:54:52 2010
@@ -69,17 +69,32 @@ public class FiqlParserTest extends Asse
     }
 
     @Test
-    public void testRedundantParens() throws FiqlParseException {
+    public void testMultilevelExpression2() throws FiqlParseException {
+        parser.parse("((name==a;level==10),name!=b;name!=c);level=gt=10");
+    }
+
+    @Test
+    public void testRedundantBrackets() throws FiqlParseException {
         parser.parse("name==a;((((level==10))))");
     }
+    
+    @Test
+    public void testAndOfOrs() throws FiqlParseException {
+        parser.parse("(name==a,name==b);(level=gt=0,level=lt=10)");
+    }
+    
+    @Test
+    public void testOrOfAnds() throws FiqlParseException {
+        parser.parse("(name==a;name==b),(level=gt=0;level=lt=10)");
+    }
 
     @Test(expected = FiqlParseException.class)
-    public void testUnmatchedParen() throws FiqlParseException {
-        parser.parse("name==a;(level==10,(name!=b)");
+    public void testUnmatchedBracket() throws FiqlParseException {
+        parser.parse("name==a;(name!=b;(level==10,(name!=b))");
     }
 
     @Test(expected = FiqlParseException.class)
-    public void testUnmatchedParen2() throws FiqlParseException {
+    public void testUnmatchedBracket2() throws FiqlParseException {
         parser.parse("name==bbb;))()level==111");
     }
 



Mime
View raw message