You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2013/06/25 19:11:38 UTC

svn commit: r1496542 - in /sis/branches/JDK7/core: sis-metadata/src/main/java/org/apache/sis/metadata/iso/ sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/ sis-utility/src/main/java/org/apache/sis/internal/util/

Author: desruisseaux
Date: Tue Jun 25 17:11:38 2013
New Revision: 1496542

URL: http://svn.apache.org/r1496542
Log:
Replaced two ImmutableIdentifier private methods by a call to Citations static method for more consistent behavior.

Modified:
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ImmutableIdentifier.java
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/Citations.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java

Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ImmutableIdentifier.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ImmutableIdentifier.java?rev=1496542&r1=1496541&r2=1496542&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ImmutableIdentifier.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ImmutableIdentifier.java [UTF-8] Tue Jun 25 17:11:38 2013
@@ -18,7 +18,6 @@ package org.apache.sis.metadata.iso;
 
 import java.util.Map;
 import java.util.Locale;
-import java.util.Collection;
 import java.util.logging.Level;
 import java.io.Serializable;
 import javax.xml.bind.annotation.XmlElement;
@@ -32,7 +31,6 @@ import org.opengis.util.InternationalStr
 import org.apache.sis.util.Locales;
 import org.apache.sis.util.Immutable;
 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;
