sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1518807 - in /sis/branches/JDK7/core: 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/
Date Thu, 29 Aug 2013 20:25:11 GMT
Author: desruisseaux
Date: Thu Aug 29 20:25:11 2013
New Revision: 1518807

URL: http://svn.apache.org/r1518807
Log:
More conservative inference of code space if not explicitely specified.

Modified:
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ImmutableIdentifier.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.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=1518807&r1=1518806&r2=1518807&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] Thu Aug 29 20:25:11 2013
@@ -29,7 +29,6 @@ import org.opengis.referencing.Reference
 import org.opengis.util.InternationalString;
 import org.apache.sis.util.Immutable;
 import org.apache.sis.util.Deprecable;
-import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.iso.Types;
 import org.apache.sis.metadata.iso.citation.Citations;
@@ -147,6 +146,7 @@ public class ImmutableIdentifier impleme
         } else {
             remarks = null;
         }
+        validate();
     }
 
     /**
@@ -188,12 +188,12 @@ public class ImmutableIdentifier impleme
     public ImmutableIdentifier(final Citation authority, final String codeSpace,
             final String code, final String version, final InternationalString remarks)
     {
-        ensureNonNull("code", code);
         this.code      = code;
         this.codeSpace = codeSpace;
         this.authority = authority;
         this.version   = version;
         this.remarks   = remarks;
+        validate();
     }
 
     /**
@@ -261,20 +261,30 @@ public class ImmutableIdentifier impleme
             throw illegalPropertyType(AUTHORITY_KEY, value);
         }
         /*
-         * Complete the code space if it was not explicitly set. We take the first
-         * identifier if there is any, otherwise we take the shortest title.
+         * Complete the code space if it was not explicitly set. We take a short identifier
(preferred) or title
+         * (as a fallback), with precedence given to Unicode identifier (see Citations.getIdentifier(…)
for more
+         * information). Then the getCodeSpace(…) method applies additional restrictions
in order to reduce the
+         * risk of false code space.
          */
         value = properties.get(CODESPACE_KEY);
-        if (value == null) {
-            final String id = Citations.getIdentifier(authority);
-            codeSpace = (id != null && CharSequences.isUnicodeIdentifier(id)) ? id
: null;
+        if (value == null && !properties.containsKey(CODESPACE_KEY)) {
+            codeSpace = getCodeSpace(authority);
         } else if (value instanceof String) {
             codeSpace = (String) value;
         } else {
             throw illegalPropertyType(CODESPACE_KEY, value);
         }
-        if (code == null) {
-            throw new IllegalArgumentException(Errors.format(Errors.Keys.MissingValueForProperty_1,
CODE_KEY));
+        validate();
+    }
+
+    /**
+     * Ensures that the properties of this {@code ImmutableIdentifier} are valid.
+     */
+    private void validate() {
+        if (code == null || code.isEmpty()) {
+            throw new IllegalArgumentException(Errors.format((code == null)
+                    ? Errors.Keys.MissingValueForProperty_1
+                    : Errors.Keys.EmptyProperty_1, CODE_KEY));
         }
     }
 
@@ -336,6 +346,37 @@ public class ImmutableIdentifier impleme
     }
 
     /**
+     * Infers a code space from the given authority. First, this method takes a short identifier
or title with
+     * preference for Unicode identifier - see {@link Citations#getIdentifier(Citation)}
for more information.
+     * Next this method applies additional restrictions in order to reduce the risk of undesired
code space.
+     * Those restrictions are arbitrary and may change in any future SIS version. Currently,
the restriction
+     * is to accept only letters or digits.
+     *
+     * @param  authority The authority for which to get a code space.
+     * @return The code space, or {@code null} if none.
+     *
+     * @see Citations#getIdentifier(Citation)
+     */
+    private static String getCodeSpace(final Citation authority) {
+        final String codeSpace = Citations.getIdentifier(authority);
+        if (codeSpace != null) {
+            final int length = codeSpace.length();
+            if (length != 0) {
+                int i = 0;
+                do {
+                    final int c = codeSpace.charAt(i);
+                    if (!Character.isLetterOrDigit(c)) {
+                        return null;
+                    }
+                    i += Character.charCount(c);
+                } while (i < length);
+                return codeSpace;
+            }
+        }
+        return null;
+    }
+
+    /**
      * Organization or party responsible for definition and maintenance of the
      * {@linkplain #getCode code}.
      *

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java?rev=1518807&r1=1518806&r2=1518807&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
[UTF-8] Thu Aug 29 20:25:11 2013
@@ -1514,7 +1514,7 @@ cmp:    while (ia < lga) {
      * Unicode identifier start} and all remaining characters (if any) are
      * {@linkplain Character#isUnicodeIdentifierPart(int) Unicode identifier parts}.
      *
-     * @param identifier The character sequence to test.
+     * @param  identifier The character sequence to test.
      * @return {@code true} if the given character sequence is a legal Unicode identifier.
      * @throws NullPointerException if the argument is null.
      */

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java?rev=1518807&r1=1518806&r2=1518807&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultInternationalString.java
[UTF-8] Thu Aug 29 20:25:11 2013
@@ -27,6 +27,7 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.logging.Level;
+import java.util.logging.LogRecord;
 import org.opengis.util.InternationalString;
 import org.apache.sis.util.Locales;
 import org.apache.sis.util.ThreadSafe;
@@ -174,8 +175,17 @@ public class DefaultInternationalString 
         final boolean i18n = (string instanceof InternationalString);
         add(locale, i18n ? ((InternationalString) string).toString(locale) : string.toString());
         if (i18n && !(string instanceof SimpleInternationalString)) {
-            Logging.log(Types.class, "toInternationalString", // This is the public facade
invoking this method.
-                    Messages.getResources(null).getLogRecord(Level.WARNING, Messages.Keys.LocalesDiscarded));
+            /*
+             * If the string may have more than one locale, log a warning telling that some
locales
+             * may have been ignored. We declare Types.toInternationalString(…) as the
source since
+             * it is the public facade invoking this method. We declare the source class
using only
+             * its name rather than Types.class in order to avoid unnecessary real dependency.
+             */
+            final LogRecord record = Messages.getResources(null).getLogRecord(Level.WARNING,
Messages.Keys.LocalesDiscarded);
+            record.setSourceClassName("org.apache.sis.util.iso.Types");
+            record.setSourceMethodName("toInternationalString");
+            record.setLoggerName("org.apache.sis.util.iso");
+            Logging.getLogger("org.apache.sis.util.iso").log(record);
         }
     }
 



Mime
View raw message