myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r940132 - in /myfaces/shared/trunk/core/src: main/java/org/apache/myfaces/shared/util/MessageUtils.java main/java/org/apache/myfaces/shared/util/ParametrizableFacesMessage.java test/java/org/apache/myfaces/shared/util/MessageUtilsTest.java
Date Sun, 02 May 2010 01:29:23 GMT
Author: lu4242
Date: Sun May  2 01:29:23 2010
New Revision: 940132

URL: http://svn.apache.org/viewvc?rev=940132&view=rev
Log:
MYFACES-2691 Enhance MessageUtils adding methods when custom library bundle should be scanned

Added:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/ParametrizableFacesMessage.java
Modified:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/MessageUtils.java
    myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/MessageUtilsTest.java

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/MessageUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/MessageUtils.java?rev=940132&r1=940131&r2=940132&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/MessageUtils.java
(original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/MessageUtils.java
Sun May  2 01:29:23 2010
@@ -78,6 +78,18 @@ public final class MessageUtils
                           new Object[]{arg},
                           FacesContext.getCurrentInstance());
     }
+    
+    public static FacesMessage getMessage(String bundleBaseName, 
+            FacesMessage.Severity severity,
+            String messageId,
+            Object arg)
+    {
+        return getMessage(bundleBaseName,
+                          severity,
+                          messageId,
+                          new Object[]{arg},
+                          FacesContext.getCurrentInstance());
+    }
 
     /**
      * @param severity serverity of message
@@ -95,6 +107,18 @@ public final class MessageUtils
                           args,
                           FacesContext.getCurrentInstance());
     }
+    
+    public static FacesMessage getMessage(String bundleBaseName, 
+            FacesMessage.Severity severity,
+            String messageId,
+            Object[] args)
+    {
+        return getMessage(bundleBaseName,
+                          severity,
+                          messageId,
+                          args,
+                          FacesContext.getCurrentInstance());
+    }    
 
     public static FacesMessage getMessage(FacesMessage.Severity severity,
                                           String messageId,
@@ -107,6 +131,18 @@ public final class MessageUtils
         return message;
     }
 
+    public static FacesMessage getMessage(String bundleBaseName,
+            FacesMessage.Severity severity,
+            String messageId,
+            Object[] args,
+            FacesContext facesContext)
+    {
+        FacesMessage message = getMessage(bundleBaseName, facesContext, messageId, args);
+        message.setSeverity(severity);
+        
+        return message;
+    }
+
     public static void addMessage(FacesMessage.Severity severity,
                                   String messageId,
                                   Object[] args)
@@ -114,6 +150,14 @@ public final class MessageUtils
         addMessage(severity, messageId, args, null, FacesContext.getCurrentInstance());
     }
 
+    public static void addMessage(String bundleBaseName, 
+            FacesMessage.Severity severity,
+            String messageId,
+            Object[] args)
+    {
+        addMessage(bundleBaseName, severity, messageId, args, null, FacesContext.getCurrentInstance());
+    }
+
     public static void addMessage(FacesMessage.Severity severity,
                                   String messageId,
                                   Object[] args,
@@ -122,6 +166,15 @@ public final class MessageUtils
         addMessage(severity, messageId, args, null, facesContext);
     }
 
+    public static void addMessage(String bundleBaseName, 
+            FacesMessage.Severity severity,
+            String messageId,
+            Object[] args,
+            FacesContext facesContext)
+    {
+        addMessage(bundleBaseName, severity, messageId, args, null, facesContext);
+    }
+
     public static void addMessage(FacesMessage.Severity severity,
                                   String messageId,
                                   Object[] args,
@@ -130,6 +183,15 @@ public final class MessageUtils
         addMessage(severity, messageId, args, forClientId, FacesContext.getCurrentInstance());
     }
 
+    public static void addMessage(String bundleBaseName,
+            FacesMessage.Severity severity,
+            String messageId,
+            Object[] args,
+            String forClientId)
+    {
+        addMessage(bundleBaseName, severity, messageId, args, forClientId, FacesContext.getCurrentInstance());
+    }
+
     public static void addMessage(FacesMessage.Severity severity,
                                   String messageId,
                                   Object[] args,
@@ -143,6 +205,20 @@ public final class MessageUtils
                                 getMessage(severity, messageId, args, facesContext));
     }
 
+    public static void addMessage(String bundleBaseName,
+            FacesMessage.Severity severity,
+            String messageId,
+            Object[] args,
+            String forClientId,
+            FacesContext facesContext)
+    {
+        if(log.isTraceEnabled()) {
+        log.trace("adding message " + messageId + " for clientId " + forClientId);
+        }
+        facesContext.addMessage(forClientId,
+                  getMessage(bundleBaseName, severity, messageId, args, facesContext));
+    }
+
     /**
      * Uses <code>MessageFormat</code> and the supplied parameters to fill in
the param placeholders in the String.
      *
@@ -156,18 +232,23 @@ public final class MessageUtils
         String localizedStr = null;
         if(params == null || msgtext == null)
             return msgtext;
-        StringBuffer b = new StringBuffer(100);
+
         if(locale != null)
         {
             MessageFormat mf = new MessageFormat(msgtext,locale);            
-            b.append(mf.format(params));
-            localizedStr = b.toString();
+            localizedStr = mf.format(params);
         }
         return localizedStr;
     }
 
     public static FacesMessage getMessage(String messageId, Object params[])
     {
+        Locale locale = getCurrentLocale();
+        return getMessage(locale, messageId, params);
+    }
+
+    public static FacesMessage getMessageFromBundle(String bundleBaseName, String messageId,
Object params[])
+    {
         Locale locale = null;
         FacesContext context = FacesContext.getCurrentInstance();
         if(context != null && context.getViewRoot() != null)
@@ -179,7 +260,7 @@ public final class MessageUtils
         {
             locale = Locale.getDefault();
         }
-        return getMessage(locale, messageId, params);
+        return getMessageFromBundle(bundleBaseName, context , locale, messageId, params);
     }
 
     public static FacesMessage getMessage(Locale locale, String messageId, Object params[])
@@ -225,23 +306,126 @@ public final class MessageUtils
         if (bundle == null)
         {
             throw new NullPointerException(
-                "Unable to locate ResrouceBundle: bundle is null");
+                "Unable to locate ResourceBundle: bundle is null");
         }
-        summary = substituteParams(locale, summary, params);
+        if (params != null && locale != null)
+        {
+            try
+            {
+                detail = bundle.getString(messageId + DETAIL_SUFFIX);
+            }
+            catch(MissingResourceException e) {
+                // NoOp
+            }
+            return new ParametrizableFacesMessage(summary, detail, params, locale);
+        }
+        else
+        {
+            summary = substituteParams(locale, summary, params);
+            try
+            {
+                detail = substituteParams(locale,
+                    bundle.getString(messageId + DETAIL_SUFFIX), params);
+            }
+            catch(MissingResourceException e) {
+                // NoOp
+            }
+            return new FacesMessage(summary, detail);
+        }
+    }
+    
+    public static FacesMessage getMessageFromBundle(String bundleBaseName, FacesContext context,
Locale locale, String messageId, Object params[])
+    {
+        String summary = null;
+        String detail = null;
+        String bundleName = context.getApplication().getMessageBundle();
+        ResourceBundle bundle = null;
 
-        try
+        if (bundleName != null)
+        {
+            try
+            {
+                bundle = ResourceBundle.getBundle(bundleName, locale, org.apache.myfaces.shared.util.ClassUtils.getCurrentLoader(bundleName));
+                summary = bundle.getString(messageId);
+            }
+            catch (MissingResourceException e) {
+                // NoOp
+            }
+        }
+
+        if (summary == null)
+        {
+            try
+            {
+                bundle = ResourceBundle.getBundle(bundleBaseName, locale, org.apache.myfaces.shared.util.ClassUtils.getCurrentLoader(bundleBaseName));
+                if(bundle == null)
+                {
+                    throw new NullPointerException();
+                }
+                summary = bundle.getString(messageId);
+            }
+            catch(MissingResourceException e) {
+                // NoOp
+            }
+        }
+        
+        if (summary == null)
         {
-            detail = substituteParams(locale,
-                bundle.getString(messageId + DETAIL_SUFFIX), params);
+            try
+            {
+                bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE, locale, org.apache.myfaces.shared.util.ClassUtils.getCurrentLoader(DEFAULT_BUNDLE));
+                if(bundle == null)
+                {
+                    throw new NullPointerException();
+                }
+                summary = bundle.getString(messageId);
+            }
+            catch(MissingResourceException e) {
+                // NoOp
+            }
         }
-        catch(MissingResourceException e) {
-            // NoOp
+
+        if(summary == null)
+        {
+            summary = messageId;
         }
 
-        return new FacesMessage(summary, detail);
+        if (bundle == null)
+        {
+            throw new NullPointerException(
+                "Unable to locate ResourceBundle: bundle is null");
+        }
+        
+        if (params != null && locale != null)
+        {
+            try
+            {
+                detail = bundle.getString(messageId + DETAIL_SUFFIX);
+            }
+            catch(MissingResourceException e) {
+                // NoOp
+            }
+            return new ParametrizableFacesMessage(summary, detail, params, locale);
+        }
+        else
+        {
+            summary = substituteParams(locale, summary, params);
+            try
+            {
+                detail = substituteParams(locale,
+                    bundle.getString(messageId + DETAIL_SUFFIX), params);
+            }
+            catch(MissingResourceException e) {
+                // NoOp
+            }
+            return new FacesMessage(summary, detail);
+        }
     }
 
     /**
+     *  Retrieve the message from a specific bundle. It does not look on application message
bundle
+     * or default message bundle. If it is required to look on those bundles use getMessageFromBundle
instead
+     * 
      * @param bundleBaseName baseName of ResourceBundle to load localized messages
      * @param messageId id of message
      * @param params parameters to set at localized message
@@ -257,14 +441,19 @@ public final class MessageUtils
      * @return  currently applicable Locale for this request.
      */
     public static Locale getCurrentLocale() {
+        return getCurrentLocale(FacesContext.getCurrentInstance());
+    }
+    
+    public static Locale getCurrentLocale(FacesContext context) {
         Locale locale;
-        
-        FacesContext context = FacesContext.getCurrentInstance();
-        if(context != null && context.getViewRoot() != null) {
+        if(context != null && context.getViewRoot() != null)
+        {
             locale = context.getViewRoot().getLocale();
             if(locale == null)
                 locale = Locale.getDefault();
-        } else {
+        }
+        else
+        {
             locale = Locale.getDefault();
         }
         
@@ -287,6 +476,9 @@ public final class MessageUtils
     }
 
     /**
+     *  Retrieve the message from a specific bundle. It does not look on application message
bundle
+     * or default message bundle. If it is required to look on those bundles use getMessageFromBundle
instead
+     * 
      * @param bundleBaseName baseName of ResourceBundle to load localized messages
      * @param locale current locale
      * @param messageId id of message
@@ -355,6 +547,11 @@ public final class MessageUtils
     {
         return getMessage(context, messageId, ((Object []) (null)));
     }
+    
+    public static FacesMessage getMessage(String bundleBaseName, FacesContext context, String
messageId)
+    {
+        return getMessage(bundleBaseName, context, messageId, ((Object []) (null)));
+    }
 
     /**
      *
@@ -367,7 +564,7 @@ public final class MessageUtils
     {
         if(context == null || messageId == null)
             throw new NullPointerException(" context " + context + " messageId " + messageId);
-        Locale locale = getCurrentLocale();
+        Locale locale = getCurrentLocale(context);
         if(null == locale)
             throw new NullPointerException(" locale " + locale);
         FacesMessage message = getMessage(locale, messageId, params);
@@ -382,13 +579,33 @@ public final class MessageUtils
             return getMessage(locale, messageId, params);
         }
     }
+    
+    public static FacesMessage getMessage(String bundleBaseName, FacesContext context, String
messageId, Object params[])
+    {
+        if(context == null || messageId == null)
+            throw new NullPointerException(" context " + context + " messageId " + messageId);
+        Locale locale = getCurrentLocale(context);
+        if(null == locale)
+            throw new NullPointerException(" locale " + locale);
+        FacesMessage message = getMessageFromBundle(bundleBaseName, context, locale, messageId,
params);
+        if(message != null)
+        {
+            return message;
+        } else
+        {
+            // TODO /FIX:  Note that this has fallback behavior to default Locale for message,
+            // but similar behavior above does not.  The methods should probably behave
+            locale = Locale.getDefault();
+            return getMessageFromBundle(bundleBaseName, context, locale, messageId, params);
+        }
+    }
 
     private static Application getApplication()
     {
         FacesContext context = FacesContext.getCurrentInstance();
         if(context != null)
         {
-            return FacesContext.getCurrentInstance().getApplication();
+            return context.getApplication();
         } else
         {
             ApplicationFactory afactory = (ApplicationFactory)FactoryFinder.getFactory("javax.faces.application.ApplicationFactory");

Added: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/ParametrizableFacesMessage.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/ParametrizableFacesMessage.java?rev=940132&view=auto
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/ParametrizableFacesMessage.java
(added)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/ParametrizableFacesMessage.java
Sun May  2 01:29:23 2010
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.shared.util;
+
+import java.text.MessageFormat;
+import java.util.Locale;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+/** 
+ * This class encapsulates a FacesMessage to evaluate the label
+ * expression on render response, where f:loadBundle is available
+ * 
+ * @author Leonardo Uribe (latest modification by $Author: skitching $)
+ * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Dom, 13 Jul 2008) $
+ */
+public class ParametrizableFacesMessage extends FacesMessage
+{
+    /**
+     * 
+     */
+    private static final long serialVersionUID = 7792947730961657948L;
+
+    private final Object _args[];
+    private String _evaluatedDetail;
+    private String _evaluatedSummary;
+    private transient Object _evaluatedArgs[];
+    private Locale _locale;
+
+    public ParametrizableFacesMessage(
+            String summary, String detail, Object[] args, Locale locale)
+    {
+        super(summary, detail);
+        if(locale == null) throw new NullPointerException("locale");
+        _locale = locale;
+        _args = args;
+    }
+
+    public ParametrizableFacesMessage(FacesMessage.Severity severity,
+            String summary, String detail, Object[] args, Locale locale)
+    {
+        super(severity, summary, detail);
+        if(locale == null) throw new NullPointerException("locale");
+        _locale = locale;
+        _args = args;
+    }
+
+    public String getDetail()
+    {
+        if (_evaluatedArgs == null && _args != null)
+        {
+            evaluateArgs();
+        }
+        if (_evaluatedDetail == null)
+        {
+            MessageFormat format = new MessageFormat(super.getDetail(), _locale);
+            _evaluatedDetail = format.format(_evaluatedArgs);
+        }
+        return _evaluatedDetail;
+    }
+
+    public String getSummary()
+    {
+        if (_evaluatedArgs == null && _args != null)
+        {
+            evaluateArgs();
+        }
+        if (_evaluatedSummary == null)
+        {
+            MessageFormat format = new MessageFormat(super.getSummary(), _locale);
+            _evaluatedSummary = format.format(_evaluatedArgs);
+        }
+        return _evaluatedSummary;
+    }
+
+    private void evaluateArgs()
+    {
+        _evaluatedArgs = new Object[_args.length];
+        FacesContext facesContext = null;
+        for (int i = 0; i < _args.length; i++)
+        {
+            if (_args[i] == null)
+            {
+                continue;
+            }
+            else if (_args[i] instanceof ValueBinding)
+            {
+                if (facesContext == null)
+                {
+                    facesContext = FacesContext.getCurrentInstance();
+                }
+                _evaluatedArgs[i] = ((ValueBinding)_args[i]).getValue(facesContext);
+            }
+            else 
+            {
+                _evaluatedArgs[i] = _args[i];
+            }
+        }
+    }
+}

Modified: myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/MessageUtilsTest.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/MessageUtilsTest.java?rev=940132&r1=940131&r2=940132&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/MessageUtilsTest.java
(original)
+++ myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/MessageUtilsTest.java
Sun May  2 01:29:23 2010
@@ -18,14 +18,14 @@
  */
 package org.apache.myfaces.shared.util;
 
-import java.text.NumberFormat;
 import java.util.Locale;
 import java.util.ResourceBundle;
 
 import javax.faces.application.FacesMessage;
 
 import junit.framework.Test;
-import org.apache.myfaces.shared.util.MessageUtils;
+import junit.framework.TestSuite;
+
 import org.apache.shale.test.base.AbstractJsfTestCase;
 
 /**
@@ -44,13 +44,25 @@ public class MessageUtilsTest extends Ab
     }
 
     public static Test suite() {
-        return null; // keep this method or maven won't run it
+        return new TestSuite(MessageUtilsTest.class); // keep this method or maven won't
run it
+    }
+
+    public void setUp()
+    {
+        // TODO Auto-generated method stub
+        super.setUp();
+    }
+
+    public void tearDown()
+    {
+        // TODO Auto-generated method stub
+        super.tearDown();
     }
 
     /**
      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity,
String, Object)'
      */
-    public void testGetMessageSeverityStringObject()
+    public void testGetMessageSeverityStringObject() throws Exception
     {
         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
 
@@ -69,7 +81,7 @@ public class MessageUtilsTest extends Ab
     /**
      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity,
String, Object[])'
      */
-    public void testGetMessageSeverityStringObjectArray()
+    public void testGetMessageSeverityStringObjectArray() throws Exception
     {
         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
         
@@ -87,7 +99,7 @@ public class MessageUtilsTest extends Ab
     /**
      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity,
String, Object[], FacesContext)'
      */
-    public void testGetMessageSeverityStringObjectArrayFacesContext()
+    public void testGetMessageSeverityStringObjectArrayFacesContext() throws Exception
     {
         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
         
@@ -106,7 +118,7 @@ public class MessageUtilsTest extends Ab
     /**
      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Locale, String,
Object[])'
      */
-    public void testGetMessageLocaleStringObjectArray()
+    public void testGetMessageLocaleStringObjectArray() throws Exception
     {
         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
         
@@ -123,7 +135,7 @@ public class MessageUtilsTest extends Ab
     /**
      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(FacesContext,
String)'
      */
-    public void testGetMessageFacesContextString()
+    public void testGetMessageFacesContextString() throws Exception
     {
         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
         
@@ -141,7 +153,7 @@ public class MessageUtilsTest extends Ab
     /**
      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(FacesContext,
String, Object[])'
      */
-    public void testGetMessageFacesContextStringObjectArray()
+    public void testGetMessageFacesContextStringObjectArray() throws Exception
     {
         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
         
@@ -159,7 +171,7 @@ public class MessageUtilsTest extends Ab
     /**
      * testGetMessageWithBundle
      */
-    public void testGetMessageWithBundle()
+    public void testGetMessageWithBundle() throws Exception
     {
         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
         
@@ -174,7 +186,7 @@ public class MessageUtilsTest extends Ab
     /**
      * testGetMessageWithBundleName
      */
-    public void testGetMessageWithBundleName()
+    public void testGetMessageWithBundleName() throws Exception
     {
         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
         
@@ -187,7 +199,7 @@ public class MessageUtilsTest extends Ab
     /**
      * testGetMessageWithBundleNameLocale
      */
-    public void testGetMessageWithBundleNameLocale()
+    public void testGetMessageWithBundleNameLocale() throws Exception
     {
         FacesMessage msg = MessageUtils.getMessage(DEFAULT_BUNDLE,
                 Locale.GERMAN, "javax.faces.component.UIInput.CONVERSION", null);
@@ -198,7 +210,7 @@ public class MessageUtilsTest extends Ab
     /**
      * testSubstituteParamsWithDELocale(
      */
-    public void testSubstituteParamsWithDELocale() {
+    public void testSubstituteParamsWithDELocale() throws Exception {
         String paramString = MessageUtils.substituteParams(Locale.GERMANY, "currency {0,number,currency}",
new Object[]{new Double(100)});
 
         System.out.println(paramString);
@@ -208,7 +220,7 @@ public class MessageUtilsTest extends Ab
     /**
      * testSubstituteParamsWithGBLocale(
      */
-    public void testSubstituteParamsWithGBLocale() {
+    public void testSubstituteParamsWithGBLocale() throws Exception {
         String paramString = MessageUtils.substituteParams(Locale.UK, "currency {0,number,currency}",
new Object[]{new Double(100)});
         
         System.out.println(paramString);



Mime
View raw message