KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > ErrorBaseTag


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.pageflow.config.PageFlowActionMapping;
21 import org.apache.beehive.netui.pageflow.config.PageFlowControllerConfig;
22 import org.apache.beehive.netui.pageflow.internal.InternalConstants;
23 import org.apache.beehive.netui.pageflow.internal.InternalExpressionUtils;
24 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
25 import org.apache.beehive.netui.pageflow.PageFlowUtils;
26 import org.apache.beehive.netui.tags.AbstractSimpleTag;
27 import org.apache.beehive.netui.util.Bundle;
28 import org.apache.beehive.netui.util.logging.Logger;
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.action.ActionMessage;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.config.ControllerConfig;
34 import org.apache.struts.config.ModuleConfig;
35 import org.apache.struts.taglib.html.Constants;
36 import org.apache.struts.util.MessageResources;
37 import org.apache.struts.util.RequestUtils;
38
39 import javax.servlet.ServletContext JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.jsp.JspException JavaDoc;
42 import javax.servlet.jsp.PageContext JavaDoc;
43 import javax.servlet.jsp.el.ELException JavaDoc;
44 import java.text.MessageFormat JavaDoc;
45 import java.util.Locale JavaDoc;
46
47 abstract public class ErrorBaseTag extends AbstractSimpleTag
48 {
49     private static final Logger LOGGER = Logger.getInstance(ErrorBaseTag.class);
50
51     /**
52      * The default locale on our server.
53      */

54     protected static Locale JavaDoc defaultLocale = Locale.getDefault();
55
56     /**
57      * The name of the message bundle, as defined in the page flow's
58      * {@link org.apache.beehive.netui.pageflow.annotations.Jpf.MessageBundle} annotation,
59      * where the error messages can be found. This defaults to org.apache.struts.action.Action.MESSAGES_KEY.
60      */

61     protected String JavaDoc _bundleName = null;
62
63     /**
64      * The session attribute key for the locale.
65      * This defaults to org.apache.struts.action.Action.LOCALE_KEY.
66      */

67     protected String JavaDoc _locale = Globals.LOCALE_KEY;
68
69     /**
70      * Set the name of the message bundle, as defined in the page flow's
71      * {@link org.apache.beehive.netui.pageflow.annotations.Jpf.MessageBundle} annotation, where the error messages
72      * can be found. If this attribute is not set, the page flow's default message bundle is used.
73      * @param bundleName the bundle name
74      * @jsptagref.attributedescription The name of the message bundle, as defined in the page flow's
75      * Jpf.MessageBundle annotation. This defaults to org.apache.struts.action.Action.MESSAGES_KEY.
76      * @jsptagref.databindable false
77      * @jsptagref.attributesyntaxvalue <i>string_bundleName</i>
78      * @netui:attribute required="false" rtexprvalue="true"
79      * description="The name of the message bundle, as defined in the page flow's Jpf.MessageBundle annotation."
80      */

81     public final void setBundleName(String JavaDoc bundleName)
82             throws JspException JavaDoc
83     {
84         _bundleName = setRequiredValueAttribute(bundleName, "bundleName");
85     }
86
87     /**
88      * Set the name of the locale attribute.
89      * @param locale the locale attribute name
90      * @jsptagref.attributedescription The name of the session attribute key for the user's locale.
91      * This defaults to org.apache.struts.action.Action.LOCALE_KEY.
92      * @jsptagref.databindable false
93      * @jsptagref.attributesyntaxvalue <i>string_locale</i>
94      * @netui:attribute required="false" rtexprvalue="true"
95      * description="The name of the session attribute key for the user's locale"
96      */

97     public final void setLocale(String JavaDoc locale)
98     {
99         _locale = setNonEmptyValueAttribute(locale);
100     }
101
102
103     /**
104      * @param report
105      * @param bundleName
106      * @return message
107      * @throws JspException
108      */

109     protected String JavaDoc getErrorMessage(ActionMessage report, String JavaDoc bundleName)
110             throws JspException JavaDoc
111     {
112         String JavaDoc key = report.getKey();
113         Object JavaDoc[] messageArgs = report.getValues();
114         PageContext JavaDoc pageContext = getPageContext();
115
116         String JavaDoc message = null;
117         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
118         if (key.length() == 0) {
119             return "";
120         }
121         else {
122             ModuleConfig curModuleConfig = RequestUtils.getModuleConfig(pageContext);
123             ServletContext JavaDoc servletContext = pageContext.getServletContext();
124             
125             // First, look in the message bundle for a shared flow that was involved in this request.
126
String JavaDoc sharedFlowModulePath = InternalUtils.getForwardingModule(pageContext.getRequest());
127             if (sharedFlowModulePath != null &&
128                 (curModuleConfig == null || !sharedFlowModulePath.equals(curModuleConfig.getPrefix()))) {
129                 ModuleConfig sfModule = InternalUtils.getModuleConfig(sharedFlowModulePath, servletContext);
130                 if (bundleName != null || !isMissingUserDefaultMessages(sfModule)) {
131                     String JavaDoc msgAttr = (bundleName != null ? bundleName : Globals.MESSAGES_KEY) + sfModule.getPrefix();
132                     MessageResources resources = (MessageResources) servletContext.getAttribute(msgAttr);
133                     message = getMessage(resources, key, messageArgs, pageContext);
134                 }
135             }
136                 
137             // Next look in the default message bundle for the page flow.
138
boolean missingUserDefaultMessages = isMissingUserDefaultMessages(pageContext);
139             if (message == null && (bundleName != null || !missingUserDefaultMessages)) {
140                 MessageResources resources =
141                         InternalUtils.getMessageResources(bundleName != null ? bundleName : Globals.MESSAGES_KEY,
142                                                           request, servletContext);
143                 message = getMessage(resources, key, messageArgs, pageContext);
144             }
145             
146             // If we still didn't find it, try the default validation message bundle (in beehive-netui-pageflow.jar).
147
if (message == null && bundleName == null) {
148                 MessageResources resources =
149                         InternalUtils.getMessageResources("_defaultValidationMessages", request, servletContext);
150                 message = getMessage(resources, key, messageArgs, pageContext);
151             }
152             
153             //
154
// We've run out of options -- the message simply doesn't exist. If the user didn't specify a default
155
// message bundle in the page flow, that's the problem; otherwise, it's simply a missing message.
156
// Register a tag error for either case.
157
//
158
if (message == null) {
159                 if (bundleName == null && missingUserDefaultMessages) {
160                     String JavaDoc s = Bundle.getString("Tags_ErrorsBundleMissing", key);
161                     registerTagError(s, null);
162                     return null;
163                 }
164                 else {
165                     String JavaDoc s = Bundle.getString("Tags_ErrorsMessageMissing", key);
166                     registerTagError(s, null);
167                     return null;
168                 }
169             }
170         }
171
172         return message;
173     }
174
175     private String JavaDoc getMessage(MessageResources resources, String JavaDoc key, Object JavaDoc[] messageArgs, PageContext JavaDoc pageContext)
176     {
177         if (resources != null) {
178             Locale JavaDoc userLocale = RequestUtils.retrieveUserLocale( pageContext, _locale );
179             if ( messageArgs == null ) {
180                 return resources.getMessage( userLocale, key );
181             }
182             else {
183                 return resources.getMessage( userLocale, key, messageArgs );
184             }
185         }
186         
187         return null;
188     }
189     
190     /**
191      * Tell whether the given Struts module has no default message bundle defined.
192      * @return <code>true</code> if the given Struts module has no user-specified default message bundle.
193      */

194     protected static boolean isMissingUserDefaultMessages(PageContext JavaDoc pageContext)
195     {
196         return isMissingUserDefaultMessages(RequestUtils.getModuleConfig(pageContext))
197                && pageContext.getRequest().getAttribute(Globals.MESSAGES_KEY) == null;
198     }
199
200     /**
201      * Tell whether the given Struts module has no default message bundle defined.
202      * @return <code>true</code> if the given Struts module has no user-specified default message bundle.
203      */

204     protected static boolean isMissingUserDefaultMessages(ModuleConfig mc)
205     {
206         if (mc != null) {
207             ControllerConfig cc = mc.getControllerConfig();
208
209             if (cc instanceof PageFlowControllerConfig) {
210                 return ((PageFlowControllerConfig) cc).isMissingDefaultMessages();
211             }
212         }
213
214         return false;
215     }
216 }
217
Popular Tags