sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1451232 - in /sis/branches/JDK7: sis-metadata/src/main/java/org/apache/sis/metadata/ sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/ sis-utility/src/main/java/org/apache/sis/util/iso/ sis-utility/src/test/java/org/apache/sis/ut...
Date Thu, 28 Feb 2013 15:22:52 GMT
Author: desruisseaux
Date: Thu Feb 28 15:22:52 2013
New Revision: 1451232

URL: http://svn.apache.org/r1451232
Log:
Remove Locale argument in Types.foo(..., Locale) and replace the return type from String to
InternationalString.
This is both more coherent with the library and more convenient for PropertyDescriptor.getRemarks().

Modified:
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyDescriptor.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/CodeListProxy.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/ResourceInternationalString.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/TypesTest.java

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyDescriptor.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyDescriptor.java?rev=1451232&r1=1451231&r2=1451232&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyDescriptor.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyDescriptor.java
[UTF-8] Thu Feb 28 15:22:52 2013
@@ -19,7 +19,6 @@ package org.apache.sis.metadata;
 import java.util.Set;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Locale;
 import java.lang.reflect.Method;
 import net.jcip.annotations.Immutable;
 import javax.measure.unit.Unit;
@@ -31,7 +30,6 @@ import org.opengis.parameter.ParameterVa
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.referencing.ReferenceIdentifier;
 import org.apache.sis.internal.simple.SimpleReferenceIdentifier;
-import org.apache.sis.util.iso.AbstractInternationalString;
 import org.apache.sis.measure.NumberRange;
 import org.apache.sis.measure.ValueRange;
 import org.apache.sis.util.iso.Types;
@@ -222,11 +220,7 @@ class PropertyDescriptor<T> extends Simp
      */
     @Override
     public final InternationalString getRemarks() {
-        return new AbstractInternationalString() {
-            @Override public String toString(final Locale locale) {
-                return Types.getDescription(container, code, locale);
-            }
-        };
+        return Types.getDescription(container, code);
     }
 
     /**

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/CodeListProxy.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/CodeListProxy.java?rev=1451232&r1=1451231&r2=1451232&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/CodeListProxy.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/CodeListProxy.java
[UTF-8] Thu Feb 28 15:22:52 2013
@@ -171,10 +171,10 @@ public final class CodeListProxy {
         codeList = schema(context, "gmxCodelists.xml", classID);
         /*
          * Get the localized name of the field identifier, if possible.
-         * This code partially duplicates Types.getCodeTitle(CodeList, Locale).
+         * This code partially duplicates Types.getCodeTitle(CodeList).
          * This duplication exists because this constructor stores more information in
          * an opportunist way. If this constructor is updated, please consider updating
-         * the Types.getCodeTitle(CodeList, Locale) method accordingly.
+         * the Types.getCodeTitle(CodeList) method accordingly.
          */
         final Locale locale = context.getLocale();
         if (locale != null) {
@@ -192,7 +192,7 @@ public final class CodeListProxy {
             // Fallback when no value is defined for the code list. Build a value from the
             // most descriptive name (excluding the field name), which is usually the UML
             // name except for CharacterSet in which case it is a string like "UTF-8".
-            value = Types.getCodeTitle(code);
+            value = Types.getCodeLabel(code);
         }
         codeListValue = fieldID;
     }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/ResourceInternationalString.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/ResourceInternationalString.java?rev=1451232&r1=1451231&r2=1451232&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/ResourceInternationalString.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/ResourceInternationalString.java
[UTF-8] Thu Feb 28 15:22:52 2013
@@ -25,34 +25,46 @@ import net.jcip.annotations.Immutable;
 
 import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
 
