Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2008-04-14 14:28:06 -0400
committerpelder2008-04-14 14:28:06 -0400
commitd9781c8b59158d109fffe69d37e93138b8466243 (patch)
tree345c25346b07a62613a1d87c89ffa6a4b0e26289
parent3280930e23f7914775ba4b24e8467f5e72829dfb (diff)
downloadorg.eclipse.jet-d9781c8b59158d109fffe69d37e93138b8466243.tar.gz
org.eclipse.jet-d9781c8b59158d109fffe69d37e93138b8466243.tar.xz
org.eclipse.jet-d9781c8b59158d109fffe69d37e93138b8466243.zip
[226981] Support XPath quote escaping as in XPath 2.0
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathParser.java14
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathTokenScanner.java16
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/XPathTokenScannerTests.java14
3 files changed, 37 insertions, 7 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathParser.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathParser.java
index 1ee2a88..2f84af8 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathParser.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathParser.java
@@ -25,13 +25,13 @@ import org.eclipse.jet.internal.xpath.ast.Axis;
import org.eclipse.jet.internal.xpath.ast.BinaryOp;
import org.eclipse.jet.internal.xpath.ast.ContextNode;
import org.eclipse.jet.internal.xpath.ast.ExprNode;
+import org.eclipse.jet.internal.xpath.ast.FilterExpr;
import org.eclipse.jet.internal.xpath.ast.Function;
import org.eclipse.jet.internal.xpath.ast.LogicalOp;
import org.eclipse.jet.internal.xpath.ast.NodeSetCast;
import org.eclipse.jet.internal.xpath.ast.NodeSetExpr;
import org.eclipse.jet.internal.xpath.ast.NodeTest;
import org.eclipse.jet.internal.xpath.ast.NumberLiteral;
-import org.eclipse.jet.internal.xpath.ast.FilterExpr;
import org.eclipse.jet.internal.xpath.ast.Predicate;
import org.eclipse.jet.internal.xpath.ast.RelOp;
import org.eclipse.jet.internal.xpath.ast.Root;
@@ -529,7 +529,7 @@ public class XPathParser
if (peekNext() == XPathTokens.LITERAL)
{
String rawLiteral = consumeToken();
- literal = rawLiteral.substring(1, rawLiteral.length() - 1);
+ literal = unescape(rawLiteral.substring(1, rawLiteral.length() - 1), rawLiteral.charAt(0));
}
if (literal != null)
{
@@ -645,7 +645,15 @@ public class XPathParser
}
String literal = consumeToken();
- return new StringLiteral(literal.substring(1, literal.length() - 1));
+ return new StringLiteral(unescape(literal.substring(1, literal.length() - 1), literal.charAt(0)));
+ }
+
+ private String unescape(String substring, char quoteChar)
+ {
+ final String regexDQuote = "\"\""; //$NON-NLS-1$
+ final String regexSQuote = "''"; //$NON-NLS-1$
+ final String regexQuotes = quoteChar == '"' ? regexDQuote : regexSQuote;
+ return substring.replaceAll(regexQuotes, Character.toString(quoteChar));
}
public ExprNode variableReference() throws XPathSyntaxException
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathTokenScanner.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathTokenScanner.java
index 0925a4a..99306a9 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathTokenScanner.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/parser/XPathTokenScanner.java
@@ -92,8 +92,16 @@ public class XPathTokenScanner
return Token.UNDEFINED_TOKEN;
}
int ch = scanner.next();
- for (; ch != EOF && ch != first; ch = scanner.next())
+ for (; ch != EOF; ch = scanner.next())
{
+ if(ch == first) {
+ if(scanner.peek() == first) {
+ scanner.next(); // consume quote char and continue...
+ // escaped quote, continue...
+ continue;
+ }
+ break;
+ }
// next
}
if (ch != first)
@@ -284,6 +292,12 @@ public class XPathTokenScanner
}
+ public int peek()
+ {
+ int next = nextOffset < input.length() ? input.charAt(nextOffset) : EOF;
+ return next;
+ }
+
public void prev()
{
--nextOffset;
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/XPathTokenScannerTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/XPathTokenScannerTests.java
index 8524c87..03b7e65 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/XPathTokenScannerTests.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/XPathTokenScannerTests.java
@@ -123,7 +123,7 @@ public class XPathTokenScannerTests extends TestCase {
}
public void testNextToken_Literal_multi_strings_and_ws() throws XPathLexicalException {
- XPathTokenScanner scanner = new XPathTokenScanner(" 'abcde\"fgh' \"abcde'fgh\"\"abcde'fgh\" ");
+ XPathTokenScanner scanner = new XPathTokenScanner(" 'abcde\"fgh' \"abcde'fgh\"\"abcde'fgh\" 'abcde''fgh\"abcde''fgh' \"\" ''");
Token t1 = scanner.nextToken();
assertEquals(XPathTokens.LITERAL, t1);
@@ -131,11 +131,19 @@ public class XPathTokenScannerTests extends TestCase {
Token t2 = scanner.nextToken();
assertEquals(XPathTokens.LITERAL, t2);
- assertEquals("\"abcde'fgh\"", scanner.getTokenText());
+ assertEquals("\"abcde'fgh\"\"abcde'fgh\"", scanner.getTokenText());
Token t3 = scanner.nextToken();
assertEquals(XPathTokens.LITERAL, t3);
- assertEquals("\"abcde'fgh\"", scanner.getTokenText());
+ assertEquals("'abcde''fgh\"abcde''fgh'", scanner.getTokenText());
+
+ Token t4 = scanner.nextToken();
+ assertEquals(XPathTokens.LITERAL, t4);
+ assertEquals("\"\"", scanner.getTokenText());
+
+ Token t5 = scanner.nextToken();
+ assertEquals(XPathTokens.LITERAL, t5);
+ assertEquals("''", scanner.getTokenText());
}

Back to the top