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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new a31362ab0a Make `WorldFileStore` class public for giving access to the 
`getFormatNames(boolean)` method. Remove a method which is not used anymore.
a31362ab0a is described below

commit a31362ab0a8de0c63f12619e21443a9b6b825b00
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Fri May 6 10:51:19 2022 +0200

    Make `WorldFileStore` class public for giving access to the 
`getFormatNames(boolean)` method.
    Remove a method which is not used anymore.
---
 .../sis/internal/storage/ResourceOnFileSystem.java |  3 ++
 .../sis/internal/storage/image/WorldFileStore.java | 52 ++++++++++++----------
 .../sis/internal/storage/image/WritableStore.java  | 22 +++++++++
 .../internal/storage/image/WorldFileStoreTest.java |  6 +++
 4 files changed, 59 insertions(+), 24 deletions(-)

diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ResourceOnFileSystem.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ResourceOnFileSystem.java
index 11bb02e958..3dcaa3f101 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ResourceOnFileSystem.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ResourceOnFileSystem.java
@@ -66,6 +66,9 @@ public interface ResourceOnFileSystem extends Resource {
      *
      * @return files used by this resource. Should never be {@code null}.
      * @throws DataStoreException if an error on the file system prevent the 
creation of the list.
+     *
+     * @todo Should be renamed to something else, because current name creates 
a confusion with
+     *       {@link org.apache.sis.storage.Aggregate#components()}.
      */
     Path[] getComponentFiles() throws DataStoreException;
 }
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java
index 0d48e9e8ef..2099f5e115 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java
@@ -25,12 +25,12 @@ import java.io.IOException;
 import java.io.EOFException;
 import java.io.FileNotFoundException;
 import java.io.UncheckedIOException;
-import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.NoSuchFileException;
 import java.nio.file.StandardOpenOption;
 import javax.imageio.ImageIO;
 import javax.imageio.ImageReader;
+import javax.imageio.spi.ImageReaderSpi;
 import javax.imageio.stream.ImageInputStream;
 import org.opengis.metadata.Metadata;
 import org.opengis.metadata.maintenance.ScopeCode;
@@ -114,9 +114,11 @@ import org.apache.sis.setup.OptionKey;
  * @since   1.2
  * @module
  */
-class WorldFileStore extends PRJDataStore {
+public class WorldFileStore extends PRJDataStore {
     /**
      * Image I/O format names (ignoring case) for which we have an entry in 
the {@code SpatialMetadata} database.
+     *
+     * @see <a href="https://issues.apache.org/jira/browse/SIS-300";>SIS-300 — 
Complete the information provided in Citations constants</a>
      */
     private static final String[] KNOWN_FORMATS = {
         "PNG"
@@ -270,25 +272,6 @@ class WorldFileStore extends PRJDataStore {
         reader.addIIOReadWarningListener(new WarningListener(listeners));
     }
 
-    /**
-     * Returns {@code true} if the image file exists and is non-empty.
-     * This is used for checking if an {@link ImageReader} should be created.
-     * If the file is going to be truncated, then it is considered already 
empty.
-     *
-     * @param  connector  the connector to use for opening the file.
-     * @return whether the image file exists and is non-empty.
-     */
-    private boolean fileExists(final StorageConnector connector) throws 
DataStoreException, IOException {
-        if (!ArraysExt.contains(connector.getOption(OptionKey.OPEN_OPTIONS), 
StandardOpenOption.TRUNCATE_EXISTING)) {
-            for (Path path : super.getComponentFiles()) {
-                if (Files.isRegularFile(path) && Files.size(path) > 0) {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
     /**
      * Returns the preferred suffix for the auxiliary world file. For TIFF 
images, this is {@code "tfw"}.
      * This method tries to use the same case (lower-case or upper-case) than 
the suffix of the main file.
@@ -414,6 +397,27 @@ loop:   for (int convention=0;; convention++) {
         return Errors.getResources(listeners.getLocale());
     }
 
+    /**
+     * Returns the Image I/O format names or MIME types of the image read by 
this data store.
+     * More than one names may be returned if the format has aliases or if the 
MIME type
+     * has legacy types (e.g. official {@code "image/png"} and legacy {@code 
"image/x-png"}).
+     *
+     * @param  asMimeType  {@code true} for MIME types, or {@code false} for 
format names.
+     * @return the requested names, or an empty array if none or unknown.
+     */
+    public String[] getImageFormat(final boolean asMimeType) {
+        if (reader != null) {
+            final ImageReaderSpi provider = reader.getOriginatingProvider();
+            if (provider != null) {
+                final String[] names = asMimeType ? provider.getMIMETypes() : 
provider.getFormatNames();
+                if (names != null) {
+                    return names;
+                }
+            }
+        }
+        return CharSequences.EMPTY_ARRAY;
+    }
+
     /**
      * Returns paths to the main file together with auxiliary files.
      *
@@ -421,7 +425,7 @@ loop:   for (int convention=0;; convention++) {
      * @throws DataStoreException if the URI can not be converted to a {@link 
Path}.
      */
     @Override
-    public final synchronized Path[] getComponentFiles() throws 
DataStoreException {
+    public synchronized Path[] getComponentFiles() throws DataStoreException {
         if (suffixWLD == null) try {
             getGridGeometry(MAIN_IMAGE);                // Will compute 
`suffixWLD` as a side effect.
         } catch (IOException e) {
@@ -490,7 +494,7 @@ loop:   for (int convention=0;; convention++) {
      * Returns information about the data store as a whole.
      */
     @Override
-    public final synchronized Metadata getMetadata() throws DataStoreException 
{
+    public synchronized Metadata getMetadata() throws DataStoreException {
         if (metadata == null) try {
             final MetadataBuilder builder = new MetadataBuilder();
             String format = reader().getFormatName();
@@ -528,7 +532,7 @@ loop:   for (int convention=0;; convention++) {
      * @return list of images in this store.
      */
     @SuppressWarnings("ReturnOfCollectionOrArrayField")
-    public final synchronized Collection<? extends GridCoverageResource> 
components() throws DataStoreException {
+    public synchronized Collection<? extends GridCoverageResource> 
components() throws DataStoreException {
         if (components == null) try {
             components = new Components(reader().getNumImages(false));
         } catch (IOException e) {
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WritableStore.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WritableStore.java
index 3d74d0b54e..18136c3de7 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WritableStore.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WritableStore.java
@@ -142,6 +142,28 @@ class WritableStore extends WorldFileStore {
         writer.addIIOWriteWarningListener(new WarningListener(listeners));
     }
 
+    /**
+     * Returns the Image I/O format names or MIME types of the image read or 
written by this data store.
+     * More than one names may be returned if the format has aliases or if the 
MIME type
+     * has legacy types (e.g. official {@code "image/png"} and legacy {@code 
"image/x-png"}).
+     *
+     * @param  asMimeType  {@code true} for MIME types, or {@code false} for 
format names.
+     * @return the requested names, or an empty array if none or unknown.
+     */
+    @Override
+    public String[] getImageFormat(final boolean asMimeType) {
+        if (writer != null) {
+            final ImageWriterSpi provider = writer.getOriginatingProvider();
+            if (provider != null) {
+                final String[] names = asMimeType ? provider.getMIMETypes() : 
provider.getFormatNames();
+                if (names != null) {
+                    return names;
+                }
+            }
+        }
+        return super.getImageFormat(asMimeType);
+    }
+
     /**
      * Returns whether this data store contains more than one image.
      * This is used for deciding if {@link WritableStore} can overwrite a grid 
geometry.
diff --git 
a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/image/WorldFileStoreTest.java
 
b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/image/WorldFileStoreTest.java
index 9c174d5241..1f4c1c58c6 100644
--- 
a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/image/WorldFileStoreTest.java
+++ 
b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/image/WorldFileStoreTest.java
@@ -31,6 +31,7 @@ import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.ProbeResult;
 import org.apache.sis.storage.ResourceAlreadyExistsException;
 import org.apache.sis.setup.OptionKey;
+import org.apache.sis.util.ArraysExt;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -82,6 +83,11 @@ public final strictfp class WorldFileStoreTest extends 
TestCase {
              */
             assertFalse(store instanceof WritableStore);
             assertTrue(store instanceof SingleImageStore);
+            /*
+             * Verify format name and MIME type.
+             */
+            assertTrue(ArraysExt.contains(store.getImageFormat(false), "PNG"));
+            assertTrue(ArraysExt.contains(store.getImageFormat(true), 
"image/png"));
             /*
              * Verify metadata content.
              */

Reply via email to