jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r328092 - in /incubator/jackrabbit/trunk/src: java/org/apache/jackrabbit/core/query/xpath/XPathQueryBuilder.java test/org/apache/jackrabbit/core/query/SimpleQueryTest.java
Date Mon, 24 Oct 2005 16:44:44 GMT
Author: mreutegg
Date: Mon Oct 24 09:44:21 2005
New Revision: 328092

URL: http://svn.apache.org/viewcvs?rev=328092&view=rev
Log:
- quotes are not unescaped in string literal when using jcr:like

Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/xpath/XPathQueryBuilder.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/query/SimpleQueryTest.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/xpath/XPathQueryBuilder.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/xpath/XPathQueryBuilder.java?rev=328092&r1=328091&r2=328092&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/xpath/XPathQueryBuilder.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/xpath/XPathQueryBuilder.java
Mon Oct 24 09:44:21 2005
@@ -634,13 +634,7 @@
      */
     private void assignValue(SimpleNode node, RelationQueryNode queryNode) {
         if (node.getId() == JJTSTRINGLITERAL) {
-            String value = node.getValue().substring(1, node.getValue().length() - 1);
-            if (node.getValue().charAt(0) == '"') {
-                value = value.replaceAll("\"\"", "\"");
-            } else {
-                value = value.replaceAll("''", "'");
-            }
-            queryNode.setStringValue(value);
+            queryNode.setStringValue(unescapeQuotes(node.getValue()));
         } else if (node.getId() == JJTDECIMALLITERAL) {
             queryNode.setDoubleValue(Double.parseDouble(node.getValue()));
         } else if (node.getId() == JJTDOUBLELITERAL) {
@@ -718,15 +712,8 @@
                     if (queryNode instanceof NAryQueryNode) {
                         SimpleNode literal = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0);
                         if (literal.getId() == JJTSTRINGLITERAL) {
-                            String value = literal.getValue();
-                            if (value.charAt(0) == '"') {
-                                value = value.replaceAll("\"\"", "\"");
-                            } else {
-                                value = value.replaceAll("''", "'");
-                            }
-                            // strip quotes
-                            value = value.substring(1, value.length() - 1);
-                            TextsearchQueryNode contains = new TextsearchQueryNode(queryNode,
value);
+                            TextsearchQueryNode contains = new TextsearchQueryNode(queryNode,
+                                    unescapeQuotes(literal.getValue()));
                             // assign property name
                             SimpleNode path = (SimpleNode) node.jjtGetChild(1);
                             path.jjtAccept(this, contains);
@@ -755,10 +742,7 @@
 
                         SimpleNode literal = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0);
                         if (literal.getId() == JJTSTRINGLITERAL) {
-                            String value = literal.getValue();
-                            // strip quotes
-                            value = value.substring(1, value.length() - 1);
-                            like.setStringValue(value);
+                            like.setStringValue(unescapeQuotes(literal.getValue()));
                         } else {
                             exceptions.add(new InvalidQueryException("Wrong second argument
type for jcr:like"));
                         }
@@ -904,4 +888,31 @@
         return false;
     }
 
+    /**
+     * Unescapes single or double quotes depending on how <code>literal</code>
+     * is enclosed and strips enclosing quotes.
+     *
+     * </p>
+     * Examples:</br>
+     * <code>"foo""bar"</code> -&gt; <code>foo"bar</code></br>
+     * <code>'foo''bar'</code> -&gt; <code>foo'bar</code></br>
+     * but:</br>
+     * <code>'foo""bar'</code> -&gt; <code>foo""bar</code>
+     *
+     * @param literal the string literal to unescape
+     * @return the unescaped and stripped literal.
+     */
+    private String unescapeQuotes(String literal) {
+        String value = literal.substring(1, literal.length() - 1);
+        if (value.length() == 0) {
+            // empty string
+            return value;
+        }
+        if (literal.charAt(0) == '"') {
+            value = value.replaceAll("\"\"", "\"");
+        } else {
+            value = value.replaceAll("''", "'");
+        }
+        return value;
+    }
 }

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/query/SimpleQueryTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/query/SimpleQueryTest.java?rev=328092&r1=328091&r2=328092&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/query/SimpleQueryTest.java
(original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/query/SimpleQueryTest.java
Mon Oct 24 09:44:21 2005
@@ -369,6 +369,33 @@
         checkResult(result, 2);
     }
 
+    public void testQuotes() throws Exception {
+        Node foo = testRootNode.addNode("foo");
+        foo.setProperty("foo", "bar'bar");
+
+        testRootNode.save();
+
+        String sql = "SELECT * FROM nt:unstructured WHERE foo = 'bar''bar'";
+        Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
+        QueryResult result = q.execute();
+        checkResult(result, 1);
+
+        String xpath = "//*[@jcr:primaryType='nt:unstructured' and @foo ='bar''bar']";
+        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);
+        result = q.execute();
+        checkResult(result, 1);
+
+        xpath = "//*[@jcr:primaryType='nt:unstructured' and jcr:like(@foo,'%ar''ba%')]";
+        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);
+        result = q.execute();
+        checkResult(result, 1);
+
+        xpath = "//*[@jcr:primaryType='nt:unstructured' and jcr:like(@foo,\"%ar'ba%\")]";
+        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);
+        result = q.execute();
+        checkResult(result, 1);
+    }
+
     public void testGeneralComparison() throws Exception {
         Node foo = testRootNode.addNode("foo");
         foo.setProperty("text", new String[]{"foo", "bar"}); // mvp



Mime
View raw message