Hi, i'm still working on adding basic Java types to the Configuration interface, here is a patch adding Locale support. The format read by the implementation in AbstractConfiguration is consistent with the output of the Locale.toString() method, that's "language_country_variant".

And thanks for applying the patch on BigInteger/BigDecimal so quickly Eric :)

Emmanuel Bourg


Index: src/java/org/apache/commons/configuration/AbstractConfiguration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/configuration/src/java/org/apache/commons/configuration/AbstractConfiguration.java,v
retrieving revision 1.4
diff -u -r1.4 AbstractConfiguration.java
--- src/java/org/apache/commons/configuration/AbstractConfiguration.java        12 Feb 
2004 12:59:19 -0000      1.4
+++ src/java/org/apache/commons/configuration/AbstractConfiguration.java        12 Feb 
2004 19:03:31 -0000
@@ -54,15 +54,16 @@
  * <http://www.apache.org/>.
  */
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 import java.util.NoSuchElementException;
 import java.util.Properties;
 import java.util.StringTokenizer;
-import java.math.BigDecimal;
-import java.math.BigInteger;
 
 import org.apache.commons.lang.BooleanUtils;
 
@@ -1478,6 +1479,82 @@
                     + value.getClass().getName());
         }
         return list;
+    }
+
+    public Locale getLocale(String key) throws NoSuchElementException {
+        Locale locale = getLocale(key, null);
+        if (locale != null)
+        {
+            return locale;
+        }
+        else
+        {
+            throw new NoSuchElementException(
+                '\'' + key + "' doesn't map to an existing object");
+        }
+    }
+
+    public Locale getLocale(String key, Locale defaultValue) {
+        Object value = resolveContainerStore(key);
+
+        if (value instanceof Locale)
+        {
+            return (Locale) value;
+        }
+        else if (value instanceof String)
+        {
+            String[] elements = split((String) value, "_");
+            String language = elements.length >= 1 ? elements[0] : "";
+            String country = elements.length >= 2 ? elements[1] : "";
+            String variant = elements.length >= 3 ? elements[2] : "";
+
+            return new Locale(language, country, variant);
+        }
+        else if (value == null)
+        {
+            if (defaults != null)
+            {
+                return defaults.getLocale(key, defaultValue);
+            }
+            else
+            {
+                return defaultValue;
+            }
+        }
+        else
+        {
+            throw new ClassCastException(
+                '\'' + key + "' doesn't map to a Locale object");
+        }
+    }
+
+    /**
+     * Split a string on the specified separator. To be removed when
+     * commons-lang has a better replacement available (Tokenizer?).
+     *
+     * @param s          the string to split
+     * @param separator  the separator
+     */
+    private String[] split(String s, String separator)
+    {
+        if (s == null)
+        {
+            return new String[0];
+        }
+
+        List list = new ArrayList();
+
+        int begin = 0;
+        while (begin < s.length())
+        {
+            int index = s.indexOf(separator, begin);
+            int end = index != -1 ? index : s.length();
+            list.add(s.substring(begin , end));
+
+            begin = end + 1;
+        }
+
+        return (String[]) list.toArray(new String[list.size()]);
     }
 
     /**
Index: src/java/org/apache/commons/configuration/CompositeConfiguration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java,v
retrieving revision 1.5
diff -u -r1.5 CompositeConfiguration.java
--- src/java/org/apache/commons/configuration/CompositeConfiguration.java       12 Feb 
2004 12:59:19 -0000      1.5
+++ src/java/org/apache/commons/configuration/CompositeConfiguration.java       12 Feb 
2004 19:03:31 -0000
@@ -60,6 +60,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
+import java.util.Locale;
 import java.util.NoSuchElementException;
 import java.util.Properties;
 
@@ -774,6 +775,22 @@
         List list = getList(key);
 
         return (list.size() == 0) ? defaultValue : list;
+    }
+
+    public Locale getLocale(String key) throws NoSuchElementException {
+        return getFirstMatchingConfig(key).getLocale(key);
+    }
+
+    public Locale getLocale(String key, Locale defaultValue)
+    {
+        try
+        {
+            return getFirstMatchingConfig(key).getLocale(key, defaultValue);
+        }
+        catch (NoSuchElementException nsee)
+        {
+            return defaultValue;
+        }
     }
 
     private Configuration getFirstMatchingConfig(String key)
Index: src/java/org/apache/commons/configuration/Configuration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/configuration/src/java/org/apache/commons/configuration/Configuration.java,v
retrieving revision 1.3
diff -u -r1.3 Configuration.java
--- src/java/org/apache/commons/configuration/Configuration.java        12 Feb 2004 
12:59:19 -0000      1.3
+++ src/java/org/apache/commons/configuration/Configuration.java        12 Feb 2004 
19:03:31 -0000
@@ -54,12 +54,13 @@
  * <http://www.apache.org/>.
  */
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Properties;
+import java.util.Locale;
 import java.util.NoSuchElementException;
