sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1466910 - in /sis/branches/JDK7: sis-metadata/src/main/java/org/apache/sis/metadata/iso/ sis-utility/src/main/java/org/apache/sis/util/ sis-utility/src/main/java/org/apache/sis/util/iso/ sis-utility/src/test/java/org/apache/sis/util/
Date Thu, 11 Apr 2013 14:47:04 GMT
Author: desruisseaux
Date: Thu Apr 11 14:47:03 2013
New Revision: 1466910

URL: http://svn.apache.org/r1466910
Log:
Slight change in the way to search for localized remarks.

Modified:
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/iso/UnmodifiableIdentifier.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Locales.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/iso/UnmodifiableIdentifier.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/iso/UnmodifiableIdentifier.java?rev=1466910&r1=1466909&r2=1466910&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/iso/UnmodifiableIdentifier.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/iso/UnmodifiableIdentifier.java
[UTF-8] Thu Apr 11 14:47:03 2013
@@ -30,11 +30,13 @@ import org.opengis.metadata.citation.Cit
 import org.opengis.parameter.InvalidParameterValueException;
 import org.opengis.referencing.ReferenceIdentifier;
 import org.opengis.util.InternationalString;
+import org.apache.sis.util.Locales;
 import org.apache.sis.util.Deprecable;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.resources.Messages;
+import org.apache.sis.util.iso.SimpleInternationalString;
 import org.apache.sis.util.iso.DefaultInternationalString;
 import org.apache.sis.internal.simple.SimpleCitation;
 import org.apache.sis.internal.jaxb.metadata.CI_Citation;
@@ -42,7 +44,6 @@ import org.apache.sis.internal.jaxb.meta
 import org.apache.sis.internal.jaxb.gco.StringAdapter;
 
 import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
-import static org.opengis.referencing.IdentifiedObject.NAME_KEY;
 import static org.opengis.referencing.IdentifiedObject.REMARKS_KEY;
 
 // Related to JDK7
@@ -258,16 +259,10 @@ public class UnmodifiableIdentifier impl
          * This algorithm is sub-optimal if the map contains a lot of entries of no interest
to
          * this identifier. Hopefully, most users will fill a map with only useful entries.
          */
