Author: craigmcc
Date: Fri Sep 16 19:42:29 2005
New Revision: 289689

URL: http://svn.apache.org/viewcvs?rev=289689&view=rev
Log:
Add a utility class (and corresonding unit tests) that supports programmatic
access to a Map representation of a localized resource bundle, similar to the
kind of resource that <f:loadBundle> makes available inside a JSP page.

Added:
    
struts/shale/trunk/core-library/src/java/org/apache/shale/util/LoadBundle.java
    struts/shale/trunk/core-library/src/test/org/apache/shale/util/
    
struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java
    
struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_en_US.properties
    
struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_fr_FR.properties

Added: 
struts/shale/trunk/core-library/src/java/org/apache/shale/util/LoadBundle.java
URL: 
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/util/LoadBundle.java?rev=289689&view=auto
==============================================================================
--- 
struts/shale/trunk/core-library/src/java/org/apache/shale/util/LoadBundle.java 
(added)
+++ 
struts/shale/trunk/core-library/src/java/org/apache/shale/util/LoadBundle.java 
Fri Sep 16 19:42:29 2005
@@ -0,0 +1,252 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation.
+ *
+ * Licensed 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.shale.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+import java.util.Set;
+import javax.faces.context.FacesContext;
+
+/**
+ * <p>Utility class emulating the behavior of the standard JSF Core Library
+ * tag <code>&lt;f:loadBundle&gt;</code>.  This class is designed to be used
+ * as a managed bean, and exposes a <code>map</code> property containing the
+ * messages in the resoruce bundle specified by the <code>basename</code>
+ * property, localized for the <code>Locale</code> specified on the current
+ * request.</p>
+ *
+ * <p>A typical use of this class would be to declare a managed bean like 
this:</p>
+ * <code>
+ *    &lt;managed-bean&gt;
+ *      &lt;managed-bean-name&gt;messages&lt;/managed-bean-name&gt;
+ *      &lt;managed-bean-class&gt;
+ *        org.apache.shale.util.LoadBundle
+ *      &lt;/managed-bean-class&gt;
+ *      &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;
+ *      &lt;managed-property&gt;
+ *        &lt;property-name&gt;basename&lt;/property-name&gt;
+ *        &lt;value&gt;com.mycompany.mypackage.Bundle&lt;/value&gt;
+ *      &lt;/managed-property&gt;
+ *    &lt;/managed-bean&gt;
+ * </code>
+ *
+ * <p>This will result in creation of a request scope object whose 
<code>map</code>
+ * property will return a <code>Map</code> representing the localized messages 
for
+ * the <code>com.mycompany.mypackage.Bundle</code> resource bundle.  You can 
look
+ * up localized messages in this <code>Map</code> by evaluating a value binding
+ * expression like <code>#{messages.map['message.key']}</code>, where
+ * <code>message.key</code> is the key for which to retrieve a localized 
message.</p>
+ */
+public class LoadBundle {
+
+   // ------------------------------------------------------------- 
Constructors
+
+
+   /** Creates a new instance of LoadBundle */
+   public LoadBundle() {
+        this(null);
+   }
+
+
+   /** <p>Creates a new instance of LoadBundle for the specified bundle.</p>
+    *
+    * @param basename Base resource bundle name for this 
<code>LoadBundle</code>
+    */
+   public LoadBundle(String basename) {
+       this.basename = basename;
+   }
+
+
+   // --------------------------------------------------------------- 
Properties
+
+
+   /**
+    * <p>The base resource bundle name for this <code>LoadBundle</code> 
instance.</p>
+    */
+   private String basename = null;
+
+
+   /**
+    * <p>Return the base resource bundle name for this <code>LoadBundle</code>
+    * instance.</p>
+    */
+   public String getBasename() {
+       return this.basename;
+   }
+
+
+   /**
+    * <p>Set the base resource bundle name for this <code>LoadBundle</code>
+    * instance.</p>
+    */
+   public void setBasename(String basename) {
+       this.basename = basename;
+   }
+
+
+   // ----------------------------------------------------------- Public 
Methods
+
+
+   /**
+    * <p>Return a <code>Map</code> whose keys and values represent the content
+    * of the application resource bundle specified by the <code>basename</code>
+    * property, localized for the <code>Locale</code> stored in the
+    * <code>UIViewRoot</code> for the current request.</p>
+    */
+   public Map getMap() {
+
+       // Validate our current state
+       if (basename == null) {
+           throw new IllegalStateException("The 'basename' property cannot be 
null"); // FIXME - i18n
+       }
+       FacesContext context = FacesContext.getCurrentInstance();
+       Locale locale = context.getViewRoot().getLocale();
+
+       // Look up the requested resource bundle
+       final ResourceBundle bundle =
+         ResourceBundle.getBundle(basename, locale,
+                                  
Thread.currentThread().getContextClassLoader());
+       if (bundle == null) {
+           throw new IllegalArgumentException
+             ("No resource bundle found for base name '" + basename + "' and 
locale '" + locale + "'"); // FIXME - i18n
+       }
+
+       // Construct and return an immutable Map representing these contents
+       Map map = new Map() {
+
+           public void clear() {
+               throw new UnsupportedOperationException();
+           }
+
+           public boolean containsKey(Object key) {
+               boolean result = false;
+               if (key != null) {
+                   result = bundle.getObject(key.toString()) != null;
+               }
+               return result;
+           }
+
+           public boolean containsValue(Object value) {
+               Enumeration keys = bundle.getKeys();
+               while (keys.hasMoreElements()) {
+                   Object val = 
bundle.getObject(keys.nextElement().toString());
+                   if ((val != null) && val.equals(value)) {
+                       return true;
+                   }
+               }
+               return false;
+           }
+
+
+           public Set entrySet() {
+               Map map = new HashMap();
+               Enumeration keys = bundle.getKeys();
+               while (keys.hasMoreElements()) {
+                   String key = keys.nextElement().toString();
+                   Object value = bundle.getObject(key);
+                   map.put(key, value);
+               }
+               return map.entrySet();
+           }
+
+           public boolean equals(Object o) {
+               if ((o == null) || !(o instanceof Map)) {
+                   return false;
+               }
+               return entrySet().equals(((Map) o).entrySet());
+           }
+
+           public Object get(Object key) {
+               if (key == null) {
+                   return null;
+               }
+               try {
+                   return bundle.getObject(key.toString());
+               } catch (MissingResourceException e) {
+                   return "???" + key.toString() + "???";
+               }
+           }
+
+           public int hashCode() {
+               return bundle.hashCode();
+           }
+
+           public boolean isEmpty() {
+               Enumeration keys = bundle.getKeys();
+               while (keys.hasMoreElements()) {
+                   return false;
+               }
+               return true;
+           }
+
+           public Set keySet() {
+               Set set = new HashSet();
+               Enumeration keys = bundle.getKeys();
+               while (keys.hasMoreElements()) {
+                   set.add(keys.nextElement());
+               }
+               return set;
+           }
+
+           public Object put(Object key, Object value) {
+               throw new UnsupportedOperationException();
+           }
+
+           public void putAll(Map map) {
+               throw new UnsupportedOperationException();
+           }
+
+           public Object remove(Object key) {
+               throw new UnsupportedOperationException();
+           }
+
+           public int size() {
+               int size = 0;
+               Enumeration keys = bundle.getKeys();
+               while (keys.hasMoreElements()) {
+                   Object key = keys.nextElement();
+                   size++;
+               }
+               return size;
+           }
+
+           public Collection values() {
+               List list = new ArrayList();
+               Enumeration keys = bundle.getKeys();
+               while (keys.hasMoreElements()) {
+                   String key = keys.nextElement().toString();
+                   list.add(bundle.getObject(key));
+               }
+               return list;
+           }
+
+       };
+       return map;
+
+   }
+
+
+}

Added: 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java
URL: 
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java?rev=289689&view=auto
==============================================================================
--- 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java
 (added)
+++ 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java
 Fri Sep 16 19:42:29 2005
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ * 
+ * Licensed 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.shale.util;
+
+import java.util.Locale;
+import java.util.Map;
+import javax.faces.context.FacesContext;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+/**
+ *
+ * @author craigmcc
+ */
+public class LoadBundleTestCase extends AbstractJsfTestCase {
+    
+
+    // ------------------------------------------------------------ 
Constructors
+
+
+    // Construct a new instance of this test case.
+    public LoadBundleTestCase(String name) {
+        super(name);
+    }
+
+
+    // ---------------------------------------------------- Overall Test 
Methods
+
+
+    // Set up instance variables required by this test case.
+    public void setUp() {
+
+        super.setUp();
+
+        // Set up the instance we will be testing
+        lb = new LoadBundle("org.apache.shale.util.TestBundle");
+
+    }
+
+
+    // Return the tests included in this test case.
+    public static Test suite() {
+
+        return (new TestSuite(LoadBundleTestCase.class));
+
+    }
+
+
+    // Tear down instance variables required by this test case.
+    public void tearDown() {
+
+        lb = null;
+        super.tearDown();
+
+    }
+
+
+    // ------------------------------------------------------ Instance 
Variables
+
+
+    // The instance to be tested
+    LoadBundle lb = null;
+
+
+    // ------------------------------------------------------------ Test 
Methods
+
+
+    // Test access to the English values for this resource bundle
+    public void testEngish() {
+
+        FacesContext.getCurrentInstance().getViewRoot().setLocale(new 
Locale("en", "US"));
+        Map map = lb.getMap();
+        assertNotNull(map);
+        assertEquals("English Key 1", map.get("key1"));
+        assertEquals("English Key 2", map.get("key2"));
+
+    }
+
+
+    // Test access to the French values for this resource bundle
+    public void testFrench() {
+
+        FacesContext.getCurrentInstance().getViewRoot().setLocale(new 
Locale("fr", "FR"));
+        Map map = lb.getMap();
+        assertNotNull(map);
+        assertEquals("French Key 1", map.get("key1"));
+        assertEquals("French Key 2", map.get("key2"));
+
+    }
+
+
+    // Test a pristine instance
+    public void testPristine() {
+
+        assertEquals("org.apache.shale.util.TestBundle", lb.getBasename());
+
+    }
+
+
+}

Added: 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_en_US.properties
URL: 
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_en_US.properties?rev=289689&view=auto
==============================================================================
--- 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_en_US.properties
 (added)
+++ 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_en_US.properties
 Fri Sep 16 19:42:29 2005
@@ -0,0 +1,4 @@
+# TestBundle (English)
+key1=English Key 1
+key2=English Key 2
+

Added: 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_fr_FR.properties
URL: 
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_fr_FR.properties?rev=289689&view=auto
==============================================================================
--- 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_fr_FR.properties
 (added)
+++ 
struts/shale/trunk/core-library/src/test/org/apache/shale/util/TestBundle_fr_FR.properties
 Fri Sep 16 19:42:29 2005
@@ -0,0 +1,3 @@
+# TestBundle (French)
+key1=French Key 1
+key2=French Key 2



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to