001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.parser;
007:
008: import org.openrdf.query.MalformedQueryException;
009: import org.openrdf.query.QueryLanguage;
010: import org.openrdf.query.UnsupportedQueryLanguageException;
011:
012: /**
013: * Utility class for creating query parsers and parsing queries in various query
014: * languages.
015: */
016: public class QueryParserUtil {
017:
018: public static QueryParser createParser(QueryLanguage ql)
019: throws UnsupportedQueryLanguageException {
020: QueryParserFactory factory = QueryParserRegistry.getInstance()
021: .get(ql);
022:
023: if (factory != null) {
024: return factory.getParser();
025: }
026:
027: throw new UnsupportedQueryLanguageException(
028: "No factory available for query language " + ql);
029: }
030:
031: /**
032: * Parses the supplied query into a query model.
033: *
034: * @param ql
035: * The language in which the query is formulated.
036: * @param query
037: * The query.
038: * @param baseURI
039: * The base URI to resolve any relative URIs that are in the query
040: * against, can be <tt>null</tt> if the query does not contain any
041: * relative URIs.
042: * @return The query model for the parsed query.
043: * @throws MalformedQueryException
044: * If the supplied query was malformed.
045: * @throws UnsupportedQueryLanguageException
046: * If the specified query language is not supported.
047: */
048: public static ParsedQuery parseQuery(QueryLanguage ql,
049: String query, String baseURI)
050: throws MalformedQueryException,
051: UnsupportedQueryLanguageException {
052: QueryParser parser = createParser(ql);
053: return parser.parseQuery(query, baseURI);
054: }
055:
056: /**
057: * Parses the supplied query into a query model.
058: *
059: * @param ql
060: * The language in which the query is formulated.
061: * @param query
062: * The query.
063: * @return The query model for the parsed query.
064: * @throws IllegalArgumentException
065: * If the supplied query is not a tuple query.
066: * @throws MalformedQueryException
067: * If the supplied query was malformed.
068: * @throws UnsupportedQueryLanguageException
069: * If the specified query language is not supported.
070: */
071: public static ParsedTupleQuery parseTupleQuery(QueryLanguage ql,
072: String query, String baseURI)
073: throws MalformedQueryException,
074: UnsupportedQueryLanguageException {
075: ParsedQuery q = parseQuery(ql, query, baseURI);
076:
077: if (q instanceof ParsedTupleQuery) {
078: return (ParsedTupleQuery) q;
079: }
080:
081: throw new IllegalArgumentException(
082: "query is not a tuple query: " + query);
083: }
084:
085: /**
086: * Parses the supplied query into a query model.
087: *
088: * @param ql
089: * The language in which the query is formulated.
090: * @param query
091: * The query.
092: * @return The query model for the parsed query.
093: * @throws IllegalArgumentException
094: * If the supplied query is not a graph query.
095: * @throws MalformedQueryException
096: * If the supplied query was malformed.
097: * @throws UnsupportedQueryLanguageException
098: * If the specified query language is not supported.
099: */
100: public static ParsedGraphQuery parseGraphQuery(QueryLanguage ql,
101: String query, String baseURI)
102: throws MalformedQueryException,
103: UnsupportedQueryLanguageException {
104: ParsedQuery q = parseQuery(ql, query, baseURI);
105:
106: if (q instanceof ParsedGraphQuery) {
107: return (ParsedGraphQuery) q;
108: }
109:
110: throw new IllegalArgumentException(
111: "query is not a graph query: " + query);
112: }
113:
114: /**
115: * Parses the supplied query into a query model.
116: *
117: * @param ql
118: * The language in which the query is formulated.
119: * @param query
120: * The query.
121: * @return The query model for the parsed query.
122: * @throws IllegalArgumentException
123: * If the supplied query is not a graph query.
124: * @throws MalformedQueryException
125: * If the supplied query was malformed.
126: * @throws UnsupportedQueryLanguageException
127: * If the specified query language is not supported.
128: */
129: public static ParsedBooleanQuery parseBooleanQuery(
130: QueryLanguage ql, String query, String baseURI)
131: throws MalformedQueryException,
132: UnsupportedQueryLanguageException {
133: ParsedQuery q = parseQuery(ql, query, baseURI);
134:
135: if (q instanceof ParsedBooleanQuery) {
136: return (ParsedBooleanQuery) q;
137: }
138:
139: throw new IllegalArgumentException(
140: "query is not a boolean query: " + query);
141: }
142: }
|