Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retrieve invalid references from Velocity Engine?
    primarykey
    data
    text
    <p>I am wondering if there is a way to retrieve a list of invalid references in Velocity after calling VelocityEngine.evaluate.</p> <p><a href="https://stackoverflow.com/questions/7649289/velocity-eventhandler">Velocity Eventhandler</a> has some nice information about creating an <strong>AppSpecificInvalidReferenceEventHandler</strong> which I have used in the VelocityEngine as follows.</p> <pre><code>&lt;bean id="templateVelocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"&gt; &lt;property name="velocityProperties"&gt; &lt;props&gt; &lt;prop key="resource.loader"&gt;class&lt;/prop&gt; &lt;prop key="class.resource.loader.class"&gt;org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader&lt;/prop&gt; &lt;prop key="class.resource.loader.cache"&gt;true&lt;/prop&gt; &lt;prop key="eventhandler.invalidreferences.class"&gt;xyz.util.AppSpecificInvalidReferenceEventHandler,org.apache.velocity.app.event.implement.ReportInvalidReferences&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>After velocity's evaluate call, I see the log statements showing that AppSpecificInvalidReferenceEventHandler is working, but I can't see how to retrieve that class and it's List of InvalidReferenceInfo if I declare the <em>eventhandler.invalidreferences.class</em> in the Spring context as above.</p> <p>Populating and evaluating the template looks like the following :-</p> <pre><code>VelocityContext context = new VelocityContext(); StringWriter bodyWriter = new StringWriter(); context.put("body", "some body text!"); boolean result = templateVelocityEngine.evaluate(context, bodyWriter, "logTag", "template ${body} text here loaded from a file"); </code></pre> <p>So I would like to do something like the following (except ec.getInvalidReferenceEventHandlers() is null)</p> <pre><code>EventCartridge ec = context.getEventCartridge(); Iterator it = ec.getInvalidReferenceEventHandlers(); while (it.hasNext()) { Object obj = it.next(); if (obj instanceof ReportInvalidReferences) { AppSpecificInvalidReferenceEventHandler handler = (AppSpecificInvalidReferenceEventHandler) obj; List invalidRefs = handler.getInvalidReferences(); if (!invalidRefs.isEmpty()) { // process the list of invalid references here } } } </code></pre> <p>The only way around this I've found so far is to not declare <em>eventhandler.invalidreferences.class</em> in the Spring bean i.e. instead I would do the following</p> <pre><code>ReportInvalidReferences reporter = new AppSpecificInvalidReferenceEventHandler(); EventCartridge ec = new EventCartridge(); ec.addEventHandler(reporter); ec.attachToContext(context); VelocityContext context = new VelocityContext(); StringWriter bodyWriter = new StringWriter(); context.put("body", "some body text!"); boolean result = templateVelocityEngine.evaluate(context, bodyWriter, "logTag", "template ${body} text here loaded from a file"); </code></pre> <p>With the additional pre Velocity evaluate setup code above (and commenting the <em>eventhandler.invalidreferences.class</em> in the Spring bean), I can then call</p> <pre><code>ec.getInvalidReferenceEventHandlers(); </code></pre> <p>And I get back the AppSpecificInvalidReferenceEventHandler in the Iterator returned...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload