OSDN Git Service

imported from subversion repository
[xerial/xerial-core.git] / src / main / java / org / xerial / lens / relation / lang / XPathExpr.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2009 Taro L. Saito\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
17 // XerialJ\r
18 //\r
19 // XPathExpr.java\r
20 // Since: Sep 29, 2009 2:26:59 PM\r
21 //\r
22 // $URL$\r
23 // $Author$\r
24 //--------------------------------------\r
25 package org.xerial.lens.relation.lang;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.List;\r
29 \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
42 \r
43 /**\r
44  * XPath expression\r
45  * \r
46  * @author leo\r
47  * \r
48  */\r
49 public class XPathExpr {\r
50 \r
51     private static Logger _logger = Logger.getLogger(XPathExpr.class);\r
52 \r
53     public static enum Axis {\r
54         Relative, PC, AD\r
55     }\r
56 \r
57     public static class Step {\r
58         public String name;\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
62     }\r
63 \r
64     public static class Predicate {\r
65         public Step step;\r
66     }\r
67 \r
68     public Step step;\r
69 \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
74 \r
75         try {\r
76             xpath_return r = parser.xpath();\r
77 \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
82             }\r
83 \r
84             return Lens.loadANTLRParseTree(XPathExpr.class, (Tree) r.getTree(),\r
85                     XPathParser.tokenNames);\r
86         }\r
87         catch (RecognitionException e) {\r
88             throw new XerialException(XerialErrorCode.PARSE_ERROR, e);\r
89         }\r
90 \r
91     }\r
92 }\r