incubator-adffaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sless...@apache.org
Subject svn commit: r519370 - in /incubator/adffaces/trunk/trinidad: trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/LocaleUtils.java trinidad-impl/src/test/java/org/apache/myfaces/trinidadinternal/renderkit/MApplication.java
Date Sat, 17 Mar 2007 17:01:10 GMT
Author: slessard
Date: Sat Mar 17 10:01:09 2007
New Revision: 519370

URL: http://svn.apache.org/viewvc?view=rev&rev=519370
Log:
Applied my patch for ADFFACES-417: Load text resources from application specific bundle before
falling back to the embedded ResourceBundle

Modified:
    incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/LocaleUtils.java
    incubator/adffaces/trunk/trinidad/trinidad-impl/src/test/java/org/apache/myfaces/trinidadinternal/renderkit/MApplication.java

Modified: incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/LocaleUtils.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/LocaleUtils.java?view=diff&rev=519370&r1=519369&r2=519370
==============================================================================
--- incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/LocaleUtils.java
(original)
+++ incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/LocaleUtils.java
Sat Mar 17 10:01:09 2007
@@ -25,6 +25,7 @@
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 
+import javax.faces.application.Application;
 import javax.faces.application.FacesMessage;
 import javax.faces.context.FacesContext;
 
@@ -122,32 +123,44 @@
   {
     _assertContextNotNull(context);
 
-    if (null == resourceId)
+    if (resourceId == null)
+    {
       throw new NullPointerException("resourceId is null");
+    }
 
     Locale locale = _getLocale(context);
     ClassLoader loader = _getClassLoader();
 
-    ResourceBundle bundle = _getAdfFacesMessageBundle(locale, loader);
-    String summary = null;
-
-    // unable to find adf faces app level bundle
-    if (null != bundle)
-    {
-      summary = _getBundleString(bundle, resourceId);
-    }
-
-    // Lookup in faces bundle
-    if (null == summary)
-    {
-      bundle = _getFacesMessageBundle(locale, loader);
-
-      if (null == bundle)
-        throw new NullPointerException("faces bundle not available ");
-      summary = _getBundleString(bundle, resourceId);
+    // First we look in the application specific bundle
+    ResourceBundle bundle = _getApplicationFacesMessageBundle(context, locale, loader);
+    BundleSummaryInfo summary = _getBundleSummaryInfo(bundle, resourceId);
+    if(summary == null)
+    {
+      // The application did not override the resource, let look in the 
+      // private Trinidad bundle
+      bundle = _getTrinidadMessageBundle(locale, loader);
+      summary = _getBundleSummaryInfo(bundle, resourceId);
+      
+      if(summary == null)
+      {
+        // The internal bundle does not know of the requested resource either,
+        // let check the default JSF IMPL message bundle
+        bundle = _getDefaultFacesMessageBundle(locale, loader);
+        if(bundle == null)
+        {
+          // That bundle is from the spec, it should never be null
+          throw new NullPointerException("The default FacesMessage.FACES_MESSAGES cannot
be found");
+        }
+        
+        summary = _getBundleSummaryInfo(bundle, resourceId);
+        if(summary == null)
+        {
+          summary = new BundleSummaryInfo(null, "???" + resourceId + "???");
+        }
+      }
     }
-
-    return new BundleSummaryInfo(bundle, summary);
+    
+    return summary;
   }
 
   /**
@@ -160,7 +173,7 @@
    * @param loader ClassLoader to pickup the bundle
    * @return Resource bundle for the given locale.
    */
-  private static ResourceBundle _getAdfFacesMessageBundle(
+  private static ResourceBundle _getTrinidadMessageBundle(
     Locale locale,
     ClassLoader loader
     )
@@ -186,8 +199,64 @@
 
     return bundle;
   }
+  
+  private static BundleSummaryInfo _getBundleSummaryInfo(
+    ResourceBundle bundle,
+    String resourceId)
+  {
+    assert resourceId != null;
+    
+    if(bundle != null)
+    {
+      // The bundle exists
+      String summary = _getBundleString(bundle, resourceId);
+      if(summary != null)
+      {
+        // The resource exists
+        return new BundleSummaryInfo(bundle, summary);
+      }
+    }
+    
+    return null;
+  }
 
-  private static ResourceBundle _getFacesMessageBundle(
+  private static ResourceBundle _getApplicationFacesMessageBundle(
+    FacesContext context,
+    Locale locale,
+    ClassLoader loader
+    )
+  {
+    assert context != null;
+    assert locale  != null;
+    assert loader  != null;
+    
+    Application application = context.getApplication();
+    if(application == null)
+    {
+      // Should not happen, but better check than a NullPointerException
+      return null;
+    }
+    
+    String bundleName = application.getMessageBundle();
+    if(bundleName == null)
+    {
+      // There's no specified message bundle in faces-config.xml
+      return null;
+    }
+    
+    try
+    {
+      return ResourceBundle.getBundle(bundleName, locale, loader);
+    }
+    catch (MissingResourceException missingResource)
+    {
+      _LOG.warning("Unable to load faces-config.xml defined message bundle {0}", bundleName);
+      _LOG.warning(missingResource);
+      return null;
+    }
+  }
+
+  private static ResourceBundle _getDefaultFacesMessageBundle(
     Locale locale,
     ClassLoader loader
     )
@@ -241,19 +310,16 @@
    */
   private static String _getBundleString(ResourceBundle bundle, String key)
   {
-    Object localeStr = null;
     try
     {
-      localeStr =  bundle.getObject(key);
-      if (null != localeStr )
-        localeStr = localeStr.toString();
+      Object localeStr = bundle.getObject(key);
+      return localeStr == null ? null : localeStr.toString();
     }
     catch (MissingResourceException mre)
     {
-      _LOG.warning("Key " + key + " not found in " + bundle);
+      _LOG.finer("Key {0} not found in {1}", new Object[]{key, bundle});
+      return null;
     }
-
-    return (String)localeStr;
   }
 
 

Modified: incubator/adffaces/trunk/trinidad/trinidad-impl/src/test/java/org/apache/myfaces/trinidadinternal/renderkit/MApplication.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/test/java/org/apache/myfaces/trinidadinternal/renderkit/MApplication.java?view=diff&rev=519370&r1=519369&r2=519370
==============================================================================
--- incubator/adffaces/trunk/trinidad/trinidad-impl/src/test/java/org/apache/myfaces/trinidadinternal/renderkit/MApplication.java
(original)
+++ incubator/adffaces/trunk/trinidad/trinidad-impl/src/test/java/org/apache/myfaces/trinidadinternal/renderkit/MApplication.java
Sat Mar 17 10:01:09 2007
@@ -91,7 +91,10 @@
   @Override
   public String getMessageBundle()
   {
-    throw new UnsupportedOperationException("Not implemented yet");
+    // The spec prevents throwing a UnsupportedOperationException and we have class 
+    // reading that property. Therefore it's better to return null which is a valid 
+    // return value.
+    return null;
   }
 
   @Override



Mime
View raw message