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
28
29
30
31
32
33 package org.dbforms.taglib;
34
35 import org.dbforms.config.DbFormsErrors;
36
37 import java.io.IOException;
38
39 import java.util.Iterator;
40 import java.util.Vector;
41
42 import javax.servlet.jsp.JspException;
43 import javax.servlet.jsp.JspWriter;
44 import javax.servlet.jsp.PageContext;
45
46
47
48 /***
49 * DOCUMENT ME!
50 *
51 * @author $author$
52 * @version $Revision: 1.17 $
53 */
54 public class DbXmlErrorsTag extends AbstractScriptHandlerTag
55 implements javax.servlet.jsp.tagext.TryCatchFinally {
56 private transient DbFormsErrors errors;
57 private String caption = "Error:";
58
59
60
61 /***
62 * Name of the request scope attribute containing our error messages, if
63 * any.
64 */
65 private String name = "errors";
66
67 /***
68 * DOCUMENT ME!
69 *
70 * @param caption DOCUMENT ME!
71 */
72 public void setCaption(String caption) {
73 this.caption = caption;
74 }
75
76
77 /***
78 * DOCUMENT ME!
79 *
80 * @return DOCUMENT ME!
81 */
82 public String getCaption() {
83 return (this.caption);
84 }
85
86
87 /***
88 * DOCUMENT ME!
89 *
90 * @param name DOCUMENT ME!
91 */
92 public void setName(String name) {
93 this.name = name;
94 }
95
96
97 /***
98 * DOCUMENT ME!
99 *
100 * @return DOCUMENT ME!
101 */
102 public String 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 pageContext) {
113 super.setPageContext(pageContext);
114 this.errors = (DbFormsErrors) pageContext.getServletContext()
115 .getAttribute(DbFormsErrors.ERRORS);
116 }
117
118
119
120 /***
121 * DOCUMENT ME!
122 */
123 public void doFinally() {
124 name = "errors";
125 caption = "Error:";
126 errors = null;
127 }
128
129
130
131
132 /***
133 * Render the specified error messages if there are any.
134 *
135 * @return DOCUMENT ME!
136 *
137 * @exception JspException if a JSP exception has occurred
138 */
139 public int doStartTag() throws JspException {
140 Vector transformedErrors = new Vector();
141
142 Vector originalErrors = (Vector) pageContext.getAttribute(name,
143 PageContext.REQUEST_SCOPE);
144
145 if (errors == null) {
146 throw new JspException("XML error handler is disabled, please supply xml error file on startup or use error tag instead!");
147 }
148
149 if ((originalErrors != null) && (originalErrors.size() > 0)) {
150 Iterator iter = originalErrors.iterator();
151
152 while (iter.hasNext()) {
153 Exception ex = (Exception) iter.next();
154
155 String result = errors.getXMLErrorMessage(ex.getMessage());
156
157
158 if (result != null) {
159 transformedErrors.add(result);
160 }
161 }
162
163 StringBuffer results = new StringBuffer();
164 results.append(caption);
165 results.append("<ul>");
166
167 for (int i = 0; i < transformedErrors.size(); i++) {
168 results.append("<li>");
169 results.append(transformedErrors.elementAt(i));
170 results.append("</li>");
171 }
172
173 results.append("</ul>");
174
175
176 JspWriter writer = pageContext.getOut();
177
178 try {
179 writer.print(results.toString());
180 } catch (IOException e) {
181 throw new JspException(e.toString());
182 }
183 }
184
185
186 return EVAL_BODY_INCLUDE;
187 }
188 }