This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 7c0bba02438a9ea17c9b52c1ba62330af370278a
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Sun Feb 23 11:47:39 2025 +0100

    Do not interpret as authorities the code spaces that are filenames.
    We do that by adding a method in `Citations` which returns a value only for 
known authorities.
---
 .../sis/metadata/iso/citation/Citations.java       | 54 +++++++++++++++-------
 .../sis/metadata/privy/NameToIdentifier.java       |  2 +-
 .../sis/referencing/ImmutableIdentifierTest.java   |  2 +-
 3 files changed, 39 insertions(+), 19 deletions(-)

diff --git 
a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/iso/citation/Citations.java
 
b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/iso/citation/Citations.java
index 5c3c60ee61..d7ea0d2907 100644
--- 
a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/iso/citation/Citations.java
+++ 
b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/iso/citation/Citations.java
@@ -22,6 +22,7 @@ import java.util.Objects;
 import java.util.Iterator;
 import java.util.Collection;
 import java.util.Locale;
+import java.util.Optional;
 import org.opengis.util.InternationalString;
 import org.opengis.metadata.Identifier;
 import org.opengis.metadata.citation.Citation;
@@ -74,7 +75,7 @@ import org.apache.sis.metadata.iso.DefaultIdentifier;         
  // For javadoc
  * </ul>
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 1.2
+ * @version 1.5
  * @since   0.3
  */
 public final class Citations extends Static {
@@ -511,6 +512,37 @@ public final class Citations extends Static {
         return Arrays.copyOf(CITATIONS, CITATIONS.length, Citation[].class);
     }
 
+    /**
+     * Returns a predefined citation for the given identifier.
+     * This method can be used as an alternative to {@link #fromName(String)}
+     * when the caller wants to conservatively restrict the result to a 
well-known set of authorities.
+     *
+     * @param  identifier  the citation title (or alternate title), or {@code 
null}.
+     * @return a predefined citation using the specified name.
+     *
+     * @since 1.5
+     */
+    public static Optional<Citation> predefined(String identifier) {
+        if (identifier == null || ((identifier = 
identifier.strip()).isEmpty())) {
+            return null;
+        }
+        for (final CitationConstant citation : CITATIONS) {
+            if (equalsFiltered(identifier, citation.namespace)) {
+                return Optional.of(citation);
+            }
+        }
+        Citation citation;
+        if (equalsFiltered(identifier, Constants.CRS) || 
equalsFiltered(identifier, "WMS")) {
+            citation = WMS;
+        } else if (equalsFiltered(identifier, "IOGP") || 
equalsFiltered(identifier, "OGP")) {
+            // "OGP" is the old name of "IOGP" organization.
+            citation = IOGP;
+        } else {
+            return Optional.empty();
+        }
+        return Optional.of(citation);
+    }
+
     /**
      * Returns a citation of the given identifier. The method makes the 
following choice:
      *
@@ -528,23 +560,11 @@ public final class Citations extends Static {
         if (identifier == null || ((identifier = 
identifier.strip()).isEmpty())) {
             return null;
         }
-        for (final CitationConstant citation : CITATIONS) {
-            if (equalsFiltered(identifier, citation.namespace)) {
-                return citation;
-            }
-        }
-        if (equalsFiltered(identifier, Constants.CRS) || 
equalsFiltered(identifier, "WMS")) {
-            return WMS;
-        }
-        // "OGP" is the old name of "IOGP" organization.
-        if (equalsFiltered(identifier, "IOGP") || equalsFiltered(identifier, 
"OGP")) {
-            return IOGP;
+        Citation citation = predefined(identifier).orElse(null);
+        if (citation == null) {
+            citation = new SimpleCitation(identifier);
         }
-        /*
-         * If we found no match, 
org.apache.sis.metadata.internal.ServicesForUtility expects
-         * that we return anything that is not an instance of CitationConstant.
-         */
-        return new SimpleCitation(identifier);
+        return citation;
     }
 
     /**
diff --git 
a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/privy/NameToIdentifier.java
 
b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/privy/NameToIdentifier.java
index d6be1ec48d..6e154515c4 100644
--- 
a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/privy/NameToIdentifier.java
+++ 
b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/privy/NameToIdentifier.java
@@ -87,7 +87,7 @@ public final class NameToIdentifier implements Identifier {
                 return null;
             }
         }
-        return Citations.fromName(scope.head().toString());
+        return Citations.predefined(scope.head().toString()).orElse(null);
     }
 
     /**
diff --git 
a/endorsed/src/org.apache.sis.referencing/test/org/apache/sis/referencing/ImmutableIdentifierTest.java
 
b/endorsed/src/org.apache.sis.referencing/test/org/apache/sis/referencing/ImmutableIdentifierTest.java
index 00f91795f9..13211c78e0 100644
--- 
a/endorsed/src/org.apache.sis.referencing/test/org/apache/sis/referencing/ImmutableIdentifierTest.java
+++ 
b/endorsed/src/org.apache.sis.referencing/test/org/apache/sis/referencing/ImmutableIdentifierTest.java
@@ -60,7 +60,7 @@ public final class ImmutableIdentifierTest extends TestCase {
      * Returns the properties map to be used in argument to test methods.
      */
     private static Map<String,Object> properties() {
-        final Map<String,Object> properties = new HashMap<>();
+        final var properties = new HashMap<String,Object>();
         assertNull(properties.put(CODE_KEY,            "This is a code"));
         assertNull(properties.put(AUTHORITY_KEY,       "This is an 
authority"));
         assertNull(properties.put(VERSION_KEY,         "This is a version"));

Reply via email to