Add name of missing resource to Velocity context for the IncludeNotFound event 
handler implementation
-----------------------------------------------------------------------------------------------------

                 Key: VELOCITY-734
                 URL: https://issues.apache.org/jira/browse/VELOCITY-734
             Project: Velocity
          Issue Type: New Feature
          Components: Engine
            Reporter: chad davis
             Fix For: 1.7


When a resource is not found, the IncludeNotFound event handler will insert a 
"notfound.vm" template into the stream.  This provides a robust way of 
finishing the rendering process.  It would be very helpful if the notfound 
template could also inform the user of the full name of the missing resource.  

The following patch fixes this problem.  I've made IncludeNotFound implement 
the ContextAware interface.  It then addes the name of the missing resource to 
the context so it can be referenced from the notfound template.  I suppose it 
might also be nice to make the name of the reference configurable . . . .

Index: 
/home/readyportal/Desktop/machineOneWorkspaces/bipSpace/velocity/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
===================================================================
--- 
/home/readyportal/Desktop/machineOneWorkspaces/bipSpace/velocity/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
      (revision 809033)
+++ 
/home/readyportal/Desktop/machineOneWorkspaces/bipSpace/velocity/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
      (working copy)
@@ -20,7 +20,9 @@
  */
 
 import org.apache.velocity.app.event.IncludeEventHandler;
+import org.apache.velocity.context.Context;
 import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.util.ContextAware;
 import org.apache.velocity.util.RuntimeServicesAware;
 import org.apache.velocity.util.StringUtils;
 
@@ -36,6 +38,13 @@
  * eventhandler.include.notfound = error.vm
  * </PRE>
  * </code>
+ * 
+ * <p>
+ * The name of the missing resource is put into the Velocity context, under the
+ * key "missingResource", so that the notfound.vm template can report the 
missing
+ * resource with a Velocity reference, like:
+ * <code>$missingResource</code>
+ * </p>
  *
  * @author <a href="mailto:wgl...@forio.com";>Will Glass-Husain</a>
  * @version $Id$
@@ -41,7 +50,7 @@
  * @version $Id$
  * @since 1.5
  */
-public class IncludeNotFound implements 
IncludeEventHandler,RuntimeServicesAware {
+public class IncludeNotFound implements 
IncludeEventHandler,RuntimeServicesAware, ContextAware {
 
     private static final String DEFAULT_NOT_FOUND = "notfound.vm";
     private static final String PROPERTY_NOT_FOUND = 
"eventhandler.include.notfound";
@@ -47,6 +56,7 @@
     private static final String PROPERTY_NOT_FOUND = 
"eventhandler.include.notfound";
     private RuntimeServices rs = null;
     String notfound;
+    Context context;
 
     /**
      * Chseck to see if included file exists, and display "not found" page if 
it
@@ -70,6 +80,11 @@
         boolean exists = (rs.getLoaderNameForResource(includeResourcePath) != 
null);
         if (!exists)
         {
+               /**
+                * Put the missingResource name in the velocity context so that 
the notfound.vm can report the info to the user.
+                */
+               context.put("missingResource", includeResourcePath );
+               
             if (rs.getLoaderNameForResource(notfound) != null)
             {
                 return notfound;
@@ -99,4 +114,12 @@
          notfound = StringUtils.nullTrim(rs.getString(PROPERTY_NOT_FOUND, 
DEFAULT_NOT_FOUND));
      }
 
+
+    /**
+     * Part of the ContextAware interface.  
+     */
+       public void setContext(Context context) {
+               this.context = context;
+       }
+
 }


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org
For additional commands, e-mail: dev-h...@velocity.apache.org

Reply via email to