-import java.math.BigDecimal;
-import java.math.BigInteger;
+import java.util.Properties;
 
 /**
  * Configuration interface.
@@ -559,4 +560,27 @@
      * object that is not a List.
      */
     List getList(String key, List defaultValue);
+
+    /**
+     * Get a Locale associated with the given configuration key.
+     *
+     * @param key The configuration key.
+     *
+     * @return The associated Locale if key is found and has valid format
+     *
+     * @exception NoSuchElementException is thrown if the key doesn't
+     * map to an existing object.
+     */
+    Locale getLocale(String key) throws NoSuchElementException;
+
+    /**
+     * Get a Locale associated with the given configuration key.
+     *
+     * @param key          The configuration key.
+     * @param defaultValue The default value.
+     *
+     * @return The associated Locale if key is found and has valid
+     * format, default value otherwise.
+     */
+    Locale getLocale(String key, Locale defaultValue);
 }
Index: src/test/org/apache/commons/configuration/TestBaseConfiguration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestBaseConfiguration.java,v
retrieving revision 1.4
diff -u -r1.4 TestBaseConfiguration.java
--- src/test/org/apache/commons/configuration/TestBaseConfiguration.java        12 Feb 
2004 12:59:19 -0000      1.4
+++ src/test/org/apache/commons/configuration/TestBaseConfiguration.java        12 Feb 
2004 19:03:32 -0000
@@ -59,6 +59,7 @@
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Properties;
+import java.util.Locale;
 
 import junit.framework.TestCase;
 
@@ -342,4 +343,37 @@
         assertEquals("default string", eprop.getString("default", "some default 
value"));
         assertEquals("some default value", eprop.getString("XXX", "some default 
value"));              
     }
+
+    public void testGetLocale()
+    {
+        // language
+        eprop.setProperty("locale", "fr");
+        assertEquals("language", new Locale("fr", ""), eprop.getLocale("locale"));
+
+        // language + variant
+        eprop.setProperty("locale", "fr__POSIX");
+        assertEquals("language + variant", new Locale("fr", "", "POSIX"), 
eprop.getLocale("locale"));
+
+        // country
+        eprop.setProperty("locale", "_FR");
+        assertEquals("country", new Locale("", "FR"), eprop.getLocale("locale"));
+
+        // country + variant
+        eprop.setProperty("locale", "_FR_WIN");
+        assertEquals("country + variant", new Locale("", "FR", "WIN"), 
eprop.getLocale("locale"));
+
+        // language + country
+        eprop.setProperty("locale", "fr_FR");
+        assertEquals("language + country", new Locale("fr", "FR"), 
eprop.getLocale("locale"));
+
+        // language + country + variant
+        eprop.setProperty("locale", "fr_FR_MAC");
+        assertEquals("language + country + varian", new Locale("fr", "FR", "MAC"), 
eprop.getLocale("locale"));
+
+        // default value
+        eprop.setProperty("locale", "fr");
+        assertEquals("Existing key with default value", Locale.FRENCH, 
eprop.getLocale("locale", Locale.GERMAN));
+        assertEquals("Missing key with default value", Locale.GERMAN, 
eprop.getLocale("localeNotInConfig", Locale.GERMAN));
+    }
+
 }
Index: xdocs/changes.xml
===================================================================
RCS file: /home/cvspublic/jakarta-commons/configuration/xdocs/changes.xml,v
retrieving revision 1.8
diff -u -r1.8 changes.xml
--- xdocs/changes.xml   12 Feb 2004 12:59:19 -0000      1.8
+++ xdocs/changes.xml   12 Feb 2004 19:03:32 -0000
@@ -8,7 +8,7 @@
   <body>
     <release version="1.0-dev-4" date="">
       <action dev="ebourg" type="add">
-        The Configuration interface now supports BigDecimal and BigInteger numbers.
+        The Configuration interface now supports BigDecimal, BigInteger and Locale.
       </action>
         <action dev="epugh" type="add">
        ConfigurationException is now thrown by public methods instead of Exception or

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

Reply via email to