This project has retired. For details please refer to its Attic page.
QueryUtil xref

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 java.io.IOException;
22  import java.io.UnsupportedEncodingException;
23  
24  import org.antlr.runtime.ANTLRStringStream;
25  import org.antlr.runtime.BaseRecognizer;
26  import org.antlr.runtime.CharStream;
27  import org.antlr.runtime.CommonTokenStream;
28  import org.antlr.runtime.RecognitionException;
29  import org.antlr.runtime.TokenSource;
30  import org.antlr.runtime.TokenStream;
31  import org.antlr.runtime.tree.CommonTree;
32  import org.antlr.runtime.tree.CommonTreeNodeStream;
33  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
34  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
35  import org.apache.chemistry.opencmis.server.support.query.CmisQlStrictParser_CmisBaseGrammar.query_return;
36  
37  /**
38   * Utility class providing convenience methods for parsing CMIS queries. 
39   *
40   */
41  public class QueryUtil {
42  
43      private CmisQueryWalker walker;
44  
45      // convenience method because everybody needs this piece of code
46      public static CmisQueryWalker getWalker(String statement) throws RecognitionException {
47          
48          CharStream input = new ANTLRStringStream(statement);
49          TokenSource lexer = new CmisQlStrictLexer(input);
50          TokenStream tokens = new CommonTokenStream(lexer);
51          CmisQlStrictParser parser = new CmisQlStrictParser(tokens);
52          CommonTree parserTree; // the ANTLR tree after parsing phase
53  
54          query_return parsedStatement = parser.query();
55          if (parser.hasErrors()) {
56              throw new CmisInvalidArgumentException(parser.getErrorMessages());
57          }
58          parserTree = (CommonTree) parsedStatement.getTree();
59  
60          CommonTreeNodeStream nodes = new CommonTreeNodeStream(parserTree);
61          nodes.setTokenStream(tokens);
62          CmisQueryWalker walker = new CmisQueryWalker(nodes);
63          return walker;
64      }
65  
66      public CmisQueryWalker traverseStatement(String statement, QueryObject queryObj, PredicateWalkerBase pw) throws UnsupportedEncodingException, IOException, RecognitionException {
67          walker = getWalker(statement);
68          walker.query(queryObj, pw);
69          walker.getWherePredicateTree();
70          return walker;        
71      }
72      
73      public CmisQueryWalker traverseStatementAndCatchExc(String statement, QueryObject queryObj, PredicateWalkerBase pw) {
74          try {
75              return traverseStatement(statement, queryObj, pw);
76          } catch (RecognitionException e) {
77              String errorMsg = queryObj.getErrorMessage();
78              throw new CmisInvalidArgumentException("Walking of statement failed with RecognitionException error: \n   " + errorMsg);
79          } catch (CmisBaseException e) {
80              throw e;
81          } catch (Exception e) {
82              throw new CmisInvalidArgumentException("Walking of statement failed with exception: \n   " + e);
83          }
84      }
85  
86      public String getErrorMessage(RecognitionException e) {
87          if (null == walker)
88              return e.toString();
89          else
90              return getErrorMessage(walker, e);
91      }
92      
93      private static String getErrorMessage(BaseRecognizer recognizer, RecognitionException e) {
94          String[] tokenNames = recognizer.getTokenNames();
95          // String hdr = walker.getErrorHeader(e);
96          String hdr = "Line "+e.line+":"+e.charPositionInLine;
97          String msg = recognizer.getErrorMessage(e, tokenNames);
98          return hdr + " " + msg;
99      }
100     
101 }