Author: byron
Date: Sun Jan 4 07:11:20 2009
New Revision: 731266
URL: http://svn.apache.org/viewvc?rev=731266&view=rev
Log:
VELOCITY-663 Re-implement #stop to correct performance bottleneck, and so that
it terminates execution as well as rendering of the template
Added:
velocity/engine/trunk/src/test/org/apache/velocity/test/StopDirectiveTestCase.java
velocity/engine/trunk/test/stop/
velocity/engine/trunk/test/stop/parse.vm
velocity/engine/trunk/test/stop/stop1.vm
velocity/engine/trunk/test/stop/stop2.vm
velocity/engine/trunk/test/stop/stop3.vm
velocity/engine/trunk/test/stop/vmlib1.vm
Modified:
velocity/engine/trunk/src/changes/changes.xml
velocity/engine/trunk/src/java/org/apache/velocity/Template.java
velocity/engine/trunk/src/java/org/apache/velocity/context/ChainedInternalContextAdapter.java
velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextAdapterImpl.java
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextBase.java
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalHousekeepingContext.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Break.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Evaluate.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Parse.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTComment.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscape.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscapedDirective.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStop.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTText.java
velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
velocity/engine/trunk/xdocs/docs/user-guide.xml
Modified: velocity/engine/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/changes/changes.xml?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
--- velocity/engine/trunk/src/changes/changes.xml (original)
+++ velocity/engine/trunk/src/changes/changes.xml Sun Jan 4 07:11:20 2009
@@ -27,6 +27,12 @@
<body>
<release version="1.7" date="In Subversion">
+ <action type="add" dev="byron" issue="VELOCITY-663">
+ Re-implement #stop so that it stops template execution and rendering.
This
+ Also addresses a performance bottleneck detected in the old
implementation.
+ #stop is a general use directive now, and not just for debugging.
+ </action>
+
<action type="add" dev="byron" issue="VELOCITY-662">
Reduce performance bottleneck with the referenceInsert event handler
call
</action>
Modified: velocity/engine/trunk/src/java/org/apache/velocity/Template.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/Template.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/Template.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/Template.java Sun Jan 4
07:11:20 2009
@@ -36,6 +36,7 @@
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.parser.ParseException;
import org.apache.velocity.runtime.parser.node.SimpleNode;
+import org.apache.velocity.runtime.parser.node.ASTStop.StopThrowable;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.ResourceManager;
@@ -338,6 +339,14 @@
( (SimpleNode) data ).render( ica, writer);
}
+ catch (StopThrowable st)
+ {
+ // The stop throwable is thrown by ASTStop (the #stop directive)
+ // The intent of the stop directive is to halt processing of the
+ // the template, so we throw a Throwable that will short circuit
+ // everthing between the call to render, and ASTStop. We just
needed to
+ // Catch the exception, nothing else to do.
+ }
catch (IOException e)
{
throw new VelocityException("IO Error rendering template '"+
name + "'", e);
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/context/ChainedInternalContextAdapter.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/context/ChainedInternalContextAdapter.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/context/ChainedInternalContextAdapter.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/context/ChainedInternalContextAdapter.java
Sun Jan 4 07:11:20 2009
@@ -223,22 +223,6 @@
}
/**
- * @see
org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering()
- */
- public boolean getAllowRendering()
- {
- return innerContext.getAllowRendering();
- }
-
- /**
- * @see
org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean)
- */
- public void setAllowRendering(boolean v)
- {
- innerContext.setAllowRendering(v);
- }
-
- /**
* @see
org.apache.velocity.context.InternalHousekeepingContext#setMacroLibraries(List)
*/
public void setMacroLibraries(List macroLibraries)
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java
Sun Jan 4 07:11:20 2009
@@ -51,7 +51,6 @@
{
/** container for any local items */
Context localContext;
- boolean allowRendering = true;
/**
* CTOR, wraps an ICA
@@ -203,20 +202,4 @@
return localContext.put(key, value);
}
- /**
- * @see
org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering()
- */
- public boolean getAllowRendering()
- {
- return allowRendering && innerContext.getAllowRendering();
- }
-
- /**
- * @see
org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean)
- */
- public void setAllowRendering(boolean v)
- {
- allowRendering = false;
- }
-
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextAdapterImpl.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextAdapterImpl.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextAdapterImpl.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextAdapterImpl.java
Sun Jan 4 07:11:20 2009
@@ -222,25 +222,6 @@
return icb.getCurrentResource();
}
-
- /**
- * @see
org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering()
- * @since 1.5
- */
- public boolean getAllowRendering()
- {
- return icb.getAllowRendering();
- }
-
- /**
- * @see
org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean)
- * @since 1.5
- */
- public void setAllowRendering(boolean v)
- {
- icb.setAllowRendering(v);
- }
-
/**
* @see
org.apache.velocity.context.InternalHousekeepingContext#setMacroLibraries(List)
* @since 1.6
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextBase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextBase.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextBase.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalContextBase.java
Sun Jan 4 07:11:20 2009
@@ -76,11 +76,6 @@
private Resource currentResource = null;
/**
- * Is rendering allowed? Defaults to true, can be changed by #stop
directive.
- */
- private boolean allowRendering = true;
-
- /**
* List for holding the macro libraries. Contains the macro library
* template name as strings.
*/
@@ -222,23 +217,6 @@
return currentResource;
}
-
- /**
- * @see
org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering()
- */
- public boolean getAllowRendering()
- {
- return allowRendering;
- }
-
- /**
- * @see
org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean)
- */
- public void setAllowRendering(boolean v)
- {
- allowRendering = v;
- }
-
/**
* @see
org.apache.velocity.context.InternalHousekeepingContext#setMacroLibraries(List)
*/
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalHousekeepingContext.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/context/InternalHousekeepingContext.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalHousekeepingContext.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/context/InternalHousekeepingContext.java
Sun Jan 4 07:11:20 2009
@@ -131,22 +131,6 @@
*/
void setCurrentResource( Resource r );
-
- /**
- * Checks to see if rendering should be allowed. Defaults to true but will
- * return false after a #stop directive.
- *
- * @return true if rendering is allowed, false if no rendering should occur
- */
- boolean getAllowRendering();
-
- /**
- * Set whether rendering is allowed. Defaults to true but is set to
- * false after a #stop directive.
- * @param v
- */
- void setAllowRendering(boolean v);
-
/**
* Set the macro library list for the current template.
*
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
Sun Jan 4 07:11:20 2009
@@ -55,6 +55,7 @@
import org.apache.velocity.runtime.parser.Parser;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;
+import org.apache.velocity.runtime.parser.node.ASTStop.StopThrowable;
import org.apache.velocity.runtime.resource.ContentResource;
import org.apache.velocity.runtime.resource.ResourceManager;
import org.apache.velocity.util.ClassUtils;
@@ -1307,6 +1308,14 @@
{
nodeTree.render(ica, writer);
}
+ catch (StopThrowable st)
+ {
+ // The stop throwable is thrown by ASTStop (the #stop
directive)
+ // The intent of the stop directive is to halt processing of
the
+ // the template, so we throw a Throwable that will short
circuit
+ // everthing between this call to render, and ASTStop. We just
needed to
+ // Catch the exception, nothing else to do.
+ }
catch (IOException e)
{
throw new VelocityException("IO Error in writer: " +
e.getMessage(), e);
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Break.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Break.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Break.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Break.java
Sun Jan 4 07:11:20 2009
@@ -107,6 +107,12 @@
public static class BreakException extends RuntimeException
{
-
+ public BreakException()
+ {
+ // If a break is thrown threw a macro or parse call, then this
exception
+ // will be logged because this method calls catch
+ // RuntimeException, so provide the user with some info.
+ super("Break");
+ }
}
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Evaluate.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Evaluate.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Evaluate.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Evaluate.java
Sun Jan 4 07:11:20 2009
@@ -34,6 +34,7 @@
import org.apache.velocity.runtime.parser.ParserTreeConstants;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;
+import org.apache.velocity.runtime.parser.node.ASTStop.StopThrowable;
import org.apache.velocity.util.introspection.Info;
/**
@@ -202,6 +203,14 @@
*/
nodeTree.render( ica, writer );
}
+ catch (StopThrowable st)
+ {
+ // The stop throwable is thrown by ASTStop (the #stop
directive)
+ // The intent of the stop directive is to halt processing
of the
+ // the template, so we throw a Throwable that will short
circuit
+ // everthing between this node, and ASTStop. We just
needed to
+ // Catch the exception, nothing else to do.
+ }
catch (ParseErrorException pex)
{
// convert any parsing errors to the correct line/col
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Parse.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Parse.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Parse.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Parse.java
Sun Jan 4 07:11:20 2009
@@ -116,15 +116,6 @@
throws IOException, ResourceNotFoundException, ParseErrorException,
MethodInvocationException
{
- /*
- * if rendering is no longer allowed (after a stop), we can safely
- * skip execution of all the parse directives.
- */
- if(!context.getAllowRendering())
- {
- return true;
- }
-
/*
* did we get an argument?
*/
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTComment.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTComment.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTComment.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTComment.java
Sun Jan 4 07:11:20 2009
@@ -97,12 +97,7 @@
public boolean render( InternalContextAdapter context, Writer writer)
throws IOException, MethodInvocationException, ParseErrorException,
ResourceNotFoundException
{
-
- if (context.getAllowRendering())
- {
- writer.write(carr);
- }
-
+ writer.write(carr);
return true;
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
Sun Jan 4 07:11:20 2009
@@ -176,11 +176,8 @@
}
else
{
- if (context.getAllowRendering())
- {
- writer.write( "#");
- writer.write( directiveName );
- }
+ writer.write( "#");
+ writer.write( directiveName );
}
return true;
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscape.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscape.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscape.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscape.java
Sun Jan 4 07:11:20 2009
@@ -81,10 +81,7 @@
public boolean render( InternalContextAdapter context, Writer writer)
throws IOException
{
- if (context.getAllowRendering())
- {
- writer.write(ctext);
- }
+ writer.write(ctext);
return true;
}
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscapedDirective.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscapedDirective.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscapedDirective.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEscapedDirective.java
Sun Jan 4 07:11:20 2009
@@ -69,10 +69,7 @@
public boolean render(InternalContextAdapter context, Writer writer)
throws IOException
{
- if (context.getAllowRendering())
- {
- writer.write(getFirstToken().image);
- }
+ writer.write(getFirstToken().image);
return true;
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
Sun Jan 4 07:11:20 2009
@@ -336,11 +336,7 @@
{
if (referenceType == RUNT)
{
- if (context.getAllowRendering())
- {
- writer.write(rootString);
- }
-
+ writer.write(rootString);
return true;
}
@@ -360,20 +356,14 @@
if (value == null)
{
- if (context.getAllowRendering())
- {
- writer.write(escPrefix);
- writer.write("\\");
- writer.write(localNullString);
- }
+ writer.write(escPrefix);
+ writer.write("\\");
+ writer.write(localNullString);
}
else
{
- if (context.getAllowRendering())
- {
- writer.write(escPrefix);
- writer.write(localNullString);
- }
+ writer.write(escPrefix);
+ writer.write(localNullString);
}
return true;
}
@@ -401,18 +391,14 @@
if (value == null || toString == null)
{
/*
- * write prefix twice, because it's schmoo, so the \ don't escape
each other...
+ * write prefix twice, because it's schmoo, so the \ don't escape
each
+ * other...
*/
-
- if (context.getAllowRendering())
- {
- localNullString = getNullString(context);
-
- writer.write(escPrefix);
- writer.write(escPrefix);
- writer.write(morePrefix);
- writer.write(localNullString);
- }
+ localNullString = getNullString(context);
+ writer.write(escPrefix);
+ writer.write(escPrefix);
+ writer.write(morePrefix);
+ writer.write(localNullString);
if (logOnNull && referenceType != QUIET_REFERENCE &&
log.isDebugEnabled())
{
@@ -427,13 +413,9 @@
/*
* non-null processing
*/
-
- if (context.getAllowRendering())
- {
- writer.write(escPrefix);
- writer.write(morePrefix);
- writer.write(toString);
- }
+ writer.write(escPrefix);
+ writer.write(morePrefix);
+ writer.write(toString);
return true;
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStop.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStop.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStop.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTStop.java
Sun Jan 4 07:11:20 2009
@@ -78,9 +78,19 @@
public boolean render( InternalContextAdapter context, Writer writer)
throws IOException, MethodInvocationException, ParseErrorException,
ResourceNotFoundException
{
- context.setAllowRendering(false);
-
- return true;
+ // The top level calls that render an AST node tree catch this
Throwable. By throwing
+ // Here we terminate rendering of this node tree.
+ throw new StopThrowable();
+ }
+
+ /**
+ * We select to overide Error here intead of RuntimeInstance because there
are
+ * certain nodes that catch RuntimeException when rendering there
children, and log
+ * the event to error. But of course in the case that the template
renders an ASTStop
+ * node we don't want this to happen.
+ */
+ public static class StopThrowable extends Error
+ {
}
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTText.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTText.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTText.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTText.java
Sun Jan 4 07:11:20 2009
@@ -80,10 +80,7 @@
public boolean render( InternalContextAdapter context, Writer writer)
throws IOException
{
- if (context.getAllowRendering())
- {
- writer.write(ctext);
- }
+ writer.write(ctext);
return true;
}
}
Modified:
velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
(original)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
Sun Jan 4 07:11:20 2009
@@ -37,7 +37,7 @@
{
protected VelocityEngine engine;
protected VelocityContext context;
- protected boolean DEBUG = false;
+ protected boolean DEBUG = true;
protected TestLogChute log;
public BaseEvalTestCase(String name)
@@ -70,7 +70,7 @@
assertEvalEquals("","");
assertEvalEquals("abc\n123","abc\n123");
}
-
+
protected void setProperties(VelocityEngine engine)
{
// extension hook
@@ -81,6 +81,41 @@
// extension hook
}
+ /**
+ * Compare an expected string with the given loaded template
+ */
+ protected void assertTmplEquals(String expected, String template)
+ {
+ StringWriter writer = new StringWriter();
+ try
+ {
+ engine.mergeTemplate(template, "utf-8", context, writer);
+ }
+ catch (RuntimeException re)
+ {
+ if (DEBUG)
+ {
+ engine.getLog().info("RuntimeException!", re);
+ }
+ throw re;
+ }
+ catch (Exception e)
+ {
+ if (DEBUG)
+ {
+ engine.getLog().info("Exception!", e);
+ }
+ throw new RuntimeException(e);
+ }
+
+ if (DEBUG)
+ {
+ engine.getLog().info("Expected: '" + expected + "'");
+ engine.getLog().info("Result: '" + writer.toString() + "'");
+ }
+ assertEquals(expected, writer.toString());
+ }
+
protected void assertContextValue(String key, Object expected)
{
if (DEBUG)
Added:
velocity/engine/trunk/src/test/org/apache/velocity/test/StopDirectiveTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/StopDirectiveTestCase.java?rev=731266&view=auto
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/StopDirectiveTestCase.java
(added)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/StopDirectiveTestCase.java
Sun Jan 4 07:11:20 2009
@@ -0,0 +1,53 @@
+package org.apache.velocity.test;
+
+/*
+ * 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.
+ */
+
+import org.apache.velocity.test.BaseEvalTestCase;
+import org.apache.velocity.runtime.RuntimeConstants;
+
+/**
+ * Test the #stop directive
+ */
+public class StopDirectiveTestCase extends BaseEvalTestCase
+{
+ public StopDirectiveTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
"test/stop/");
+ engine.setProperty(RuntimeConstants.VM_LIBRARY, "vmlib1.vm");
+ }
+
+ public void testStop()
+ {
+ // Make it works through the evaluate method call
+ assertEvalEquals("Text1", "Text1#{stop}Text2");
+ // Make sure stop works in a template
+ assertTmplEquals("Text 1", "stop1.vm");
+ // Make sure stop works when called from a velocity macro
+ assertTmplEquals("Text123stuff1", "stop2.vm");
+ // Make sure stop works when called located in another parsed file
+ assertTmplEquals("text1blaa1", "stop3.vm");
+ }
+}
\ No newline at end of file
Added: velocity/engine/trunk/test/stop/parse.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/test/stop/parse.vm?rev=731266&view=auto
==============================================================================
--- velocity/engine/trunk/test/stop/parse.vm (added)
+++ velocity/engine/trunk/test/stop/parse.vm Sun Jan 4 07:11:20 2009
@@ -0,0 +1 @@
+blaa1#{stop}blaa2
Added: velocity/engine/trunk/test/stop/stop1.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/test/stop/stop1.vm?rev=731266&view=auto
==============================================================================
--- velocity/engine/trunk/test/stop/stop1.vm (added)
+++ velocity/engine/trunk/test/stop/stop1.vm Sun Jan 4 07:11:20 2009
@@ -0,0 +1,2 @@
+Text 1#stop
+Text 2 ## We should not see this
Added: velocity/engine/trunk/test/stop/stop2.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/test/stop/stop2.vm?rev=731266&view=auto
==============================================================================
--- velocity/engine/trunk/test/stop/stop2.vm (added)
+++ velocity/engine/trunk/test/stop/stop2.vm Sun Jan 4 07:11:20 2009
@@ -0,0 +1,2 @@
+Text123#test1()
+stuff
Added: velocity/engine/trunk/test/stop/stop3.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/test/stop/stop3.vm?rev=731266&view=auto
==============================================================================
--- velocity/engine/trunk/test/stop/stop3.vm (added)
+++ velocity/engine/trunk/test/stop/stop3.vm Sun Jan 4 07:11:20 2009
@@ -0,0 +1 @@
+text1#parse("parse.vm")text2
Added: velocity/engine/trunk/test/stop/vmlib1.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/test/stop/vmlib1.vm?rev=731266&view=auto
==============================================================================
--- velocity/engine/trunk/test/stop/vmlib1.vm (added)
+++ velocity/engine/trunk/test/stop/vmlib1.vm Sun Jan 4 07:11:20 2009
@@ -0,0 +1,3 @@
+#macro(test1)
+stuff1#{stop}stuff2
+#end
Modified: velocity/engine/trunk/xdocs/docs/user-guide.xml
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/xdocs/docs/user-guide.xml?rev=731266&r1=731265&r2=731266&view=diff
==============================================================================
--- velocity/engine/trunk/xdocs/docs/user-guide.xml (original)
+++ velocity/engine/trunk/xdocs/docs/user-guide.xml Sun Jan 4 07:11:20 2009
@@ -1535,9 +1535,12 @@
<section name="Stop" href="stop">
<p>
- The <em>#stop</em> script element prevents any further text or references
- in the page from being rendered. This is useful
- for debugging purposes.
+ The <em>#stop</em> directive stops any further rendering and execution
+ of the template. This is true even when the directive is nested within
+ another template accessed through #parse or located in a velocity macro.
+ The resulting merged output will contain all the content up to the point
+ the #stop directive was encountered. This is handy as an early exit from
+ a template.
</p>
</section>