This adds the remaining two java.util.spi services
and links LocaleNameProvider into java.util.Locale.
I haven't done anything with TimeZone yet, as at the
moment TimeZone is calling DateFormatSymbols whereas
it should be the other way round.  I'll change this
when I also fix DateFormatSymbols.

Changelog:

2007-01-02  Andrew John Hughes  <[EMAIL PROTECTED]>

        * java/util/Currency.java:
        (getSymbol(Locale)): Removed unneeded variable
        and terminate loop early.
        * java/util/Locale.java:
        (getDisplayLanguage(Locale)): Fixed to use
        LocaleNameProvider.
        (getDisplayCountry(Locale)): Likewise.
        (getDisplayVariant(Locale)): Likewise.
        * java/util/spi/LocaleNameProvider.java:
        New file.
        * java/util/spi/TimeZoneNameProvider.java:
        Likewise.

-- 
Andrew :-)

Escape the Java Trap with GNU Classpath!
http://www.gnu.org/philosophy/java-trap.html
public class gcj extends Freedom implements Java { ... }
Index: java/util/Currency.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Currency.java,v
retrieving revision 1.17
diff -u -3 -p -u -r1.17 Currency.java
--- java/util/Currency.java     2 Jan 2007 00:51:01 -0000       1.17
+++ java/util/Currency.java     2 Jan 2007 21:35:26 -0000
@@ -404,20 +404,17 @@ public final class Currency 
    */
   public String getSymbol(Locale locale)
   {
-    String localizedString;
     String property = "currenciesSymbol." + currencyCode;
     try
       {
-        localizedString =
-         ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
-                                  locale).getString(property);
+        return ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
+                                       locale).getString(property);
       }
     catch (MissingResourceException exception)
       {
-       localizedString = null;
+       /* This means runtime support for the locale
+        * is not available, so we check providers. */
       }
-    if (localizedString != null)
-      return localizedString;
     for (CurrencyNameProvider p :
           ServiceLoader.load(CurrencyNameProvider.class))
       {
@@ -425,10 +422,11 @@ public final class Currency 
          {
            if (loc.equals(locale))
              {
-               localizedString = p.getSymbol(currencyCode,
-                                             locale);
+               String localizedString = p.getSymbol(currencyCode,
+                                                    locale);
                if (localizedString != null)
                  return localizedString;
+               break;
              }
          }
       }