-// Related to JDK7
-import java.util.Objects;
-
 
 /**
- * An international string backed by a {@linkplain ResourceBundle resource bundle}.
- * A resource bundle can be a Java class or a {@linkplain Properties properties} file,
- * one for each language. The constructor expects the fully qualified class name of the base
- * resource bundle (the one used when no resource was found in the client's language). The
appropriate
- * resource bundle is loaded at runtime for the client's language by looking for a class
or a
- * properties file with the right suffix, for example {@code "_en"} for English or {@code
"_fr"}
- * for French. This mechanism is explained in J2SE javadoc for the
- * {@link ResourceBundle#getBundle(String, Locale, ClassLoader) getBundle(…)} static method.
+ * An international string backed by a {@link ResourceBundle}.
+ * Resource bundles can be Java classes or {@linkplain Properties properties} files, one
for each
+ * language. The fully qualified class name of the base resource bundle (without locale suffix)
+ * is specified at {@linkplain #ResourceInternationalString(String, String) construction
time}.
+ * The appropriate resource bundle is loaded at runtime for the client's language by looking
for
+ * a class or a properties file with the right suffix, for example {@code "_en"} for English
or
+ * {@code "_fr"} for French.
+ * See the {@link ResourceBundle#getBundle(String, Locale, ClassLoader) ResourceBundle.getBundle(…)}
+ * Javadoc for more information.
  *
  * {@section Example}
- * If a file named "{@code MyResources.properties}" exists in the package {@code org.mypackage}
- * and contains a line like "{@code MyKey = some value}", then an international string for
- * {@code "some value"} can be created using the following code:
+ * If a file named "{@code MyResources.properties}" exists in {@code org.mypackage}
+ * and contains the following line:
+ *
+ * {@preformat text
+ *     MyKey = some value
+ * }
+ *
+ * Then an international string for {@code "some value"} can be created using the following
code:
  *
  * {@preformat java
  *     InternationalString value = new ResourceInternationalString("org.mypackage.MyResources",
"MyKey");
  * }
  *
  * The {@code "some value"} string will be localized if the required properties files exist,
for
- * example "{@code MyResources_fr.properties}" for French, "{@code MyResources_it.properties}"
+ * example "{@code MyResources_fr.properties}" for French, or "{@code MyResources_it.properties}"
  * for Italian, <i>etc</i>.
  * If needed, users can gain more control by overriding the {@link #getBundle(Locale)} method.
  *
+ * {@section Class loaders}
+ * Developers can specify explicitely the {@link ClassLoader} to use be overriding the
+ * {@link #getBundle(Locale)} method. This is recommended if the running environment
+ * loads modules in isolated class loaders, as OSGi does for instance.
+ *
+ * {@note We do not provide <code>ClassLoader</code> argument in the constructor
of this class
+ *        because class loaders can often be hard-coded (thus avoiding the cost of an extra
field)
+ *        and are usually not serializable.}
+ *
  * {@section Apache SIS resources}
  * Apache SIS has its own resources mechanism, built on top of the standard {@code ResourceBundle}
  * with the addition of type safety and optional arguments to be formatted in the localized
string.
@@ -65,6 +77,8 @@ import java.util.Objects;
  * @since   0.3 (derived from geotk-2.1)
  * @version 0.3
  * @module
+ *
+ * @see ResourceBundle#getBundle(String, Locale)
  */
 @Immutable
 public class ResourceInternationalString extends AbstractInternationalString implements Serializable
{
@@ -74,33 +88,28 @@ public class ResourceInternationalString
     private static final long serialVersionUID = 6339944890723487336L;
 
     /**
-     * The name of the resource bundle from which to fetch the string.
+     * The name of the resource bundle, as a fully qualified class name.
+     * This value is given at construction time and can not be {@code null}.
      */
-    private final String resources;
+    protected final String resources;
 
     /**
      * The key for the resource to fetch.
+     * This value is given at construction time and can not be {@code null}.
      */
-    private final String key;
+    protected final String key;
 
