KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/ErrorsTag.java,v 1.15 2004/10/11 08:55:03 hkollmann Exp $
3  * $Revision: 1.15 $
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  * the idea for this class was taken from the class ErrorsTag-class of the Apache Struts Project
26  */

27 package org.dbforms.taglib;
28
29 import org.dbforms.util.MessageResources;
30
31 import java.io.IOException JavaDoc;
32
33 import java.util.Enumeration JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.jsp.JspException JavaDoc;
38 import javax.servlet.jsp.JspWriter JavaDoc;
39 import javax.servlet.jsp.PageContext JavaDoc;
40
41
42
43 /**
44  * Custom tag that renders error messages if an appropriate request attribute
45  * has been created.
46  *
47  * <p>
48  * This idea for this class came from a - much more powerful - class done by
49  * Craig R. McClanahan for the Apache struts framework.
50  * </p>
51  *
52  * @author Joe Peer
53  */

54 public class ErrorsTag extends TagSupportWithScriptHandler
55    implements javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
56    private String JavaDoc caption = "Error:";
57    private String JavaDoc messagePrefix;
58    private String JavaDoc name = "errors";
59
60    /**
61     * DOCUMENT ME!
62     *
63     * @param caption DOCUMENT ME!
64     */

65    public void setCaption(String JavaDoc caption) {
66       this.caption = caption;
67    }
68
69
70    /**
71     * DOCUMENT ME!
72     *
73     * @return DOCUMENT ME!
74     */

75    public String JavaDoc getCaption() {
76       return (this.caption);
77    }
78
79
80    /**
81     * Insert the method's description here. Creation date: (2001-05-10
82     * 12:57:08)
83     *
84     * @param newMessagePrefix java.lang.String
85     */

86    public void setMessagePrefix(java.lang.String JavaDoc newMessagePrefix) {
87       messagePrefix = newMessagePrefix;
88    }
89
90
91    /**
92     * Insert the method's description here. Creation date: (2001-05-10
93     * 12:57:08)
94     *
95     * @return java.lang.String
96     */

97    public java.lang.String JavaDoc getMessagePrefix() {
98       return messagePrefix;
99    }
100
101
102    /**
103     * DOCUMENT ME!
104     *
105     * @param name DOCUMENT ME!
106     */

107    public void setName(String JavaDoc name) {
108       this.name = name;
109    }
110
111
112    /**
113     * DOCUMENT ME!
114     *
115     * @return DOCUMENT ME!
116     */

117    public String JavaDoc getName() {
118       return (this.name);
119    }
120
121
122    /**
123     * DOCUMENT ME!
124     *
125     * @param t DOCUMENT ME!
126     *
127     * @throws Throwable DOCUMENT ME!
128     */

129    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
130       throw t;
131    }
132
133
134    /**
135     * DOCUMENT ME!
136     */

137    public void doFinally() {
138       messagePrefix = null;
139       name = "errors";
140       caption = "Error:";
141    }
142
143
144    // ------------------------------------------------------- Public Methods
145

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

153    public int doStartTag() throws JspException JavaDoc {
154       Vector JavaDoc originalErrors = (Vector JavaDoc) pageContext.getAttribute(getName(),
155                                                                             PageContext.REQUEST_SCOPE);
156       HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
157
158       if ((originalErrors != null) && (originalErrors.size() > 0)) {
159          // Extract out only user defined text
160
Vector JavaDoc errors = this.extractUserDefinedErrors(originalErrors);
161
162          StringBuffer JavaDoc results = new StringBuffer JavaDoc();
163          results.append(caption);
164
165          results.append("<ul>");
166
167          for (int i = 0; i < errors.size(); i++) {
168             results.append("<li>");
169             results.append(MessageResources.getMessage(request,
170                                                        (String JavaDoc) errors
171                                                        .elementAt(i)));
172             results.append("</li>");
173          }
174
175          results.append("</ul>");
176
177          // Print the results to our output writer
178
JspWriter JavaDoc writer = pageContext.getOut();
179
180          try {
181             writer.print(results.toString());
182          } catch (IOException JavaDoc e) {
183             throw new JspException JavaDoc(e.toString());
184          }
185       }
186
187       // Continue processing this page
188
return EVAL_BODY_INCLUDE;
189    }
190
191
192    /**
193     * philip.grunikiewicz 2001-05-10 Iterate through the errors and extract
194     * only user-specified text
195     *
196     * @param errors DOCUMENT ME!
197     *
198     * @return DOCUMENT ME!
199     */

200    public Vector JavaDoc extractUserDefinedErrors(Vector JavaDoc errors) {
201       Vector JavaDoc newErrors = new Vector JavaDoc();
202       String JavaDoc message = null;
203       int index = 0;
204
205       //Get user defined delimiter from messagePrefix attribute
206
String JavaDoc delimiter = this.getMessagePrefix();
207
208       if (errors != null) {
209          Enumeration JavaDoc e = errors.elements();
210
211          while (e.hasMoreElements()) {
212             message = ((Exception JavaDoc) e.nextElement()).getMessage();
213
214             //Check for delimiter
215
if ((delimiter != null)
216                       && ((index = message.indexOf(delimiter)) != -1)) {
217                // Add only what is needed
218
message = message.substring(index + delimiter.length());
219             }
220
221             newErrors.add(message);
222          }
223       }
224
225       return newErrors;
226    }
227 }
228
Popular Tags