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 fd212eb0627b940459b522518e99ea029d11118f
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Tue Apr 5 15:15:32 2022 +0200

    Add a default `getIdentifier()` implementation for `URIDataStore`.
---
 .../apache/sis/internal/storage/URIDataStore.java  | 54 +++++++++++++++-------
 .../java/org/apache/sis/storage/DataStore.java     |  6 +--
 2 files changed, 41 insertions(+), 19 deletions(-)

diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/URIDataStore.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/URIDataStore.java
index ce4faf81d0..dd0e0c7a8d 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/URIDataStore.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/URIDataStore.java
@@ -23,6 +23,7 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.FileSystemNotFoundException;
 import java.nio.charset.Charset;
+import org.opengis.util.GenericName;
 import org.opengis.parameter.ParameterValueGroup;
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.parameter.ParameterDescriptorGroup;
@@ -35,6 +36,7 @@ import org.apache.sis.storage.DataStoreProvider;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.IllegalOpenParameterException;
 import org.apache.sis.internal.storage.io.IOUtilities;
+import org.apache.sis.util.iso.Names;
 import org.apache.sis.util.logging.Logging;
 
 
@@ -93,15 +95,6 @@ public abstract class URIDataStore extends DataStore 
implements StoreResource, R
         locationIsPath = (locationAsPath != null);
     }
 
-    /**
-     * If the location was specified as a {@link Path} or {@link File} 
instance, returns that path.
-     * Otherwise returns {@code null}. This method does not try to convert URI 
to {@link Path}
-     * because this conversion may fail for HTTP and FTP connections.
-     */
-    final Path getSpecifiedPath() {
-        return locationIsPath ? locationAsPath : null;
-    }
-
     /**
      * Returns the originator of this resource, which is this data store 
itself.
      *
@@ -112,6 +105,39 @@ public abstract class URIDataStore extends DataStore 
implements StoreResource, R
         return this;
     }
 
+    /**
+     * Returns an identifier for the root resource of this data store, or an 
empty value if none.
+     * The default implementation returns the filename without path and 
without file extension.
+     *
+     * @return an identifier for the root resource of this data store.
+     * @throws DataStoreException if an error occurred while fetching the 
identifier.
+     */
+    @Override
+    public Optional<GenericName> getIdentifier() throws DataStoreException {
+        final String filename = getFilename();
+        return (filename != null) ? Optional.of(Names.createLocalName(null, 
null, filename)) : super.getIdentifier();
+    }
+
+    /**
+     * Returns the filename without path and without file extension, or {@code 
null} if none.
+     */
+    private String getFilename() {
+        if (location == null) {
+            return null;
+        }
+        return IOUtilities.filenameWithoutExtension(location.isOpaque()
+                ? location.getSchemeSpecificPart() : location.getPath());
+    }
+
+    /**
+     * If the location was specified as a {@link Path} or {@link File} 
instance, returns that path.
+     * Otherwise returns {@code null}. This method does not try to convert URI 
to {@link Path}
+     * because this conversion may fail for HTTP and FTP connections.
+     */
+    final Path getSpecifiedPath() {
+        return locationIsPath ? locationAsPath : null;
+    }
+
     /**
      * Returns the {@linkplain #location} as a {@code Path} component or an 
empty array if none.
      * The default implementation returns the storage specified at 
construction time if it was
@@ -331,13 +357,9 @@ public abstract class URIDataStore extends DataStore 
implements StoreResource, R
      * @param  builder  where to add the title or identifier.
      */
     protected final void addTitleOrIdentifier(final MetadataBuilder builder) {
-        if (location != null) {
-            /*
-             * The getDisplayName() contract does not allow us to use it as an 
identifier. However current implementation
-             * in super.getDisplayName() returns the filename provided that 
the input was a URI, URL, File or Path. Since
-             * all those types are convertibles to URI, we can use (location 
!= null) as a criterion.
-             */
-            
builder.addTitleOrIdentifier(IOUtilities.filenameWithoutExtension(super.getDisplayName()),
 MetadataBuilder.Scope.ALL);
+        final String filename = getFilename();
+        if (filename != null) {
+            builder.addTitleOrIdentifier(filename, MetadataBuilder.Scope.ALL);
         }
     }
 }
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java 
b/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
index 2237b2953e..fe9c6f037b 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
@@ -283,7 +283,7 @@ public abstract class DataStore implements Resource, 
Localized, AutoCloseable {
     }
 
     /**
-     * Returns an identifier for the root resource of this data store, or 
{@code null} if none.
+     * Returns an identifier for the root resource of this data store, or an 
empty value if none.
      * If this data store contains many resources (as in an {@link Aggregate}),
      * the returned identifier shall be different than the identifiers of 
those child resources.
      * In other words, the following equality shall hold without ambiguity:
@@ -400,9 +400,9 @@ public abstract class DataStore implements Resource, 
Localized, AutoCloseable {
      * representation of the return value of {@link Resource#getIdentifier()} 
on the desired resource.
      * Implementation may also accept aliases for convenience. For example if 
the full name of a resource
      * is {@code "foo:bar"}, then this method may accept {@code "bar"} as a 
synonymous of {@code "foo:bar"}
-     * provided that it does not introduce ambiguity.
+     * provided that it is unambiguous.
      *
-     * <p>The default implementation verifies if above criterion matches to 
this {@code DataStore}
+     * <p>The default implementation verifies if above criterion apply to this 
{@code DataStore}
      * (which is itself a resource), then iterates recursively over {@link 
Aggregate} components
      * if this data store is an aggregate.
      * If a match is found without ambiguity, the associated resource is 
returned. Otherwise an exception is thrown.

Reply via email to