     /**
-     * The class loader to use for loading the resources file, or {@code null} for the default
-     * class loader.
-     */
-    private final transient ClassLoader loader;
-
-    /**
-     * Creates a new international string from the specified resource bundle, key and class
loader.
+     * Creates a new international string from the specified resource bundle and key.
+     * The class loader will be the one of the {@link #toString(Locale)} caller,
+     * unless the {@link #getBundle(Locale)} method is overridden.
      *
      * @param resources The name of the resource bundle, as a fully qualified class name.
-     * @param key The key for the resource to fetch.
-     * @param loader The class loader to use for loading the resources file,
-     *        or {@code null} for the default class loader.
+     * @param key       The key for the resource to fetch.
      */
-    public ResourceInternationalString(final String resources, final String key, final ClassLoader
loader) {
+    public ResourceInternationalString(final String resources, final String key) {
         this.resources = resources;
         this.key       = key;
-        this.loader    = loader;
         ensureNonNull("resources", resources);
         ensureNonNull("key",       key);
     }
@@ -110,14 +119,26 @@ public class ResourceInternationalString
      * bundle from the name given at {@linkplain #ResourceInternationalString construction
time}.
      * Subclasses can override this method if they need to fetch the bundle in an other way.
      *
+     * {@section Class loaders}
+     * By default, this method loads the resources using the caller's class loader.
+     * Subclasses can override this method in order to specify a different class loader.
+     * For example, the code below works well if {@code MyResource} is a class defined
+     * in the same module than the one that contain the resources to load:
+     *
+     * {@preformat java
+     *     &#64;Override
+     *     protected ResourceBundle getBundle(final Locale locale) {
+     *         return ResourceBundle.getBundle(resources, locale, MyResource.class.getClassLoader());
+     *     }
+     * }
+     *
      * @param  locale The locale for which to get the resource bundle.
      * @return The resource bundle for the given locale.
      *
-     * @see ResourceBundle#getBundle(String, Locale)
+     * @see ResourceBundle#getBundle(String, Locale, ClassLoader)
      */
     protected ResourceBundle getBundle(final Locale locale) {
-        return (loader == null) ? ResourceBundle.getBundle(resources, locale) :
-                ResourceBundle.getBundle(resources, locale, loader);
+        return ResourceBundle.getBundle(resources, locale);
     }
 
     /**
@@ -152,8 +173,7 @@ public class ResourceInternationalString
     public boolean equals(final Object object) {
         if (object != null && object.getClass() == getClass()) {
             final ResourceInternationalString that = (ResourceInternationalString) object;
-            return Objects.equals(this.key,       that.key) &&
-                   Objects.equals(this.resources, that.resources);
+            return key.equals(that.key) && resources.equals(resources);
         }
         return false;
     }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java?rev=1451232&r1=1451231&r2=1451232&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/Types.java [UTF-8]
Thu Feb 28 15:22:52 2013
@@ -51,8 +51,8 @@ import org.apache.sis.internal.util.Defa
  *       for creating name-related objects from various objects.</li>
  *   <li>{@link #getStandardName(Class)}, {@link #getListName(CodeList)} and {@link
#getCodeName(CodeList)}
  *       for fetching ISO names if possible.</li>
- *   <li>{@link #getCodeTitle(CodeList, Locale)}, {@link #getDescription(CodeList,
Locale)} and
- *       {@link #getDescription(Class, Locale)} for fetching human-readable descriptions.</li>
+ *   <li>{@link #getCodeTitle(CodeList)}, {@link #getDescription(CodeList)} and
+ *       {@link #getDescription(Class)} for fetching human-readable descriptions.</li>
  *   <li>{@link #forStandardName(String)} and {@link #forCodeName(Class, String, boolean)}
for
  *       fetching an instance from a name (converse of above {@code get} methods).</li>
  * </ul>
@@ -64,13 +64,6 @@ import org.apache.sis.internal.util.Defa
  */
 public final class Types extends Static {
     /**
-     * The class loader to use for fetching GeoAPI resources.
-     * Since the resources are bundled in the GeoAPI JAR file,
-     * we use the instance that loaded GeoAPI for more determinist behavior.
-     */
-    private static final ClassLoader CLASSLOADER = UML.class.getClassLoader();
-
-    /**
      * The types for ISO 19115 UML identifiers. The keys are UML identifiers. Values
      * are either class names as {@link String} objects, or the {@link Class} instances.
      * This map will be built only when first needed.
@@ -159,8 +152,9 @@ public final class Types extends Static 
      * @return The UML identifiers or programmatic name for the given code,
      *         or {@code null} if the given code is null.
      *
+     * @see #getCodeLabel(CodeList)
      * @see #getCodeTitle(CodeList)
-     * @see #getDescription(CodeList, Locale)
+     * @see #getDescription(CodeList)
      * @see #forCodeName(Class, String, boolean)
      */
     public static String getCodeName(final CodeList<?> code) {
@@ -175,7 +169,7 @@ public final class Types extends Static 
      * Returns a unlocalized title for the given code.
      * This method builds a title using heuristics rules, which should give reasonable
      * results without the need of resource bundles. For better results, consider using
-     * {@link #getCodeTitle(CodeList, Locale)} instead.
+     * {@link #getCodeTitle(CodeList)} instead.
      *
      * <p>The current heuristic implementation iterates over {@linkplain CodeList#names()
all
      * code names}, selects the longest one excluding the {@linkplain CodeList#name() field
name}
@@ -183,18 +177,19 @@ public final class Types extends Static 
      * from that name. Examples:</p>
      *
      * <ul>
-     *   <li>{@code getCodeTitle(AxisDirection.NORTH)} returns {@code "North"}.</li>
-     *   <li>{@code getCodeTitle(CharacterSet.UTF_8)} returns {@code "UTF-8"}.</li>
-     *   <li>{@code getCodeTitle(ImagingCondition.BLURRED_IMAGE)} returns {@code "Blurred
image"}.</li>
+     *   <li>{@code getCodeLabel(AxisDirection.NORTH)} returns {@code "North"}.</li>
+     *   <li>{@code getCodeLabel(CharacterSet.UTF_8)} returns {@code "UTF-8"}.</li>
+     *   <li>{@code getCodeLabel(ImagingCondition.BLURRED_IMAGE)} returns {@code "Blurred
image"}.</li>
      * </ul>
      *
      * @param  code The code from which to get a title, or {@code null}.
      * @return A unlocalized title for the given code, or {@code null} if the given code
is null.
      *
      * @see #getCodeName(CodeList)
-     * @see #getDescription(CodeList, Locale)
+     * @see #getCodeTitle(CodeList)
+     * @see #getDescription(CodeList)
      */
-    public static String getCodeTitle(final CodeList<?> code) {
+    public static String getCodeLabel(final CodeList<?> code) {
         if (code == null) {
             return null;
         }
@@ -212,152 +207,197 @@ public final class Types extends Static 
     }
 
     /**
-     * Returns the localized title of the given code.
-     * Special cases:
+     * Returns the title of the given code. Title are usually much shorter than descriptions.
+     * English titles are often the same than the {@linkplain #getCodeLabel(CodeList) code
labels}.
      *
-     * <ul>
-     *   <li>If {@code code} is {@code null}, then this method returns {@code null}.</li>
-     *   <li>If {@code locale} is {@code null}, then this method uses {@link Locale#ROOT}
-     *       for "unlocalized" (typically English) strings.</li>
-     *   <li>If there is no resources for the given code in the given language, then
this method
-     *       fallback on other languages as described in {@link ResourceBundle} javadoc.</li>
-     *   <li>If there is no localized resources for the given code, then this method
fallback
-     *       on {@link #getCodeTitle(CodeList)}.</li>
-     * </ul>
-     *
-     * @param  code   The code for which to get the localized name, or {@code null}.
-     * @param  locale The locale, or {@code null} if none.
-     * @return The localized title, or {@code null} if the given code is null.
+     * @param  code The code for which to get the title, or {@code null}.
+     * @return The title, or {@code null} if the given code is null.
      *
-     * @see #getDescription(CodeList, Locale)
+     * @see #getDescription(CodeList)
      */
-    public static String getCodeTitle(final CodeList<?> code, Locale locale) {
-        if (code == null) {
-            return null;
-        }
-        if (locale == null) {
-            locale = Locale.ROOT;
-        }
-        /*
-         * The code below is a duplicated - in a different way - of CodeListProxy(CodeList)
-         * constructor (org.apache.sis.internal.jaxb.code package). This duplication exists
-         * because CodeListProxy constructor stores more information in an opportunist way.
-         * If this method is updated, please update CodeListProxy(CodeList) accordingly.
-         */
-        final String key = getListName(code) + '.' + getCodeName(code);
-        try {
-            return ResourceBundle.getBundle("org.opengis.metadata.CodeLists", locale, CLASSLOADER).getString(key);
-        } catch (MissingResourceException e) {
-            Logging.recoverableException(Types.class, "getCodeTitle", e);
-            return getCodeTitle(code);
-        }
+    public static InternationalString getCodeTitle(final CodeList<?> code) {
+        return (code != null) ? new CodeTitle(code) : null;
     }
 
     /**
-     * Returns the localized description of the given code, or {@code null} if none.
-     * Special cases:
-     *
-     * <ul>
-     *   <li>If {@code code} is {@code null}, then this method returns {@code null}.</li>
-     *   <li>If {@code locale} is {@code null}, then this method uses the
-     *       {@linkplain Locale#getDefault() default locale} - there is no such thing
-     *       like "unlocalized" description.</li>
-     *   <li>If there is no resources for the given code in the given language, then
this method
-     *       fallback on other languages as described in {@link ResourceBundle} javadoc.</li>
-     *   <li>If there is no localized resources for the given code, then this method
returns
-     *       {@code null} - there is no fallback.</li>
-     * </ul>
-     *
+     * Returns the description of the given code, or {@code null} if none.
      * For a description of the code list as a whole instead than a particular code,
-     * see {@link Types#getDescription(Class, Locale)}.
+     * see {@link Types#getDescription(Class)}.
      *
-     * @param  code   The code for which to get the localized description, or {@code null}.
-     * @param  locale The desired locale, or {@code null} for the default locale.
-     * @return The localized description, or {@code null} if the given code is null.
+     * @param  code The code for which to get the localized description, or {@code null}.
+     * @return The description, or {@code null} if none or if the given code is null.
      *
-     * @see #getCodeTitle(CodeList, Locale)
-     * @see #getDescription(Class, Locale)
+     * @see #getCodeTitle(CodeList)
+     * @see #getDescription(Class)
      */
-    public static String getDescription(final CodeList<?> code, final Locale locale)
{
-        return (code != null) ? getDescription(getListName(code) + '.' + getCodeName(code),
locale) : null;
+    public static InternationalString getDescription(final CodeList<?> code) {
+        if (code != null) {
+            final String resources = getResources(code.getClass().getName());
+            if (resources != null) {
+                return new Description(resources, getListName(code) + '.' + getCodeName(code));
+            }
+        }
+        return null;
     }
 
     /**
-     * Returns a localized description for the given class, or {@code null} if none.
+     * Returns a description for the given class, or {@code null} if none.
      * This method can be used for GeoAPI interfaces or {@link CodeList}.
-     * Special cases:
-     *
-     * <ul>
-     *   <li>If {@code type} is {@code null}, then this method returns {@code null}.</li>
-     *   <li>If {@code locale} is {@code null}, then this method uses the
-     *       {@linkplain Locale#getDefault() default locale} - there is no such thing
-     *       like "unlocalized" description.</li>
-     *   <li>If there is no resources for the given type in the given language, then
this method
-     *       fallback on other languages as described in {@link ResourceBundle} javadoc.</li>
-     *   <li>If there is no localized resources for the given type, then this method
returns
-     *       {@code null} - there is no fallback.</li>
-     * </ul>
      *
      * @param  type The GeoAPI interface or code list from which to get the description,
or {@code null}.
-     * @param  locale The desired locale, or {@code null} for the default locale.
-     * @return The localized description, or {@code null} if none or if the given type is
{@code null}.
+     * @return The description, or {@code null} if none or if the given type is {@code null}.
      *
-     * @see #getDescription(CodeList, Locale)
+     * @see #getDescription(CodeList)
      */
-    public static String getDescription(final Class<?> type, final Locale locale) {
-        return getDescription(getStandardName(type), locale);
+    public static InternationalString getDescription(final Class<?> type) {
+        final String name = getStandardName(type);
+        if (name != null) {
+            final String resources = getResources(type.getName());
+            if (resources != null) {
+                return new Description(resources, name);
+            }
+        }
+        return null;
     }
 
     /**
-     * Returns a localized description for the given property, or {@code null} if none.
-     * The given property name shall be a UML identifier.
-     * Special cases:
-     *
-     * <ul>
-     *   <li>If {@code type} or {@code property} is {@code null}, then this method
returns {@code null}.</li>
-     *   <li>If {@code locale} is {@code null}, then this method uses the
-     *       {@linkplain Locale#getDefault() default locale} - there is no such thing
-     *       like "unlocalized" description.</li>
-     *   <li>If there is no resources for the given property in the given language,
then this method
-     *       fallback on other languages as described in {@link ResourceBundle} javadoc.</li>
-     *   <li>If there is no localized resources for the given property, then this method
returns
-     *       {@code null} - there is no fallback.</li>
-     * </ul>
+     * Returns a description for the given property, or {@code null} if none.
+     * The given type shall be a GeoAPI interface, and the given property shall
+     * be a UML identifier. If any of the input argument is {@code null}, then
+     * this method returns {@code null}.
      *
      * @param  type     The GeoAPI interface from which to get the description of a property,
or {@code null}.
      * @param  property The ISO name of the property for which to get the description, or
{@code null}.
-     * @param  locale   The desired locale, or {@code null} for the default locale.
-     * @return The localized description, or {@code null} if none or if the given type
-     *         or property name is {@code null}.
+     * @return The description, or {@code null} if none or if the given type or property
name is {@code null}.
      */
-    public static String getDescription(final Class<?> type, final String property,
final Locale locale) {
+    public static InternationalString getDescription(final Class<?> type, final String
property) {
         if (property != null) {
             final String name = getStandardName(type);
             if (name != null) {
-                return getDescription(name + '.' + property, locale);
+                final String resources = getResources(type.getName());
+                if (resources != null) {
+                    return new Description(resources, name + '.' + property);
+                }
             }
         }
         return null;
     }
 
     /**
-     * Returns the descriptions for the given key in the given locale.
+     * The {@link InternationalString} returned by the {@code Types.getDescription(…)}
methods.
      *
-     * @param  key     The ISO identifier of a class, or a class property, or a code list
value.
-     * @param  locale  The locale in which to get the description.
-     * @return The description, or {@code null} if none.
-     */
-    private static String getDescription(final String key, Locale locale) {
-        if (key != null) {
-            if (locale == null) {
-                locale = Locale.getDefault();
-            }
+     * @author  Martin Desruisseaux (Geomatys)
+     * @since   0.3
+     * @version 0.3
+     * @module
+     */
+    private static class Description extends ResourceInternationalString {
+        /**
+         * For cross-version compatibility.
+         */
+        private static final long serialVersionUID = 7336442452947376873L;
+
+        /**
+         * The class loader to use for fetching GeoAPI resources.
+         * Since the resources are bundled in the GeoAPI JAR file,
+         * we use the instance that loaded GeoAPI for more determinist behavior.
+         */
+        private static final ClassLoader CLASSLOADER = UML.class.getClassLoader();
+
+        /**
+         * Creates a new international string from the specified resource bundle and key.
+         *
+         * @param resources The name of the resource bundle, as a fully qualified class name.
+         * @param key       The key for the resource to fetch.
+         */
+        Description(final String resources, final String key) {
+            super(resources, key);
+        }
+
+        /**
+         * Loads the resources using the class loader used for loading GeoAPI interfaces.
+         */
+        @Override
+        protected final ResourceBundle getBundle(final Locale locale) {
+            return ResourceBundle.getBundle(resources, locale, CLASSLOADER);
+        }
+
+        /**
+         * Returns the description for the given locale, or fallback on a default description
+         * if no resources exist for that locale.
+         */
+        @Override
+        public final String toString(final Locale locale) {
             try {
-                return ResourceBundle.getBundle("org.opengis.metadata.Descriptions", locale,
CLASSLOADER).getString(key);
+                return super.toString(locale);
             } catch (MissingResourceException e) {
-                Logging.recoverableException(Types.class, "getDescription", e);
+                Logging.recoverableException(ResourceInternationalString.class, "toString",
e);
+                return fallback();
             }
         }
+
+        /**
+         * Returns a fallback if no resource is found.
+         */
+        String fallback() {
+            return CharSequences.camelCaseToSentence(key.substring(key.lastIndexOf('.') +
1)).toString();
+        }
+    }
+
+    /**
+     * The {@link InternationalString} returned by the {@code Types.getCodeTitle(…)} method.
+     * The code below is a duplicated - in a different way - of {@code CodeListProxy(CodeList)}
+     * constructor ({@link org.apache.sis.internal.jaxb.code package}). This duplication
exists
+     * because {@code CodeListProxy} constructor stores more information in an opportunist
way.
+     * If this method is updated, please update {@code CodeListProxy(CodeList)} accordingly.
+     *
+     * @author  Martin Desruisseaux (Geomatys)
+     * @since   0.3
+     * @version 0.3
+     * @module
+     */
+    private static final class CodeTitle extends Description {
+        /**
+         * For cross-version compatibility.
+         */
+        private static final long serialVersionUID = 3306532357801489365L;
+
+        /**
+         * The code list for which to create a title.
+         */
+        private final CodeList<?> code;
+
+        /**
+         * Creates a new international string for the given code list element.
+         *
+         * @param code The code list for which to create a title.
+         */
+        CodeTitle(final CodeList<?> code) {
+            super("org.opengis.metadata.CodeLists", getListName(code) + '.' + getCodeName(code));
+            this.code = code;
+        }
+
+        /**
+         * Returns a fallback if no resource is found.
+         */
+        @Override
+        String fallback() {
+            return getCodeLabel(code);
+        }
+    }
+
+    /**
+     * Returns the resource name for the given GeoAPI type, or {@code null} if none.
+     *
+     * @param  classname The fully qualified name of the GeoAPI type.
+     * @return The resource bundle to load, or {@code null} if none.
+     */
+    static String getResources(final String classname) {
+        String resources = "org.opengis.metadata.Descriptions";
+        if (classname.regionMatches(0, resources, 0, 21)) { // 21 is the location after the
last dot.
+            return resources;
+        }
+        // Add more checks here (maybe in a loop) if there is more resource candidates.
         return null;
     }
 

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/TypesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/TypesTest.java?rev=1451232&r1=1451231&r2=1451232&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/TypesTest.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/TypesTest.java [UTF-8]
Thu Feb 28 15:22:52 2013
@@ -20,6 +20,7 @@ import java.util.Set;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Locale;
+import org.opengis.util.InternationalString;
 import org.opengis.metadata.citation.Address;
 import org.opengis.metadata.citation.Citation;
 import org.opengis.metadata.citation.OnLineFunction;
@@ -77,40 +78,51 @@ public final strictfp class TypesTest ex
     }
 
     /**
-     * Tests the {@link Types#getDescription(Class, Locale)} method.
+     * Tests the {@link Types#getResources(String)} method.
+     */
+    @Test
+    public void testGetResources() {
+        assertEquals("org.opengis.metadata.Descriptions", Types.getResources("org.opengis.metadata.Identifier"));
+        assertNull(Types.getResources("org.opengis.metadata2.Identifier"));
+    }
+
+    /**
+     * Tests the {@link Types#getDescription(Class)} method.
      */
     @Test
     public void testGetDescription() {
+        final InternationalString description = Types.getDescription(CharacterSet.class);
         assertEquals("Name of the character coding standard used in the resource.",
-                Types.getDescription(CharacterSet.class, Locale.ROOT));
+                description.toString(Locale.ROOT));
         assertEquals("Name of the character coding standard used in the resource.",
-                Types.getDescription(CharacterSet.class, Locale.ENGLISH));
+                description.toString(Locale.ENGLISH));
         assertEquals("Jeu de caractères.",
-                Types.getDescription(CharacterSet.class, Locale.FRENCH));
+                description.toString(Locale.FRENCH));
     }
 
     /**
-     * Tests the {@link Types#getDescription(Class, String, Locale)} method.
+     * Tests the {@link Types#getDescription(Class, String)} method.
      */
     @Test
     public void testGetPropertyDescription() {
         assertEquals("The city of the location.",
-                Types.getDescription(Address.class, "city", Locale.ROOT));
+                Types.getDescription(Address.class, "city").toString(Locale.ROOT));
         assertEquals("Country of the physical address.",
-                Types.getDescription(Address.class, "country", Locale.ENGLISH));
+                Types.getDescription(Address.class, "country").toString(Locale.ENGLISH));
     }
 
     /**
-     * Tests the {@link Types#getDescription(CodeList, Locale)} method.
+     * Tests the {@link Types#getDescription(CodeList)} method.
      */
     @Test
     public void testGetCodeDescription() {
+        final InternationalString description = Types.getDescription(CharacterSet.ISO_8859_1);
         assertEquals("ISO/IEC 8859-1, Information technology - 8-bit single byte coded graphic
character sets - Part 1 : Latin alphabet No.1.",
-                Types.getDescription(CharacterSet.ISO_8859_1, Locale.ROOT));
+                description.toString(Locale.ROOT));
         assertEquals("ISO/IEC 8859-1, Information technology - 8-bit single byte coded graphic
character sets - Part 1 : Latin alphabet No.1.",
-                Types.getDescription(CharacterSet.ISO_8859_1, Locale.ENGLISH));
+                description.toString(Locale.ENGLISH));
         assertEquals("ISO/IEC 8859-1, alphabet latin 1.",
-                Types.getDescription(CharacterSet.ISO_8859_1, Locale.FRENCH));
+                description.toString(Locale.FRENCH));
     }
 
     /**
@@ -134,23 +146,23 @@ public final strictfp class TypesTest ex
     }
 
     /**
-     * Tests the examples given in {@link Types#getCodeTitle(CodeList)} javadoc.
+     * Tests the examples given in {@link Types#getCodeLabel(CodeList)} javadoc.
      */
     @Test
-    public void testGetCodeTitle() {
-        assertEquals("North",         Types.getCodeTitle(AxisDirection   .NORTH));
-        assertEquals("UTF-8",         Types.getCodeTitle(CharacterSet    .UTF_8));
-        assertEquals("Blurred image", Types.getCodeTitle(ImagingCondition.BLURRED_IMAGE));
+    public void testGetCodeLabel() {
+        assertEquals("North",         Types.getCodeLabel(AxisDirection   .NORTH));
+        assertEquals("UTF-8",         Types.getCodeLabel(CharacterSet    .UTF_8));
+        assertEquals("Blurred image", Types.getCodeLabel(ImagingCondition.BLURRED_IMAGE));
     }
 
     /**
-     * Tests {@link Types#getCodeTitle(CodeList, Locale)}.
+     * Tests {@link Types#getCodeTitle(CodeList)}.
      */
     @Test
-    public void testGetLocalizedCodeTitle() {
-        assertEquals("Download",       Types.getCodeTitle(OnLineFunction.DOWNLOAD, Locale.ROOT));
-        assertEquals("Download",       Types.getCodeTitle(OnLineFunction.DOWNLOAD, Locale.ENGLISH));
-        assertEquals("Téléchargement", Types.getCodeTitle(OnLineFunction.DOWNLOAD, Locale.FRENCH));
+    public void testGetCodeTitle() {
+        assertEquals("Download",       Types.getCodeTitle(OnLineFunction.DOWNLOAD).toString(Locale.ROOT));
+        assertEquals("Download",       Types.getCodeTitle(OnLineFunction.DOWNLOAD).toString(Locale.ENGLISH));
+        assertEquals("Téléchargement", Types.getCodeTitle(OnLineFunction.DOWNLOAD).toString(Locale.FRENCH));
     }
 
     /**



Mime
View raw message