diff options
author | Claude Brisson <cbrisson@apache.org> | 2016-07-17 23:33:09 +0000 |
---|---|---|
committer | Claude Brisson <cbrisson@apache.org> | 2016-07-17 23:33:09 +0000 |
commit | 92d42e6fadb5e554b5223c93169bb4850da31b4c (patch) | |
tree | 0c9d5af4d90e69cfcbb594af5f4adcba9a582777 /velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive | |
parent | afe506bb4b3c93d331072e0d2814e8d01cf09eac (diff) | |
download | apache-velocity-engine-92d42e6fadb5e554b5223c93169bb4850da31b4c.tar.gz |
[engine] keep macros in defining templates (applied reviewed patch from VELOCITY-797, fixes VELOCITY-797 and VELOCITY-776); plus some code cleaning that was in the patch
git-svn-id: https://svn.apache.org/repos/asf/velocity/engine/trunk@1753137 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive')
15 files changed, 88 insertions, 69 deletions
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Block.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Block.java index 71ac3dd1..50e9e78c 100755 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Block.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Block.java @@ -19,18 +19,17 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; - -import org.slf4j.Logger; - import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.TemplateInitException; import org.apache.velocity.runtime.Renderable; import org.apache.velocity.runtime.RuntimeServices; import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.util.StringUtils; +import org.slf4j.Logger; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; /** * Directive that puts an unrendered AST block in the context diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/BlockMacro.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/BlockMacro.java index 71556c72..f263c886 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/BlockMacro.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/BlockMacro.java @@ -19,15 +19,15 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.Writer; - import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.TemplateInitException; import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.runtime.RuntimeServices; import org.apache.velocity.runtime.parser.node.Node; +import java.io.IOException; +import java.io.Writer; + /** * BlockMacro directive is used to invoke Velocity macros with normal parameters and a macro body. * <p> @@ -102,7 +102,7 @@ public class BlockMacro extends Block maxDepth = rs.getInt(RuntimeConstants.VM_MAX_DEPTH); macro = new RuntimeMacro(name); - macro.setLocation(getLine(), getColumn(), getTemplateName()); + macro.setLocation(getLine(), getColumn(), getTemplate()); macro.init(rs, context, node); } diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Break.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Break.java index bff06199..5583667c 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Break.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Break.java @@ -19,8 +19,6 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.Writer; -import java.util.ArrayList; import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.VelocityException; import org.apache.velocity.runtime.RuntimeServices; @@ -29,6 +27,9 @@ import org.apache.velocity.runtime.parser.Token; import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.util.StringUtils; +import java.io.Writer; +import java.util.ArrayList; + /** * Break directive used for interrupting scopes. * diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Define.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Define.java index cbebd0ad..8786cb18 100755 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Define.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Define.java @@ -19,9 +19,6 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.Writer; -import java.util.ArrayList; - import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.TemplateInitException; import org.apache.velocity.exception.VelocityException; @@ -33,6 +30,9 @@ import org.apache.velocity.runtime.parser.Token; import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.util.StringUtils; +import java.io.Writer; +import java.util.ArrayList; + /** * Directive that puts an unrendered AST block in the context * under the specified key, postponing rendering until the diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Directive.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Directive.java index c9631168..13c783ff 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Directive.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Directive.java @@ -19,10 +19,7 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; - +import org.apache.velocity.Template; import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.MethodInvocationException; import org.apache.velocity.exception.ParseErrorException; @@ -34,6 +31,10 @@ import org.apache.velocity.runtime.parser.ParseException; import org.apache.velocity.runtime.parser.Token; import org.apache.velocity.runtime.parser.node.Node; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; + /** * Base class for all directives used in Velocity. @@ -47,7 +48,7 @@ public abstract class Directive implements DirectiveConstants, Cloneable private int line = 0; private int column = 0; private boolean provideScope = false; - private String templateName; + private Template template; /** * @@ -82,10 +83,19 @@ public abstract class Directive implements DirectiveConstants, Cloneable * @param line * @param column */ - public void setLocation(int line, int column, String templateName) + public void setLocation(int line, int column, Template template) { setLocation(line, column); - this.templateName = templateName; + this.template = template; + } + + /** + * returns the template in which this directive appears + * @return template + */ + public Template getTemplate() + { + return template; } /** @@ -112,7 +122,7 @@ public abstract class Directive implements DirectiveConstants, Cloneable */ public String getTemplateName() { - return templateName; + return template.getName(); } /** diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Evaluate.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Evaluate.java index 89d8a51d..19c2943e 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Evaluate.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Evaluate.java @@ -19,9 +19,7 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.StringReader; -import java.io.Writer; +import org.apache.velocity.Template; import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.MethodInvocationException; import org.apache.velocity.exception.ParseErrorException; @@ -34,6 +32,10 @@ import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.runtime.parser.node.SimpleNode; import org.apache.velocity.util.introspection.Info; +import java.io.IOException; +import java.io.StringReader; +import java.io.Writer; + /** * Evaluates the directive argument as a VTL string, using the existing * context. @@ -151,11 +153,17 @@ public class Evaluate extends Directive * The new string needs to be parsed since the text has been dynamically generated. */ String templateName = context.getCurrentTemplateName(); + Template template = (Template)context.getCurrentResource(); + if (template == null) + { + template = new Template(); + template.setName(templateName); + } SimpleNode nodeTree = null; try { - nodeTree = rsvc.parse(new StringReader(sourceText), templateName, false); + nodeTree = rsvc.parse(new StringReader(sourceText), template); } catch (ParseException pex) { diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Foreach.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Foreach.java index bd70f1e0..d17654f6 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Foreach.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Foreach.java @@ -19,12 +19,6 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.Closeable; -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Iterator; - import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.TemplateInitException; import org.apache.velocity.exception.VelocityException; @@ -39,6 +33,12 @@ import org.apache.velocity.runtime.parser.node.SimpleNode; import org.apache.velocity.util.StringUtils; import org.apache.velocity.util.introspection.Info; +import java.io.Closeable; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Iterator; + /** * Foreach directive used for moving through arrays, * or objects that provide an Iterator. diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Include.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Include.java index 41725185..ef59e2e5 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Include.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Include.java @@ -19,9 +19,6 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.Writer; - import org.apache.velocity.app.event.EventHandlerUtil; import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.MethodInvocationException; @@ -35,6 +32,9 @@ import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.util.StringUtils; +import java.io.IOException; +import java.io.Writer; + /** * <p>Pluggable directive that handles the #include() statement in VTL. * This #include() can take multiple arguments of either diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java index ce082d5c..03256d51 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java @@ -19,11 +19,6 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; -import java.util.List; - import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.TemplateInitException; import org.apache.velocity.runtime.RuntimeServices; @@ -32,6 +27,11 @@ import org.apache.velocity.runtime.parser.ParserTreeConstants; import org.apache.velocity.runtime.parser.Token; import org.apache.velocity.runtime.parser.node.Node; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + /** * Macro implements the macro definition directive of VTL. * @@ -115,7 +115,7 @@ public class Macro extends Directive List<MacroArg> macroArgs = getArgArray(node, rs); int numArgs = node.jjtGetNumChildren(); rs.addVelocimacro(macroArgs.get(0).name, node.jjtGetChild(numArgs - 1), - macroArgs, node.getTemplateName()); + macroArgs, node.getTemplate()); } /** diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Parse.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Parse.java index 2153a4d6..b440d201 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Parse.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Parse.java @@ -19,11 +19,6 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; -import java.util.List; - import org.apache.velocity.Template; import org.apache.velocity.app.event.EventHandlerUtil; import org.apache.velocity.context.InternalContextAdapter; @@ -41,6 +36,11 @@ import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.runtime.parser.node.SimpleNode; import org.apache.velocity.util.StringUtils; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + /** * Pluggable directive that handles the <code>#parse()</code> * statement in VTL. @@ -266,7 +266,8 @@ public class Parse extends InputBase context.setMacroLibraries(macroLibraries); - macroLibraries.add(arg); + /* instead of adding the name of the template, add the Template reference */ + macroLibraries.add(t); /* * and render it diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java index 85c9c7be..83efcef0 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java @@ -19,10 +19,7 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.Writer; -import java.util.List; - +import org.apache.velocity.Template; import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.MethodInvocationException; import org.apache.velocity.exception.ParseErrorException; @@ -37,6 +34,10 @@ import org.apache.velocity.runtime.parser.Token; import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.util.StringUtils; +import java.io.IOException; +import java.io.Writer; +import java.util.List; + /** * This class acts as a proxy for potential macros. When the AST is built * this class is inserted as a placeholder for the macro (whether or not @@ -247,12 +248,12 @@ public class RuntimeMacro extends Directive ParseErrorException, MethodInvocationException { VelocimacroProxy vmProxy = null; - String renderingTemplate = context.getCurrentTemplateName(); + Template renderingTemplate = (Template)context.getCurrentResource(); /** * first look in the source template */ - Object o = rsvc.getVelocimacro(macroName, getTemplateName(), renderingTemplate); + Object o = rsvc.getVelocimacro(macroName, renderingTemplate, getTemplate()); if( o != null ) { @@ -271,8 +272,7 @@ public class RuntimeMacro extends Directive { for (int i = macroLibraries.size() - 1; i >= 0; i--) { - o = rsvc.getVelocimacro(macroName, - (String)macroLibraries.get(i), renderingTemplate); + o = rsvc.getVelocimacro(macroName, renderingTemplate, (Template)macroLibraries.get(i)); // get the first matching macro if (o != null) diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Scope.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Scope.java index 04c4f1bb..940c1187 100755 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Scope.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Scope.java @@ -19,11 +19,12 @@ package org.apache.velocity.runtime.directive; * under the License. */ +import org.apache.velocity.Template; + import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; import java.util.Set; -import org.apache.velocity.Template; /** * This handles context scoping and metadata for directives. diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Stop.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Stop.java index 76786192..9550bbad 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Stop.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Stop.java @@ -19,15 +19,15 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.Writer; -import java.util.ArrayList; import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.runtime.RuntimeServices; import org.apache.velocity.runtime.parser.ParseException; -import org.apache.velocity.runtime.parser.ParserTreeConstants; import org.apache.velocity.runtime.parser.Token; import org.apache.velocity.runtime.parser.node.Node; +import java.io.Writer; +import java.util.ArrayList; + /** * This class implements the #stop directive which allows * a user to stop the merging and rendering process. The #stop directive diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java index ee5ee793..92152083 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java @@ -19,10 +19,6 @@ package org.apache.velocity.runtime.directive; * under the License. */ -import java.io.IOException; -import java.io.Writer; -import java.util.List; - import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.MacroOverflowException; import org.apache.velocity.exception.VelocityException; @@ -34,6 +30,10 @@ import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.runtime.parser.node.SimpleNode; import org.apache.velocity.util.StringUtils; +import java.io.IOException; +import java.io.Writer; +import java.util.List; + /** * VelocimacroProxy.java * @@ -274,7 +274,6 @@ public class VelocimacroProxy extends Directive { if (maxCallDepth > 0 && maxCallDepth == context.getCurrentMacroCallDepth()) { - String templateName = context.getCurrentTemplateName(); String[] stack = context.getMacroNameStack(); StringBuffer out = new StringBuffer(100) diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/contrib/For.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/contrib/For.java index 7ee16600..6e491911 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/contrib/For.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/contrib/For.java @@ -18,10 +18,6 @@ */ package org.apache.velocity.runtime.directive.contrib; -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; - import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.TemplateInitException; import org.apache.velocity.runtime.RuntimeServices; @@ -33,6 +29,10 @@ import org.apache.velocity.runtime.parser.Token; import org.apache.velocity.runtime.parser.node.ASTReference; import org.apache.velocity.runtime.parser.node.Node; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; + /** * The #for directive provides the behavior of the #foreach directive but also * provides an 'index' keyword that allows the user to define an optional index variable |