sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1551838 - in /sis/branches/JDK6: ./ application/sis-console/src/main/java/org/apache/sis/console/ core/sis-utility/src/main/java/org/apache/sis/internal/converter/ core/sis-utility/src/main/java/org/apache/sis/util/ core/sis-utility/src/ma...
Date Wed, 18 Dec 2013 04:48:06 GMT
Author: desruisseaux
Date: Wed Dec 18 04:48:05 2013
New Revision: 1551838

URL: http://svn.apache.org/r1551838
Log:
Merge from the JDK7 branch, with adaptation for non-existant API on JDK6.

Modified:
    sis/branches/JDK6/   (props changed)
    sis/branches/JDK6/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
    sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/internal/converter/StringConverter.java
    sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/Locales.java
    sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java
    sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/Pooled.java
    sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/ValueConverter.java
    sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/internal/converter/StringConverterTest.java
    sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java

Propchange: sis/branches/JDK6/
------------------------------------------------------------------------------
  Merged /sis/branches/JDK7:r1551739-1551835

Modified: sis/branches/JDK6/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java?rev=1551838&r1=1551837&r2=1551838&view=diff
==============================================================================
--- sis/branches/JDK6/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
[UTF-8] (original)
+++ sis/branches/JDK6/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
[UTF-8] Wed Dec 18 04:48:05 2013
@@ -201,7 +201,7 @@ abstract class SubCommand {
             value = options.get(option = Option.COLORS);
             console = System.console();
             colors = (value != null) ? Option.COLORS.parseBoolean(value) : (console != null)
&& X364.isAnsiSupported();
-        } catch (IllegalArgumentException e) {
+        } catch (RuntimeException e) {
             final String name = option.name().toLowerCase(Locale.US);
             throw new InvalidOptionException(Errors.format(Errors.Keys.IllegalOptionValue_2,
name, value), name);
         }

Modified: sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/internal/converter/StringConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/internal/converter/StringConverter.java?rev=1551838&r1=1551837&r2=1551838&view=diff
==============================================================================
--- sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/internal/converter/StringConverter.java
[UTF-8] (original)
+++ sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/internal/converter/StringConverter.java
[UTF-8] Wed Dec 18 04:48:05 2013
@@ -247,7 +247,7 @@ abstract class StringConverter<T> extend
         private static final long serialVersionUID = -794933131690043494L;
         public Locale() {super(java.util.Locale.class);} // Instantiated by ServiceLoader.
 
-        @Override java.util.Locale doConvert(String source) throws IllegalArgumentException
{
+        @Override java.util.Locale doConvert(String source) {
             return Locales.parse(source);
         }
     }

Modified: sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/Locales.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/Locales.java?rev=1551838&r1=1551837&r2=1551838&view=diff
==============================================================================
--- sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/Locales.java [UTF-8]
(original)
+++ sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/Locales.java [UTF-8]
Wed Dec 18 04:48:05 2013
@@ -26,7 +26,6 @@ import java.util.LinkedHashSet;
 import java.util.MissingResourceException;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
-
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.resources.Errors;
 
