You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2008/08/20 18:57:22 UTC

svn commit: r687386 - in /velocity/engine/trunk/src: java/org/apache/velocity/runtime/parser/node/NodeUtils.java java/org/apache/velocity/runtime/parser/node/SimpleNode.java test/org/apache/velocity/test/issues/Velocity355And552TestCase.java

Author: nbubna
Date: Wed Aug 20 09:57:07 2008
New Revision: 687386

URL: http://svn.apache.org/viewvc?rev=687386&view=rev
Log:
fix VELOCITY-355 and VELOCITY-552 by ensuring special tokens are included in literal output

Added:
    velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java   (with props)
Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SimpleNode.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java?rev=687386&r1=687385&r2=687386&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/NodeUtils.java Wed Aug 20 09:57:07 2008
@@ -35,6 +35,18 @@
 public class NodeUtils
 {
     /**
+     * @deprecated use getSpecialText(Token t)
+     */
+    public static String specialText(Token t)
+    {
+        if (t.specialToken == null || t.specialToken.image.startsWith("##") )
+        {
+            return "";
+        }
+        return getSpecialText(t).toString();
+    }
+
+    /**
      * Collect all the <SPECIAL_TOKEN>s that
      * are carried along with a token. Special
      * tokens do not participate in parsing but
@@ -42,16 +54,11 @@
      * In some cases you may want to retrieve these
      * special tokens, this is simply a way to
      * extract them.
-     * @param t
-     * @return String with the special tokens.
+     * @param t the Token
+     * @return StrBuilder with the special tokens.
      */
-    public static String specialText(Token t)
+    public static StrBuilder getSpecialText(Token t)
     {
-        if (t.specialToken == null || t.specialToken.image.startsWith("##") )
-        {
-            return "";
-        }
-
         StrBuilder sb = new StrBuilder();
 
         Token tmp_t = t.specialToken;
@@ -125,15 +132,13 @@
 
             tmp_t = tmp_t.next;
         }
-
-        return sb.toString();
+        return sb;
     }
 
     /**
      *  complete node literal
      * @param t
      * @return A node literal.
-     *
      */
     public static String tokenLiteral( Token t )
     {
@@ -142,9 +147,18 @@
         {
             return "";
         } 
+        else if (t.specialToken == null || t.specialToken.image.startsWith("##"))
+        {
+            return t.image;
+        }
         else 
         {
-            return specialText( t ) + t.image;
+            StrBuilder special = getSpecialText(t);
+            if (special.length() > 0)
+            {
+                return special.append(t.image).toString();
+            }
+            return t.image;
         }
     } 
     

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SimpleNode.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SimpleNode.java?rev=687386&r1=687385&r2=687386&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SimpleNode.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/SimpleNode.java Wed Aug 20 09:57:07 2008
@@ -257,15 +257,15 @@
         // buffer allocation. VELOCITY-606
         if (first == last)
         {
-            return first.image;
+            return NodeUtils.tokenLiteral(first);
         }
 
         Token t = first;
-        StrBuilder sb = new StrBuilder(t.image);
+        StrBuilder sb = new StrBuilder(NodeUtils.tokenLiteral(t));
         while (t != last)
         {
             t = t.next;
-            sb.append(t.image);
+            sb.append(NodeUtils.tokenLiteral(t));
         }
         return sb.toString();
     }

Added: velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java?rev=687386&view=auto
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java (added)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java Wed Aug 20 09:57:07 2008
@@ -0,0 +1,49 @@
+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.BaseEvalTestCase;
+
+/**
+ * This class tests VELOCITY-355 and its twin, VELOCITY-552.
+ */
+public class Velocity355And552TestCase extends BaseEvalTestCase
+{
+    public Velocity355And552TestCase(String name)
+    {
+       super(name);
+    }
+
+    public void testMissingDollar()
+    {
+        String base = "export $VAR=$(echo $testme)\n";
+        assertEvalEquals(base, base);
+        String literal = "#literal()\n"+base+"#end";
+        assertEvalEquals(base, literal);
+    }
+
+    public void testMissingPound()
+    {
+        String base = "#!/usr/bin/perl\n";
+        assertEvalEquals(base, base);
+        String literal = "#literal()\n"+base+"#end";
+        assertEvalEquals(base, literal);
+    }
+}

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity355And552TestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain