001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.parser.sparql;
007:
008: import info.aduna.text.StringUtil;
009:
010: /**
011: * SPARQL-related utility methods.
012: *
013: * @author Arjohn Kampman
014: */
015: public class SPARQLUtil {
016:
017: /**
018: * Encodes the supplied string for inclusion as a 'normal' string in a SPARQL
019: * query.
020: */
021: public static String encodeString(String s) {
022: s = StringUtil.gsub("\\", "\\\\", s);
023: s = StringUtil.gsub("\t", "\\t", s);
024: s = StringUtil.gsub("\n", "\\n", s);
025: s = StringUtil.gsub("\r", "\\r", s);
026: s = StringUtil.gsub("\b", "\\b", s);
027: s = StringUtil.gsub("\f", "\\f", s);
028: s = StringUtil.gsub("\"", "\\\"", s);
029: s = StringUtil.gsub("'", "\\'", s);
030: return s;
031: }
032:
033: /**
034: * Decodes an encoded SPARQL string. Any \-escape sequences are substituted
035: * with their decoded value.
036: *
037: * @param s
038: * An encoded SPARQL string.
039: * @return The unencoded string.
040: * @exception IllegalArgumentException
041: * If the supplied string is not a correctly encoded SPARQL
042: * string.
043: */
044: public static String decodeString(String s) {
045: int backSlashIdx = s.indexOf('\\');
046:
047: if (backSlashIdx == -1) {
048: // No escaped characters found
049: return s;
050: }
051:
052: int startIdx = 0;
053: int sLength = s.length();
054: StringBuilder sb = new StringBuilder(sLength);
055:
056: while (backSlashIdx != -1) {
057: sb.append(s.substring(startIdx, backSlashIdx));
058:
059: if (backSlashIdx + 1 >= sLength) {
060: throw new IllegalArgumentException(
061: "Unescaped backslash in: " + s);
062: }
063:
064: char c = s.charAt(backSlashIdx + 1);
065:
066: if (c == 't') {
067: sb.append('\t');
068: startIdx = backSlashIdx + 2;
069: } else if (c == 'n') {
070: sb.append('\n');
071: startIdx = backSlashIdx + 2;
072: } else if (c == 'r') {
073: sb.append('\r');
074: startIdx = backSlashIdx + 2;
075: } else if (c == 'b') {
076: sb.append('\b');
077: startIdx = backSlashIdx + 2;
078: } else if (c == 'f') {
079: sb.append('\f');
080: startIdx = backSlashIdx + 2;
081: } else if (c == '"') {
082: sb.append('"');
083: startIdx = backSlashIdx + 2;
084: } else if (c == '\'') {
085: sb.append('\'');
086: startIdx = backSlashIdx + 2;
087: } else if (c == '\\') {
088: sb.append('\\');
089: startIdx = backSlashIdx + 2;
090: } else {
091: throw new IllegalArgumentException(
092: "Unescaped backslash in: " + s);
093: }
094:
095: backSlashIdx = s.indexOf('\\', startIdx);
096: }
097:
098: sb.append(s.substring(startIdx));
099:
100: return sb.toString();
101: }
102: }
|