View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath.ri;
18  
19  import java.io.StringReader;
20  
21  import org.apache.commons.jxpath.JXPathInvalidSyntaxException;
22  import org.apache.commons.jxpath.ri.parser.ParseException;
23  import org.apache.commons.jxpath.ri.parser.TokenMgrError;
24  import org.apache.commons.jxpath.ri.parser.XPathParser;
25  
26  /**
27   * XPath parser
28   *
29   * @author Dmitri Plotnikov
30   * @version $Revision: 652845 $ $Date: 2008-05-02 13:46:46 -0400 (Fri, 02 May 2008) $
31   */
32  public class Parser {
33  
34      private static XPathParser parser = new XPathParser(new StringReader(""));
35  
36      /**
37       * Parses the XPath expression. Throws a JXPathException in case
38       * of a syntax error.
39       * @param expression to parse
40       * @param compiler the compiler
41       * @return parsed Object
42       */
43      public static Object parseExpression(
44          String expression,
45          Compiler compiler) {
46          synchronized (parser) {
47              parser.setCompiler(compiler);
48              Object expr = null;
49              try {
50                  parser.ReInit(new StringReader(expression));
51                  expr = parser.parseExpression();
52              }
53              catch (TokenMgrError e) {
54                  throw new JXPathInvalidSyntaxException(
55                      "Invalid XPath: '"
56                          + addEscapes(expression)
57                          + "'. Invalid symbol '"
58                          + addEscapes(String.valueOf(e.getCharacter()))
59                          + "' "
60                          + describePosition(expression, e.getPosition()));
61              }
62              catch (ParseException e) {
63                  throw new JXPathInvalidSyntaxException(
64                      "Invalid XPath: '"
65                          + addEscapes(expression)
66                          + "'. Syntax error "
67                          + describePosition(
68                              expression,
69                              e.currentToken.beginColumn));
70              }
71              return expr;
72          }
73      }
74  
75      /**
76       * Describe a parse position.
77       * @param expression to parse
78       * @param position parse position
79       * @return String
80       */
81      private static String describePosition(String expression, int position) {
82          if (position <= 0) {
83              return "at the beginning of the expression";
84          }
85          if (position >= expression.length()) {
86              return "- expression incomplete";
87          }
88          return "after: '"
89              + addEscapes(expression.substring(0, position)) + "'";
90      }
91  
92      /**
93       * Add escapes to the specified String.
94       * @param string incoming String
95       * @return String
96       */
97      private static String addEscapes(String string) {
98          // Piggy-back on the code generated by JavaCC
99          return TokenMgrError.addEscapes(string);
100     }
101 }