Index: java/util/Locale.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Locale.java,v
retrieving revision 1.37
diff -u -3 -p -u -r1.37 Locale.java
--- java/util/Locale.java       2 Jan 2007 00:51:01 -0000       1.37
+++ java/util/Locale.java       2 Jan 2007 21:35:26 -0000
@@ -46,6 +46,8 @@ import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 
+import java.util.spi.LocaleNameProvider;
+
 /**
  * Locales represent a specific country and culture. Classes which can be
  * passed a Locale object tailor their information for a given locale. For
@@ -679,6 +681,8 @@ public final class Locale implements Ser
    */
   public String getDisplayLanguage(Locale inLocale)
   {
+    if (language.isEmpty())
+      return "";
     try
       {
        ResourceBundle res =
@@ -690,8 +694,27 @@ public final class Locale implements Ser
       }
     catch (MissingResourceException e)
       {
-       return language;
+       /* This means runtime support for the locale
+        * is not available, so we check providers. */
+      }
+    for (LocaleNameProvider p :
+          ServiceLoader.load(LocaleNameProvider.class))
+      {
+       for (Locale loc : p.getAvailableLocales())
+         {
+           if (loc.equals(inLocale))
+             {
+               String locLang = p.getDisplayLanguage(language,
+                                                     inLocale);
+               if (locLang != null)
+                 return locLang;
+               break;
+             }
+         }
       }
+    if (inLocale.equals(Locale.ROOT)) // Base case
+      return language;
+    return getDisplayLanguage(LocaleHelper.getFallbackLocale(inLocale));
   }
 
   /**
@@ -737,6 +760,8 @@ public final class Locale implements Ser
    */
   public String getDisplayCountry(Locale inLocale)
   {
+    if (country.isEmpty())
+      return "";
     try
       {
         ResourceBundle res =
@@ -748,8 +773,27 @@ public final class Locale implements Ser
       }
     catch (MissingResourceException e)
       {
-        return country;
+       /* This means runtime support for the locale
+        * is not available, so we check providers. */
       }
+    for (LocaleNameProvider p :
+          ServiceLoader.load(LocaleNameProvider.class))
+      {
+       for (Locale loc : p.getAvailableLocales())
+         {
+           if (loc.equals(inLocale))
+             {
+               String locCountry = p.getDisplayCountry(country,
+                                                       inLocale);
+               if (locCountry != null)
+                 return locCountry;
+               break;
+             }
+         }
+      }
+    if (inLocale.equals(Locale.ROOT)) // Base case
+      return country;
+    return getDisplayCountry(LocaleHelper.getFallbackLocale(inLocale));
   }
 
   /**
@@ -796,6 +840,8 @@ public final class Locale implements Ser
    */
   public String getDisplayVariant(Locale inLocale)
   {
+    if (variant.isEmpty())
+      return "";
     try
       {
         ResourceBundle res =
@@ -807,8 +853,27 @@ public final class Locale implements Ser
       }
     catch (MissingResourceException e)
       {
-        return variant;
+       /* This means runtime support for the locale
+        * is not available, so we check providers. */
+      }
+    for (LocaleNameProvider p :
+          ServiceLoader.load(LocaleNameProvider.class))
+      {
+       for (Locale loc : p.getAvailableLocales())
+         {
+           if (loc.equals(inLocale))
+             {
+               String locVar = p.getDisplayVariant(variant,
+                                                   inLocale);
+               if (locVar != null)
+                 return locVar;
+               break;
+             }
+         }
       }
+    if (inLocale.equals(Locale.ROOT)) // Base case
+      return country;
+    return getDisplayVariant(LocaleHelper.getFallbackLocale(inLocale));
   }
 
   /**
Index: java/util/spi/LocaleNameProvider.java
===================================================================
RCS file: java/util/spi/LocaleNameProvider.java
diff -N java/util/spi/LocaleNameProvider.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ java/util/spi/LocaleNameProvider.java       2 Jan 2007 21:35:26 -0000
@@ -0,0 +1,135 @@
+/* LocaleNameProvider.java -- Providers of localized locale names
+   Copyright (C) 2007 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package java.util.spi;
+
+import java.util.Locale;
+
+/**
+ * A [EMAIL PROTECTED] LocaleNameProvider} provides localized
+ * versions of the names that represent a particular
+ * locale.  Note that a <code>null</code> value may
+ * be returned, which should be treated as a lack of
+ * support for the specified [EMAIL PROTECTED] Locale}.
+ *
+ * @author Andrew John Hughes ([EMAIL PROTECTED])
+ * @since 1.6
+ */
+public abstract class LocaleNameProvider
+  extends LocaleServiceProvider
+{
+
+  /**
+   * Constructs a new [EMAIL PROTECTED] LocaleNameProvider}.
+   * Provided for implicit invocation by subclasses.
+   */
+  protected LocaleNameProvider()
+  {
+  }
+
+  /**
+   * Returns the localized name for the specified ISO 3166
+   * country in the supplied [EMAIL PROTECTED] java.util.Locale}.
+   * For example, if the country code is <code>"DE"</code>,
+   * this method will return <code>"Germany"</code> for
+   * [EMAIL PROTECTED] Locale.ENGLISH} but <code>"Deutschland"</code>
+   * for [EMAIL PROTECTED] Locale.GERMANY}.  If the name of the country
+   * in the given locale is not supported, <code>null</code>
+   * is returned.
+   *
+   * @param countryCode the ISO 3166 country code, consisting
+   *                    of two uppercase letters from 'A' to 'Z'
+   * @param locale the locale to express the country in.
+   * @return the country name, or <code>null</code> if one is
+   *         not available.
+   * @throws NullPointerException if the locale is null.
+   * @throws IllegalArgumentException if the country code is
+   *                                  not in the correct format
+   *                                  or the locale is not one
+   *                                  returned by
+   *                                  [EMAIL PROTECTED] getAvailableLocales()}
+   * @see java.util.Locale#getDisplayCountry(java.util.Locale)
+   */
+  public abstract String getDisplayCountry(String countryCode,
+                                          Locale locale);
+
+  /**
+   * Returns the localized name for the specified ISO 639
+   * language in the supplied [EMAIL PROTECTED] java.util.Locale}.
+   * For example, if the language code is <code>"de"</code>,
+   * this method will return <code>"German"</code> for
+   * [EMAIL PROTECTED] Locale.ENGLISH} but <code>"Deutsch"</code>
+   * for [EMAIL PROTECTED] Locale.GERMANY}.  If the name of the language
+   * in the given locale is not supported, <code>null</code>
+   * is returned.
+   *
+   * @param langCode the ISO 639 language code, consisting
+   *                 of two lowercase letters from 'a' to 'z'
+   * @param locale the locale to express the language in.
+   * @return the country name, or <code>null</code> if one is
+   *         not available.
+   * @throws NullPointerException if the locale is null.
+   * @throws IllegalArgumentException if the language code is
+   *                                  not in the correct format
+   *                                  or the locale is not one
+   *                                  returned by
+   *                                  [EMAIL PROTECTED] getAvailableLocales()}
+   * @see java.util.Locale#getDisplayLanguage(java.util.Locale)
+   */
+  public abstract String getDisplayLanguage(String langCode,
+                                           Locale locale);
+
+  /**
+   * Returns the localized name for the specified variant
+   * in the supplied [EMAIL PROTECTED] java.util.Locale}.  If the name
+   * of the variant in the given locale is not supported,
+   * <code>null</code> is returned.
+   *
+   * @param variant the variant.
+   * @param locale the locale to express the variant in.
+   * @return the localized variant, or <code>null</code> if one is
+   *         not available.
+   * @throws NullPointerException if the locale is null.
+   * @throws IllegalArgumentException if the locale is not one
+   *                                  returned by
+   *                                  [EMAIL PROTECTED] getAvailableLocales()}
+   * @see java.util.Locale#getDisplayVariant(java.util.Locale)
+   */
+  public abstract String getDisplayVariant(String variant,
+                                          Locale locale);
+
+}
Index: java/util/spi/TimeZoneNameProvider.java
===================================================================
RCS file: java/util/spi/TimeZoneNameProvider.java
diff -N java/util/spi/TimeZoneNameProvider.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ java/util/spi/TimeZoneNameProvider.java     2 Jan 2007 21:35:26 -0000
@@ -0,0 +1,97 @@
+/* TimeZoneNameProvider.java -- Providers of localized currency symbols
+   Copyright (C) 2007 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package java.util.spi;
+
+import java.util.Locale;
+
+/**
+ * A [EMAIL PROTECTED] TimeZoneNameProvider} provides localized
+ * versions of the names that represent a particular
+ * timezone.  A <code>null</code> value may
+ * be returned, which should be treated as a lack of
+ * support for the specified [EMAIL PROTECTED] Locale}.  The names
+ * from this class are also used by
+ * [EMAIL PROTECTED] DateFormatSymbols#getZoneStrings()}.
+ *
+ * @author Andrew John Hughes ([EMAIL PROTECTED])
+ * @since 1.6
+ */
+public abstract class TimeZoneNameProvider
+  extends LocaleServiceProvider
+{
+
+  /**
+   * Constructs a new [EMAIL PROTECTED] TimeZoneNameProvider}.
+   * Provided for implicit invocation by subclasses.
+   */
+  protected TimeZoneNameProvider()
+  {
+  }
+
+  /**
+   * Returns a name for the specified time zone identifier
+   * localized to the supplied [EMAIL PROTECTED] java.util.Locale}.
+   * The time zone identifier is either <code>"GMT"</code>
+   * or one of the identifiers from the public domain "tz
+   * database" found at <a href="ftp://elsie.nci.nih.gov/pub/";>
+   * ftp://elsie.nci.nih.gov/pub</a>.  Note that a translated
+   * name for the daylight savings time variant should be returned,
+   * even if the timezone has not observed daylight savings
+   * time in the past.  If the name of the timezone
+   * in the given locale is not supported, <code>null</code>
+   * is returned.
+   *
+   * @param id a time zone identifier.
+   * @param daylight true if the daylight savings time variant
+   *                 should be returned.
+   * @param style either [EMAIL PROTECTED] java.util.TimeZone.LONG} or
+   *              [EMAIL PROTECTED] java.util.TimeZone.SHORT}
+   * @param locale the locale to express the timezone in.
+   * @return the localized time zone name, or <code>null</code>
+   *         if one is not available.
+   * @throws NullPointerException if the identifer or locale is null.
+   * @throws IllegalArgumentException if the style is invalid
+   *                                  or the locale is not one
+   *                                  returned by
+   *                                  [EMAIL PROTECTED] getAvailableLocales()}
+   * @see java.util.TimeZone#getDisplayName(boolean,int,java.util.Locale)
+   */
+  public abstract String getDisplayName(String id, boolean daylight,
+                                       int style, Locale locale);
+
+}

Attachment: signature.asc
Description: Digital signature

Reply via email to