Author: nbubna
Date: Fri Sep 19 13:49:31 2008
New Revision: 697234
URL: http://svn.apache.org/viewvc?rev=697234&view=rev
Log:
VELOCITY-614 make #schmoo(Blah) work again
Added:
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
(with props)
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj
velocity/engine/trunk/src/parser/Parser.jjt
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java?rev=697234&r1=697233&r2=697234&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
Fri Sep 19 13:49:31 2008
@@ -31,6 +31,7 @@
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.runtime.parser.ParserTreeConstants;
import org.apache.velocity.runtime.parser.node.ASTDirective;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;
@@ -303,6 +304,20 @@
return;
}
}
+
+ /* now validate that none of the arguments are plain words,
(VELOCITY-614)
+ * they should be string literals, references, inline maps, or inline
lists */
+ for (int n=0; n < i; n++)
+ {
+ Node child = node.jjtGetChild(n);
+ if (child.getType() == ParserTreeConstants.JJTWORD)
+ {
+ /* indicate col/line assuming it starts at 0
+ * this will be corrected one call up */
+ throw new TemplateInitException("Invalid arg #"
+ + n + " in VM #" + macroName,
context.getCurrentTemplateName(), 0, 0);
+ }
+ }
}
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java?rev=697234&r1=697233&r2=697234&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java
Fri Sep 19 13:49:31 2008
@@ -110,7 +110,7 @@
* thrown by the Macro class when something is amiss in the
* Macro specification
*/
- rsvc.getLog().error("Parser Error: #macro() : " + templateName,
mee);
+ rsvc.getLog().error("Parser Error: " + templateName, mee);
throw mee;
}
catch (ParseException pe)
@@ -785,20 +785,23 @@
{
if (doItNow && argPos == 0)
{
- /* if a VM and it's the 0th arg... ok */
- ;
+ /* if #macro and it's the 0th arg, ok */
}
- else if( (t.image.equals("#foreach") ||
t.image.equals("#{foreach}") ) && argPos == 1)
+ else if (isVM)
{
- /* if a foreach and it's the 2nd arg ok */
- ;
+ throw new MacroParseException("Invalid arg #"
+ + argPos + " in VM " + t.image, currentTemplateName,
t);
+ }
+ /* if #foreach and it's the 2nd arg,
ok */
+ else if (d != null && (!directiveName.equals("foreach") ||
argPos != 1))
+ {
+ throw new MacroParseException("Invalid arg #"
+ + argPos + " in directive " + t.image,
currentTemplateName, t);
}
else
{
- {if (true) throw new MacroParseException("Invalid arg
#"
- + argPos + " in "
- + (isVM ? "VM " : "directive " )
- + t.image, currentTemplateName, t);}
+ /* either schmoo or a late-defined macro,
+ * VelocimacroProxy will have to check for latter. */
}
}
else
@@ -807,9 +810,9 @@
{
/* if a VM and it's the 0th arg, not ok */
- {if (true) throw new MacroParseException("Invalid
first arg"
- + " in #macro() directive - must be a"
- + " word token (no \' or \" surrounding)",
currentTemplateName, t);}
+ throw new MacroParseException("Invalid first arg"
+ + " in #macro() directive - must be a"
+ + " word token (no \' or \" surrounding)",
currentTemplateName, t);
}
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj?rev=697234&r1=697233&r2=697234&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj
Fri Sep 19 13:49:31 2008
@@ -166,7 +166,7 @@
* thrown by the Macro class when something is amiss in the
* Macro specification
*/
- rsvc.getLog().error("Parser Error: #macro() : " + templateName,
mee);
+ rsvc.getLog().error("Parser Error: " + templateName, mee);
throw mee;
}
catch (ParseException pe)
@@ -1540,20 +1540,23 @@
{
if (doItNow && argPos == 0)
{
- /* if a VM and it's the 0th arg... ok */
- ;
+ /* if #macro and it's the 0th arg, ok */
}
- else if( (t.image.equals("#foreach") ||
t.image.equals("#{foreach}") ) && argPos == 1)
+ else if (isVM)
{
- /* if a foreach and it's the 2nd arg ok */
- ;
+ throw new MacroParseException("Invalid arg #"
+ + argPos + " in VM " + t.image, currentTemplateName,
t);
}
- else
+ /* if #foreach and it's the 2nd arg,
ok */
+ else if (d != null && (!directiveName.equals("foreach") ||
argPos != 1))
{
throw new MacroParseException("Invalid arg #"
- + argPos + " in "
- + (isVM ? "VM " : "directive " )
- + t.image, currentTemplateName, t);
+ + argPos + " in directive " + t.image,
currentTemplateName, t);
+ }
+ else
+ {
+ /* either schmoo or a late-defined macro,
+ * VelocimacroProxy will have to check for latter. */
}
}
else
@@ -1563,8 +1566,8 @@
/* if a VM and it's the 0th arg, not ok */
throw new MacroParseException("Invalid first arg"
- + " in #macro() directive - must be a"
- + " word token (no \' or \" surrounding)",
currentTemplateName, t);
+ + " in #macro() directive - must be a"
+ + " word token (no \' or \" surrounding)",
currentTemplateName, t);
}
}
Modified: velocity/engine/trunk/src/parser/Parser.jjt
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/parser/Parser.jjt?rev=697234&r1=697233&r2=697234&view=diff
==============================================================================
--- velocity/engine/trunk/src/parser/Parser.jjt (original)
+++ velocity/engine/trunk/src/parser/Parser.jjt Fri Sep 19 13:49:31 2008
@@ -187,7 +187,7 @@
* thrown by the Macro class when something is amiss in the
* Macro specification
*/
- rsvc.getLog().error("Parser Error: #macro() : " + templateName,
mee);
+ rsvc.getLog().error("Parser Error: " + templateName, mee);
throw mee;
}
catch (ParseException pe)
@@ -1419,20 +1419,23 @@
{
if (doItNow && argPos == 0)
{
- /* if a VM and it's the 0th arg... ok */
- ;
+ /* if #macro and it's the 0th arg, ok */
}
- else if( (t.image.equals("#foreach") ||
t.image.equals("#{foreach}") ) && argPos == 1)
+ else if (isVM)
{
- /* if a foreach and it's the 2nd arg ok */
- ;
+ throw new MacroParseException("Invalid arg #"
+ + argPos + " in VM " + t.image, currentTemplateName,
t);
}
- else
+ /* if #foreach and it's the 2nd arg,
ok */
+ else if (d != null && (!directiveName.equals("foreach") ||
argPos != 1))
{
throw new MacroParseException("Invalid arg #"
- + argPos + " in "
- + (isVM ? "VM " : "directive " )
- + t.image, currentTemplateName, t);
+ + argPos + " in directive " + t.image,
currentTemplateName, t);
+ }
+ else
+ {
+ /* either schmoo or a late-defined macro,
+ * VelocimacroProxy will have to check for latter. */
}
}
else
@@ -1442,8 +1445,8 @@
/* if a VM and it's the 0th arg, not ok */
throw new MacroParseException("Invalid first arg"
- + " in #macro() directive - must be a"
- + " word token (no \' or \" surrounding)",
currentTemplateName, t);
+ + " in #macro() directive - must be a"
+ + " word token (no \' or \" surrounding)",
currentTemplateName, t);
}
}
Added:
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java?rev=697234&view=auto
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
(added)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
Fri Sep 19 13:49:31 2008
@@ -0,0 +1,74 @@
+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.exception.ParseErrorException;
+import org.apache.velocity.test.BaseEvalTestCase;
+
+/**
+ * This class tests VELOCITY-614.
+ */
+public class Velocity614TestCase extends BaseEvalTestCase
+{
+ public Velocity614TestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testSchmoo()
+ {
+ String template = "#something(Stuff)";
+ assertEvalEquals(template, template);
+ }
+
+ public void testEarlyDefinedMacroWithBadArg()
+ {
+ assertEvalException("#macro( evil $arg )$arg#end #evil(bar)");
+ }
+
+ // just make sure this doesn't get broken
+ public void testLateDefinedMacroWithGoodArg()
+ {
+ String good = "#good('bar') #macro( good $arg )$arg#end";
+ assertEvalEquals("bar ", good);
+ }
+
+ public void testDirectivesWithBadArg()
+ {
+ // make sure these all still bomb, but don't spam sysout
+ log.off();
+ assertEvalException("#foreach(Stuff in That)foo#end");
+ assertEvalException("#include(Stuff)");
+ assertEvalException("#parse(Stuff)");
+ assertEvalException("#define(Stuff)foo#end");
+ assertEvalException("#macro( name Stuff)foo#end");
+ assertEvalException("#foreach($i in [1..3])#break(Stuff)#end");
+ assertEvalException("#literal(Stuff)foo#end");
+ assertEvalException("#evaluate(Stuff)", ParseErrorException.class);
+ log.on();
+ }
+
+ public void testLateDefinedMacroWithBadArg()
+ {
+ String evil = "#evil(bar) #macro( evil $arg )$arg#end";
+ assertEvalException(evil, ParseErrorException.class);
+ }
+
+}
Propchange:
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
------------------------------------------------------------------------------
svn:executable = *
Propchange:
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
------------------------------------------------------------------------------
svn:keywords = Revision
Propchange:
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain