Source Code Cross Referenced for MessageUtils.java in  » J2EE » myfaces-core-1.2.0 » org » apache » myfaces » shared_impl » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » myfaces core 1.2.0 » org.apache.myfaces.shared_impl.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.myfaces.shared_impl.util;
017:
018:        import org.apache.commons.logging.Log;
019:        import org.apache.commons.logging.LogFactory;
020:
021:        import javax.faces.FactoryFinder;
022:        import javax.faces.application.Application;
023:        import javax.faces.application.ApplicationFactory;
024:        import javax.faces.application.FacesMessage;
025:        import javax.faces.context.FacesContext;
026:        import java.text.MessageFormat;
027:        import java.util.Locale;
028:        import java.util.MissingResourceException;
029:        import java.util.ResourceBundle;
030:
031:        /**
032:         * Utility class to support multilingual FacesMessages using ResourceBundles.
033:         * Standard messages are stored at <code>DEFAULT_BUNDLE</code>.<br>
034:         * The summary of the message is stored at the requested key value. The detail
035:         * of the message is stored at &lt;messageId&gt;_detail.
036:         *
037:         * @see FacesMessage
038:         * @see java.util.ResourceBundle
039:         *
040:         * @author Thomas Spiegl (latest modification by $Author: baranda $)
041:         * @author Manfred Geiler
042:         * @author Sean Schofield
043:         * @author Stpehan Strittmatter
044:         * @version $Revision: 544646 $ $Date: 2007-06-05 23:51:27 +0200 (Di, 05 Jun 2007) $
045:         */
046:        public final class MessageUtils {
047:            /** Utility class, do not instatiate */
048:            private MessageUtils() {
049:                // nope
050:            }
051:
052:            /** Default bundle for messages (<code>javax.faces.Messages</code>) */
053:            private static final String DEFAULT_BUNDLE = "javax.faces.Messages";
054:
055:            /** Suffix for message details (<code>_detail</code>)*/
056:            private static final String DETAIL_SUFFIX = "_detail";
057:            private static Log log = LogFactory.getLog(MessageUtils.class);
058:
059:            /**
060:             * @param severity serverity of message
061:             * @param messageId id of message
062:             * @param arg arument of message
063:             *
064:             * @return generated FacesMessage
065:             */
066:            public static FacesMessage getMessage(
067:                    FacesMessage.Severity severity, String messageId, Object arg) {
068:                return getMessage(severity, messageId, new Object[] { arg },
069:                        FacesContext.getCurrentInstance());
070:            }
071:
072:            /**
073:             * @param severity serverity of message
074:             * @param messageId id of message
075:             * @param args aruments of message
076:             *
077:             * @return generated FacesMessage
078:             */
079:            public static FacesMessage getMessage(
080:                    FacesMessage.Severity severity, String messageId,
081:                    Object[] args) {
082:                return getMessage(severity, messageId, args, FacesContext
083:                        .getCurrentInstance());
084:            }
085:
086:            public static FacesMessage getMessage(
087:                    FacesMessage.Severity severity, String messageId,
088:                    Object[] args, FacesContext facesContext) {
089:                FacesMessage message = getMessage(facesContext, messageId, args);
090:                message.setSeverity(severity);
091:
092:                return message;
093:            }
094:
095:            public static void addMessage(FacesMessage.Severity severity,
096:                    String messageId, Object[] args) {
097:                addMessage(severity, messageId, args, null, FacesContext
098:                        .getCurrentInstance());
099:            }
100:
101:            public static void addMessage(FacesMessage.Severity severity,
102:                    String messageId, Object[] args, FacesContext facesContext) {
103:                addMessage(severity, messageId, args, null, facesContext);
104:            }
105:
106:            public static void addMessage(FacesMessage.Severity severity,
107:                    String messageId, Object[] args, String forClientId) {
108:                addMessage(severity, messageId, args, forClientId, FacesContext
109:                        .getCurrentInstance());
110:            }
111:
112:            public static void addMessage(FacesMessage.Severity severity,
113:                    String messageId, Object[] args, String forClientId,
114:                    FacesContext facesContext) {
115:                if (log.isTraceEnabled()) {
116:                    log.trace("adding message " + messageId + " for clientId "
117:                            + forClientId);
118:                }
119:                facesContext.addMessage(forClientId, getMessage(severity,
120:                        messageId, args, facesContext));
121:            }
122:
123:            /**
124:             * Uses <code>MessageFormat</code> and the supplied parameters to fill in the param placeholders in the String.
125:             *
126:             * @param locale The <code>Locale</code> to use when performing the substitution.
127:             * @param msgtext The original parameterized String.
128:             * @param params The params to fill in the String with.
129:             * @return The updated String.
130:             */
131:            public static String substituteParams(Locale locale,
132:                    String msgtext, Object params[]) {
133:                String localizedStr = null;
134:                if (params == null || msgtext == null)
135:                    return msgtext;
136:                StringBuffer b = new StringBuffer(100);
137:                MessageFormat mf = new MessageFormat(msgtext);
138:                if (locale != null) {
139:                    mf.setLocale(locale);
140:                    b.append(mf.format(params));
141:                    localizedStr = b.toString();
142:                }
143:                return localizedStr;
144:            }
145:
146:            public static FacesMessage getMessage(String messageId,
147:                    Object params[]) {
148:                Locale locale = null;
149:                FacesContext context = FacesContext.getCurrentInstance();
150:                if (context != null && context.getViewRoot() != null) {
151:                    locale = context.getViewRoot().getLocale();
152:                    if (locale == null)
153:                        locale = Locale.getDefault();
154:                } else {
155:                    locale = Locale.getDefault();
156:                }
157:                return getMessage(locale, messageId, params);
158:            }
159:
160:            public static FacesMessage getMessage(Locale locale,
161:                    String messageId, Object params[]) {
162:                String summary = null;
163:                String detail = null;
164:                String bundleName = getApplication().getMessageBundle();
165:                ResourceBundle bundle = null;
166:
167:                if (bundleName != null) {
168:                    try {
169:                        bundle = ResourceBundle.getBundle(bundleName, locale,
170:                                org.apache.myfaces.shared_impl.util.ClassUtils
171:                                        .getCurrentLoader(bundleName));
172:                        summary = bundle.getString(messageId);
173:                    } catch (MissingResourceException e) {
174:                        // NoOp
175:                    }
176:                }
177:
178:                if (summary == null) {
179:                    try {
180:                        bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE,
181:                                locale,
182:                                org.apache.myfaces.shared_impl.util.ClassUtils
183:                                        .getCurrentLoader(DEFAULT_BUNDLE));
184:                        if (bundle == null) {
185:                            throw new NullPointerException();
186:                        }
187:                        summary = bundle.getString(messageId);
188:                    } catch (MissingResourceException e) {
189:                        // NoOp
190:                    }
191:                }
192:
193:                if (summary == null) {
194:                    summary = messageId;
195:                }
196:
197:                if (bundle == null) {
198:                    throw new NullPointerException(
199:                            "Unable to locate ResrouceBundle: bundle is null");
200:                }
201:                summary = substituteParams(locale, summary, params);
202:
203:                try {
204:                    detail = substituteParams(locale, bundle
205:                            .getString(messageId + DETAIL_SUFFIX), params);
206:                } catch (MissingResourceException e) {
207:                    // NoOp
208:                }
209:
210:                return new FacesMessage(summary, detail);
211:            }
212:
213:            /**
214:             * @param bundleBaseName baseName of ResourceBundle to load localized messages
215:             * @param messageId id of message
216:             * @param params parameters to set at localized message
217:             * @return generated FacesMessage
218:             */
219:            public static FacesMessage getMessage(String bundleBaseName,
220:                    String messageId, Object params[]) {
221:                return getMessage(bundleBaseName, getCurrentLocale(),
222:                        messageId, params);
223:            }
224:
225:            /**
226:             *
227:             * @return  currently applicable Locale for this request.
228:             */
229:            public static Locale getCurrentLocale() {
230:                Locale locale;
231:                FacesContext context = FacesContext.getCurrentInstance();
232:                if (context != null && context.getViewRoot() != null) {
233:                    locale = context.getViewRoot().getLocale();
234:                    if (locale == null)
235:                        locale = Locale.getDefault();
236:                } else {
237:                    locale = Locale.getDefault();
238:                }
239:
240:                return locale;
241:            }
242:
243:            /**
244:             * @param severity severity of message
245:             * @param bundleBaseName baseName of ResourceBundle to load localized messages
246:             * @param messageId id of message
247:             * @param params parameters to set at localized message
248:             * @return generated FacesMessage
249:             */
250:            public static FacesMessage getMessage(
251:                    FacesMessage.Severity severity, String bundleBaseName,
252:                    String messageId, Object params[]) {
253:                FacesMessage msg = getMessage(bundleBaseName, messageId, params);
254:                msg.setSeverity(severity);
255:
256:                return msg;
257:            }
258:
259:            /**
260:             * @param bundleBaseName baseName of ResourceBundle to load localized messages
261:             * @param locale current locale
262:             * @param messageId id of message
263:             * @param params parameters to set at localized message
264:             * @return generated FacesMessage
265:             */
266:            public static FacesMessage getMessage(String bundleBaseName,
267:                    Locale locale, String messageId, Object params[]) {
268:                if (bundleBaseName == null) {
269:                    throw new NullPointerException(
270:                            "Unable to locate ResrouceBundle: bundle is null");
271:                }
272:
273:                ResourceBundle bundle = ResourceBundle.getBundle(
274:                        bundleBaseName, locale);
275:
276:                return getMessage(bundle, messageId, params);
277:            }
278:
279:            /**
280:             * @param bundle ResourceBundle to load localized messages
281:             * @param messageId id of message
282:             * @param params parameters to set at localized message
283:             * @return generated FacesMessage
284:             */
285:            public static FacesMessage getMessage(ResourceBundle bundle,
286:                    String messageId, Object params[]) {
287:
288:                String summary = null;
289:                String detail = null;
290:
291:                try {
292:                    summary = bundle.getString(messageId);
293:                } catch (MissingResourceException e) {
294:                    // NoOp
295:                }
296:
297:                if (summary == null) {
298:                    summary = messageId;
299:                }
300:
301:                summary = substituteParams(bundle.getLocale(), summary, params);
302:
303:                try {
304:                    detail = substituteParams(bundle.getLocale(), bundle
305:                            .getString(messageId + DETAIL_SUFFIX), params);
306:                } catch (MissingResourceException e) {
307:                    // NoOp
308:                }
309:
310:                return new FacesMessage(summary, detail);
311:            }
312:
313:            /**
314:             *
315:             * @param context
316:             * @param messageId
317:             * @return generated FacesMessage
318:             */
319:            public static FacesMessage getMessage(FacesContext context,
320:                    String messageId) {
321:                return getMessage(context, messageId, ((Object[]) (null)));
322:            }
323:
324:            /**
325:             *
326:             * @param context
327:             * @param messageId
328:             * @param params
329:             * @return generated FacesMessage
330:             */
331:            public static FacesMessage getMessage(FacesContext context,
332:                    String messageId, Object params[]) {
333:                if (context == null || messageId == null)
334:                    throw new NullPointerException(" context " + context
335:                            + " messageId " + messageId);
336:                Locale locale = getCurrentLocale();
337:                if (null == locale)
338:                    throw new NullPointerException(" locale " + locale);
339:                FacesMessage message = getMessage(locale, messageId, params);
340:                if (message != null) {
341:                    return message;
342:                } else {
343:                    // TODO /FIX:  Note that this has fallback behavior to default Locale for message,
344:                    // but similar behavior above does not.  The methods should probably behave
345:                    locale = Locale.getDefault();
346:                    return getMessage(locale, messageId, params);
347:                }
348:            }
349:
350:            private static Application getApplication() {
351:                FacesContext context = FacesContext.getCurrentInstance();
352:                if (context != null) {
353:                    return FacesContext.getCurrentInstance().getApplication();
354:                }
355:
356:                ApplicationFactory afactory = (ApplicationFactory) FactoryFinder
357:                        .getFactory("javax.faces.application.ApplicationFactory");
358:                return afactory.getApplication();
359:
360:            }
361:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.