View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.support.query;
20  
21  import org.antlr.runtime.ANTLRStringStream;
22  import org.antlr.runtime.CharStream;
23  import org.antlr.runtime.CommonTokenStream;
24  import org.antlr.runtime.RecognitionException;
25  import org.antlr.runtime.TokenSource;
26  import org.antlr.runtime.TokenStream;
27  import org.antlr.runtime.tree.CommonTree;
28  import org.antlr.runtime.tree.CommonTreeNodeStream;
29  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
30  import org.apache.chemistry.opencmis.server.support.TypeManager;
31  import org.apache.chemistry.opencmis.server.support.query.CmisQlStrictParser_CmisBaseGrammar.query_return;
32  
33  public class QueryUtilStrict extends QueryUtilBase<CmisQueryWalker> {
34  
35      private CommonTree parserTree; // the ANTLR tree after parsing phase
36      private TokenStream tokens;    // the ANTLR token stream
37      private boolean parseFulltext = true; 
38      
39      public QueryUtilStrict(String statement, TypeManager tm, PredicateWalkerBase pw) {
40          super(statement, tm, pw);
41      }
42  
43      public QueryUtilStrict(String statement, TypeManager tm, PredicateWalkerBase pw, boolean parseFulltext) {
44          super(statement, tm, pw);
45          this.parseFulltext = parseFulltext;
46      }
47  
48      @Override
49      public CommonTree parseStatement() throws RecognitionException {
50          CharStream input = new ANTLRStringStream(statement);
51          TokenSource lexer = new CmisQlStrictLexer(input);
52          tokens = new CommonTokenStream(lexer);
53          CmisQlStrictParser parser = new CmisQlStrictParser(tokens);
54  
55          query_return parsedStatement = parser.query();
56          if (parser.hasErrors()) {
57              throw new CmisInvalidArgumentException(parser.getErrorMessages());
58          } else if (tokens.index() != tokens.size()) {
59              throw new CmisInvalidArgumentException("Query String has illegal tokens after end of statement: " + tokens.get(tokens.index()));
60          }
61          
62          parserTree = (CommonTree) parsedStatement.getTree();
63          return parserTree;
64      }
65      
66      @Override
67      public void walkStatement() throws RecognitionException {
68          
69          if (null == parserTree)
70              throw new CmisQueryException("You must parse the query before you can walk it.");
71          
72          CommonTreeNodeStream nodes = new CommonTreeNodeStream(parserTree);
73          nodes.setTokenStream(tokens);
74          walker = new CmisQueryWalker(nodes);
75          walker.setDoFullTextParse(parseFulltext);
76          walker.query(queryObj, predicateWalker);
77          walker.getWherePredicateTree();    
78      }
79  
80  }