Added: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/query/QueryUtil.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/query/QueryUtil.java?rev=1026325&view=auto ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/query/QueryUtil.java (added) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/query/QueryUtil.java Fri Oct 22 13:52:33 2010 @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.chemistry.opencmis.server.support.query; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +import org.antlr.runtime.ANTLRInputStream; +import org.antlr.runtime.CharStream; +import org.antlr.runtime.CommonTokenStream; +import org.antlr.runtime.RecognitionException; +import org.antlr.runtime.TokenSource; +import org.antlr.runtime.TokenStream; +import org.antlr.runtime.tree.CommonTree; +import org.antlr.runtime.tree.CommonTreeNodeStream; +import org.apache.chemistry.opencmis.server.support.query.CmisQlStrictParser_CmisBaseGrammar.query_return; + +/** + * Class with static methods that contains convenience methods to repeating functionality + * in context with query and ANTLR parsing + * + */ +public class QueryUtil extends QueryObject { + + // convenience method because everybody needs this piece of code + static public CmisQueryWalker getWalker(String statement) throws UnsupportedEncodingException, IOException, RecognitionException { + CharStream input = new ANTLRInputStream(new ByteArrayInputStream(statement.getBytes("UTF-8"))); + TokenSource lexer = new CmisQlStrictLexer(input); + TokenStream tokens = new CommonTokenStream(lexer); + CmisQlStrictParser parser = new CmisQlStrictParser(tokens); + CommonTree parserTree; // the ANTLR tree after parsing phase + + query_return parsedStatement = parser.query(); + parserTree = (CommonTree) parsedStatement.getTree(); + + CommonTreeNodeStream nodes = new CommonTreeNodeStream(parserTree); + nodes.setTokenStream(tokens); + CmisQueryWalker walker = new CmisQueryWalker(nodes); + return walker; + } +}
