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=928463&r1=928462&r2=928463&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 Sun Mar 28 18:11:34 2010 @@ -106,13 +106,20 @@ public class ASTStringLiteral extends Si /* * get the contents of the string, minus the '/" at each end */ - - image = getFirstToken().image.substring(1, getFirstToken().image - .length() - 1); - if (getFirstToken().image.startsWith("\"")) + String img = getFirstToken().image; + + image = img.substring(1, img.length() - 1); + + if (img.startsWith("\"")) { image = unescape(image); } + if (img.charAt(0) == '"' || img.charAt(0) == '\'' ) + { + // replace double-double quotes like "" with a single double quote " + // replace double single quotes '' with a single quote ' + image = replaceQuotes(image); + } /** * note. A kludge on a kludge. The first part, Geir calls this the @@ -210,7 +217,34 @@ public class ASTStringLiteral extends Si tok = tok.next; } } + + /** + * Replaces double double-quotes with a single double quote ("" to ") + * Replaces double single quotes with a single quote ('' to ') + */ + private String replaceQuotes(String s) + { + if( s.indexOf("\"") == -1 && s.indexOf("'") == -1 ) + return s; + StrBuilder result = new StrBuilder(); + char prev = ' '; + for(int i = 0, is = s.length(); i < is; i++) + { + char c = s.charAt(i); + result.append(c); + + if( i + 1 < is ) + { + char next = s.charAt(i + 1); + if( (next == '"' && c == '"') || (next == '\'' && c == '\'') ) + { + i++; + } + } + } + return result.toString(); + } /** * @since 1.6
Modified: velocity/engine/trunk/src/parser/Parser.jjt URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/parser/Parser.jjt?rev=928463&r1=928462&r2=928463&view=diff ============================================================================== --- velocity/engine/trunk/src/parser/Parser.jjt (original) +++ velocity/engine/trunk/src/parser/Parser.jjt Sun Mar 28 18:11:34 2010 @@ -920,12 +920,13 @@ TOKEN : ("\"" ( (~["\""]) | ("\\" - ( ["n","t","b","r","f","\\","'","\""] + ( ["n","t","b","r","f"] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] | "u" ["0"-"9", "a"-"f", "A"-"F"] ["0"-"9", "a"-"f", "A"-"F"] ["0"-"9", "a"-"f", "A"-"F"] ["0"-"9", "a"-"f", "A"-"F"] ) ) + | ("\"\"") | ( "\\" (" ")* "\n") )* "\"" @@ -933,6 +934,7 @@ TOKEN : | ("\'" ( (~["\'"]) + | ("''") | ( "\\" (" ")* "\n") )* "\'" Added: velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity709TestCase.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity709TestCase.java?rev=928463&view=auto ============================================================================== --- velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity709TestCase.java (added) +++ velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity709TestCase.java Sun Mar 28 18:11:34 2010 @@ -0,0 +1,55 @@ +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-709. + */ +public class Velocity709TestCase extends BaseTestCase +{ + public Velocity709TestCase(String name) + { + super(name); + // DEBUG = true; + } + + public void testEscapedBackslashInSetDirective() + { + String backslash = "\\"; + String template = "#set($var = \"" + backslash + "\" )#set($var2 = \"${var}\")$var2"; + System.out.println(template); + assertEvalEquals("\\", template); + } + + public void testEscapedDoubleQuote() + { + String template = "#set($foo = \"jeah \"\"baby\"\" jeah! \"\"\"\"\")$foo"; + assertEvalEquals("jeah \"baby\" jeah! \"\"", template); + } + + public void testEscapedSingleQuote() + { + String template = "#set($foo = 'jeah ''baby'' jeah!')$foo"; + assertEvalEquals("jeah 'baby' jeah!", template); + } +}
