Author: nbubna
Date: Tue Sep 1 05:14:27 2009
New Revision: 809816
URL: http://svn.apache.org/viewvc?rev=809816&view=rev
Log:
VELOCITY-734 put $missingResource name in context on includenotfound event
(thanks to Chad davis)
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
velocity/engine/trunk/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java?rev=809816&r1=809815&r2=809816&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
Tue Sep 1 05:14:27 2009
@@ -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,17 +38,24 @@
* eventhandler.include.notfound = error.vm
* </PRE>
* </code>
+ * </p><p>
+ * The name of the missing resource is put into the Velocity context, under
the
+ * key "missingResource", so that the "notfound" template can report the
missing
+ * resource with a Velocity reference, like:
+ * <code>$missingResource</code>
+ * </p>
*
* @author <a href="mailto:[email protected]">Will Glass-Husain</a>
* @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";
private RuntimeServices rs = null;
String notfound;
+ Context context;
/**
* Chseck to see if included file exists, and display "not found" page if
it
@@ -70,10 +79,10 @@
boolean exists = (rs.getLoaderNameForResource(includeResourcePath) !=
null);
if (!exists)
{
+ context.put("missingResource", includeResourcePath);
if (rs.getLoaderNameForResource(notfound) != null)
{
return notfound;
-
}
else
{
@@ -83,7 +92,6 @@
rs.getLog().error("Can't find include not found page: " +
notfound);
return null;
}
-
}
else
return includeResourcePath;
@@ -97,6 +105,13 @@
{
this.rs = rs;
notfound = StringUtils.nullTrim(rs.getString(PROPERTY_NOT_FOUND,
DEFAULT_NOT_FOUND));
- }
+ }
+ /**
+ * @see
org.apache.velocity.util.ContextAware#setContext(org.apache.velocity.context.Context)
+ */
+ public void setContext(Context context)
+ {
+ this.context = context;
+ }
}
Modified:
velocity/engine/trunk/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java?rev=809816&r1=809815&r2=809816&view=diff
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
(original)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
Tue Sep 1 05:14:27 2009
@@ -93,9 +93,10 @@
super(name);
}
- public void setUp()
+ public void setUp() throws Exception
{
assureResultsDirectoryExists(RESULTS_DIR);
+ super.setUp();
}
public static Test suite()
@@ -391,6 +392,14 @@
}
+ public void testIncludeNotFoundMissingResourceName() throws Exception
+ {
+ // uses base test support
+ engine.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE,
"org.apache.velocity.app.event.implement.IncludeNotFound");
+ addTemplate("notfound.vm", "$missingResource");
+ assertEvalEquals("foo", "#parse('foo')");
+ }
+
public void testIncludeRelativePath() throws Exception
{
VelocityEngine ve = new VelocityEngine();