You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2018/10/15 13:08:41 UTC
svn commit: r1843909 - in /velocity/engine/trunk: src/changes/
velocity-engine-core/src/main/java/org/apache/velocity/runtime/
velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/
velocity-engine-core/src/main/java/org/apache/veloc...
Author: cbrisson
Date: Mon Oct 15 13:08:41 2018
New Revision: 1843909
URL: http://svn.apache.org/viewvc?rev=1843909&view=rev
Log:
[VELOCITY-904] Added velocimacro.preserve.arguments.literals boolean flag (false by default) - if true, when printing null arguments, the macros will use the provided arguments literals
Added:
velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java
- copied, changed from r1843856, velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java
Modified:
velocity/engine/trunk/src/changes/changes.xml
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
Modified: velocity/engine/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/changes/changes.xml?rev=1843909&r1=1843908&r2=1843909&view=diff
==============================================================================
--- velocity/engine/trunk/src/changes/changes.xml (original)
+++ velocity/engine/trunk/src/changes/changes.xml Mon Oct 15 13:08:41 2018
@@ -26,7 +26,12 @@
<body>
<release version="2.1" date="In subversion">
- <action type="add" dev="cbrisson" issue="VELOCITY-906">
+ <action type="add" dev="cbrisson" issue="VELOCITY-904">
+ Added the <code>velocimacro.preserve.arguments.literals</code> flag (false by default), for backward compatibility.
+ When true, for any macro argument that evaluates to null, the macro will display the provided argument literal representation
+ instead of the literal argument reference.
+ </action>
+ <action type="add" dev="cbrisson" issue="VELOCITY-903">
Default block for empty loops: <code>#foreach($i in []) loop block #else empty #end</code>
</action>
<action type="fix" dev="cbrisson" issue="VELOCITY-888">
Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=1843909&r1=1843908&r2=1843909&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java Mon Oct 15 13:08:41 2018
@@ -227,6 +227,13 @@ public interface RuntimeConstants
*/
String VM_BODY_REFERENCE = "velocimacro.body.reference";
+ /**
+ * When displaying null or invalid non-quiet references, use the argument literal reference
+ * instead of the one in the macro block. Defaults to false.
+ * @since 2.1
+ **/
+ String VM_PRESERVE_ARGUMENTS_LITERALS = "velocimacro.preserve.arguments.literals";
+
/*
* ----------------------------------------------------------------------
* S T I C T M O D E B E H A V I O U R
@@ -289,16 +296,19 @@ public interface RuntimeConstants
/**
* Allow dash in identifiers (backward compatibility option)
+ * @since 2.1
*/
String PARSER_DASH_ALLOWED = "parser.allows.dash.in.identifiers";
/**
* Space gobbling mode
+ * @since 2.0
*/
String SPACE_GOBBLING = "space.gobbling";
/**
* Space gobbling modes
+ * @since 2.0
*/
enum SpaceGobbling
{
Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java?rev=1843909&r1=1843908&r2=1843909&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java Mon Oct 15 13:08:41 2018
@@ -323,11 +323,11 @@ public class VelocimacroManager
this.sourceTemplate = sourceTemplate;
vp = new VelocimacroProxy();
+ vp.init(rsvc);
vp.setName(this.vmName);
vp.setMacroArgs(this.macroArgs);
vp.setNodeTree(this.nodeTree);
vp.setLocation(macro.getLine(), macro.getColumn(), macro.getTemplate());
- vp.init(rsvc);
}
/**
Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java?rev=1843909&r1=1843908&r2=1843909&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java Mon Oct 15 13:08:41 2018
@@ -52,6 +52,7 @@ public class VelocimacroProxy extends Di
private boolean strictArguments;
private int maxCallDepth;
private String bodyReference;
+ private boolean preserveArgumentsLiterals;
/**
* Return name of this Velocimacro.
@@ -92,10 +93,13 @@ public class VelocimacroProxy extends Di
// for performance reasons we precache these strings - they are needed in
// "render literal if null" functionality
- literalArgArray = new String[macroArgs.size()];
- for(int i = 0; i < macroArgs.size(); i++)
+ if (preserveArgumentsLiterals)
{
- literalArgArray[i] = ".literal.$" + macroArgs.get(i);
+ literalArgArray = new String[macroArgs.size()];
+ for (int i = 0; i < macroArgs.size(); i++)
+ {
+ literalArgArray[i] = ".literal.$" + macroArgs.get(i).name;
+ }
}
/*
@@ -148,6 +152,8 @@ public class VelocimacroProxy extends Di
// get name of the reference that refers to AST block passed to block macro call
bodyReference = rsvc.getString(RuntimeConstants.VM_BODY_REFERENCE, "bodyContent");
+
+ preserveArgumentsLiterals = rsvc.getBoolean(RuntimeConstants.VM_PRESERVE_ARGUMENTS_LITERALS, false);
}
public boolean render(InternalContextAdapter context, Writer writer, Node node)
@@ -239,6 +245,15 @@ public class VelocimacroProxy extends Di
}
}
}
+
+ if (preserveArgumentsLiterals)
+ {
+ for (String literalKey : literalArgArray)
+ {
+ // The behavior is not recursive.
+ context.remove(literalKey);
+ }
+ }
}
}
@@ -299,7 +314,8 @@ public class VelocimacroProxy extends Di
/**
* Gets the macro argument values and puts them in the context under
* the argument names. Store and return an array of old and new values
- * paired for each argument name, for later cleanup.
+ * paired for each argument name, for later cleanup. Also, put literal
+ * representations of arguments which evaluate to null in the context.
*/
protected Object[] handleArgValues(InternalContextAdapter context,
Node node, int callArgNum)
@@ -315,10 +331,12 @@ public class VelocimacroProxy extends Di
// put the new value in
Object newVal = null;
+ Node argNode = null;
if (i - 1 < callArgNum)
{
// There's a calling value.
- newVal = node.jjtGetChild(i - 1).value(context);
+ argNode = node.jjtGetChild(i - 1);
+ newVal = argNode.value(context);
}
else if (macroArg.defaultVal != null)
{
@@ -349,6 +367,11 @@ public class VelocimacroProxy extends Di
context.put(macroArg.name, newVal);
values[(i-1) * 2 + 1] = newVal;
+
+ if (preserveArgumentsLiterals && newVal == null && argNode != null)
+ {
+ context.put(literalArgArray[i], argNode);
+ }
}
// return the array of replaced and new values
Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=1843909&r1=1843908&r2=1843909&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java Mon Oct 15 13:08:41 2018
@@ -68,6 +68,7 @@ public class ASTReference extends Simple
private boolean escaped = false;
private boolean computableReference = true;
private boolean logOnNull = true;
+ private boolean lookupAlternateLiteral = false;
private String escPrefix = "";
private String morePrefix = "";
private String identifier = "";
@@ -137,6 +138,7 @@ public class ASTReference extends Simple
strictEscape = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE, false);
strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
+ lookupAlternateLiteral = rsvc.getBoolean(RuntimeConstants.VM_PRESERVE_ARGUMENTS_LITERALS, false);
/*
* the only thing we can do in init() is getRoot()
@@ -585,12 +587,17 @@ public class ASTReference extends Simple
*/
private String getNullString(InternalContextAdapter context)
{
- Object callingArgument = context.get(".literal." + nullString);
+ String ret = nullString;
- if (callingArgument != null)
- return ((Node) callingArgument).literal();
- else
- return nullString;
+ if (lookupAlternateLiteral)
+ {
+ Node callingArgument = (Node)context.get(".literal." + nullString);
+ if (callingArgument != null)
+ {
+ ret = ((Node) callingArgument).literal();
+ }
+ }
+ return ret;
}
/**
Modified: velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties?rev=1843909&r1=1843908&r2=1843909&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties Mon Oct 15 13:08:41 2018
@@ -132,6 +132,16 @@ velocimacro.arguments.strict = false
velocimacro.body.reference=bodyContent
# ----------------------------------------------------------------------------
+# VELOCIMACRO PRESERVE ARGUMENTS LITERALS
+# ----------------------------------------------------------------------------
+# if true, when a macro has to render a null or invalid argument reference
+# which is not quiet, it will print the provided literal reference instead
+# of the one found in the body of the macro
+# ----------------------------------------------------------------------------
+velocimacro.preserve.arguments.literals = false
+
+
+# ----------------------------------------------------------------------------
# STRICT REFERENCE MODE
# ----------------------------------------------------------------------------
# if true, will throw a MethodInvocationException for references
Copied: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java (from r1843856, velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java)
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java?p2=velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java&p1=velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java&r1=1843856&r2=1843909&rev=1843909&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java Mon Oct 15 13:08:41 2018
@@ -19,27 +19,26 @@ package org.apache.velocity.test;
* under the License.
*/
-import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
/**
- * This class tests passing expressions as method arguments
+ * This class tests the mode where velocimacros do preserve arguments literals
*/
-public class DashInIdentifiersTestCase extends BaseTestCase
+public class PreserveArgumentsLiteralsTestCase extends BaseTestCase
{
- public DashInIdentifiersTestCase(final String name)
+ public PreserveArgumentsLiteralsTestCase(final String name)
{
super(name);
}
protected void setUpEngine(VelocityEngine engine)
{
- engine.addProperty("parser.allows.dash.in.identifiers", true);
+ engine.setProperty("velocimacro.preserve.arguments.literals", true);
}
- public void testDash()
+ public void testPreserveLiterals()
{
- assertEvalEquals("6","#set($var-1 = 7)#set($var-2 = $var-1 - 1)$var-2");
+ assertEvalEquals("$bar","#macro(m $foo)$foo#end#m($bar)");
}
}