1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2009 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
20 // Since: Sep 29, 2009 2:26:59 PM
\r
24 //--------------------------------------
\r
25 package org.xerial.lens.relation.lang;
\r
27 import java.util.ArrayList;
\r
28 import java.util.List;
\r
30 import org.antlr.runtime.ANTLRStringStream;
\r
31 import org.antlr.runtime.CommonTokenStream;
\r
32 import org.antlr.runtime.RecognitionException;
\r
33 import org.antlr.runtime.tree.Tree;
\r
34 import org.xerial.core.XerialErrorCode;
\r
35 import org.xerial.core.XerialException;
\r
36 import org.xerial.lens.Lens;
\r
37 import org.xerial.lens.relation.query.impl.XPathLexer;
\r
38 import org.xerial.lens.relation.query.impl.XPathParser;
\r
39 import org.xerial.lens.relation.query.impl.XPathParser.xpath_return;
\r
40 import org.xerial.util.antlr.ANTLRUtil;
\r
41 import org.xerial.util.log.Logger;
\r
49 public class XPathExpr {
\r
51 private static Logger _logger = Logger.getLogger(XPathExpr.class);
\r
53 public static enum Axis {
\r
57 public static class Step {
\r
59 public Axis axis = Axis.Relative;
\r
60 public List<Step> step = new ArrayList<Step>();
\r
61 public List<Predicate> predicate = new ArrayList<Predicate>();
\r
64 public static class Predicate {
\r
70 public static XPathExpr parse(String xpathExpr) throws XerialException {
\r
71 XPathLexer lexer = new XPathLexer(new ANTLRStringStream(xpathExpr));
\r
72 CommonTokenStream token = new CommonTokenStream(lexer);
\r
73 XPathParser parser = new XPathParser(token);
\r
76 xpath_return r = parser.xpath();
\r
78 if (_logger.isDebugEnabled()) {
\r
79 // _logger.debug(ANTLRUtil.prettyPrintTokenList(token.getTokens(), ANTLRUtil
\r
80 // .getTokenTable(XPathLexer.class, "XPath.tokens")));
\r
81 _logger.debug(ANTLRUtil.parseTree((Tree) r.getTree(), XPathParser.tokenNames));
\r
84 return Lens.loadANTLRParseTree(XPathExpr.class, (Tree) r.getTree(),
\r
85 XPathParser.tokenNames);
\r
87 catch (RecognitionException e) {
\r
88 throw new XerialException(XerialErrorCode.PARSE_ERROR, e);
\r