KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > taglib > DbXmlErrorsTag


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbXmlErrorsTag.java,v 1.14 2004/10/11 08:55:03 hkollmann Exp $
3  * $Revision: 1.14 $
4  * $Date: 2004/10/11 08:55:03 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 /*
25  * Grunikiewicz.philip@hydro.qc.ca
26  * 2001-12-18
27  *
28  * Custom tag that renders error messages if an appropriate request attribute
29  * has been created.
30  *
31  * Error messages are retrieved via an xml file
32  */

33 package org.dbforms.taglib;
34
35 import org.dbforms.config.DbFormsErrors;
36
37 import java.io.IOException JavaDoc;
38
39 import java.util.Iterator JavaDoc;
40 import java.util.Vector JavaDoc;
41
42 import javax.servlet.jsp.JspException JavaDoc;
43 import javax.servlet.jsp.JspWriter JavaDoc;
44 import javax.servlet.jsp.PageContext JavaDoc;
45
46
47
48 /**
49  * DOCUMENT ME!
50  *
51  * @author $author$
52  * @version $Revision: 1.14 $
53  */

54 public class DbXmlErrorsTag extends TagSupportWithScriptHandler
55    implements javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
56    private transient DbFormsErrors errors;
57    private String JavaDoc caption = "Error:";
58
59    // ----------------------------------------------------------- Properties
60

61    /**
62     * Name of the request scope attribute containing our error messages, if
63     * any.
64     */

65    private String JavaDoc name = "errors";
66
67    /**
68     * DOCUMENT ME!
69     *
70     * @param caption DOCUMENT ME!
71     */

72    public void setCaption(String JavaDoc caption) {
73       this.caption = caption;
74    }
75
76
77    /**
78     * DOCUMENT ME!
79     *
80     * @return DOCUMENT ME!
81     */

82    public String JavaDoc getCaption() {
83       return (this.caption);
84    }
85
86
87    /**
88     * DOCUMENT ME!
89     *
90     * @param name DOCUMENT ME!
91     */

92    public void setName(String JavaDoc name) {
93       this.name = name;
94    }
95
96
97    /**
98     * DOCUMENT ME!
99     *
100     * @return DOCUMENT ME!
101     */

102    public String JavaDoc getName() {
103       return (this.name);
104    }
105
106
107    /**
108     * DOCUMENT ME!
109     *
110     * @param pageContext DOCUMENT ME!
111     */

112    public void setPageContext(final javax.servlet.jsp.PageContext JavaDoc pageContext) {
113       super.setPageContext(pageContext);
114       this.errors = (DbFormsErrors) pageContext.getServletContext()
115                                                .getAttribute(DbFormsErrors.ERRORS);
116    }
117
118
119    /**
120     * DOCUMENT ME!
121     *
122     * @param t DOCUMENT ME!
123     *
124     * @throws Throwable DOCUMENT ME!
125     */

126    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
127       throw t;
128    }
129
130
131    /**
132     * DOCUMENT ME!
133     */

134    public void doFinally() {
135       name = "errors";
136       caption = "Error:";
137       errors = null;
138    }
139
140
141    // ------------------------------------------------------- Public Methods
142

143    /**
144     * Render the specified error messages if there are any.
145     *
146     * @return DOCUMENT ME!
147     *
148     * @exception JspException if a JSP exception has occurred
149     */

150    public int doStartTag() throws JspException JavaDoc {
151       Vector JavaDoc transformedErrors = new Vector JavaDoc();
152
153       Vector JavaDoc originalErrors = (Vector JavaDoc) pageContext.getAttribute(name,
154                                                                 PageContext.REQUEST_SCOPE);
155
156       if (errors == null) {
157          throw new JspException JavaDoc("XML error handler is disabled, please supply xml error file on startup or use error tag instead!");
158       }
159
160       if ((originalErrors != null) && (originalErrors.size() > 0)) {
161          Iterator JavaDoc iter = originalErrors.iterator();
162
163          while (iter.hasNext()) {
164             Exception JavaDoc ex = (Exception JavaDoc) iter.next();
165
166             String JavaDoc result = errors.getXMLErrorMessage(ex.getMessage());
167
168             // ignore empty messages
169
if (result != null) {
170                transformedErrors.add(result);
171             }
172          }
173
174          StringBuffer JavaDoc results = new StringBuffer JavaDoc();
175          results.append(caption);
176          results.append("<ul>");
177
178          for (int i = 0; i < transformedErrors.size(); i++) {
179             results.append("<li>");
180             results.append(transformedErrors.elementAt(i));
181             results.append("</li>");
182          }
183
184          results.append("</ul>");
185
186          // Print the results to our output writer
187
JspWriter JavaDoc writer = pageContext.getOut();
188
189          try {
190             writer.print(results.toString());
191          } catch (IOException JavaDoc e) {
192             throw new JspException JavaDoc(e.toString());
193          }
194       }
195
196       // Continue processing this page
197
return EVAL_BODY_INCLUDE;
198    }
199 }
200
Popular Tags