Author: nbubna
Date: Thu Oct 16 10:59:24 2008
New Revision: 705297

URL: http://svn.apache.org/viewvc?rev=705297&view=rev
Log:
VELOCITY-629 fix line/col numbers within string literals (thx to Byron Foster)

Added:
    
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity629TestCase.java
   (with props)
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=705297&r1=705296&r2=705297&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
 Thu Oct 16 10:59:24 2008
@@ -32,6 +32,8 @@
 import org.apache.velocity.runtime.log.Log;
 import org.apache.velocity.runtime.parser.ParseException;
 import org.apache.velocity.runtime.parser.Parser;
+import org.apache.velocity.runtime.parser.Token;
+import org.apache.velocity.runtime.visitor.BaseVisitor;
 
 /**
  * ASTStringLiteral support. Will interpolate!
@@ -172,6 +174,8 @@
                 throw new TemplateInitException(msg, e, templateName, 
getColumn(), getLine());
             }
 
+            adjTokenLineNums(nodeTree);
+            
             /*
              * init with context. It won't modify anything
              */
@@ -181,7 +185,37 @@
 
         return data;
     }
-
+    
+    /**
+     * Adjust all the line and column numbers that comprise a node so that they
+     * are corrected for the string literals position within the template file.
+     * This is neccessary if an exception is thrown while processing the node 
so
+     * that the line and column position reported reflects the error position
+     * within the template and not just relative to the error position within
+     * the string literal.
+     */
+    public void adjTokenLineNums(Node node)
+    {
+        Token tok = node.getFirstToken();
+        // Test against null is probably not neccessary, but just being safe
+        while(tok != null && tok != node.getLastToken())
+        {
+            // If tok is on the first line, then the actual column is 
+            // offset by the template column.
+          
+            if (tok.beginLine == 1)
+                tok.beginColumn += getColumn();
+            
+            if (tok.endLine == 1)
+                tok.endColumn += getColumn();
+            
+            tok.beginLine += getLine()- 1;
+            tok.endLine += getLine() - 1;
+            tok = tok.next;
+        }
+    }
+    
+    
     /**
      * @since 1.6
      */

Added: 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity629TestCase.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity629TestCase.java?rev=705297&view=auto
==============================================================================
--- 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity629TestCase.java
 (added)
+++ 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity629TestCase.java
 Thu Oct 16 10:59:24 2008
@@ -0,0 +1,62 @@
+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;
+import org.apache.velocity.runtime.RuntimeConstants;
+
+/**
+ * This class tests VELOCITY-629.  Make sure string literals
+ * Error message reports correct line and column numbers.
+ */
+public class Velocity629TestCase extends BaseEvalTestCase
+{
+    public Velocity629TestCase(String name)
+    {
+        super(name);
+        DEBUG = true;
+    }
+  
+    public void test629()
+    {
+        String template = "##\n"+
+                          "##\n"+
+                          "#set($list=[1])#set($x=\"\n"+
+                          "$list.get(1)\n"+
+                          "\")";
+        Exception e = assertEvalException(template);
+        // Make sure the error ouput contains "line 4" if not throw
+        assertTrue(e.getMessage().indexOf("[line 4, column 7]") > -1);
+
+        template = "##\n"+
+                   "##\n"+
+                   "#set($x=\"#if\")";
+        e = assertEvalException(template);
+        // Make sure the error ouput contains "line 3" if not throw
+        assertTrue(e.getMessage().indexOf("[line 3, column 9]") > -1);
+
+        template = "##\n"+
+                   "##\n"+
+                   "#macro(test 
$i)$i#end#set($list=[1])#test(\"$list.get(1)\")";
+        e = assertEvalException(template);
+        // Make sure the error ouput contains "line 3" if not throw
+        assertTrue(e.getMessage().indexOf("[line 3, column 50]") > -1);
+    }
+}

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

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

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


Reply via email to