KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > core > CatchTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.common.core;
18
19 import javax.servlet.jsp.PageContext JavaDoc;
20 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
21 import javax.servlet.jsp.tagext.TryCatchFinally JavaDoc;
22
23 /**
24  * <p>Tag handler for &lt;catch&gt; in JSTL 1.0.</p>
25  *
26  * <p>&lt;catch&gt; simply catches any Throwables that occur in its body
27  * and optionally exposes them.
28  *
29  * @author Shawn Bayern
30  */

31
32 public class CatchTag extends TagSupport JavaDoc implements TryCatchFinally JavaDoc {
33
34     /*
35      * If all tags that I proposed were this simple, people might
36      * think I was just trying to avoid work. :-)
37      */

38
39     //*********************************************************************
40
// Constructor and lifecycle management
41

42     // initialize inherited and local state
43
public CatchTag() {
44         super();
45         init();
46     }
47
48     // Releases any resources we may have (or inherit)
49
public void release() {
50         super.release();
51         init();
52     }
53
54     private void init() {
55         var = null;
56     }
57
58
59     //*********************************************************************
60
// Private state
61

62     private String JavaDoc var; // tag attribute
63
private boolean caught; // internal status
64

65
66     //*********************************************************************
67
// Tag logic
68

69     public int doStartTag() {
70         caught = false;
71     return EVAL_BODY_INCLUDE;
72     }
73
74     public void doCatch(Throwable JavaDoc t) {
75         if (var != null)
76             pageContext.setAttribute(var, t, PageContext.PAGE_SCOPE);
77         caught = true;
78     }
79
80     public void doFinally() {
81         if (var != null && !caught)
82             pageContext.removeAttribute(var, PageContext.PAGE_SCOPE);
83     }
84
85
86     //*********************************************************************
87
// Attribute accessors
88

89     public void setVar(String JavaDoc var) {
90         this.var = var;
91     }
92
93 }
94
Popular Tags