View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/ErrorsTag.java,v 1.18 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.18 $
4    * $Date: 2006/01/13 13:38:51 $
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;
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    // ------------------------------------------------------- Public Methods
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          // Extract out only user defined text
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          // Print the results to our output writer
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       // Continue processing this page
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       //Get user defined delimiter from messagePrefix attribute
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             //Check for delimiter
203             if ((delimiter != null)
204                       && ((index = message.indexOf(delimiter)) != -1)) {
205                // Add only what is needed
206                message = message.substring(index + delimiter.length());
207             }
208 
209             newErrors.add(message);
210          }
211       }
212 
213       return newErrors;
214    }
215 }