Author: byron
Date: Sun Jan 11 02:55:13 2009
New Revision: 733448
URL: http://svn.apache.org/viewvc?rev=733448&view=rev
Log:
Check that macro arguments are valid during init instead of during render.
This check as added for VELOCITY-614
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java?rev=733448&r1=733447&r2=733448&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
Sun Jan 11 02:55:13 2009
@@ -24,6 +24,7 @@
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.runtime.log.Log;
import org.apache.velocity.runtime.parser.node.Node;
+import org.apache.velocity.runtime.parser.ParserTreeConstants;
import org.apache.velocity.runtime.parser.Token;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.RuntimeServices;
@@ -66,6 +67,14 @@
* Indicates if we are running in strict reference mode.
*/
protected boolean strictRef = false;
+
+ /**
+ * badArgsErrorMsg will be non null if the arguments to this macro
+ * are deamed bad at init time, see the init method. If his is non null,
then this macro
+ * cannot be rendered, and if there is an attempt to render we throw an
exception
+ * with this as the message.
+ */
+ private String badArgsErrorMsg = null;
/**
* Create a RuntimeMacro instance. Macro name and source
@@ -129,6 +138,26 @@
{
strictRef =
rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
}
+
+ // 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 < node.jjtGetNumChildren(); n++)
+ {
+ Node child = node.jjtGetChild(n);
+ if (child.getType() == ParserTreeConstants.JJTWORD)
+ {
+ badArgsErrorMsg = "Invalid arg '" +
child.getFirstToken().image
+ + "' in macro #" + macroName + " at " +
Log.formatFileString(child);
+
+ if (strictRef) // If strict, throw now
+ {
+ /* indicate col/line assuming it starts at 0
+ * this will be corrected one call up */
+ throw new TemplateInitException(badArgsErrorMsg,
+ context.getCurrentTemplateName(), 0, 0);
+ }
+ }
+ }
}
/**
@@ -234,6 +263,12 @@
+ Log.formatFileString(node), new Info(node));
}
+ if (badArgsErrorMsg != null)
+ {
+ throw new TemplateInitException(badArgsErrorMsg,
+ context.getCurrentTemplateName(), 0, 0);
+ }
+
try
{
return vmProxy.render(context, writer, node);
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=733448&r1=733447&r2=733448&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
Sun Jan 11 02:55:13 2009
@@ -247,7 +247,7 @@
/**
* check if we are calling this macro with the right number of arguments.
If
* we are not, and strictArguments is active, then throw
TemplateInitException.
- * This method must be thread safe.
+ * This method is called during macro render, so it must be thread safe.
*/
public void checkArgs(InternalContextAdapter context, Node node)
{
@@ -274,20 +274,6 @@
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/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=733448&r1=733447&r2=733448&view=diff
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
(original)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity614TestCase.java
Sun Jan 11 02:55:13 2009
@@ -20,6 +20,7 @@
*/
import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.TemplateInitException;
import org.apache.velocity.test.BaseEvalTestCase;
/**
@@ -83,7 +84,7 @@
public void testLateDefinedMacroWithBadArg()
{
String evil = "#evil(bar) #macro( evil $arg )$arg#end";
- assertEvalException(evil, ParseErrorException.class);
+ assertEvalException(evil, TemplateInitException.class);
}
}