@@ -334,7 +332,7 @@ public class ImmutableIdentifier impleme
          * identifier if there is any, otherwise we take the shortest title.
          */
         if (codeSpace == null && authority instanceof Citation) {
-            codeSpace = getCodeSpace((Citation) authority);
+            codeSpace = Citations.getIdentifier((Citation) authority);
         }
         /*
          * Store the definitive reference to the attributes. Note that casts are performed only
@@ -357,50 +355,6 @@ public class ImmutableIdentifier impleme
     }
 
     /**
-     * Returns the shortest title inferred from the specified authority.
-     * This is used both for creating a generic name, or for inferring a
-     * default identifier code space.
-     */
-    private static InternationalString getShortestTitle(final Citation authority) {
-        InternationalString title = authority.getTitle();
-        int length = title.length();
-        final Collection<? extends InternationalString> alt = authority.getAlternateTitles();
-        if (alt != null) {
-            for (final InternationalString candidate : alt) {
-                final int candidateLength = candidate.length();
-                if (candidateLength > 0 && candidateLength < length) {
-                    title = candidate;
-                    length = candidateLength;
-                }
-            }
-        }
-        return title;
-    }
-
-    /**
-     * Tries to get a code space from the specified authority. This method scans first
-     * through the identifier, then through the titles if no suitable identifier were found.
-     */
-    private static String getCodeSpace(final Citation authority) {
-        if (authority != null) {
-            final Collection<? extends Identifier> identifiers = authority.getIdentifiers();
-            if (identifiers != null) {
-                for (final Identifier id : identifiers) {
-                    final String identifier = id.getCode();
-                    if (CharSequences.isUnicodeIdentifier(identifier)) {
-                        return identifier;
-                    }
-                }
-            }
-            final String title = getShortestTitle(authority).toString(Locale.ROOT);
-            if (CharSequences.isUnicodeIdentifier(title)) {
-                return title;
-            }
-        }
-        return null;
-    }
-
-    /**
      * Returns a SIS identifier implementation with the values of the given arbitrary implementation.
      * This method performs the first applicable actions in the following choices:
      *

Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/Citations.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/Citations.java?rev=1496542&r1=1496541&r2=1496542&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/Citations.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/Citations.java [UTF-8] Tue Jun 25 17:11:38 2013
@@ -193,13 +193,34 @@ public final class Citations extends Sta
     }
 
     /**
-     * Returns the shortest identifier for the specified citation, or the title if there is
-     * no identifier. This method is useful for extracting the namespace from an authority,
-     * for example {@code "EPSG"}.
+     * Infers an identifier from the given citation, or returns {@code null} if no identifier has been found.
+     * This method is useful for extracting the namespace from an authority, for example {@code "EPSG"}.
+     * The implementation performs the following choices:
+     *
+     * <ul>
+     *   <li>If the given citation is {@code null}, then this method returns {@code null}.</li>
+     *   <li>Otherwise if the citation contains at least one {@linkplain Citation#getIdentifiers() identifier}, then:
+     *     <ul>
+     *       <li>If at least one identifier is a {@linkplain org.apache.sis.util.CharSequences#isUnicodeIdentifier
+     *           unicode identifier}, then the shortest of those identifiers is returned.</li>
+     *       <li>Otherwise the shortest identifier is returned, despite not being a Unicode identifier.</li>
+     *     </ul></li>
+     *   <li>Otherwise if the citation contains at least one {@linkplain Citation#getTitle() title} or
+     *       {@linkplain Citation#getAlternateTitles() alternate title}, then:
+     *     <ul>
+     *       <li>If at least one title is a {@linkplain org.apache.sis.util.CharSequences#isUnicodeIdentifier
+     *           unicode identifier}, then the shortest of those titles is returned.</li>
+     *       <li>Otherwise the shortest title is returned, despite not being a Unicode identifier.</li>
+     *     </ul></li>
+     *   <li>Otherwise this method returns {@code null}.</li>
+     * </ul>
+     *
+     * This method searches in alternate titles as a fallback because ISO specification said
+     * that those titles are often used for abbreviations.
      *
      * @param  citation The citation for which to get the identifier, or {@code null}.
-     * @return The shortest identifier of the given citation, or {@code null} if the
-     *         given citation was null or doesn't declare any identifier or title.
+     * @return An identifier for the given citation, or {@code null} if the given citation is null
+     *         or does not declare any identifier or title.
      */
     public static String getIdentifier(final Citation citation) {
         return org.apache.sis.internal.util.Citations.getIdentifier(citation);

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java?rev=1496542&r1=1496541&r2=1496542&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java [UTF-8] Tue Jun 25 17:11:38 2013
@@ -26,6 +26,7 @@ import org.apache.sis.util.Static;
 
 import static org.apache.sis.util.CharSequences.equalsFiltered;
 import static org.apache.sis.util.CharSequences.trimWhitespaces;
+import static org.apache.sis.util.CharSequences.isUnicodeIdentifier;
 import static org.apache.sis.util.Characters.Filter.LETTERS_AND_DIGITS;
 
 // Related to JDK7
@@ -67,6 +68,15 @@ public final class Citations extends Sta
     }
 
     /**
+     * Returns a "unlocalized" string representation of the given international string,
+     * or {@code null} if none. This method is used by {@link #getIdentifier(Citation)},
+     * which is why we don't want the localized string.
+     */
+    private static String toString(final InternationalString title) {
+        return (title != null) ? trimWhitespaces(title.toString(Locale.ROOT)) : null;
+    }
+
+    /**
      * Returns {@code true} if at least one {@linkplain Citation#getTitle() title} or
      * {@linkplain Citation#getAlternateTitles() alternate title} in {@code c1} is leniently
      * equal to a title or alternate title in {@code c2}. The comparison is case-insensitive
@@ -225,15 +235,37 @@ public final class Citations extends Sta
     }
 
     /**
-     * Returns the shortest identifier for the specified citation, or the title if there is
-     * no identifier. This method is useful for extracting the namespace from an authority,
-     * for example {@code "EPSG"}.
+     * Infers an identifier from the given citation, or returns {@code null} if no identifier has been found.
+     * This method is useful for extracting the namespace from an authority, for example {@code "EPSG"}.
+     * The implementation performs the following choices:
+     *
+     * <ul>
+     *   <li>If the given citation is {@code null}, then this method returns {@code null}.</li>
+     *   <li>Otherwise if the citation contains at least one {@linkplain Citation#getIdentifiers() identifier}, then:
+     *     <ul>
+     *       <li>If at least one identifier is a {@linkplain org.apache.sis.util.CharSequences#isUnicodeIdentifier
+     *           unicode identifier}, then the shortest of those identifiers is returned.</li>
+     *       <li>Otherwise the shortest identifier is returned, despite not being a Unicode identifier.</li>
+     *     </ul></li>
+     *   <li>Otherwise if the citation contains at least one {@linkplain Citation#getTitle() title} or
+     *       {@linkplain Citation#getAlternateTitles() alternate title}, then:
+     *     <ul>
+     *       <li>If at least one title is a {@linkplain org.apache.sis.util.CharSequences#isUnicodeIdentifier
+     *           unicode identifier}, then the shortest of those titles is returned.</li>
+     *       <li>Otherwise the shortest title is returned, despite not being a Unicode identifier.</li>
+     *     </ul></li>
+     *   <li>Otherwise this method returns {@code null}.</li>
+     * </ul>
+     *
+     * This method searches in alternate titles as a fallback because ISO specification said
+     * that those titles are often used for abbreviations.
      *
      * @param  citation The citation for which to get the identifier, or {@code null}.
-     * @return The shortest identifier of the given citation, or {@code null} if the
-     *         given citation was null or doesn't declare any identifier or title.
+     * @return An identifier for the given citation, or {@code null} if the given citation is null
+     *         or does not declare any identifier or title.
      */
     public static String getIdentifier(final Citation citation) {
+        boolean isUnicode = false; // Whether 'identifier' is a Unicode identifier.
         String identifier = null;
         if (citation != null) {
             final Iterator<? extends Identifier> it = iterator(citation.getIdentifiers());
@@ -243,18 +275,43 @@ public final class Citations extends Sta
                     final String candidate = trimWhitespaces(id.getCode());
                     if (candidate != null) {
                         final int length = candidate.length();
-                        if (length != 0) {
-                            if (identifier == null || length < identifier.length()) {
+                        if (length != 0 && (identifier == null || length < identifier.length())) {
+                            final boolean s = isUnicodeIdentifier(candidate);
+                            if (s || !isUnicode) {
                                 identifier = candidate;
+                                isUnicode = s;
                             }
                         }
                     }
                 }
             }
+            /*
+             * If no identifier has been found, fallback on the shortest title or alternate title.
+             * We search for alternate titles because ISO specification said that those titles are
+             * often used for abbreviations.
+             */
             if (identifier == null) {
-                final InternationalString title = citation.getTitle();
-                if (title != null) {
-                    identifier = title.toString();
+                identifier = toString(citation.getTitle());
+                if (identifier != null) {
+                    if (identifier.isEmpty()) {
+                        identifier = null;
+                    } else {
+                        isUnicode = isUnicodeIdentifier(identifier);
+                    }
+                }
+                final Iterator<? extends InternationalString> iterator = iterator(citation.getAlternateTitles());
+                if (iterator != null) while (iterator.hasNext()) {
+                    final String candidate = toString(iterator.next());
+                    if (candidate != null) {
+                        final int length = candidate.length();
+                        if (length != 0 && (identifier == null || length < identifier.length())) {
+                            final boolean s = isUnicodeIdentifier(candidate);
+                            if (s || !isUnicode) {
+                                identifier = candidate;
+                                isUnicode = s;
+                            }
+                        }
+                    }
                 }
             }
         }