You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2015/11/09 15:42:12 UTC

olingo-odata4 git commit: [OLINGO-568] Improved states

Repository: olingo-odata4
Updated Branches:
  refs/heads/OLINGO-568_SearchParserPoC b3bbfa6fe -> 43bc49f96


[OLINGO-568] Improved states


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/43bc49f9
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/43bc49f9
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/43bc49f9

Branch: refs/heads/OLINGO-568_SearchParserPoC
Commit: 43bc49f9631189d1ac49ee5c7ff0875da83b3d73
Parents: b3bbfa6
Author: Michael Bolz <mi...@sap.com>
Authored: Mon Nov 9 15:39:02 2015 +0100
Committer: Michael Bolz <mi...@sap.com>
Committed: Mon Nov 9 15:39:02 2015 +0100

----------------------------------------------------------------------
 .../core/uri/parser/search/SearchTokenizer.java | 68 +++++++++-----------
 .../uri/parser/search/SearchTokenizerTest.java  | 13 +++-
 2 files changed, 40 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/43bc49f9/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizer.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizer.java
index 67d4655..f62e0f4 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizer.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizer.java
@@ -82,6 +82,10 @@ public class SearchTokenizer {
       return token;
     }
 
+    public boolean close() {
+      return nextChar(EOF).isFinished();
+    }
+
     static boolean isAllowedChar(final char character) {
       // TODO mibo: add missing allowed characters
       return CHAR_A <= character && character <= 'Z' // case A..Z
@@ -173,11 +177,9 @@ public class SearchTokenizer {
       if (c == CHAR_OPEN) {
         return new OpenState();
       } else if (isWhitespace(c)) {
-        return new RwsState();
+        return new RwsImplicitAndState();
       } else if(c == CHAR_CLOSE) {
         return new CloseState();
-      } else if(isEof(c)) {
-        return finish();
       } else if(isWhitespace(c)) {
         return new AndState(c);
       } else {
@@ -186,6 +188,11 @@ public class SearchTokenizer {
     }
 
     @Override
+    public boolean close() {
+      return true;
+    }
+
+    @Override
     public State init(char c) {
       return nextChar(c);
     }
@@ -229,7 +236,7 @@ public class SearchTokenizer {
         return new CloseState();
       } else if (isWhitespace(c)) {
         finish();
-        return new RwsState();
+        return new RwsImplicitAndState();
       } else if (isEof(c)) {
         return finish();
       }
@@ -253,10 +260,16 @@ public class SearchTokenizer {
         return allowed(c);
       } else if (c == QUOTATION_MARK) {
         finish();
-        return allowed(c);
+        allowed(c);
+        return new SearchExpressionState();
       } else if (isWhitespace(c)) {
         if(isFinished()) {
-          return new RwsState();
+          return new RwsImplicitAndState();
+        }
+        return allowed(c);
+      } else if (c == CHAR_CLOSE) {
+        if(isFinished()) {
+          return new CloseState();
         }
         return allowed(c);
       } else if (isEof(c)) {
@@ -319,22 +332,6 @@ public class SearchTokenizer {
     }
   }
 
-  private class ImplicitAndState extends LiteralState {
-    private State followingState;
-    public ImplicitAndState(char c) {
-      super(Token.AND);
-      finish();
-      followingState = new SearchExpressionState().init(c);
-    }
-    public State nextState() {
-      return followingState;
-    }
-    @Override
-    public State nextChar(char c) {
-      return followingState.nextChar(c);
-    }
-  }
-
   private class AndState extends LiteralState {
     public AndState(char c) {
       super(Token.AND, c);
@@ -393,9 +390,10 @@ public class SearchTokenizer {
     }
   }
 
-  private class RwsState extends State {
-    public RwsState() {
-      super(Token.RWS);
+  // implicit and
+  private class RwsImplicitAndState extends State {
+    public RwsImplicitAndState() {
+      super(Token.AND);
     }
     @Override
     public State nextChar(char c) {
@@ -406,7 +404,8 @@ public class SearchTokenizer {
       } else if (c == CHAR_A) {
         return new AndState(c);
       } else {
-        return new ImplicitAndState(c);
+        finish();
+        return new SearchExpressionState().init(c);
       }
     }
   }
@@ -417,25 +416,18 @@ public class SearchTokenizer {
 
     State state = new SearchExpressionState();
     List<SearchQueryToken> states = new ArrayList<SearchQueryToken>();
-    State lastAdded = null;
     for (char aChar : chars) {
       State next = state.nextChar(aChar);
-      if (next instanceof ImplicitAndState) {
-        lastAdded = next;
-        states.add(next);
-        next = ((ImplicitAndState)next).nextState();
-      } else if (state.isFinished() && state != lastAdded) {
-        lastAdded = state;
+      if (state.isFinished()) {
         states.add(state);
       }
       state = next;
     }
 
-    final State lastState = state.nextChar(State.EOF);
-    if(lastState.isFinished()) {
-      if(state != lastAdded) {
-        states.add(state);
-      }
+    if(state.close()) {
+     if(state.isFinished()) {
+       states.add(state);
+     }
     } else {
       throw new IllegalStateException("State: " + state + " not finished and list is: " + states.toString());
     }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/43bc49f9/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java
index 9044282..e53b6b2 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java
@@ -86,6 +86,8 @@ public class SearchTokenizerTest {
     SearchTokenizer tokenizer = new SearchTokenizer();
     List<SearchQueryToken> result;
 
+    SearchValidator.init("abc AND \"x-y_z\" AND 123").validate();
+
     //
     result = tokenizer.tokenize("\"abc\"");
     Assert.assertNotNull(result);
@@ -325,12 +327,17 @@ public class SearchTokenizerTest {
     // parenthesis
     validate("(abc)");
     validate("(abc AND  def)");
-    validate("(abc AND  def)   OR  ghi ");
-    validate("(abc AND  def)       ghi ");
+    validate("(abc AND  def)   OR  ghi");
+    validate("(abc AND  def)       ghi");
     validate("abc AND (def    OR  ghi)");
     validate("abc AND (def        ghi)");
   }
-  
+
+  @Test
+  public void parseInvalid() {
+    SearchValidator.init("abc AND OR something").validate();
+  }
+
   public boolean validate(String query) {
     return new SearchValidator(query).validate();
   }