/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.velocity.introspection;

import java.io.IOException;
import java.io.Writer;

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 org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;

/**
 * Allows catching exception from Velocity. Example usage:
 * <pre>
 * {@code #try()
 *   ...
 *   #end
 *
 *   ## At this stage $exception contains the exception if any has been thrown.
 *   ## It can be tested like this:
 *   #if ("$!exception" != '')
 *     ## An exception has been thrown
 *   #end}
 * </pre>
 *
 * @version $Id$
 * @since 6.3M1
 */
public class TryCatchDirective extends Directive
{
    private static final String EXCEPTION_KEY_NAME = "exception";

    @Override
    public String getName()
    {
        return "try";
    }

    @Override
    public int getType()
    {
        return BLOCK;
    }

    @Override
    public boolean render(InternalContextAdapter context, Writer writer, Node node)
        throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException
    {
        try {
            // Make sure to clear any previous exception
            context.remove(EXCEPTION_KEY_NAME);
            return node.jjtGetChild(0).render(context, writer);
        } catch (Exception e) {
            // Save the exception in the $exception velocity context variable
            context.put(EXCEPTION_KEY_NAME, e);
            return true;
        }
    }
}

@@ -1283,16 +1283,73 @@
 ective;%0A
+import org.apache.velocity.runtime.parser.node.ASTBlock;%0A
 import o
@@ -1393,16 +1393,16 @@
 e.Node;%0A
-
 %0A/**%0A * 
@@ -2239,14 +2239,770 @@
     
-try %7B%0A
+boolean result = true;%0A%0A        String exceptionVariableName = EXCEPTION_KEY_NAME;%0A        for (int i = 0; i %3C node.jjtGetNumChildren(); i++) %7B%0A            if (node.jjtGetChild(i) != null) %7B%0A                if (!(node.jjtGetChild(i) instanceof ASTBlock)) %7B%0A                    // Reading and casting inline parameters. We handle one parameter which is the name of the Velocity%0A                    // variable in which we'll save the exception. If not specified we'll use EXCEPTION_KEY_NAME%0A                    if (i == 0) %7B%0A                        exceptionVariableName = (String) node.jjtGetChild(i).value(context);%0A                    %7D else %7B%0A                        break;%0A                    %7D%0A                %7D else %7B%0A                    try %7B%0A            
     
@@ -3050,24 +3050,36 @@
 s exception%0A
+            
             
@@ -3097,29 +3097,44 @@
 ove(
-EXCEPTION_KEY_NAME);%0A
+exceptionVariableName);%0A            
     
@@ -3143,20 +3143,22 @@
       re
-turn
+sult =
  node.jj
@@ -3167,17 +3167,17 @@
 etChild(
-0
+i
 ).render
@@ -3203,16 +3203,28 @@
         
+      
+      
 %7D catch 
@@ -3235,24 +3235,36 @@
 eption e) %7B%0A
+            
             
@@ -3345,78 +3345,212 @@
     
-context.put(EXCEPTION_KEY_NAME, e);%0A            return true;%0A        %7D
+            context.put(exceptionVariableName, e);%0A                        result = true;%0A                    %7D%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A%0A        return result;
 %0A   

Add ability to specify the name of the variable to store the exception into for the #try() directive