pixiang.lq created VELOCITY-881:
-----------------------------------
Summary: Even handler didn't work when the reference appear in #if
statement
Key: VELOCITY-881
URL: https://issues.apache.org/jira/browse/VELOCITY-881
Project: Velocity
Issue Type: Bug
Components: Engine
Affects Versions: 1.7
Environment: it a common case. It nothing to do with a specified
environment
Reporter: pixiang.lq
Fix For: 2.0
My code like:
----------------------------------------------------------------
public static void main(String[] args) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(Velocity.RESOURCE_LOADER, "class");
ve.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
try {
ve.init();
Template tp = ve.getTemplate("my-template.xml");
Context context = new VelocityContext();
// add my event handler
Map map = new HashMap<>();
map.put("deploy.domain", "offline");
map.put("deploy_domain", "offline");
map.put("provider.version", "1.0.0");
map.put("provider_version", "1.0.0");
EventCartridge eventCartridge = new EventCartridge();
eventCartridge.addEventHandler(new
MyReferenceInsertionHandler(map));
eventCartridge.attachToContext(context);
StringWriter writer = new StringWriter();
tp.merge(context, writer);
System.out.println(writer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
----------------------------------------------------------------
My template file to be converted by velocity like:
----------------------------------------------------------------
#if(${deploy.domain}=='testString')
<bean class="MY_CLASS1">
<property name="MY_PROPERTY_NAME1">
<value>aaa</value>
</property>
</bean>
#end
<bean class="MY_CLASS2">
<property name="MY_PROPERTY_NAME2">
<value>${provider.version}</value>
</property>
</bean>
----------------------------------------------
My got the output in fact:
----------------------------------------------
<bean class="MY_CLASS2">
<property name="MY_PROPERTY_NAME2">
<value>1.0.0</value>
</property>
</bean>
----------------------------------------------
My expected output:
----------------------------------------------
<bean class="MY_CLASS1">
<property name="MY_PROPERTY1">
<value>aaa</value>
</property>
</bean>
<bean class="MY_CLASS2">
<property name="MY_PROPERTY_NAME2">
<value>1.0.0</value>
</property>
</bean>
----------------------------------------------
My suggestion to fix this bug:
ASTReference.java
----------------------------------------------
The method before change:
public Object value(InternalContextAdapter context)
throws MethodInvocationException
{
return (computableReference ? execute(null, context) : null);
}
After change:
public Object value(InternalContextAdapter context)
throws MethodInvocationException
{
Object value = this.computableReference?this.execute((Object)null,
context):null;
if(value == null && !this.escaped) {
// process the value by EventHandlerUtil
value = EventHandlerUtil.referenceInsert(this.rsvc, context,
this.literal, value);
}
return value;
}
----------------------------------------------
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]