KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > taglib > ErrorMessageBundleTag


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.ext.taglib;
65
66 import com.jcorporate.expresso.core.controller.ControllerResponse;
67 import com.jcorporate.expresso.core.controller.ErrorCollection;
68 import com.jcorporate.expresso.core.i18n.Messages;
69 import com.jcorporate.expresso.ext.struts.taglib.ControllerUtils;
70 import org.apache.struts.action.ActionError;
71
72 import javax.servlet.http.HttpServletRequest JavaDoc;
73 import javax.servlet.jsp.JspTagException JavaDoc;
74 import javax.servlet.jsp.JspWriter JavaDoc;
75 import javax.servlet.jsp.PageContext JavaDoc;
76 import java.util.Iterator JavaDoc;
77 import java.util.Locale JavaDoc;
78 import java.util.Stack JavaDoc;
79
80
81 /**
82  * Description of the Class
83  * created June 27, 2002 *
84  *
85  * @author Created by: kris
86  * @version $Id: ErrorMessageBundleTag.java,v 1.15 2004/11/17 20:48:16 lhamel Exp $
87  */

88 public class ErrorMessageBundleTag extends ExpressoTagSupport {
89
90     /**
91      *
92      */

93     public ErrorMessageBundleTag() {
94         super();
95     }
96
97     private ErrorCollection errorCollection = null;
98     private String JavaDoc name = null;
99
100     private String JavaDoc nameToUse = null;
101
102     /*
103      * getName()
104      */

105
106
107     /**
108      * Insert the method's description here. Creation date: (12/29/2000 %r)
109      *
110      * @param newName The new Name value
111      */

112     public void setName(java.lang.String JavaDoc newName) {
113         name = newName;
114     }
115
116     /*
117      * doStartTag()
118      */

119
120     /**
121      * Setter method for Name.
122      *
123      * @return java.lang.String
124      */

125     public String JavaDoc getName() {
126         return nameToUse;
127     }
128
129     /*
130      * ErrorTag()
131      */

132
133     /**
134      * Standard doEndTag.
135      *
136      * @return int
137      * @throws javax.servlet.jsp.JspTagException
138      * Description of Exception
139      */

140     public int doEndTag()
141             throws javax.servlet.jsp.JspTagException JavaDoc {
142
143         nameToUse = name;
144
145         try {
146             if (nameToUse == null) {
147                 nameToUse = ErrorCollection.ERRORCOLLECTIONKEY;
148                 //by default
149
}
150             errorCollection = (ErrorCollection) pageContext.getAttribute(nameToUse, PageContext.REQUEST_SCOPE);
151
152             if (errorCollection == null) {
153                 errorCollection = (ErrorCollection) pageContext.getAttribute(nameToUse, PageContext.SESSION_SCOPE);
154             }
155
156             ControllerResponse myResponse =
157                     ControllerUtils.getResponse(pageContext);
158
159             Locale JavaDoc locale = (Locale JavaDoc) pageContext.getAttribute(Messages.LOCALE_KEY,
160                     PageContext.SESSION_SCOPE);
161
162             if (errorCollection != null) {
163                 if (errorCollection.getErrorCount() > 0) {
164                     //we have an errorcollection with some errors
165
JspWriter JavaDoc writer = pageContext.getOut();
166
167                     //Try the default application. If it doesn't exist there,
168
//then grab the one that we know exists in Expresso
169
try {
170                         writer.println(myResponse.getString("errors.header"));
171                     } catch (java.lang.IllegalArgumentException JavaDoc ex) {
172                         //We try to get it from the expresso schema if it
173
//doesn't exist
174
writer.println(Messages.getString(com.jcorporate.expresso.core.ExpressoSchema.class.getName(),
175                                 (HttpServletRequest JavaDoc) pageContext.getRequest(),
176                                 "errors.header", new Object JavaDoc[]{}));
177                     }
178                     ActionError oneError = null;
179
180                     Iterator JavaDoc iter = iter = errorCollection.get();
181
182                     while (iter.hasNext()) {
183                         oneError = (ActionError) iter.next();
184                         try {
185                             Stack JavaDoc s = myResponse.getSchemaStack();
186
187                             writer.println(Messages.getString(s, locale, "errors.item.header", null));
188                             writer.println(Messages.getString(s, locale, oneError.getKey(), oneError.getValues()));
189                             writer.println(Messages.getString(s, locale, "errors.item.footer", null));
190                         } catch (Exception JavaDoc nokey) {
191                             writer.println(oneError.getKey());
192                         }
193                     }
194
195                     //Try the default application. If it doesn't exist there,
196
//then grab the one that we know exists in Expresso
197
try {
198                         writer.println(myResponse.getString("errors.footer"));
199                     } catch (java.lang.IllegalArgumentException JavaDoc ex) {
200                         //We try to get it from the expresso schema if it
201
//doesn't exist
202
writer.println(Messages.getString(com.jcorporate.expresso.core.ExpressoSchema.class.getName(),
203                                 (HttpServletRequest JavaDoc) pageContext.getRequest(),
204                                 "errors.footer", new Object JavaDoc[]{}));
205                     }
206                 }
207             }
208         } catch (Exception JavaDoc e) {
209             throw new JspTagException JavaDoc("ErrorMessageBundleTag: " + e.getMessage());
210         }
211
212         return EVAL_PAGE;
213     }
214
215     /*
216      * doEndTag()
217      */

218
219     /**
220      * Do nothing until end tag.
221      *
222      * @return int
223      * @throws javax.servlet.jsp.JspTagException
224      * Description of Exception
225      * @throws javax.servlet.jsp.JspTagException
226      * The exception description.
227      */

228     public int doStartTag()
229             throws javax.servlet.jsp.JspTagException JavaDoc {
230
231         return SKIP_BODY;
232     }
233     /*
234      * setName(java.lang.String)
235      */

236
237
238 }
239
240 /*
241  * ErrorTag
242  */

243
244
Popular Tags