View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  
19  package org.apache.struts2.jasper.tagplugins.jstl.core;
20  
21  import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
22  import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
23  
24  public class Catch implements TagPlugin {
25  
26      public void doTag(TagPluginContext ctxt) {
27  
28          //flag for the existence of the var attribute
29          boolean hasVar = ctxt.isAttributeSpecified("var");
30  
31          //temp name for exception and caught
32          String exceptionName = ctxt.getTemporaryVariableName();
33          String caughtName = ctxt.getTemporaryVariableName();
34  
35          //main part to generate code
36          ctxt.generateJavaSource("boolean " + caughtName + " = false;");
37          ctxt.generateJavaSource("try{");
38          ctxt.generateBody();
39          ctxt.generateJavaSource("}");
40  
41          //do catch
42          ctxt.generateJavaSource("catch(Throwable " + exceptionName + "){");
43  
44          //if the var specified, the exception object should 
45          //be set to the attribute "var" defines in page scope 
46          if (hasVar) {
47              String strVar = ctxt.getConstantAttribute("var");
48              ctxt.generateJavaSource("    pageContext.setAttribute(\"" + strVar + "\", "
49                      + exceptionName + ", PageContext.PAGE_SCOPE);");
50          }
51  
52          //whenever there's exception caught, 
53          //the flag caught should be set true;
54          ctxt.generateJavaSource("    " + caughtName + " = true;");
55          ctxt.generateJavaSource("}");
56  
57          //do finally
58          ctxt.generateJavaSource("finally{");
59  
60          //if var specified, the attribute it defines 
61          //in page scope should be removed
62          if (hasVar) {
63              String strVar = ctxt.getConstantAttribute("var");
64              ctxt.generateJavaSource("    if(!" + caughtName + "){");
65              ctxt.generateJavaSource("        pageContext.removeAttribute(\"" + strVar + "\", PageContext.PAGE_SCOPE);");
66              ctxt.generateJavaSource("    }");
67          }
68  
69          ctxt.generateJavaSource("}");
70      }
71  
72  }