01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.parser.sparql;
07:
08: import org.openrdf.query.MalformedQueryException;
09: import org.openrdf.query.parser.sparql.ast.ASTQueryContainer;
10: import org.openrdf.query.parser.sparql.ast.ASTString;
11: import org.openrdf.query.parser.sparql.ast.VisitorException;
12:
13: /**
14: * Processes escape sequences in strings, replacing the escape sequence with
15: * their actual value. Escape sequences for SPARQL are documented in section <a
16: * href="http://www.w3.org/TR/rdf-sparql-query/#grammarEscapes">A.7 Escape
17: * sequences in strings</a>.
18: *
19: * @author Arjohn Kampman
20: */
21: class StringEscapesProcessor {
22:
23: /**
24: * Processes escape sequences in ASTString objects.
25: *
26: * @param qc
27: * The query that needs to be processed.
28: * @throws MalformedQueryException
29: * If an invalid escape sequence was found.
30: */
31: public static void process(ASTQueryContainer qc)
32: throws MalformedQueryException {
33: StringProcessor visitor = new StringProcessor();
34: try {
35: qc.jjtAccept(visitor, null);
36: } catch (VisitorException e) {
37: throw new MalformedQueryException(e);
38: }
39: }
40:
41: private static class StringProcessor extends ASTVisitorBase {
42:
43: public StringProcessor() {
44: }
45:
46: @Override
47: public Object visit(ASTString stringNode, Object data)
48: throws VisitorException {
49: String value = stringNode.getValue();
50: try {
51: value = SPARQLUtil.decodeString(value);
52: stringNode.setValue(value);
53: } catch (IllegalArgumentException e) {
54: // Invalid escape sequence
55: throw new VisitorException(e.getMessage());
56: }
57:
58: return super.visit(stringNode, data);
59: }
60: }
61: }
|