Uploaded image for project: 'Velocity'
  1. Velocity
  2. VELOCITY-881

Even handler didn't work when the reference appear in #if statement

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 1.7
    • Fix Version/s: None
    • Component/s: Engine
    • Labels:
    • Environment:
      it a common case. It nothing to do with a specified environment
    • Flags:
      Patch

      Description

      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;
      }
      ----------------------------------------------

        Attachments

          Activity

            People

            • Assignee:
              claude Claude Brisson
              Reporter:
              pixiang.lq pixiang.lq
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 2h
                2h
                Remaining:
                Remaining Estimate - 2h
                2h
                Logged:
                Time Spent - Not Specified
                Not Specified