Author: cbrisson Date: Thu Sep 4 02:06:22 2008 New Revision: 691943 URL: http://svn.apache.org/viewvc?rev=691943&view=rev Log: Some work for the #define directive: - #defined references can now be used anywhere (as #set RHS or as method argument) - docs updated
Added: velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/Renderable.java Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Define.java velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java velocity/engine/trunk/src/test/org/apache/velocity/test/DefineTestCase.java velocity/engine/trunk/xdocs/docs/user-guide.xml velocity/engine/trunk/xdocs/docs/vtl-reference-guide.xml Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Define.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Define.java?rev=691943&r1=691942&r2=691943&view=diff ============================================================================== --- velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Define.java (original) +++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Define.java Thu Sep 4 02:06:22 2008 @@ -21,6 +21,8 @@ import java.io.Writer; import java.io.IOException; +import java.io.StringWriter; + import org.apache.commons.lang.text.StrBuilder; import org.apache.velocity.runtime.log.Log; import org.apache.velocity.runtime.RuntimeConstants; @@ -152,14 +154,9 @@ } /** - * The hack: toString() will be called when Velocity encounters - * the reference in a template. The node is rendered at this point - * using the stored context and writer. So far, this appears safe; - * We have no idea what the writer is doing at this point we just assume - * it's ok to make it write what we want right now. :) the alternate - * would be to render into a StringWriter and return the result + * */ - public String toString() + public boolean render(InternalContextAdapter context, Writer writer) { try { @@ -175,13 +172,13 @@ */ parent.log.debug("Max recursion depth reached for "+parent.id(context)); depth--; - return null; + return false; } else { parent.block.render(context, writer); depth--; - return ""; + return true; } } catch (IOException e) @@ -197,5 +194,18 @@ throw ve; } } + + public String toString() + { + Writer stringwriter = new StringWriter(); + if(render(context,stringwriter)) + { + return stringwriter.toString(); + } + else + { + return null; + } + } } } 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=691943&r1=691942&r2=691943&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 Thu Sep 4 02:06:22 2008 @@ -280,7 +280,6 @@ public boolean render(InternalContextAdapter context, Writer writer) throws IOException, MethodInvocationException { - if (referenceType == RUNT) { if (context.getAllowRendering()) @@ -322,7 +321,6 @@ writer.write(localNullString); } } - return true; } @@ -337,6 +335,12 @@ String toString = null; if (value != null) { + + if(value instanceof Renderable && ((Renderable)value).render(context,writer)) + { + return true; + } + toString = value.toString(); } Added: velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/Renderable.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/Renderable.java?rev=691943&view=auto ============================================================================== --- velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/Renderable.java (added) +++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/Renderable.java Thu Sep 4 02:06:22 2008 @@ -0,0 +1,44 @@ +package org.apache.velocity.runtime.parser.node; + +/* + * 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.context.InternalContextAdapter; +import org.apache.velocity.exception.MethodInvocationException; +import org.apache.velocity.exception.ParseErrorException; +import org.apache.velocity.exception.ResourceNotFoundException; + +import java.io.Writer; +import java.io.IOException; + +/** + * This interface caraterize objects other than ASTNodes that can be rendered + * to a writer using a context. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Claude Brisson</a> + * @version $Id:$ + * @since 1.6 + */ + +public interface Renderable { + + public boolean render( InternalContextAdapter context, Writer writer) + throws IOException, MethodInvocationException, ParseErrorException, ResourceNotFoundException; + +} Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/DefineTestCase.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/DefineTestCase.java?rev=691943&r1=691942&r2=691943&view=diff ============================================================================== --- velocity/engine/trunk/src/test/org/apache/velocity/test/DefineTestCase.java (original) +++ velocity/engine/trunk/src/test/org/apache/velocity/test/DefineTestCase.java Thu Sep 4 02:06:22 2008 @@ -71,6 +71,11 @@ assertEvalEquals("1 2 3", "#define( $i )$inc#end$i $i $i"); } + public void testAssignation() + { + assertEvalEquals("[][hello]","#define( $orig )hello#end[#set( $assig = $orig )][$assig]"); + } + public void testRecursionLimit() { try Modified: velocity/engine/trunk/xdocs/docs/user-guide.xml URL: http://svn.apache.org/viewvc/velocity/engine/trunk/xdocs/docs/user-guide.xml?rev=691943&r1=691942&r2=691943&view=diff ============================================================================== --- velocity/engine/trunk/xdocs/docs/user-guide.xml (original) +++ velocity/engine/trunk/xdocs/docs/user-guide.xml Thu Sep 4 02:06:22 2008 @@ -1460,6 +1460,23 @@ </section> +<section name="Define" href="define"> + + <p> + The <em>#define</em> directive lets one assign a block of VTL to a reference. + </p> + + <p>The example below will display <code>hello world!</code>. + </p> + +<source><![CDATA[ +#define( $block )hello world!#end +$block +]]></source> + + + </section> + <section name="Velocimacros" href="velocimacros"> <p> Modified: velocity/engine/trunk/xdocs/docs/vtl-reference-guide.xml URL: http://svn.apache.org/viewvc/velocity/engine/trunk/xdocs/docs/vtl-reference-guide.xml?rev=691943&r1=691942&r2=691943&view=diff ============================================================================== --- velocity/engine/trunk/xdocs/docs/vtl-reference-guide.xml (original) +++ velocity/engine/trunk/xdocs/docs/vtl-reference-guide.xml Thu Sep 4 02:06:22 2008 @@ -466,6 +466,32 @@ </ul> </subsection> + <subsection name="#define - Assigns a block of VTL to a reference" href="define"> + <p> + Format: + </p> + + <p> + <strong>#</strong> [ <strong>{</strong> ] <strong>define</strong> [ <strong>}</strong> ] <strong>( </strong><em>$ref</em><strong> )</strong> + <em>statement</em> + <strong>#</strong> [ <strong>{</strong> ] <strong>end</strong> [ <strong>}</strong> ] + + </p> + + <ul> + <li><em>$ref</em> - Reference that is assigned the VTL block as a value.</li> + <li><em>statement</em> - Statement that is assigned to the reference.</li> + </ul> + + <p> + Example: + </p> + + <ul> + <li>#define( $hello ) Hello World! #end $hello ## displays Hello World!</li> + </ul> + </subsection> + <subsection name="#macro - Allows users to define a Velocimacro (VM), a repeated segment of a VTL template, as required" href="macro-AllowsuserstodefineaVelocimacro(VM),arepeatedsegmentofaVTLtemplate,asrequired"> <p> Format: