Author: musachy
Date: Mon Aug 10 23:30:03 2009
New Revision: 802956
URL: http://svn.apache.org/viewvc?rev=802956&view=rev
Log:
WW-2807 Improve Velocity tag syntax and performance
thanks to Christopher Schultz for patch
Modified:
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java
Modified:
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java?rev=802956&r1=802955&r2=802956&view=diff
==============================================================================
---
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java
(original)
+++
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java
Mon Aug 10 23:30:03 2009
@@ -94,15 +94,34 @@
* if the was an error in the format of the property
*/
protected Map createPropertyMap(InternalContextAdapter contextAdapter,
Node node) throws ParseErrorException, MethodInvocationException {
- Map propertyMap = new HashMap();
+ Map propertyMap;
int children = node.jjtGetNumChildren();
if (getType() == BLOCK) {
children--;
}
- for (int index = 0, length = children; index < length; index++) {
- this.putProperty(propertyMap, contextAdapter,
node.jjtGetChild(index));
+ // Velocity supports an on-the-fly Map-definition syntax that leads
+ // to more readable and faster code:
+ //
+ // #url({'id':'url', 'action':'MyAction'})
+ //
+ // We support this syntax by checking for a single Map argument
+ // to any directive and using that as the property map instead
+ // of building one from individual name-value pair strings.
+ Node firstChild = null;
+ Object firstValue = null;
+ if(children == 1
+ && null != (firstChild = node.jjtGetChild(0))
+ && null != (firstValue = firstChild.value(contextAdapter))
+ && firstValue instanceof Map) {
+ propertyMap = (Map)firstValue;
+ } else {
+ propertyMap = new HashMap();
+
+ for (int index = 0, length = children; index < length; index++) {
+ this.putProperty(propertyMap, contextAdapter,
node.jjtGetChild(index));
+ }
}
return propertyMap;