myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sc...@apache.org
Subject svn commit: r168206 - /myfaces/trunk/src/share/org/apache/myfaces/util/MessageUtils.java
Date Wed, 04 May 2005 23:00:25 GMT
Author: schof
Date: Wed May  4 16:00:24 2005
New Revision: 168206

URL: http://svn.apache.org/viewcvs?rev=168206&view=rev
Log:
Refactored some of the message code to make it a little easier to use

Modified:
    myfaces/trunk/src/share/org/apache/myfaces/util/MessageUtils.java

Modified: myfaces/trunk/src/share/org/apache/myfaces/util/MessageUtils.java
URL: http://svn.apache.org/viewcvs/myfaces/trunk/src/share/org/apache/myfaces/util/MessageUtils.java?rev=168206&r1=168205&r2=168206&view=diff
==============================================================================
--- myfaces/trunk/src/share/org/apache/myfaces/util/MessageUtils.java (original)
+++ myfaces/trunk/src/share/org/apache/myfaces/util/MessageUtils.java Wed May  4 16:00:24
2005
@@ -18,8 +18,14 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import org.apache.myfaces.util.ClassUtils;
+
 import javax.faces.application.FacesMessage;
+import javax.faces.application.ApplicationFactory;
+import javax.faces.application.Application;
 import javax.faces.context.FacesContext;
+import javax.faces.FactoryFinder;
+
 import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.MissingResourceException;
@@ -28,6 +34,7 @@
 /**
  * @author Thomas Spiegl (latest modification by $Author$)
  * @author Manfred Geiler
+ * @author Sean Schofield
  * @version $Revision$ $Date$
  * $Log$
  * Revision 1.4  2004/10/13 11:51:01  matze
@@ -75,20 +82,10 @@
                                           Object[] args,
                                           FacesContext facesContext)
     {
-        Locale locale;
-        if (facesContext.getViewRoot() != null)
-        {
-            locale = facesContext.getViewRoot().getLocale();
-        }
-        else
-        {
-            locale = facesContext.getApplication().getDefaultLocale();
-        }
-        return internalGetMessage(facesContext,
-                                  locale,
-                                  severity,
-                                  messageId,
-                                  args);
+        FacesMessage message = getMessage(facesContext, messageId, args);
+        message.setSeverity(severity);
+        
+        return message;
     }
 
     public static void addMessage(FacesMessage.Severity severity,
@@ -123,128 +120,136 @@
         facesContext.addMessage(forClientId,
                                 getMessage(severity, messageId, args, facesContext));
     }
-
-
-
-
-    private static FacesMessage internalGetMessage(FacesContext facesContext,
-                                                   Locale locale,
-                                                   FacesMessage.Severity severity,
-                                                   String messageId,
-                                                   Object args[])
-    {
-        ResourceBundle appBundle;
-        ResourceBundle defBundle;
-        String summary;
-        String detail;
-
-        appBundle = getApplicationBundle(facesContext, locale);
-        summary = getBundleString(appBundle, messageId);
-        if (summary != null)
-        {
-            detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
-        }
-        else
+ 
+    /**
+     * Uses <code>MessageFormat</code> and the supplied parameters to fill in
the param placeholders in the String.
+     * 
+     * @param locale The <code>Locale</code> to use when performing the substitution.
+     * @param msgtext The original parameterized String.
+     * @param params The params to fill in the String with.
+     * @return The updated String.
+     */
+    public static String substituteParams(Locale locale, String msgtext, Object params[])
+    {
+        String localizedStr = null;
+        if(params == null || msgtext == null)
+            return msgtext;
+        StringBuffer b = new StringBuffer(100);
+        MessageFormat mf = new MessageFormat(msgtext);
+        if(locale != null)
+        {
+            mf.setLocale(locale);
+            b.append(mf.format(((Object) (params))));
+            localizedStr = b.toString();
+        }
+        return localizedStr;
+    }
+
+    public static FacesMessage getMessage(String messageId, Object params[])
+    {
+        Locale locale = null;
+        FacesContext context = FacesContext.getCurrentInstance();
+        if(context != null && context.getViewRoot() != null)
+        {
+            locale = context.getViewRoot().getLocale();
+            if(locale == null)
+                locale = Locale.getDefault();
+        } else
+        {
+            locale = Locale.getDefault();
+        }
+        return getMessage(locale, messageId, params);
+    }
+
+    public static FacesMessage getMessage(Locale locale, String messageId, Object params[])
+    {
+        String summary = null;
+        String detail = null;
+        String bundleName = getApplication().getMessageBundle();
+        ResourceBundle bundle = null;        
+        
+        if (bundleName != null)
         {
-            defBundle = getDefaultBundle(locale);
-            summary = getBundleString(defBundle, messageId);
-            if (summary != null)
+            bundle = ResourceBundle.getBundle(bundleName, locale);
+            try 
             {
-                detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
-            }
-            else
-            {
-                //Try to find detail alone
-                detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
-                if (detail != null)
-                {
-                    summary = null;
-                }
-                else
-                {
-                    detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
-                    if (detail != null)
-                    {
-                        summary = null;
-                    }
-                    else
-                    {
-                        //Neither detail nor summary found
-                        if (log.isWarnEnabled()) log.warn("No message with id " + messageId
+ " found in any bundle");
-                        return new FacesMessage(severity, messageId, null);
-                    }
-                }
+                summary = bundle.getString(messageId);
             }
+            catch (MissingResourceException e) {}
         }
 
-        if (args != null && args.length > 0)
+        if (summary == null)
         {
-            MessageFormat format;
-
-            if (summary != null)
+            bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE, locale, ClassUtils.getCurrentLoader(bundleName));
+            if(bundle == null)
             {
-                format = new MessageFormat(summary, locale);
-                summary = format.format(args);
+                throw new NullPointerException();
             }
-
-            if (detail != null)
+            
+            try
             {
-                format = new MessageFormat(detail, locale);
-                detail = format.format(args);
+                summary = bundle.getString(messageId);
             }
+            catch(MissingResourceException e) { }
         }
 
