Author: cbrisson
Date: Sat Nov  6 20:19:39 2010
New Revision: 1032134

URL: http://svn.apache.org/viewvc?rev=1032134&view=rev
Log:
applied Jarkko patch for VELOCITY-785

Added:
    
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity785TestCase.java
Modified:
    
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java

Modified: 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java?rev=1032134&r1=1032133&r2=1032134&view=diff
==============================================================================
--- 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java
 (original)
+++ 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java
 Sat Nov  6 20:19:39 2010
@@ -118,7 +118,7 @@ public class ASTStringLiteral extends Si
         {
             // replace double-double quotes like "" with a single double quote 
"
             // replace double single quotes '' with a single quote '
-            image = replaceQuotes(image);
+            image = replaceQuotes(image, img.charAt(0));
         }
 
         /**
@@ -219,15 +219,21 @@ public class ASTStringLiteral extends Si
     }
 
     /**
-     * Replaces double double-quotes with a single double quote ("" to ")
-     * Replaces double single quotes with a single quote ('' to ')
+     * Replaces double double-quotes with a single double quote ("" to ").
+     * Replaces double single quotes with a single quote ('' to ').
+        *
+        * @param s StringLiteral without the surrounding quotes
+        * @param literalQuoteChar char that starts the StringLiteral (" or ')
      */     
-    private String replaceQuotes(String s)
+    private String replaceQuotes(String s, char literalQuoteChar)
     {
-        if( s.indexOf("\"") == -1 && s.indexOf("'") == -1 )
+        if( (literalQuoteChar == '"' && s.indexOf("\"") == -1) ||
+            (literalQuoteChar == '\'' && s.indexOf("'") == -1) )
+        {
             return s;
+        }
     
-        StrBuilder result = new StrBuilder();
+        StrBuilder result = new StrBuilder(s.length());
         char prev = ' ';
         for(int i = 0, is = s.length(); i < is; i++)
         {
@@ -237,7 +243,11 @@ public class ASTStringLiteral extends Si
             if( i + 1 < is )
             {
                 char next =  s.charAt(i + 1);
-                if( (next == '"' && c == '"') || (next == '\'' && c == '\'') )
+                               // '""' -> "", "''" -> '' 
+                               // thus it is not necessary to double quotes if 
the "surrounding" quotes
+                               // of the StringLiteral are different. See 
VELOCITY-785
+                if( (literalQuoteChar == '"' && (next == '"' && c == '"')) || 
+                                   (literalQuoteChar == '\'' && (next == '\'' 
&& c == '\'')) )
                 {
                     i++;
                 }

Added: 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity785TestCase.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity785TestCase.java?rev=1032134&view=auto
==============================================================================
--- 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity785TestCase.java
 (added)
+++ 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity785TestCase.java
 Sat Nov  6 20:19:39 2010
@@ -0,0 +1,43 @@
+package org.apache.velocity.test.issues;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+import org.apache.velocity.test.BaseTestCase;
+import org.apache.velocity.exception.VelocityException;
+
+/**
+ * This class tests VELOCITY-785.
+ */
+public class Velocity785TestCase extends BaseTestCase
+{
+    public Velocity785TestCase(String name)
+    {
+        super(name);
+        // DEBUG = true;
+    }
+
+    public void testQuoteEscapes()
+    {
+        assertEvalEquals("\"", "#set($double_double = 
\"\"\"\")$double_double");
+        assertEvalEquals("'", "#set($single_single = '''')$single_single");
+        assertEvalEquals("''", "#set($double_single = \"''\")$double_single");
+        assertEvalEquals("\"\"", "#set($single_double = 
'\"\"')$single_double");
+    }
+}


Reply via email to