1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity.introspection

File TryCatchDirective.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

8
17
3
1
100
52
8
0.47
5.67
3
2.67

Classes

Class Line # Actions
TryCatchDirective 50 17 0% 8 6
0.7857142778.6%
 

Contributing tests

This file is covered by 60 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.velocity.introspection;
21   
22    import java.io.IOException;
23    import java.io.Writer;
24   
25    import org.apache.velocity.context.InternalContextAdapter;
26    import org.apache.velocity.exception.MethodInvocationException;
27    import org.apache.velocity.exception.ParseErrorException;
28    import org.apache.velocity.exception.ResourceNotFoundException;
29    import org.apache.velocity.runtime.directive.Directive;
30    import org.apache.velocity.runtime.parser.node.ASTBlock;
31    import org.apache.velocity.runtime.parser.node.Node;
32   
33    /**
34    * Allows catching exception from Velocity. Example usage:
35    * <pre>
36    * {@code #try()
37    * ...
38    * #end
39    *
40    * ## At this stage $exception contains the exception if any has been thrown.
41    * ## It can be tested like this:
42    * #if ("$!exception" != '')
43    * ## An exception has been thrown
44    * #end}
45    * </pre>
46    *
47    * @version $Id: 46ce1f6ad6e46a92e92f8c62a6a16fbaf67c65e7 $
48    * @since 6.3M1
49    */
 
50    public class TryCatchDirective extends Directive
51    {
52    private static final String EXCEPTION_KEY_NAME = "exception";
53   
 
54  5893 toggle @Override
55    public String getName()
56    {
57  5893 return "try";
58    }
59   
 
60  5783 toggle @Override
61    public int getType()
62    {
63  5783 return BLOCK;
64    }
65   
 
66  5769 toggle @Override
67    public boolean render(InternalContextAdapter context, Writer writer, Node node)
68    throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException
69    {
70  5769 boolean result = true;
71   
72  5770 String exceptionVariableName = EXCEPTION_KEY_NAME;
73  11538 for (int i = 0; i < node.jjtGetNumChildren(); i++) {
74  11539 if (node.jjtGetChild(i) != null) {
75  11538 if (!(node.jjtGetChild(i) instanceof ASTBlock)) {
76    // Reading and casting inline parameters. We handle one parameter which is the name of the Velocity
77    // variable in which we'll save the exception. If not specified we'll use EXCEPTION_KEY_NAME
78  5769 if (i == 0) {
79  5769 exceptionVariableName = (String) node.jjtGetChild(i).value(context);
80    } else {
81  0 break;
82    }
83    } else {
84  5769 try {
85    // Make sure to clear any previous exception
86  5769 context.remove(exceptionVariableName);
87  5769 result = node.jjtGetChild(i).render(context, writer);
88    } catch (Exception e) {
89    // Save the exception in the $exception velocity context variable
90  0 context.put(exceptionVariableName, e);
91  0 result = true;
92    }
93  5770 break;
94    }
95    }
96    }
97   
98  5770 return result;
99    }
100    }