KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > mailer > ErrorTag


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.mailer;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
24
25 /**
26  * ErrorTag - JSP tag <b>Error</b> is used to get the error message when creating
27  * an error page.
28  *
29  * <tag>
30  * <name>error</name>
31  * <tagclass>org.apache.taglibs.mail.ErrorTag</tagclass>
32  * <bodycontent>JSP</bodycontent>
33  * <info>Get the error message if mail was unable to be sent</info>
34  *
35  * <attribute>
36  * <name>id</name>
37  * <required>true</required>
38  * <rtexprvalue>false</rtexprvalue>
39  * </attribute>
40  * </tag>
41  *
42  * @author Rich Catlett
43  *
44  * @version 1.0
45  *
46  */

47
48 public class ErrorTag extends BodyTagSupport JavaDoc {
49
50     // an iterator for the list of error messages
51
private Iterator JavaDoc errorlist = null;
52     private ArrayList JavaDoc errors = null; // the error message
53
private String JavaDoc error = null; // the current error from the list
54

55     /**
56      * implementation of the method from the tag interface that tells the JSP
57      * page what to do when the start tag is encountered
58      *
59      * @throws JspException thrown when an error occurs while processing the
60      * body of this method
61      *
62      * @return - SKIP_BODY if no errors exist, EVAL_BODY_TAG if errors do exist
63      *
64      */

65     public int doStartTag() throws JspException JavaDoc {
66
67     // parent tag must be a MailTag, gives access to methods in parent
68
SendTag myparent = (SendTag)findAncestorWithClass(this, SendTag.class);
69
70     if (myparent == null)
71         throw new JspException JavaDoc("error tag not nested within send tag");
72         else
73         if ((errors = myparent.getError()) == null)
74         return SKIP_BODY;
75
76     errorlist = errors.iterator();
77     if (!errorlist.hasNext())
78         return SKIP_BODY;
79
80     // get the next element in the list
81
error = (String JavaDoc)errorlist.next();
82
83     // final check to make sure property exists
84
if (error == null)
85         return SKIP_BODY;
86
87     pageContext.setAttribute(id, this, PageContext.PAGE_SCOPE);
88     // taglibs 1.1
89
return EVAL_BODY_TAG;
90     // taglibs 1.2
91
//return EVAL_BODY_BUFFERED;
92
}
93
94     /**
95      * Implementation of the method from the tag interface that gets called at
96      * end of each error tag.
97      *
98      * @return EVAL_BODY_TAG if there is another error, or SKIP_BODY if there
99      * are no more errors
100      */

101     public int doAfterBody() throws JspException JavaDoc
102     {
103     // See if this is the last or a named parameter
104
if (!errorlist.hasNext())
105         return SKIP_BODY;
106     // There is another parameter, so loop again
107
error = (String JavaDoc)errorlist.next();
108     if (error == null)
109         return SKIP_BODY;
110     // taglibs 1.1
111
return EVAL_BODY_TAG;
112     // taglibs 1.2
113
// return EVAL_BODY_AGAIN;
114
}
115
116     /**
117      * implementation of the method from the tag interface that tells the JSP
118      * page what to do when the start tag is encountered
119      *
120      * @throws JspException thrown when an error occurs while processing the
121      * body of this method
122      *
123      * @return - EVAL_PAGE
124      *
125      */

126     public int doEndTag() throws JspException JavaDoc {
127     try {
128         if (bodyContent != null)
129         bodyContent.writeOut(bodyContent.getEnclosingWriter());
130     } catch (java.io.IOException JavaDoc e) {
131         throw new JspException JavaDoc("IO Error: " + e.getMessage());
132     }
133     return EVAL_PAGE;
134     }
135
136     /**
137      * Returns the name of the current error.
138      * <p>
139      * &lt;jsp:getProperty name=<i>"id"</i> property="error"/&gt;
140      *
141      * @return String - the current error in the list
142      */

143     public String JavaDoc getError()
144     {
145     return error;
146     }
147
148     /**
149      * Remove the script variable after error tag is closed out
150      */

151     public void release()
152     {
153     if( id != null && id.length() > 0 )
154         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
155     }
156 }
157
Popular Tags