-        return new FacesMessage(severity, summary, detail);
-    }
-
-
-    private static String getBundleString(ResourceBundle bundle, String key)
-    {
-        try
+        if(summary == null)
         {
-            return bundle == null ? null : bundle.getString(key);
+            return null;
         }
-        catch (MissingResourceException e)
+        
+        if (bundle == null)
         {
-            return null;
+            throw new NullPointerException("Unable to locate ResrouceBundle: bundle is null");
+        }
+        summary = substituteParams(locale, summary, params);
+
+        try
+        {
+            detail = substituteParams(locale, bundle.getString(messageId + DETAIL_SUFFIX),
params);
         }
+        catch(MissingResourceException e) { }
+
+        return new FacesMessage(summary, detail);
     }
 
+    public static FacesMessage getMessage(FacesContext context, String messageId)
+    {
+        return getMessage(context, messageId, ((Object []) (null)));
+    }
 
-    private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale
locale)
+    public static FacesMessage getMessage(FacesContext context, String messageId, Object
params[])
     {
-        String bundleName = facesContext.getApplication().getMessageBundle();
-        if (bundleName != null)
-        {
-            try
-            {
-                return ResourceBundle.getBundle(bundleName, locale, Thread.currentThread().getContextClassLoader());
-            }
-            catch (MissingResourceException e)
-            {
-                log.error("Resource bundle " + bundleName + " could not be found.");
-                return null;
-            }
-        }
+        if(context == null || messageId == null)
+            throw new NullPointerException(" context " + context + " messageId " + messageId);
+        Locale locale = null;
+        if(context != null && context.getViewRoot() != null)
+            locale = context.getViewRoot().getLocale();
         else
+            locale = Locale.getDefault();
+        if(null == locale)
+            throw new NullPointerException(" locale " + locale);
+        FacesMessage message = getMessage(locale, messageId, params);
+        if(message != null)
         {
-            return null;
+            return message;
+        } else
+        {
+            locale = Locale.getDefault();
+            return getMessage(locale, messageId, params);
         }
     }
 
-    private static ResourceBundle getDefaultBundle(Locale locale)
+    private static Application getApplication()
     {
-        try
+        FacesContext context = FacesContext.getCurrentInstance();
+        if(context != null)
         {
-            return ResourceBundle.getBundle(DEFAULT_BUNDLE,
-                                            locale,
-                                            FacesContext.class.getClassLoader());
-        }
-        catch (MissingResourceException e)
+            return FacesContext.getCurrentInstance().getApplication();
+        } else
         {
-            log.error("Resource bundle " + DEFAULT_BUNDLE + " could not be found.");
-            return null;
+            ApplicationFactory afactory = (ApplicationFactory)FactoryFinder.getFactory("javax.faces.application.ApplicationFactory");
+            return afactory.getApplication();
         }
     }
-
 }



Mime
View raw message