@@ -257,11 +256,11 @@ filter: for (final Locale locale : local
      *
      * @param  code The language code, optionally followed by country code and variant.
      * @return The language for the given code (never {@code null}).
-     * @throws IllegalArgumentException If the given code doesn't seem to be a valid locale.
+     * @throws RuntimeException If the given code is not valid ({@code IllformedLocaleException}
on the JDK7 branch).
      *
      * @see Locale#forLanguageTag(String)
      */
-    public static Locale parse(final String code) throws IllegalArgumentException {
+    public static Locale parse(final String code) {
         return parse(code, 0);
     }
 
@@ -279,43 +278,65 @@ filter: for (final Locale locale : local
      * @param  code The language code, which may be followed by country code.
      * @param  fromIndex Index of the first character to parse.
      * @return The language for the given code (never {@code null}).
-     * @throws IllegalArgumentException If the given code doesn't seem to be a valid locale.
+     * @throws RuntimeException If the given code is not valid ({@code IllformedLocaleException}
on the JDK7 branch).
      *
      * @see Locale#forLanguageTag(String)
      * @see org.apache.sis.util.iso.Types#toInternationalString(Map, String)
      */
-    public static Locale parse(final String code, final int fromIndex) throws IllegalArgumentException
{
+    public static Locale parse(final String code, final int fromIndex) {
         ArgumentChecks.ensureNonNull("code", code);
         ArgumentChecks.ensurePositive("fromIndex", fromIndex);
-        boolean hasMore = false;
-        String language, country, variant;
-        int ci = code.indexOf('_', fromIndex);
-        if (ci < 0) {
-            language = (String) trimWhitespaces(code, fromIndex, code.length());
-            country  = "";
-            variant  = "";
+        int p1 = code.indexOf('_', fromIndex);
+        // JDK7 branch contains a code here with the following comment:
+            /*
+             * IETF BCP 47 language tag string. This syntax uses the '-' separator instead
of '_'.
+             * Note that the '_' character is illegal for the language code, but is legal
for the
+             * variant. Consequently we require the '-' character to appear before the first
'_'.
+             */
+        // End of JDK7-specific.
+        /*
+         * Old syntax (e.g. "en_US"). Split in (language, country, variant) components,
+         * then convert the 3-letters codes to the 2-letters ones.
+         */
+        String language, country = "", variant = "";
+        if (p1 < 0) {
+            p1 = code.length();
         } else {
-            language = (String) trimWhitespaces(code, fromIndex, ci);
-            int vi = code.indexOf('_', ++ci);
-            if (vi < 0) {
-                country = (String) trimWhitespaces(code, ci, code.length());
-                variant = "";
+            final int s = p1 + 1;
+            int p2 = code.indexOf('_', s);
+            if (p2 < 0) {
+                p2 = code.length();
             } else {
-                country = (String) trimWhitespaces(code, ci, vi);
-                variant = (String) trimWhitespaces(code, ++vi, code.length());
-                hasMore = code.indexOf('_', vi) >= 0;
+                variant = (String) trimWhitespaces(code, p2+1, code.length());
             }
+            country = (String) trimWhitespaces(code, s, p2);
         }
-        if (hasMore || language.length() > 3 || country.length() > 3) {
-            throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalLanguageCode_1,
-                    code.substring(fromIndex)));
-        }
+        language = (String) trimWhitespaces(code, fromIndex, p1);
         language = toISO2(language, LANGUAGE);
         country  = toISO2(country,  COUNTRY);
+        if (language.length() > 8 || !isAlpha(language) ||
+             country.length() > 3 || !isAlpha(country))
+        {
+            throw new RuntimeException( // IllformedLocaleException (indirectly) on the JDK7
branch.
+                    Errors.format(Errors.Keys.IllegalLanguageCode_1, code.substring(fromIndex)));
+        }
         return unique(new Locale(language, country, variant));
     }
 
     /**
+     * Returns {@code true} if the given text contains only Latin alphabetic characters.
+     */
+    private static boolean isAlpha(final String text) {
+        for (int i=text.length(); --i>=0;) {
+            final char c = text.charAt(i);
+            if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' &&
c <= 'z')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
      * Parses the language encoded in the suffix of a property key. This convenience method
      * is used when a property in a {@link java.util.Map} may have many localized variants.
      * For example the {@code "remarks"} property may be defined by values associated to
the
@@ -336,14 +357,14 @@ filter: for (final Locale locale : local
      * @param  prefix The prefix to skip at the beginning of the {@code key}.
      * @param  key    The property key from which to extract the locale, or {@code null}.
      * @return The locale encoded in the given key name, or {@code null} if the key has not
been recognized.
-     * @throws IllegalArgumentException if the locale after the prefix is an illegal code.
+     * @throws RuntimeException If the given code is not valid ({@code IllformedLocaleException}
on the JDK7 branch).
      *
      * @see org.apache.sis.util.iso.Types#toInternationalString(Map, String)
      *
      * @deprecated Users can easily perform this operation themselves, thus avoiding this
class initialization.
      */
     @Deprecated
-    public static Locale parseSuffix(final String prefix, final String key) throws IllegalArgumentException
{
+    public static Locale parseSuffix(final String prefix, final String key) {
         ArgumentChecks.ensureNonNull("prefix", prefix);
         if (key != null) { // Tolerance for Map that accept null keys.
             if (key.startsWith(prefix)) {
@@ -418,8 +439,7 @@ filter: for (final Locale locale : local
      * Otherwise returns the {@code locale} unchanged.
      *
      * @param  locale The locale to canonicalize.
-     * @return A unique instance of the given locale, or {@code locale} if
-     *         the given locale is not cached.
+     * @return A unique instance of the given locale, or {@code locale} if the given locale
is not cached.
      */
     public static Locale unique(final Locale locale) {
         final Locale candidate = POOL.get(locale);

Modified: sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java?rev=1551838&r1=1551837&r2=1551838&view=diff
==============================================================================
--- sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java [UTF-8]
(original)
+++ sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java [UTF-8]
Wed Dec 18 04:48:05 2013
@@ -648,7 +648,13 @@ public final class Types extends Static 
                     if (isSorted && c > '_') break;
                     continue;
                 }
-                locale = Locales.parse(key, offset + 1);
+                final int s = offset + 1;
+                try {
+                    locale = Locales.parse(key, s);
+                } catch (RuntimeException e) { // IllformedLocaleException on the JDK7 branch.
+                    throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalLanguageCode_1,
+                            '(' + key.substring(0, s) + ')' + key.substring(s), e));
+                }
             }
             final Object value = entry.getValue();
             if (value != null) {

Modified: sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/Pooled.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/Pooled.java?rev=1551838&r1=1551837&r2=1551838&view=diff
==============================================================================
--- sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/Pooled.java [UTF-8]
(original)
+++ sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/Pooled.java [UTF-8]
Wed Dec 18 04:48:05 2013
@@ -397,7 +397,7 @@ abstract class Pooled {
                     return;
                 }
             }
-        } catch (RuntimeException e) { // (ClassCastException | IllegalArgumentException)
on the JDK7 branch.
+        } catch (RuntimeException e) { // (ClassCastException | IllformedLocaleException)
on the JDK7 branch.
             throw new PropertyException(Errors.format(
                     Errors.Keys.IllegalPropertyClass_2, name, value.getClass()), e);
         }

Modified: sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/ValueConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/ValueConverter.java?rev=1551838&r1=1551837&r2=1551838&view=diff
==============================================================================
--- sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/ValueConverter.java
[UTF-8] (original)
+++ sis/branches/JDK6/core/sis-utility/src/main/java/org/apache/sis/xml/ValueConverter.java
[UTF-8] Wed Dec 18 04:48:05 2013
@@ -188,15 +188,16 @@ public class ValueConverter {
      * @param  value The string to convert to a locale, or {@code null}.
      * @return The converted locale, or {@code null} if the given value was null or empty,
or
      *         if an exception was thrown and {@code exceptionOccured(…)} returned {@code
true}.
-     * @throws IllegalArgumentException If the given string can not be converted to a locale.
+     * @throws RuntimeException If the given string can not be converted to a locale
+     *         ({@code IllformedLocaleException} on the JDK7 branch).
      *
      * @see Locales#parse(String)
      */
-    public Locale toLocale(final MarshalContext context, String value) throws IllegalArgumentException
{
+    public Locale toLocale(final MarshalContext context, String value) {
         value = trimWhitespaces(value);
         if (value != null && !value.isEmpty()) try {
             return Locales.parse(value);
-        } catch (IllegalArgumentException e) {
+        } catch (RuntimeException e) { // IllformedLocaleException on the JDK7 branch.
             if (!exceptionOccured(context, value, String.class, Locale.class, e)) {
                 throw e;
             }

Modified: sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/internal/converter/StringConverterTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/internal/converter/StringConverterTest.java?rev=1551838&r1=1551837&r2=1551838&view=diff
==============================================================================
--- sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/internal/converter/StringConverterTest.java
[UTF-8] (original)
+++ sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/internal/converter/StringConverterTest.java
[UTF-8] Wed Dec 18 04:48:05 2013
@@ -71,11 +71,11 @@ public final strictfp class StringConver
      */
     private static void tryUnconvertibleValue(final ObjectConverter<String,?> c) {
         try {
-            c.apply("Saturday");
+            c.apply("他の言葉");
             fail("Should not accept a text.");
         } catch (UnconvertibleObjectException e) {
             // This is the expected exception.
-            assertTrue(e.getMessage().contains("Saturday"));
+            assertTrue(e.getMessage().contains("他の言葉"));
         }
     }
 

Modified: sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java?rev=1551838&r1=1551837&r2=1551838&view=diff
==============================================================================
--- sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java
[UTF-8] (original)
+++ sis/branches/JDK6/core/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java
[UTF-8] Wed Dec 18 04:48:05 2013
@@ -81,11 +81,10 @@ public final strictfp class LocalesTest 
         assertSame(Locale.CANADA_FRENCH, Locales.parse("fra_CAN"));
         assertSame(Locale.ENGLISH,       Locales.parse("en"));
 
-        assertEquals(new Locale("de", "DE"),        Locales.parse("de_DE"));
-        assertEquals(new Locale("",   "GB"),        Locales.parse("_GB"));
-        assertEquals(new Locale("en", "US", "WIN"), Locales.parse("en_US_WIN"));
-        assertEquals(new Locale("de", "", "POSIX"), Locales.parse("de__POSIX"));
-        assertEquals(new Locale("fr", "", "MAC"),   Locales.parse("fr__MAC"));
+        assertEquals(new Locale("de", "DE"),            Locales.parse("de_DE"));
+        assertEquals(new Locale("",   "GB"),            Locales.parse("_GB"));
+        assertEquals(new Locale("en", "US", "WINDOWS"), Locales.parse("en_US_WINDOWS"));
+        assertEquals(new Locale("de", "",   "POSIX"),   Locales.parse("de__POSIX"));
     }
 
     /**
@@ -96,8 +95,9 @@ public final strictfp class LocalesTest 
         try {
             Locales.parse("orange_APPLE");
             fail("Shall not parse invalid locale.");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().contains("orange_APPLE"));
+        } catch (RuntimeException e) { // IllformedLocaleException on the JDK7 branch.
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("APPLE"));
         }
     }
 



Mime
View raw message