aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src
diff options
context:
space:
mode:
authorClaude Brisson <cbrisson@apache.org>2016-12-11 08:27:14 +0000
committerClaude Brisson <cbrisson@apache.org>2016-12-11 08:27:14 +0000
commit5d70000132ba2eddf8f27f1ec38160ff3630a1dc (patch)
treef6d9fce2eeb57029918e4f1b40616bfc5dcfd20b /velocity-engine-core/src
parent29ccc7beebd7d551165b36bd285349f6389b5c6a (diff)
downloadapache-velocity-engine-5d70000132ba2eddf8f27f1ec38160ff3630a1dc.tar.gz
[engine] review event handling
1) Add a Context argument for all events. 2) Get rid of the Executor pattern ; event handlers are directly called by the cartridge. git-svn-id: https://svn.apache.org/repos/asf/velocity/engine/trunk@1773548 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'velocity-engine-core/src')
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventCartridge.java349
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerMethodExecutor.java57
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java297
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/IncludeEventHandler.java60
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java150
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java68
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/ReferenceInsertionEventHandler.java59
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java3
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java1
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeRelativePath.java2
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java6
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java1
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java3
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java22
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java4
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/IncludeEventHandlingTestCase.java2
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java6
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java6
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity758TestCase.java3
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java7
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java4
21 files changed, 362 insertions, 748 deletions
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventCartridge.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventCartridge.java
index 437f3679..b3506fdb 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventCartridge.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventCartridge.java
@@ -20,13 +20,17 @@ package org.apache.velocity.app.event;
*/
import org.apache.velocity.context.Context;
+import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.context.InternalEventContext;
+import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.util.RuntimeServicesAware;
+import org.apache.velocity.util.introspection.Info;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Set;
@@ -53,16 +57,72 @@ import java.util.Set;
*/
public class EventCartridge
{
- private List referenceHandlers = new ArrayList();
- private List methodExceptionHandlers = new ArrayList();
- private List includeHandlers = new ArrayList();
- private List invalidReferenceHandlers = new ArrayList();
+ private List<ReferenceInsertionEventHandler> referenceHandlers = new ArrayList();
+ private MethodExceptionEventHandler methodExceptionHandler = null;
+ private List<IncludeEventHandler> includeHandlers = new ArrayList();
+ private List<InvalidReferenceEventHandler> invalidReferenceHandlers = new ArrayList();
/**
* Ensure that handlers are not initialized more than once.
*/
Set initializedHandlers = new HashSet();
+ protected RuntimeServices rsvc = null;
+
+ protected Logger getLog()
+ {
+ return rsvc == null ? LoggerFactory.getLogger(EventCartridge.class) : rsvc.getLog();
+ }
+
+ /**
+ * runtime services setter, called during initialization
+ * @param rs runtime services
+ * @since 2.0
+ */
+ public synchronized void setRuntimeServices(RuntimeServices rs)
+ {
+ if (rsvc == null)
+ {
+ rsvc = rs;
+ /* allow for this method to be called *after* adding event handlers */
+ for (EventHandler handler : referenceHandlers)
+ {
+ if (handler instanceof RuntimeServicesAware && !initializedHandlers.contains(handler))
+ {
+ ((RuntimeServicesAware)handler).setRuntimeServices(rs);
+ initializedHandlers.add(handler);
+ }
+ }
+ if (methodExceptionHandler != null &&
+ methodExceptionHandler instanceof RuntimeServicesAware &&
+ !initializedHandlers.contains(methodExceptionHandler))
+ {
+ ((RuntimeServicesAware)methodExceptionHandler).setRuntimeServices(rs);
+ initializedHandlers.add(methodExceptionHandler);
+ }
+ for (EventHandler handler : includeHandlers)
+ {
+ if (handler instanceof RuntimeServicesAware && !initializedHandlers.contains(handler))
+ {
+ ((RuntimeServicesAware)handler).setRuntimeServices(rs);
+ initializedHandlers.add(handler);
+ }
+ }
+ for (EventHandler handler : invalidReferenceHandlers)
+ {
+ if (handler instanceof RuntimeServicesAware && !initializedHandlers.contains(handler))
+ {
+ ((RuntimeServicesAware)handler).setRuntimeServices(rs);
+ initializedHandlers.add(handler);
+ }
+ }
+ }
+ else if (rsvc != rs)
+ {
+ throw new VelocityException("an event cartridge cannot be used by several different runtime services instances");
+ }
+ }
+
/**
* Adds an event handler(s) to the Cartridge. This method
* will find all possible event handler interfaces supported
@@ -88,22 +148,28 @@ public class EventCartridge
if ( ev instanceof MethodExceptionEventHandler )
{
- addMethodExceptionHandler( (MethodExceptionEventHandler) ev );
+ addMethodExceptionHandler((MethodExceptionEventHandler) ev);
found = true;
}
if ( ev instanceof IncludeEventHandler )
{
- addIncludeEventHandler( (IncludeEventHandler) ev );
+ addIncludeEventHandler((IncludeEventHandler) ev);
found = true;
}
if ( ev instanceof InvalidReferenceEventHandler )
{
- addInvalidReferenceEventHandler( (InvalidReferenceEventHandler) ev );
+ addInvalidReferenceEventHandler((InvalidReferenceEventHandler) ev);
found = true;
}
+ if (found && rsvc != null && ev instanceof RuntimeServicesAware && !initializedHandlers.contains(ev))
+ {
+ ((RuntimeServicesAware)ev).setRuntimeServices(rsvc);
+ initializedHandlers.add(ev);
+ }
+
return found;
}
@@ -126,7 +192,14 @@ public class EventCartridge
*/
public void addMethodExceptionHandler( MethodExceptionEventHandler ev )
{
- methodExceptionHandlers.add( ev );
+ if (methodExceptionHandler == null)
+ {
+ methodExceptionHandler = ev;
+ }
+ else
+ {
+ getLog().warn("ignoring extra method exception handler");
+ }
}
/**
@@ -168,153 +241,181 @@ public class EventCartridge
return false;
}
- boolean found = false;
-
if ( ev instanceof ReferenceInsertionEventHandler )
- return referenceHandlers.remove( ev );
+ {
+ return referenceHandlers.remove(ev);
+ }
if ( ev instanceof MethodExceptionEventHandler )
- return methodExceptionHandlers.remove(ev );
+ {
+ if (ev == methodExceptionHandler)
+ {
+ methodExceptionHandler = null;
+ return true;
+ }
+ }
if ( ev instanceof IncludeEventHandler )
- return includeHandlers.remove( ev );
+ {
+ return includeHandlers.remove(ev);
+ }
if ( ev instanceof InvalidReferenceEventHandler )
- return invalidReferenceHandlers.remove( ev );
+ {
+ return invalidReferenceHandlers.remove(ev);
+ }
- return found;
+ return false;
}
/**
- * Iterate through all the stored ReferenceInsertionEventHandler objects
+ * Call reference insertion handlers
*
- * @return iterator of handler objects, null if there are not handlers
- * @since 1.5
+ * @return value returned by handlers
+ * @since 2.0
*/
- public Iterator getReferenceInsertionEventHandlers()
+ public Object referenceInsert(InternalContextAdapter context, String reference, Object value)
{
- return referenceHandlers.size() == 0 ? null : referenceHandlers.iterator();
+ for (ReferenceInsertionEventHandler handler : referenceHandlers)
+ {
+ value = handler.referenceInsert(context, reference, value);
+ }
+ return value;
}
- /**
- * Iterate through all the stored MethodExceptionEventHandler objects
- *
- * @return iterator of handler objects
- * @since 1.5
- */
- public Iterator getMethodExceptionEventHandlers()
- {
- return methodExceptionHandlers.iterator();
- }
+ /**
+ * Check whether this event cartridge has a method exception event handler
+ * @return true if a method exception event handler has been registered
+ * @since 2.0
+ */
+ boolean hasMethodExceptionEventHandler()
+ {
+ return methodExceptionHandler != null;
+ }
/**
- * Iterate through all the stored IncludeEventHandlers objects
+ * Call method exception event handler
*
- * @return iterator of handler objects
+ * @return value returned by handler
+ * @since 2.0
*/
- public Iterator getIncludeEventHandlers()
+ public Object methodException(Context context, Class claz, String method, Exception e, Info info )
{
- return includeHandlers.iterator();
- }
-
- /**
- * Iterate through all the stored InvalidReferenceEventHandlers objects
- *
- * @return iterator of handler objects
- * @since 1.5
- */
- public Iterator getInvalidReferenceEventHandlers()
- {
- return invalidReferenceHandlers.iterator();
- }
-
- /**
- * Attached the EventCartridge to the context
- *
- * Final because not something one should mess with lightly :)
- *
- * @param context context to attach to
- * @return true if successful, false otherwise
- */
- public final boolean attachToContext( Context context )
- {
- if ( context instanceof InternalEventContext )
+ if (methodExceptionHandler != null)
{
- InternalEventContext iec = (InternalEventContext) context;
-
- iec.attachEventCartridge( this );
-
- /**
- * while it's tempting to call setContext on each handler from here,
- * this needs to be done before each method call. This is
- * because the specific context will change as inner contexts
- * are linked in through macros, foreach, or directly by the user.
- */
-
- return true;
- }
- else
- {
- return false;
+ return methodExceptionHandler.methodException(context, claz, method, e, info);
}
+ return null;
}
/**
- * Initialize the handlers. For global handlers this is called when Velocity
- * is initialized. For local handlers this is called when the first handler
- * is executed. Handlers will not be initialized more than once.
+ * Call include event handlers
*
- * @param rs
- * @since 1.5
+ * @return include path
+ * @since 2.0
*/
- public void initialize (RuntimeServices rs)
+ public String includeEvent(Context context, String includeResourcePath, String currentResourcePath, String directiveName)
{
-
- for ( Iterator i = referenceHandlers.iterator(); i.hasNext(); )
+ for (IncludeEventHandler handler : includeHandlers)
{
- EventHandler eh = ( EventHandler ) i.next();
- if ( (eh instanceof RuntimeServicesAware) &&
- !initializedHandlers.contains(eh) )
+ includeResourcePath = handler.includeEvent(context, includeResourcePath, currentResourcePath, directiveName);
+ /* reflect 1.x behavior: exit after at least one execution whenever a null include path has been found */
+ if (includeResourcePath == null)
{
- ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
- initializedHandlers.add( eh );
+ break;
}
}
-
- for ( Iterator i = methodExceptionHandlers.iterator(); i.hasNext(); )
- {
- EventHandler eh = ( EventHandler ) i.next();
- if ( (eh instanceof RuntimeServicesAware) &&
- !initializedHandlers.contains(eh) )
- {
- ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
- initializedHandlers.add( eh );
- }
- }
-
- for ( Iterator i = includeHandlers.iterator(); i.hasNext(); )
- {
- EventHandler eh = ( EventHandler ) i.next();
- if ( (eh instanceof RuntimeServicesAware) &&
- !initializedHandlers.contains(eh) )
- {
- ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
- initializedHandlers.add( eh );
- }
- }
-
- for ( Iterator i = invalidReferenceHandlers.iterator(); i.hasNext(); )
- {
- EventHandler eh = ( EventHandler ) i.next();
- if ( (eh instanceof RuntimeServicesAware) &&
- !initializedHandlers.contains(eh) )
- {
- ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
- initializedHandlers.add( eh );
- }
- }
-
+ return includeResourcePath;
}
-
-}
+ /**
+ * Call invalid reference handlers for an invalid getter
+ *
+ * @return value returned by handlers
+ * @since 2.0
+ */
+ public Object invalidGetMethod(Context context, String reference, Object object, String property, Info info)
+ {
+ Object result = null;
+ for (InvalidReferenceEventHandler handler : invalidReferenceHandlers)
+ {
+ result = handler.invalidGetMethod(context, reference, object, property, info);
+ /* reflect 1.x behavior: exit after at least one execution whenever a non-null value has been found */
+ if (result != null)
+ {
+ break;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Call invalid reference handlers for an invalid setter
+ *
+ * @return whether to stop further chaining in the next cartridge
+ * @since 2.0
+ */
+ public boolean invalidSetMethod(Context context, String leftreference, String rightreference, Info info)
+ {
+ for (InvalidReferenceEventHandler handler : invalidReferenceHandlers)
+ {
+ if (handler.invalidSetMethod(context, leftreference, rightreference, info))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Call invalid reference handlers for an invalid method call
+ *
+ * @return value returned by handlers
+ * @since 2.0
+ */
+ public Object invalidMethod(Context context, String reference, Object object, String method, Info info)
+ {
+ Object result = null;
+ for (InvalidReferenceEventHandler handler : invalidReferenceHandlers)
+ {
+ result = handler.invalidMethod(context, reference, object, method, info);
+ /* reflect 1.x behavior: exit after at least one execution whenever a non-null value has been found */
+ if (result != null)
+ {
+ break;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Attached the EventCartridge to the context
+ *
+ * Final because not something one should mess with lightly :)
+ *
+ * @param context context to attach to
+ * @return true if successful, false otherwise
+ */
+ public final boolean attachToContext( Context context )
+ {
+ if ( context instanceof InternalEventContext )
+ {
+ InternalEventContext iec = (InternalEventContext) context;
+
+ iec.attachEventCartridge( this );
+
+ /**
+ * while it's tempting to call setContext on each handler from here,
+ * this needs to be done before each method call. This is
+ * because the specific context will change as inner contexts
+ * are linked in through macros, foreach, or directly by the user.
+ */
+
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerMethodExecutor.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerMethodExecutor.java
deleted file mode 100644
index 7cd633cf..00000000
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerMethodExecutor.java
+++ /dev/null
@@ -1,57 +0,0 @@
-package org.apache.velocity.app.event;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-/**
- * Strategy object used to execute event handler method. Will be called
- * while looping through all the chained event handler implementations.
- * Each EventHandler method call should have a parallel executor object
- * defined.
- *
- * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
- * @version $Id$
- * @since 1.5
- */
-public interface EventHandlerMethodExecutor
-{
- /**
- * Execute the event handler method. If Object is not null, do not
- * iterate further through the handler chain.
- * If appropriate, the returned Object will be the return value.
- *
- * @param handler call the appropriate method on this handler
- */
- public void execute(EventHandler handler);
-
- /**
- * Called after execute() to see if iterating should stop. Should
- * always return false before method execute() is run.
- *
- * @return true if no more event handlers for this method should be called.
- */
- public boolean isDone();
-
- /**
- * Get return value at end of all the iterations
- *
- * @return null if no return value is required
- */
- public Object getReturnValue();
-}
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java
index 3bb4a99e..7e476561 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java
@@ -56,49 +56,16 @@ public class EventHandlerUtil {
public static Object referenceInsert(RuntimeServices rsvc,
InternalContextAdapter context, String reference, Object value)
{
- // app level cartridges have already been initialized
-
- /*
- * Performance modification: EventCartridge.getReferenceInsertionEventHandlers
- * now returns a null if there are no handlers. Thus we can avoid creating the
- * Iterator object.
- */
- EventCartridge ev1 = rsvc.getApplicationEventCartridge();
- Iterator applicationEventHandlerIterator =
- (ev1 == null) ? null: ev1.getReferenceInsertionEventHandlers();
-
- EventCartridge ev2 = context.getEventCartridge();
- initializeEventCartridge(rsvc, ev2);
- Iterator contextEventHandlerIterator =
- (ev2 == null) ? null: ev2.getReferenceInsertionEventHandlers();
-
- try
+ try
{
- /*
- * Performance modification: methodExecutor is created only if one of the
- * iterators is not null.
- */
-
- EventHandlerMethodExecutor methodExecutor = null;
-
- if( applicationEventHandlerIterator != null )
- {
- methodExecutor =
- new ReferenceInsertionEventHandler.referenceInsertExecutor(context, reference, value);
- iterateOverEventHandlers(applicationEventHandlerIterator, methodExecutor);
- }
-
- if( contextEventHandlerIterator != null )
+ value = rsvc.getApplicationEventCartridge().referenceInsert(context, reference, value);
+ EventCartridge contextCartridge = context.getEventCartridge();
+ if (contextCartridge != null)
{
- if( methodExecutor == null )
- methodExecutor =
- new ReferenceInsertionEventHandler.referenceInsertExecutor(context, reference, value);
-
- iterateOverEventHandlers(contextEventHandlerIterator, methodExecutor);
+ contextCartridge.setRuntimeServices(rsvc);
+ value = contextCartridge.referenceInsert(context, reference, value);
}
-
-
- return methodExecutor != null ? methodExecutor.getReturnValue() : value;
+ return value;
}
catch (RuntimeException e)
{
@@ -109,7 +76,7 @@ public class EventHandlerUtil {
throw new VelocityException("Exception in event handler.",e);
}
}
-
+
/**
* Called when a method exception is generated during Velocity merge. Only
* the first valid event handler in the sequence is called. The default
@@ -131,30 +98,31 @@ public class EventHandlerUtil {
InternalContextAdapter context, Class claz, String method,
Exception e, Info info) throws Exception
{
- // app level cartridges have already been initialized
- EventCartridge ev1 = rsvc.getApplicationEventCartridge();
- Iterator applicationEventHandlerIterator =
- (ev1 == null) ? null: ev1.getMethodExceptionEventHandlers();
-
- EventCartridge ev2 = context.getEventCartridge();
- initializeEventCartridge(rsvc, ev2);
- Iterator contextEventHandlerIterator =
- (ev2 == null) ? null: ev2.getMethodExceptionEventHandlers();
-
- EventHandlerMethodExecutor methodExecutor =
- new MethodExceptionEventHandler.MethodExceptionExecutor(context, claz, method, e, info);
-
- if ( ((applicationEventHandlerIterator == null) || !applicationEventHandlerIterator.hasNext()) &&
- ((contextEventHandlerIterator == null) || !contextEventHandlerIterator.hasNext()) )
+ try
{
- throw e;
+ EventCartridge ev = rsvc.getApplicationEventCartridge();
+ if (ev.hasMethodExceptionEventHandler())
+ {
+ return ev.methodException(context, claz, method, e, info);
+ }
+ EventCartridge contextCartridge = context.getEventCartridge();
+ if (contextCartridge != null)
+ {
+ contextCartridge.setRuntimeServices(rsvc);
+ return contextCartridge.methodException(context, claz, method, e, info);
+ }
+ }
+ catch (RuntimeException re)
+ {
+ throw re;
+ }
+ catch (Exception ex)
+ {
+ throw new VelocityException("Exception in event handler.", ex);
}
-
- callEventHandlers(
- applicationEventHandlerIterator,
- contextEventHandlerIterator, methodExecutor);
-
- return methodExecutor.getReturnValue();
+
+ /* default behaviour is to re-throw exception */
+ throw e;
}
/**
@@ -181,28 +149,16 @@ public class EventHandlerUtil {
InternalContextAdapter context, String includeResourcePath,
String currentResourcePath, String directiveName)
{
- // app level cartridges have already been initialized
- EventCartridge ev1 = rsvc.getApplicationEventCartridge();
- Iterator applicationEventHandlerIterator =
- (ev1 == null) ? null: ev1.getIncludeEventHandlers();
-
- EventCartridge ev2 = context.getEventCartridge();
- initializeEventCartridge(rsvc, ev2);
- Iterator contextEventHandlerIterator =
- (ev2 == null) ? null: ev2.getIncludeEventHandlers();
-
- try
+ try
{
- EventHandlerMethodExecutor methodExecutor =
- new IncludeEventHandler.IncludeEventExecutor(
- context, includeResourcePath,
- currentResourcePath, directiveName);
-
- callEventHandlers(
- applicationEventHandlerIterator,
- contextEventHandlerIterator, methodExecutor);
-
- return (String) methodExecutor.getReturnValue();
+ includeResourcePath = rsvc.getApplicationEventCartridge().includeEvent(context, includeResourcePath, currentResourcePath, directiveName);
+ EventCartridge contextCartridge = context.getEventCartridge();
+ if (contextCartridge != null)
+ {
+ contextCartridge.setRuntimeServices(rsvc);
+ includeResourcePath = contextCartridge.includeEvent(context, includeResourcePath, currentResourcePath, directiveName);
+ }
+ return includeResourcePath;
}
catch (RuntimeException e)
{
@@ -230,15 +186,27 @@ public class EventHandlerUtil {
InternalContextAdapter context, String reference,
Object object, String property, Info info)
{
- return
- invalidReferenceHandlerCall (
- new InvalidReferenceEventHandler.InvalidGetMethodExecutor
- (context, reference, object, property, info),
- rsvc,
- context);
+ try
+ {
+ Object result = rsvc.getApplicationEventCartridge().invalidGetMethod(context, reference, object, property, info);
+ EventCartridge contextCartridge = context.getEventCartridge();
+ if (contextCartridge != null)
+ {
+ contextCartridge.setRuntimeServices(rsvc);
+ result = contextCartridge.invalidGetMethod(context, reference, object, property, info);
+ }
+ return result;
+ }
+ catch (RuntimeException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ throw new VelocityException("Exception in event handler.",e);
+ }
}
-
-
+
/**
* Called when an invalid set method is encountered.
*
@@ -252,14 +220,26 @@ public class EventHandlerUtil {
InternalContextAdapter context, String leftreference,
String rightreference, Info info)
{
- /**
- * ignore return value
- */
- invalidReferenceHandlerCall (
- new InvalidReferenceEventHandler.InvalidSetMethodExecutor
- (context, leftreference, rightreference, info),
- rsvc,
- context);
+ try
+ {
+ if (!rsvc.getApplicationEventCartridge().invalidSetMethod(context, leftreference, rightreference, info))
+ {
+ EventCartridge contextCartridge = context.getEventCartridge();
+ if (contextCartridge != null)
+ {
+ contextCartridge.setRuntimeServices(rsvc);
+ contextCartridge.invalidSetMethod(context, leftreference, rightreference, info);
+ }
+ }
+ }
+ catch (RuntimeException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ throw new VelocityException("Exception in event handler.",e);
+ }
}
/**
@@ -277,45 +257,16 @@ public class EventHandlerUtil {
InternalContextAdapter context, String reference,
Object object, String method, Info info)
{
- return
- invalidReferenceHandlerCall (
- new InvalidReferenceEventHandler.InvalidMethodExecutor
- (context, reference, object, method, info),
- rsvc,
- context);
- }
-
-
- /**
- * Calls event handler method with appropriate chaining across event handlers.
- *
- * @param methodExecutor
- * @param rsvc current instance of RuntimeServices
- * @param context The current context
- * @return return value from method, or null if no return value
- */
- public static Object invalidReferenceHandlerCall(
- EventHandlerMethodExecutor methodExecutor,
- RuntimeServices rsvc,
- InternalContextAdapter context)
- {
- // app level cartridges have already been initialized
- EventCartridge ev1 = rsvc.getApplicationEventCartridge();
- Iterator applicationEventHandlerIterator =
- (ev1 == null) ? null: ev1.getInvalidReferenceEventHandlers();
-
- EventCartridge ev2 = context.getEventCartridge();
- initializeEventCartridge(rsvc, ev2);
- Iterator contextEventHandlerIterator =
- (ev2 == null) ? null: ev2.getInvalidReferenceEventHandlers();
-
try
{
- callEventHandlers(
- applicationEventHandlerIterator,
- contextEventHandlerIterator, methodExecutor);
-
- return methodExecutor.getReturnValue();
+ Object result = rsvc.getApplicationEventCartridge().invalidMethod(context, reference, object, method, info);
+ EventCartridge contextCartridge = context.getEventCartridge();
+ if (contextCartridge != null)
+ {
+ contextCartridge.setRuntimeServices(rsvc);
+ result = contextCartridge.invalidMethod(context, reference, object, method, info);
+ }
+ return result;
}
catch (RuntimeException e)
{
@@ -325,77 +276,5 @@ public class EventHandlerUtil {
{
throw new VelocityException("Exception in event handler.",e);
}
-
- }
-
- /**
- * Initialize the event cartridge if appropriate.
- *
- * @param rsvc current instance of RuntimeServices
- * @param eventCartridge the event cartridge to be initialized
- */
- private static void initializeEventCartridge(RuntimeServices rsvc, EventCartridge eventCartridge)
- {
- if (eventCartridge != null)
- {
- try
- {
- eventCartridge.initialize(rsvc);
- }
- catch (Exception e)
- {
- throw new VelocityException("Couldn't initialize event cartridge : ", e);
- }
- }
- }
-
-
- /**
- * Loop through both the application level and context-attached event handlers.
- *
- * @param applicationEventHandlerIterator Iterator that loops through all global event handlers declared at application level
- * @param contextEventHandlerIterator Iterator that loops through all global event handlers attached to context
- * @param eventExecutor Strategy object that executes event handler method
- */
- private static void callEventHandlers(
- Iterator applicationEventHandlerIterator,
- Iterator contextEventHandlerIterator,
- EventHandlerMethodExecutor eventExecutor)
- {
- /**
- * First loop through the event handlers configured at the app level
- * in the properties file.
- */
- iterateOverEventHandlers(applicationEventHandlerIterator, eventExecutor);
-
- /**
- * Then loop through the event handlers attached to the context.
- */
- iterateOverEventHandlers(contextEventHandlerIterator, eventExecutor);
}
-
- /**
- * Loop through a given iterator of event handlers.
- *
- * @param handlerIterator Iterator that loops through event handlers
- * @param eventExecutor Strategy object that executes event handler method
- */
- private static void iterateOverEventHandlers(
- Iterator handlerIterator,
- EventHandlerMethodExecutor eventExecutor)
- {
- if (handlerIterator != null)
- {
- for (Iterator i = handlerIterator; i.hasNext();)
- {
- EventHandler eventHandler = (EventHandler) i.next();
-
- if (!eventExecutor.isDone())
- {
- eventExecutor.execute(eventHandler);
- }
- }
- }
- }
-
}
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/IncludeEventHandler.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/IncludeEventHandler.java
index 0e1fa9a6..e0571fe9 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/IncludeEventHandler.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/IncludeEventHandler.java
@@ -39,6 +39,7 @@ public interface IncludeEventHandler extends EventHandler
* registered IncludeEventHandlers are called unless null is returned. If
* none are registered the template at the includeResourcePath is retrieved.
*
+ * @param context current context
* @param includeResourcePath the path as given in the include directive.
* @param currentResourcePath the path of the currently rendering template that includes the
* include directive.
@@ -48,62 +49,5 @@ public interface IncludeEventHandler extends EventHandler
* @return a new resource path for the directive, or null to block the
* include from occurring.
*/
- public String includeEvent( String includeResourcePath, String currentResourcePath, String directiveName );
-
-
-
- /**
- * Defines the execution strategy for includeEvent
- */
- static class IncludeEventExecutor implements EventHandlerMethodExecutor
- {
- private Context context;
- private String includeResourcePath;
- private String currentResourcePath;
- private String directiveName;
-
- private boolean executed = false;
-
- IncludeEventExecutor(
- Context context,
- String includeResourcePath,
- String currentResourcePath,
- String directiveName)
- {
- this.context = context;
- this.includeResourcePath = includeResourcePath;
- this.currentResourcePath = currentResourcePath;
- this.directiveName = directiveName;
- }
-
- /**
- * Call the method includeEvent()
- *
- * @param handler call the appropriate method on this handler
- */
- public void execute(EventHandler handler)
- {
- IncludeEventHandler eh = (IncludeEventHandler) handler;
-
- if (eh instanceof ContextAware)
- ((ContextAware) eh).setContext(context);
-
- executed = true;
- includeResourcePath = ((IncludeEventHandler) handler)
- .includeEvent(includeResourcePath, currentResourcePath, directiveName);
- }
-
- public Object getReturnValue()
- {
- return includeResourcePath;
- }
-
- public boolean isDone()
- {
- return executed && (includeResourcePath == null);
- }
-
-
- }
-
+ public String includeEvent(Context context, String includeResourcePath, String currentResourcePath, String directiveName);
}
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java
index 8fa61b74..747a58df 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java
@@ -85,154 +85,4 @@ public interface InvalidReferenceEventHandler extends EventHandler
*/
public Object invalidMethod(Context context, String reference,
Object object, String method, Info info);
-
-
- /**
- * Defines the execution strategy for invalidGetMethod
- */
- static class InvalidGetMethodExecutor implements EventHandlerMethodExecutor
- {
- private Context context;
- private String reference;
- private Object object;
- private String property;
- private Info info;
-
- private Object result;
-
- InvalidGetMethodExecutor(
- Context context,
- String reference,
- Object object,
- String property,
- Info info)
- {
- this.context = context;
- this.reference = reference;
- this.object = object;
- this.property = property;
- this.info = info;
- }
-
- /**
- * Call the method invalidGetMethod()
- *
- * @param handler call the appropriate method on this handler
- */
- public void execute(EventHandler handler)
- {
- result = ((InvalidReferenceEventHandler) handler).invalidGetMethod(
- context, reference, object, property, info);
- }
-
- public Object getReturnValue()
- {
- return result;
- }
-
- public boolean isDone()
- {
- return (result != null);
- }
- }
-
- /**
- * Defines the execution strategy for invalidGetMethod
- */
- static class InvalidSetMethodExecutor implements EventHandlerMethodExecutor
- {
- private Context context;
- private String leftreference;
- private String rightreference;
- private Info info;
-
- private boolean result;
-
- InvalidSetMethodExecutor(
- Context context,
- String leftreference,
- String rightreference,
- Info info)
- {
- this.context = context;
- this.leftreference = leftreference;
- this.rightreference = rightreference;
- this.info = info;
- }
-
- /**
- * Call the method invalidSetMethod()
- *
- * @param handler call the appropriate method on this handler
- */
- public void execute(EventHandler handler)
- {
- result = ((InvalidReferenceEventHandler) handler).invalidSetMethod(
- context, leftreference, rightreference, info);
- }
-
- public Object getReturnValue()
- {
- return null;
- }
-
- public boolean isDone()
- {
- return result;
- }
-
- }
-
- /**
- * Defines the execution strategy for invalidGetMethod
- */
- static class InvalidMethodExecutor implements EventHandlerMethodExecutor
- {
- private Context context;
- private String reference;
- private Object object;
- private String method;
- private Info info;
-
- private Object result;
- private boolean executed = false;
-
- InvalidMethodExecutor(
- Context context,
- String reference,
- Object object,
- String method,
- Info info)
- {
- this.context = context;
- this.reference = reference;
- this.object = object;
- this.method = method;
- this.info = info;
- }
-
- /**
- * Call the method invalidMethod()
- *
- * @param handler call the appropriate method on this handler
- */
- public void execute(EventHandler handler)
- {
- executed = true;
- result = ((InvalidReferenceEventHandler) handler).invalidMethod(
- context, reference, object, method, info);
- }
-
- public Object getReturnValue()
- {
- return result;
- }
-
- public boolean isDone()
- {
- return executed && (result != null);
- }
-
- }
-
}
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java
index df5879c4..283ee658 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java
@@ -41,6 +41,7 @@ public interface MethodExceptionEventHandler extends EventHandler
* Only the first registered MethodExceptionEventHandler is called. If
* none are registered a MethodInvocationException is thrown.
*
+ * @param context current context
* @param claz the class of the object the method is being applied to
* @param method the method
* @param e the thrown exception
@@ -48,70 +49,5 @@ public interface MethodExceptionEventHandler extends EventHandler
* @return an object to insert in the page
* @throws RuntimeException an exception to be thrown instead inserting an object
*/
- public Object methodException( Class claz, String method, Exception e, Info info );
-
- /**
- * Defines the execution strategy for methodException
- * @since 1.5
- */
- static class MethodExceptionExecutor implements EventHandlerMethodExecutor
- {
- private Context context;
- private Class claz;
- private String method;
- private Exception e;
- private Info info;
-
- private Object result;
- private boolean executed = false;
-
- MethodExceptionExecutor(
- Context context,
- Class claz,
- String method,
- Exception e,
- Info info)
- {
- this.context = context;
- this.claz = claz;
- this.method = method;
- this.e = e;
- this.info = info;
- }
-
- /**
- * Call the method methodException()
- *
- * @param handler call the appropriate method on this handler
- * @exception Exception generic exception thrown by methodException event handler method call
- */
- public void execute(EventHandler handler)
- {
- MethodExceptionEventHandler eh = (MethodExceptionEventHandler) handler;
-
- if (eh instanceof ContextAware)
- ((ContextAware) eh).setContext(context);
-
- executed = true;
- result = ((MethodExceptionEventHandler) handler).methodException(claz, method, e, info);
- }
-
- public Object getReturnValue()
- {
- return result;
- }
-
- /**
- * Only run the first MethodExceptionEventHandler
- *
- * @return true after this is executed once.
- */
- public boolean isDone()
- {
- return executed;
- }
-
-
- }
-
+ public Object methodException(Context context, Class claz, String method, Exception e, Info info);
}
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/ReferenceInsertionEventHandler.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/ReferenceInsertionEventHandler.java
index c167cabc..90c01bfc 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/ReferenceInsertionEventHandler.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/ReferenceInsertionEventHandler.java
@@ -20,7 +20,6 @@ package org.apache.velocity.app.event;
*/
import org.apache.velocity.context.Context;
-import org.apache.velocity.util.ContextAware;
/**
* Reference 'Stream insertion' event handler. Called with object
@@ -41,66 +40,14 @@ public interface ReferenceInsertionEventHandler extends EventHandler
* ReferenceInsertionEventHandlers are are registered then reference value
* is inserted into the output stream as is.
*
+ *
+ * @param context current context
* @param reference Reference from template about to be inserted.
* @param value Value about to be inserted (after its <code>toString()</code>
* method is called).
* @return Object on which <code>toString()</code> should be called for
* output.
*/
- public Object referenceInsert( String reference, Object value );
-
- /**
- * Defines the execution strategy for referenceInsert
- * @since 1.5
- */
- static class referenceInsertExecutor implements EventHandlerMethodExecutor
- {
- private Context context;
- private String reference;
- private Object value;
-
- referenceInsertExecutor(
- Context context,
- String reference,
- Object value)
- {
- this.context = context;
- this.reference = reference;
- this.value = value;
- }
-
- /**
- * Call the method referenceInsert()
- *
- * @param handler call the appropriate method on this handler
- */
- public void execute(EventHandler handler)
- {
- ReferenceInsertionEventHandler eh = (ReferenceInsertionEventHandler) handler;
-
- if (eh instanceof ContextAware)
- ((ContextAware) eh).setContext(context);
-
- /**
- * Every successive call will alter the same value
- */
- value = ((ReferenceInsertionEventHandler) handler).referenceInsert(reference, value);
- }
-
- public Object getReturnValue()
- {
- return value;
- }
-
- /**
- * Continue to end of event handler iteration
- *
- * @return always returns false
- */
- public boolean isDone()
- {
- return false;
- }
- }
+ public Object referenceInsert( Context context, String reference, Object value );
}
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java
index 091f4aea..20c0d487 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java
@@ -20,6 +20,7 @@ package org.apache.velocity.app.event.implement;
*/
import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
+import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.util.RuntimeServicesAware;
@@ -90,7 +91,7 @@ public abstract class EscapeReference implements ReferenceInsertionEventHandler,
* @param value
* @return Escaped text.
*/
- public Object referenceInsert(String reference, Object value)
+ public Object referenceInsert(Context context, String reference, Object value)
{
if(value == null)
{
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
index fd1b5ee5..f298d66d 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
@@ -74,6 +74,7 @@ public class IncludeNotFound implements IncludeEventHandler, RuntimeServicesAwar
* @return message.
*/
public String includeEvent(
+ Context context,
String includeResourcePath,
String currentResourcePath,
String directiveName)
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeRelativePath.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeRelativePath.java
index d5957d16..a83543b8 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeRelativePath.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeRelativePath.java
@@ -20,6 +20,7 @@ package org.apache.velocity.app.event.implement;
*/
import org.apache.velocity.app.event.IncludeEventHandler;
+import org.apache.velocity.context.Context;
/**
* <p>Event handler that looks for included files relative to the path of the
@@ -44,6 +45,7 @@ public class IncludeRelativePath implements IncludeEventHandler {
* @return new path relative to the current template's path
*/
public String includeEvent(
+ Context context,
String includeResourcePath,
String currentResourcePath,
String directiveName)
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java
index bf1c7638..7f5f4175 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java
@@ -20,6 +20,7 @@ package org.apache.velocity.app.event.implement;
*/
import org.apache.velocity.app.event.MethodExceptionEventHandler;
+import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.util.RuntimeServicesAware;
import org.apache.velocity.util.introspection.Info;
@@ -57,14 +58,15 @@ public class PrintExceptions implements MethodExceptionEventHandler, RuntimeServ
/**
* Render the method exception, and optionally the exception message and stack trace.
- *
+ *
+ * @param context current context
* @param claz the class of the object the method is being applied to
* @param method the method
* @param e the thrown exception
* @param info template name and line, column informations
* @return an object to insert in the page
*/
- public Object methodException(Class claz, String method, Exception e, Info info)
+ public Object methodException(Context context, Class claz, String method, Exception e, Info info)
{
boolean showTemplateInfo = rs.getBoolean(SHOW_TEMPLATE_INFO, false);
boolean showStackTrace = rs.getBoolean(SHOW_STACK_TRACE,false);
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
index 97deaecf..2294cabc 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
@@ -789,6 +789,7 @@ public class RuntimeInstance implements RuntimeConstants, RuntimeServices
{
eventCartridge = new EventCartridge();
+ eventCartridge.setRuntimeServices(this);
/**
* For each type of event handler, get the class name, instantiate it, and store it.
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java
index 4295f7ee..e01f4754 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java
@@ -218,8 +218,7 @@ public class ASTIdentifier extends SimpleNode
/**
* If the event handler throws an exception, then wrap it
- * in a MethodInvocationException. Don't pass through RuntimeExceptions like other
- * similar catchall code blocks.
+ * in a MethodInvocationException.
*/
catch( Exception e )
{
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
index 47f4bee7..2c89955d 100644
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
@@ -175,9 +175,9 @@ public class BuiltInEventHandlerTestCase extends BaseTestCase {
public void testEscapeHtml() throws Exception
{
EscapeReference esc = new EscapeHtmlReference();
- assertEquals("test string&amp;another&lt;b&gt;bold&lt;/b&gt;test",esc.referenceInsert("","test string&another<b>bold</b>test"));
- assertEquals("&lt;&quot;&gt;",esc.referenceInsert("","<\">"));
- assertEquals("test string",esc.referenceInsert("","test string"));
+ assertEquals("test string&amp;another&lt;b&gt;bold&lt;/b&gt;test",esc.referenceInsert(null,"","test string&another<b>bold</b>test"));
+ assertEquals("&lt;&quot;&gt;",esc.referenceInsert(null,"","<\">"));
+ assertEquals("test string",esc.referenceInsert(null,"","test string"));
log("Correctly escaped HTML");
@@ -190,10 +190,10 @@ public class BuiltInEventHandlerTestCase extends BaseTestCase {
public void testEscapeXml() throws Exception
{
EscapeReference esc = new EscapeXmlReference();
- assertEquals("test string&amp;another&lt;b&gt;bold&lt;/b&gt;test",esc.referenceInsert("","test string&another<b>bold</b>test"));
- assertEquals("&lt;&quot;&gt;",esc.referenceInsert("","<\">"));
- assertEquals("&apos;",esc.referenceInsert("","'"));
- assertEquals("test string",esc.referenceInsert("","test string"));
+ assertEquals("test string&amp;another&lt;b&gt;bold&lt;/b&gt;test",esc.referenceInsert(null,"","test string&another<b>bold</b>test"));
+ assertEquals("&lt;&quot;&gt;",esc.referenceInsert(null,"","<\">"));
+ assertEquals("&apos;",esc.referenceInsert(null,"","'"));
+ assertEquals("test string",esc.referenceInsert(null,"","test string"));
log("Correctly escaped XML");
@@ -206,8 +206,8 @@ public class BuiltInEventHandlerTestCase extends BaseTestCase {
public void testEscapeSql() throws Exception
{
EscapeReference esc = new EscapeSqlReference();
- assertEquals("Jimmy''s Pizza",esc.referenceInsert("","Jimmy's Pizza"));
- assertEquals("test string",esc.referenceInsert("","test string"));
+ assertEquals("Jimmy''s Pizza",esc.referenceInsert(null,"","Jimmy's Pizza"));
+ assertEquals("test string",esc.referenceInsert(null,"","test string"));
log("Correctly escaped SQL");
@@ -220,8 +220,8 @@ public class BuiltInEventHandlerTestCase extends BaseTestCase {
public void testEscapeJavaScript() throws Exception
{
EscapeReference esc = new EscapeJavaScriptReference();
- assertEquals("Jimmy\\'s Pizza",esc.referenceInsert("","Jimmy's Pizza"));
- assertEquals("test string",esc.referenceInsert("","test string"));
+ assertEquals("Jimmy\\'s Pizza",esc.referenceInsert(null,"","Jimmy's Pizza"));
+ assertEquals("test string",esc.referenceInsert(null,"","test string"));
log("Correctly escaped Javascript");
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java
index d0f6be69..d84cfdba 100644
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java
@@ -199,7 +199,7 @@ public class EventHandlingTestCase extends BaseTestCase
/**
* Event handler for when a reference is inserted into the output stream.
*/
- public Object referenceInsert( String reference, Object value )
+ public Object referenceInsert( Context context, String reference, Object value )
{
// as a test, make sure this EventHandler is initialized
if (rs == null)
@@ -233,7 +233,7 @@ public class EventHandlingTestCase extends BaseTestCase
/**
* Handles exceptions thrown during in-template method access
*/
- public Object methodException( Class claz, String method, Exception e, Info info )
+ public Object methodException( Context context, Class claz, String method, Exception e, Info info )
{
// as a test, make sure this EventHandler is initialized
if (rs == null)
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/IncludeEventHandlingTestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/IncludeEventHandlingTestCase.java
index 1b1a03b3..91396a54 100644
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/IncludeEventHandlingTestCase.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/IncludeEventHandlingTestCase.java
@@ -204,7 +204,7 @@ public class IncludeEventHandlingTestCase extends BaseTestCase implements Includ
/**
* Sample handler with different behaviors for the different tests.
*/
- public String includeEvent( String includeResourcePath, String currentResourcePath, String directiveName)
+ public String includeEvent( Context context, String includeResourcePath, String currentResourcePath, String directiveName)
{
if (EventHandlerBehavior == PASS_THROUGH)
return includeResourcePath;
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java
index c3a6631c..49b76967 100644
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java
@@ -22,6 +22,7 @@ package org.apache.velocity.test.eventhandler;
import org.apache.velocity.app.event.IncludeEventHandler;
import org.apache.velocity.app.event.MethodExceptionEventHandler;
import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
+import org.apache.velocity.context.Context;
import org.apache.velocity.util.introspection.Info;
/**
@@ -36,7 +37,7 @@ public class Handler1
/**
* display output twice, once uppercase and once lowercase
*/
- public Object referenceInsert(String reference, Object value)
+ public Object referenceInsert(Context context, String reference, Object value)
{
if (value == null)
return null;
@@ -47,7 +48,7 @@ public class Handler1
/**
* throw the exception
*/
- public Object methodException(Class claz, String method, Exception e, Info info)
+ public Object methodException(Context context, Class claz, String method, Exception e, Info info)
{
throw new RuntimeException(e);
}
@@ -56,6 +57,7 @@ public class Handler1
* redirect all requests to a page "login.vm" (simulates access control).
*/
public String includeEvent(
+ Context context,
String includeResourcePath,
String currentResourcePath,
String directiveName)
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java
index ee4a11ed..14eae6b2 100644
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java
@@ -22,6 +22,7 @@ package org.apache.velocity.test.eventhandler;
import org.apache.velocity.app.event.IncludeEventHandler;
import org.apache.velocity.app.event.MethodExceptionEventHandler;
import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
+import org.apache.velocity.context.Context;
import org.apache.velocity.util.introspection.Info;
/**
@@ -36,7 +37,7 @@ public class Handler2
/**
* convert output to upper case
*/
- public Object referenceInsert(String reference, Object value)
+ public Object referenceInsert(Context context, String reference, Object value)
{
if (value == null)
return null;
@@ -47,7 +48,7 @@ public class Handler2
/**
* print the exception
*/
- public Object methodException(Class claz, String method, Exception e, Info info)
+ public Object methodException(Context context, Class claz, String method, Exception e, Info info)
{
return "Exception: " + e;
}
@@ -56,6 +57,7 @@ public class Handler2
* redirect all requests to a new directory "subdir" (simulates localization).
*/
public String includeEvent(
+ Context context,
String includeResourcePath,
String currentResourcePath,
String directiveName)
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity758TestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity758TestCase.java
index ae08c2f9..100d1755 100644
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity758TestCase.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity758TestCase.java
@@ -21,6 +21,7 @@ package org.apache.velocity.test.issues;
import org.apache.velocity.app.event.EventCartridge;
import org.apache.velocity.app.event.IncludeEventHandler;
+import org.apache.velocity.context.Context;
import org.apache.velocity.test.BaseTestCase;
/**
@@ -52,7 +53,7 @@ public class Velocity758TestCase extends BaseTestCase
public static class Handler implements IncludeEventHandler
{
- public String includeEvent(String parsePath, String parentPath, String directive)
+ public String includeEvent(Context context, String parsePath, String parentPath, String directive)
{
if (parsePath == null)
{
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java
index fc064115..5fc8c122 100644
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java
@@ -22,6 +22,7 @@ package org.apache.velocity.test.misc;
import org.apache.velocity.app.event.IncludeEventHandler;
import org.apache.velocity.app.event.MethodExceptionEventHandler;
import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
+import org.apache.velocity.context.Context;
import org.apache.velocity.util.introspection.Info;
/**
@@ -35,15 +36,15 @@ public class ExceptionGeneratingEventHandler implements IncludeEventHandler,
MethodExceptionEventHandler, ReferenceInsertionEventHandler
{
- public String includeEvent(String includeResourcePath, String currentResourcePath,
+ public String includeEvent(Context context, String includeResourcePath, String currentResourcePath,
String directiveName)
{
throw new RuntimeException("exception");
}
- public Object methodException(Class claz, String method, Exception e, Info info) { throw new RuntimeException("exception"); }
+ public Object methodException(Context context, Class claz, String method, Exception e, Info info) { throw new RuntimeException("exception"); }
- public Object referenceInsert(String reference, Object value)
+ public Object referenceInsert(Context context, String reference, Object value)
{
throw new RuntimeException("exception");
}
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
index 5033cd2f..1d107779 100644
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
@@ -24,6 +24,7 @@ import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.app.event.MethodExceptionEventHandler;
+import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.RuntimeInstance;
import org.apache.velocity.test.BaseTestCase;
@@ -200,7 +201,8 @@ public class ConversionHandlerTestCase extends BaseTestCase
public static class PrintException implements MethodExceptionEventHandler
{
- public Object methodException(Class claz,
+ public Object methodException(Context context,
+ Class claz,
String method,
Exception e,
Info info)