Project

General

Profile

1 1845 katerina.i
2
package eu.dnetlib.utils.cql;
3
4
import java.io.IOException;
5
import java.io.StringReader;
6
7
import org.antlr.runtime.ANTLRReaderStream;
8
import org.antlr.runtime.CommonTokenStream;
9
import org.antlr.runtime.RecognitionException;
10
11
12
/**
13
 * @author stoumpos
14
 *
15
 */
16
public class Cql {
17
18
	/**
19
	 * Parse cql query in <code>text</code> and return the respective cql
20
	 * query object.
21
	 *
22
	 * @param text
23
	 *            The cql text.
24
	 * @return The cql tree object.
25
	 */
26
	public static CqlQuery parse(String text) throws CqlException {
27
28
		try {
29
			StringReader reader = new StringReader(text);
30
			ANTLRReaderStream input = new ANTLRReaderStream(reader);
31
			CqlLexer lexer = new CqlLexer(input);
32
			CommonTokenStream tokens = new CommonTokenStream(lexer);
33
			CqlParser parser = new CqlParser(tokens);
34 3129 vassilis.s
35 1845 katerina.i
			CqlQuery query = parser.cql().query;
36 3129 vassilis.s
37 1845 katerina.i
			return query;
38
39 3129 vassilis.s
		} catch (IOException ioe) {
40
			throw new CqlException("Error building cql parser.", ioe);
41
42 1845 katerina.i
		} catch (RecognitionException re) {
43
			throw new CqlException("Error parsing cql query.", re);
44 3129 vassilis.s
45
		} catch (CqlRuntimeException cre) {
46
			throw new CqlException("Error parsing cql query.", cre.getCause());
47 1845 katerina.i
		}
48
	}
49
50
	/**
51
	 * Writes <code>query</code> as string. The same functionality is possible
52
	 * through {@link CqlQuery#toString()}.
53
	 *
54
	 * @param query
55
	 * @return
56
	 */
57
	public static String encode(CqlQuery query) {
58
		return query.toString();
59
	}
60
}