View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbXmlErrorsTag.java,v 1.17 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.17 $
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   * 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;
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     // ----------------------------------------------------------- Properties
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    // ------------------------------------------------------- Public Methods
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             // ignore empty messages
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          // Print the results to our output writer
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       // Continue processing this page
186       return EVAL_BODY_INCLUDE;
187    }
188 }