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.serql;
07:
08: import org.openrdf.query.MalformedQueryException;
09: import org.openrdf.query.parser.serql.ast.ASTLiteral;
10: import org.openrdf.query.parser.serql.ast.ASTQueryContainer;
11: import org.openrdf.query.parser.serql.ast.ASTString;
12: import org.openrdf.query.parser.serql.ast.VisitorException;
13:
14: /**
15: * Processes escape sequences in strings, replacing the escape sequence with
16: * their actual value. Escape sequences for SPARQL are documented in section <a
17: * href="http://www.w3.org/TR/rdf-sparql-query/#grammarEscapes">A.7 Escape
18: * sequences in strings</a>.
19: *
20: * @author Arjohn Kampman
21: */
22: class StringEscapesProcessor {
23:
24: /**
25: * Processes escape sequences in ASTString objects.
26: *
27: * @param qc
28: * The query that needs to be processed.
29: * @throws MalformedQueryException
30: * If an invalid escape sequence was found.
31: */
32: public static void process(ASTQueryContainer qc)
33: throws MalformedQueryException {
34: StringProcessor visitor = new StringProcessor();
35: try {
36: qc.jjtAccept(visitor, null);
37: } catch (VisitorException e) {
38: throw new MalformedQueryException(e.getMessage(), e);
39: }
40: }
41:
42: private static class StringProcessor extends ASTVisitorBase {
43:
44: public StringProcessor() {
45: }
46:
47: @Override
48: public Object visit(ASTString stringNode, Object data)
49: throws VisitorException {
50: String value = stringNode.getValue();
51: try {
52: value = SeRQLUtil.decodeString(value);
53: stringNode.setValue(value);
54: } catch (IllegalArgumentException e) {
55: // Invalid escape sequence
56: throw new VisitorException(e.getMessage());
57: }
58:
59: return super .visit(stringNode, data);
60: }
61:
62: @Override
63: public Object visit(ASTLiteral literalNode, Object data)
64: throws VisitorException {
65: String label = literalNode.getLabel();
66: try {
67: label = SeRQLUtil.decodeString(label);
68: literalNode.setLabel(label);
69: } catch (IllegalArgumentException e) {
70: // Invalid escape sequence
71: throw new VisitorException(e.getMessage());
72: }
73:
74: return super.visit(literalNode, data);
75: }
76: }
77: }
|