View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jcr.xpath;
25  
26  import javax.jcr.query.Query;
27  import org.modeshape.common.text.ParsingException;
28  import org.modeshape.graph.query.model.QueryCommand;
29  import org.modeshape.graph.query.model.TypeSystem;
30  import org.modeshape.graph.query.parse.InvalidQueryException;
31  import org.modeshape.graph.query.parse.QueryParser;
32  import org.modeshape.jcr.xpath.XPath.Component;
33  
34  /**
35   * A {@link QueryParser} implementation that accepts XPath expressions and converts them to a {@link QueryCommand ModeShape
36   * Abstract Query Model} representation.
37   */
38  public class XPathQueryParser implements QueryParser {
39  
40      static final boolean COLLAPSE_INNER_COMPONENTS = true;
41      @SuppressWarnings( "deprecation" )
42      private static final String LANGUAGE = Query.XPATH;
43  
44      /**
45       * {@inheritDoc}
46       * 
47       * @see org.modeshape.graph.query.parse.QueryParser#getLanguage()
48       */
49      public String getLanguage() {
50          return LANGUAGE;
51      }
52  
53      /**
54       * {@inheritDoc}
55       * 
56       * @see java.lang.Object#toString()
57       */
58      @Override
59      public String toString() {
60          return LANGUAGE;
61      }
62  
63      /**
64       * {@inheritDoc}
65       * 
66       * @see java.lang.Object#equals(java.lang.Object)
67       */
68      @Override
69      public boolean equals( Object obj ) {
70          if (obj == this) return true;
71          if (obj instanceof QueryParser) {
72              QueryParser that = (QueryParser)obj;
73              return this.getLanguage().equals(that.getLanguage());
74          }
75          return false;
76      }
77  
78      /**
79       * {@inheritDoc}
80       * 
81       * @see org.modeshape.graph.query.parse.QueryParser#parseQuery(java.lang.String, org.modeshape.graph.query.model.TypeSystem)
82       */
83      public QueryCommand parseQuery( String query,
84                                      TypeSystem typeSystem ) throws InvalidQueryException, ParsingException {
85          Component xpath = new XPathParser(typeSystem).parseXPath(query);
86          // Convert the result into a QueryCommand ...
87          QueryCommand command = new XPathToQueryTranslator(typeSystem, query).createQuery(xpath);
88          return command;
89      }
90  }