1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.dbforms.taglib;
28
29 import org.dbforms.util.MessageResources;
30
31 import java.io.IOException;
32
33 import java.util.Enumeration;
34 import java.util.Vector;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.jsp.JspException;
38 import javax.servlet.jsp.JspWriter;
39 import javax.servlet.jsp.PageContext;
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 AbstractScriptHandlerTag
55 implements javax.servlet.jsp.tagext.TryCatchFinally {
56 private String caption = "Error:";
57 private String messagePrefix;
58 private String name = "errors";
59
60 /***
61 * DOCUMENT ME!
62 *
63 * @param caption DOCUMENT ME!
64 */
65 public void setCaption(String caption) {
66 this.caption = caption;
67 }
68
69
70 /***
71 * DOCUMENT ME!
72 *
73 * @return DOCUMENT ME!
74 */
75 public String 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 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 getMessagePrefix() {
98 return messagePrefix;
99 }
100
101
102 /***
103 * DOCUMENT ME!
104 *
105 * @param name DOCUMENT ME!
106 */
107 public void setName(String name) {
108 this.name = name;
109 }
110
111
112 /***
113 * DOCUMENT ME!
114 *
115 * @return DOCUMENT ME!
116 */
117 public String getName() {
118 return (this.name);
119 }
120
121
122 /***
123 * DOCUMENT ME!
124 */
125 public void doFinally() {
126 messagePrefix = null;
127 name = "errors";
128 caption = "Error:";
129 }
130
131
132
133
134 /***
135 * Render the specified error messages if there are any.
136 *
137 * @return DOCUMENT ME!
138 *
139 * @exception JspException if a JSP exception has occurred
140 */
141 public int doStartTag() throws JspException {
142 Vector originalErrors = (Vector) pageContext.getAttribute(getName(),
143 PageContext.REQUEST_SCOPE);
144 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
145
146 if ((originalErrors != null) && (originalErrors.size() > 0)) {
147
148 Vector errors = this.extractUserDefinedErrors(originalErrors);
149
150 StringBuffer results = new StringBuffer();
151 results.append(caption);
152
153 results.append("<ul>");
154
155 for (int i = 0; i < errors.size(); i++) {
156 results.append("<li>");
157 results.append(MessageResources.getMessage(request,
158 (String) errors
159 .elementAt(i)));
160 results.append("</li>");
161 }
162
163 results.append("</ul>");
164
165
166 JspWriter writer = pageContext.getOut();
167
168 try {
169 writer.print(results.toString());
170 } catch (IOException e) {
171 throw new JspException(e.toString());
172 }
173 }
174
175
176 return EVAL_BODY_INCLUDE;
177 }
178
179
180 /***
181 * philip.grunikiewicz 2001-05-10 Iterate through the errors and extract
182 * only user-specified text
183 *
184 * @param errors DOCUMENT ME!
185 *
186 * @return DOCUMENT ME!
187 */
188 public Vector extractUserDefinedErrors(Vector errors) {
189 Vector newErrors = new Vector();
190 String message = null;
191 int index = 0;
192
193
194 String delimiter = this.getMessagePrefix();
195
196 if (errors != null) {
197 Enumeration e = errors.elements();
198
199 while (e.hasMoreElements()) {
200 message = ((Exception) e.nextElement()).getMessage();
201
202
203 if ((delimiter != null)
204 && ((index = message.indexOf(delimiter)) != -1)) {
205
206 message = message.substring(index + delimiter.length());
207 }
208
209 newErrors.add(message);
210 }
211 }
212
213 return newErrors;
214 }
215 }