[jira] [Resolved] (VELOCITY-881) Even handler didn't work when the reference appear in #if statement

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Resolved] (VELOCITY-881) Even handler didn't work when the reference appear in #if statement

JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/VELOCITY-881?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Claude Brisson resolved VELOCITY-881.
-------------------------------------
       Resolution: Won't Fix
         Assignee: Claude Brisson
    Fix Version/s:     (was: 2.0)

A "reference insertion handler" does what its name states it does: it allows to alter what is to be inserted in the template, at the time of insertion.

Your proposed change is to allow the handler to change the value after each and every *evaluation*, which means that $foo.bar.baz would call the handler three times, which is not at all desirable.

You can sometimes _force_ a rendering with code like:

    #if( "$some.reference" == 'some value')

but the example you give is also invalid because you use dots in property names, which is forbidden ; $\{deploy.domain} will always be null, since Velocity will first search the context for a _deploy_ property.


> 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
>            Assignee: Claude Brisson
>              Labels: easyfix
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> 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: [hidden email]
For additional commands, e-mail: [hidden email]