-        String key   = null;
-        Object value = null;
         for (final Map.Entry<String,?> entry : properties.entrySet()) {
-            key   = entry.getKey().trim().toLowerCase();
-            value = entry.getValue();
+            String key   = entry.getKey().trim().toLowerCase();
+            Object value = entry.getValue();
             switch (key) {
-                case NAME_KEY: {
-                    code = value;
-                    continue;
-                }
                 case CODE_KEY: {
                     code = value;
                     continue;
@@ -282,31 +277,36 @@ public class UnmodifiableIdentifier impl
                 }
                 case AUTHORITY_KEY: {
                     if (value instanceof String) {
-                        value = new SimpleCitation(value.toString());
+                        value = new SimpleCitation((String) value);
                     }
                     authority = value;
                     continue;
                 }
                 case REMARKS_KEY: {
-                    if (value instanceof InternationalString) {
-                        remarks = value;
-                        continue;
+                    if (value instanceof String) {
+                        value = new SimpleInternationalString((String) value);
                     }
-                    break; // The string will be converted to InternationalString below.
+                    remarks = value;
+                    continue;
                 }
             }
             /*
              * Search for additional locales (e.g. "remarks_fr").
              */
-            if (value instanceof String) {
+            final Locale locale = Locales.parseSuffix(REMARKS_KEY, key);
+            if (locale != null) {
                 if (localized == null) {
                     if (remarks instanceof DefaultInternationalString) {
                         localized = (DefaultInternationalString) remarks;
                     } else {
                         localized = new DefaultInternationalString();
+                        if (remarks instanceof CharSequence) { // String or InternationalString.
+                            localized.add(Locale.ROOT, remarks.toString());
+                            remarks = null;
+                        }
                     }
                 }
-                localized.add(REMARKS_KEY, key, value.toString());
+                localized.add(locale, (String) value);
             }
         }
         /*
@@ -334,6 +334,8 @@ public class UnmodifiableIdentifier impl
          * there (not before). This is a wanted feature, since we want to catch ClassCastExceptions
          * and rethrown them as more informative exceptions.
          */
+        String key   = null;
+        Object value = null;
         try {
             key=      CODE_KEY; this.code      = (String)              (value = code);
             key=   VERSION_KEY; this.version   = (String)              (value = version);

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Locales.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Locales.java?rev=1466910&r1=1466909&r2=1466910&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Locales.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Locales.java [UTF-8] Thu
Apr 11 14:47:03 2013
@@ -202,9 +202,16 @@ public final class Locales extends Stati
      * country code (again either as 2 or 3 letters), optionally followed by {@code '_'}
and
      * the variant.
      *
+     * <p>This method can be used when the caller wants the same {@code Locale} constants
no
+     * matter if the language and country codes use 2 or 3 letters. Apache SIS usually don't
+     * distinguish those two cases since ISO 19139 documents have to use the 3 letters codes
+     * anyway.</p>
+     *
      * @param  code The language code, which may be followed by country code.
      * @return The language for the given code.
      * @throws IllegalArgumentException If the given code doesn't seem to be a valid locale.
+     *
+     * @see Locale#forLanguageTag(String)
      */
     public static Locale parse(final String code) throws IllegalArgumentException {
         final String language, country, variant;
@@ -263,6 +270,43 @@ public final class Locales extends Stati
     }
 
     /**
+     * Parses the locale 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
+     * {@code "remarks_en"} and {@code "remarks_fr"} keys, for English and French locales
+     * respectively.
+     *
+     * <p>This method infers the {@code Locale} from the property {@code key}
+     * with the following steps:</p>
+     *
+     * <ul>
+     *   <li>If the given {@code key} is exactly equals to {@code prefix},
+     *       then this method returns {@link Locale#ROOT}.</li>
+     *   <li>Otherwise if the given {@code key} does not start with the specified {@code
prefix}
+     *       followed by the {@code '_'} character, then this method returns {@code null}.</li>
+     *   <li>Otherwise, the characters after the {@code '_'} are parsed as an ISO language
+     *       and country code by the {@link #parse(String)} method.</li>
+     * </ul>
+     *
+     * @param  prefix The prefix to skip at the beginning of the {@code key}.
+     * @param  key    The property key from which to extract the locale.
+     * @return {@code true} if the key has been recognized, or {@code false} otherwise.
+     * @throws IllegalArgumentException if the locale after the prefix is an illegal code.
+     */
+    public static Locale parseSuffix(final String prefix, final String key) throws IllegalArgumentException
{
+        if (key.startsWith(prefix)) {
+            final int offset = prefix.length();
+            if (key.length() == offset) {
+                return Locale.ROOT;
+            }
+            if (key.charAt(offset) == '_') {
+                return parse(key.substring(offset + 1));
+            }
+        }
+        return null;
+    }
+
+    /**
      * Returns {@code true} if the following code is 3 letters, or {@code false} if 2 letters.
      */
     private static boolean isThreeLetters(final String code) {

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java?rev=1466910&r1=1466909&r2=1466910&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java
[UTF-8] Thu Apr 11 14:47:03 2013
@@ -157,48 +157,6 @@ public class DefaultInternationalString 
     }
 
     /**
-     * Adds a string for the given property key. This is a convenience method for constructing
an
-     * {@code DefaultInternationalString} during iteration through the entries in a {@link
Map}.
-     * It infers the {@code Locale} from the property {@code key}, using the following steps:
-     *
-     * <ul>
-     *   <li>If the {@code key} does not start with the specified {@code prefix}, then
-     *       this method does nothing and returns {@code false}.</li>
-     *   <li>Otherwise, the characters after the {@code prefix} are parsed as an ISO
language
-     *       and country code, and the {@link #add(Locale, String)} method is invoked.</li>
-     * </ul>
-     *
-     * For example if the prefix is {@code "remarks"}, then the {@code "remarks_fr"} property
key
-     * stands for remarks in {@linkplain Locale#FRENCH French} while the {@code "remarks_fr_CA"}
-     * property key stands for remarks in {@linkplain Locale#CANADA_FRENCH French Canadian}.
-     *
-     * @param  prefix The prefix to skip at the beginning of the {@code key}.
-     * @param  key    The property key which will determined the locale.
-     * @param  value  The localized string for the specified {@code key}.
-     * @return {@code true} if the key has been recognized, or {@code false} otherwise.
-     * @throws IllegalArgumentException if the locale after the prefix is an illegal code,
-     *         or a different string value was already set for the given locale.
-     */
-    public boolean add(final String prefix, final String key, final String value)
-            throws IllegalArgumentException
-    {
-        if (key.startsWith(prefix)) {
-            Locale locale = null;
-            final int offset = prefix.length();
-            if (key.length() != offset) {
-                if (key.charAt(offset) == '_') {
-                    locale = Locales.parse(key.substring(offset + 1));
-                } else {
-                    return false;
-                }
-            }
-            add(locale, value);
-            return true;
-        }
-        return false;
-    }
-
-    /**
      * Returns the set of locales defined in this international string.
      *
      * @return The set of locales.

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java?rev=1466910&r1=1466909&r2=1466910&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java [UTF-8]
Thu Apr 11 14:47:03 2013
@@ -19,6 +19,7 @@ package org.apache.sis.util;
 import java.util.Locale;
 import org.apache.sis.test.TestCase;
 import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.DependsOnMethod;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -58,10 +59,19 @@ public final strictfp class LocalesTest 
     }
 
     /**
+     * Tests the {@link Locales#unique(Locale)} method.
+     */
+    @Test
+    public void testUnique() {
+        assertSame(Locale.ENGLISH, Locales.unique(new Locale("en")));
+        assertSame(Locale.FRENCH,  Locales.unique(new Locale("fr")));
+    }
+
+    /**
      * Tests the {@link Locales#parse(String)} method.
-     * Depends on {@link #testUnique()}.
      */
     @Test
+    @DependsOnMethod("testUnique")
     public void testParse() {
         assertSame(Locale.FRENCH,        Locales.parse("fr"));
         assertSame(Locale.FRENCH,        Locales.parse("fra"));
@@ -79,11 +89,16 @@ public final strictfp class LocalesTest 
     }
 
     /**
-     * Tests the {@link Locales#unique(Locale)} method.
+     * Tests the {@link Locales#parseSuffix(String, String)} method.
      */
     @Test
-    public void testUnique() {
-        assertSame(Locale.ENGLISH, Locales.unique(new Locale("en")));
-        assertSame(Locale.FRENCH,  Locales.unique(new Locale("fr")));
+    @DependsOnMethod("testParse")
+    public void testParseSuffix() {
+        assertSame(null,           Locales.parseSuffix("remarks", "remark"));
+        assertSame(Locale.ROOT,    Locales.parseSuffix("remarks", "remarks"));
+        assertSame(Locale.ENGLISH, Locales.parseSuffix("remarks", "remarks_en"));
+        assertSame(Locale.FRENCH,  Locales.parseSuffix("remarks", "remarks_fr"));
+        assertSame(Locale.FRENCH,  Locales.parseSuffix("remarks", "remarks_fra"));
+        assertSame(null,           Locales.parseSuffix("remarks", "remarks2_en"));
     }
 }



Mime
View raw message