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 2023/12/09 15:57:57 UTC
(sis) branch geoapi-4.0 updated (ff9dc10dcb -> 558c65499f)
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a change to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
from ff9dc10dcb Complete `MetadataBuilder.useParentElements()`.
new 11025a742e Deprecate `OptionKey.BYTE_BUFFER` because it forces unconditional allocation of byte buffer.
new 172d3c1866 Allow to specify a metadata auxiliary file using wildcard. For example if the main file is "city-center.tiff" and metadata path is "*.xml", then the actual metadata file will be "city-center.xml".
new 55ecacf9dc Accept also as auxiliary metadata a Reference System definition from a GML document.
new 558c65499f Add a "--metadata" option to the command line for making possible to specify the auxiliary metadata.
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../main/org/apache/sis/console/Command.java | 11 +-
.../main/org/apache/sis/console/CommandRunner.java | 61 ++++++++++-
.../apache/sis/console/FormattedOutputCommand.java | 15 +--
.../main/org/apache/sis/console/InfoCommand.java | 2 +-
.../org/apache/sis/console/MetadataCommand.java | 5 +-
.../main/org/apache/sis/console/Option.java | 5 +
.../main/org/apache/sis/console/Options.properties | 7 +-
.../org/apache/sis/console/Options_fr.properties | 7 +-
.../main/org/apache/sis/console/SIS.java | 26 +++++
.../org/apache/sis/console/TransformCommand.java | 4 +-
.../org/apache/sis/console/TranslateCommand.java | 4 +-
.../main/org/apache/sis/io/stream/IOUtilities.java | 61 +++++++----
.../main/org/apache/sis/storage/DataOptionKey.java | 29 ++++++
.../org/apache/sis/storage/StorageConnector.java | 6 +-
.../apache/sis/storage/base/MetadataBuilder.java | 7 +-
.../org/apache/sis/storage/base/PRJDataStore.java | 6 +-
.../org/apache/sis/storage/base/URIDataStore.java | 115 ++++++++++++++++++---
.../sis/storage/esri/RawRasterStoreProvider.java | 5 +-
.../apache/sis/storage/folder/WritableStore.java | 3 +-
.../apache/sis/storage/image/WorldFileStore.java | 5 +-
.../org/apache/sis/io/stream/IOUtilitiesTest.java | 15 +--
.../org/apache/sis/storage/DataOptionKeyTest.java} | 22 ++--
.../main/org/apache/sis/setup/OptionKey.java | 14 ++-
.../test/org/apache/sis/setup/OptionKeyTest.java | 4 +-
24 files changed, 345 insertions(+), 94 deletions(-)
copy endorsed/src/{org.apache.sis.feature/test/org/apache/sis/coverage/grid/j2d/ImageLayoutTest.java => org.apache.sis.storage/test/org/apache/sis/storage/DataOptionKeyTest.java} (65%)
(sis) 03/04: Accept also as auxiliary metadata a Reference System definition from a GML document.
Posted by de...@apache.org.
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 55ecacf9dc5ebcab4da16d273e76edb6dc5d32b5
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Dec 9 14:56:43 2023 +0100
Accept also as auxiliary metadata a Reference System definition from a GML document.
---
.../main/org/apache/sis/storage/base/MetadataBuilder.java | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/MetadataBuilder.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/MetadataBuilder.java
index e43960eec0..e0799a4824 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/MetadataBuilder.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/MetadataBuilder.java
@@ -3366,7 +3366,12 @@ parse: for (int i = 0; i < length;) {
else if (source instanceof Platform) target = platform();
else if (source instanceof ProcessStep) target = processStep();
else if (source instanceof Processing) target = processing();
- else return false;
+ else if (source instanceof ReferenceSystem) {
+ addReferenceSystem((ReferenceSystem) source);
+ return true;
+ } else {
+ return false;
+ }
final Merger merger = new Merger(locale);
merger.copy(source, target);
useParentElements();
(sis) 02/04: Allow to specify a metadata auxiliary file using wildcard. For example if the main file is "city-center.tiff" and metadata path is "*.xml", then the actual metadata file will be "city-center.xml".
Posted by de...@apache.org.
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 172d3c1866dd638f8ae9fd550385dfe134d80924
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Dec 9 14:44:34 2023 +0100
Allow to specify a metadata auxiliary file using wildcard.
For example if the main file is "city-center.tiff" and metadata path
is "*.xml", then the actual metadata file will be "city-center.xml".
---
.../main/org/apache/sis/io/stream/IOUtilities.java | 61 +++++++----
.../main/org/apache/sis/storage/DataOptionKey.java | 5 +
.../org/apache/sis/storage/base/PRJDataStore.java | 6 +-
.../org/apache/sis/storage/base/URIDataStore.java | 115 ++++++++++++++++++---
.../sis/storage/esri/RawRasterStoreProvider.java | 5 +-
.../apache/sis/storage/folder/WritableStore.java | 3 +-
.../apache/sis/storage/image/WorldFileStore.java | 5 +-
.../org/apache/sis/io/stream/IOUtilitiesTest.java | 15 +--
8 files changed, 168 insertions(+), 47 deletions(-)
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/IOUtilities.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/IOUtilities.java
index a4232f2849..9c3d270eb4 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/IOUtilities.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/IOUtilities.java
@@ -72,6 +72,18 @@ public final class IOUtilities extends Static {
*/
public static final String CURRENT_DIRECTORY_SYMBOL = ".";
+ /**
+ * The separator between a filename components in an URI.
+ * Declared for making easier to identify the code doing such separation.
+ */
+ public static final char URI_PATH_SEPARATOR = '/';
+
+ /**
+ * The separator between a filename and its suffix.
+ * Declared for making easier to identify the code doing such separation.
+ */
+ public static final char EXTENSION_SEPARATOR = '.';
+
/**
* Do not allow instantiation of this class.
*/
@@ -113,7 +125,7 @@ public final class IOUtilities extends Static {
* {@link URI} or {@link CharSequence} instance. If no extension is found, returns an empty string.
* If the given object is of unknown type, return {@code null}.
*
- * @param path the filename extension (may be an empty string), or {@code null} if unknown.
+ * @param path the path as an instance of one of the above-cited types, or {@code null}.
* @return the extension in the given path, or an empty string if none, or {@code null}
* if the given object is null or of unknown type.
*/
@@ -123,6 +135,11 @@ public final class IOUtilities extends Static {
/**
* Implementation of {@link #filename(Object)} and {@link #extension(Object)} methods.
+ *
+ * @param path the path as an instance of the types listed in public methods, or {@code null}.
+ * @param exension {@code true} for requesting the extension instead of the file name.
+ * @return the filename or extension in the given path, or an empty string if none, or {@code null}
+ * if the given object is null or of unknown type.
*/
private static String part(final Object path, final boolean extension) {
int fromIndex = 0;
@@ -132,10 +149,12 @@ public final class IOUtilities extends Static {
name = ((File) path).getName();
end = name.length();
} else if (path instanceof Path) {
- name = ((Path) path).getFileName().toString();
+ final Path tip = ((Path) path).getFileName();
+ if (tip == null) return null;
+ name = tip.toString();
end = name.length();
} else {
- char separator = '/';
+ char separator = URI_PATH_SEPARATOR;
if (path instanceof URL) {
name = ((URL) path).getPath();
} else if (path instanceof URI) {
@@ -155,8 +174,8 @@ public final class IOUtilities extends Static {
end = name.length();
do {
if (--end < 0) return ""; // `end` is temporarily inclusive in this loop.
- fromIndex = name.lastIndexOf('/', end);
- if (separator != '/') {
+ fromIndex = name.lastIndexOf(URI_PATH_SEPARATOR, end);
+ if (separator != URI_PATH_SEPARATOR) {
// Search for platform-specific character only if the object is neither a URL or a URI.
fromIndex = Math.max(fromIndex, name.lastIndexOf(separator, end));
}
@@ -165,7 +184,7 @@ public final class IOUtilities extends Static {
end++; // Make exclusive.
}
if (extension) {
- fromIndex = CharSequences.lastIndexOf(name, '.', fromIndex, end) + 1;
+ fromIndex = CharSequences.lastIndexOf(name, EXTENSION_SEPARATOR, fromIndex, end) + 1;
if (fromIndex <= 1) {
// If the dot is the first character, do not consider as a filename extension.
return "";
@@ -221,16 +240,19 @@ public final class IOUtilities extends Static {
}
/**
- * Converts the given URI to a new URI with the same path except for the file extension,
- * which is replaced by the given extension. This method is used for opening auxiliary files
- * such as {@code "*.prj"} and {@code "*.tfw"} files that come with e.g. TIFF files.
+ * Converts the given URI to a new URI with the same path except for the filename or extension.
+ * This method is used for opening auxiliary files such as {@code "*.prj"} and {@code "*.tfw"}
+ * files that come with e.g. TIFF files.
*
- * @param location the URI to convert to a URL with a different extension, or {@code null}.
- * @param extension the file extension (without {@code '.'}) of the auxiliary file.
+ * @param location the URI to convert to a URL with a different extension, or {@code null}.
+ * @param replacement the filename (including extension) or file extension (without {@code '.'}) of the auxiliary file.
+ * @param extension whether the replacement is the filename or only the file extension.
* @return URI for the auxiliary file with the given extension, or {@code null} if none.
* @throws URISyntaxException if the URI cannot be reconstructed.
*/
- public static URI toAuxiliaryURI(final URI location, final String extension) throws URISyntaxException {
+ public static URI toAuxiliaryURI(final URI location, final String replacement, final boolean extension)
+ throws URISyntaxException
+ {
if (location == null || !location.isAbsolute() || location.isOpaque()) {
return null;
}
@@ -242,11 +264,12 @@ public final class IOUtilities extends Static {
s = path.length();
}
}
- s = path.lastIndexOf('.', s);
- if (s >= 0) {
- path = path.substring(0, s+1) + extension;
+ final int base = path.lastIndexOf(URI_PATH_SEPARATOR, s);
+ s = extension ? path.lastIndexOf(EXTENSION_SEPARATOR, s) : base;
+ if (!extension || s > base) {
+ path = path.substring(0, s+1) + replacement;
} else {
- path = path + '.' + extension;
+ path = path + EXTENSION_SEPARATOR + replacement;
}
return new URI(location.getScheme(), // http, https, file or jar.
location.getRawAuthority(), // Host name or literal IP address.
@@ -264,10 +287,10 @@ public final class IOUtilities extends Static {
public static String filenameWithoutExtension(String path) {
if (path != null) {
int s = path.lastIndexOf(File.separatorChar);
- if (s < 0 && File.separatorChar != '/') {
- s = path.lastIndexOf('/');
+ if (s < 0 && File.separatorChar != URI_PATH_SEPARATOR) {
+ s = path.lastIndexOf(URI_PATH_SEPARATOR);
}
- int e = path.lastIndexOf('.');
+ int e = path.lastIndexOf(EXTENSION_SEPARATOR);
if (e <= ++s) {
e = path.length();
}
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataOptionKey.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataOptionKey.java
index 44d069e4e1..116ff542cf 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataOptionKey.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataOptionKey.java
@@ -50,6 +50,11 @@ public final class DataOptionKey<T> extends OptionKey<T> {
* If the file exists, it is parsed and its content is merged or appended after the
* metadata read by the storage. If the file does not exist, then it is ignored.
*
+ * <h4>Wildcard</h4>
+ * It the {@code '*'} character is present in the path, then it is replaced by the name of the
+ * main file without its extension. For example if the main file is {@code "city-center.tiff"},
+ * then {@code "*.xml"} will become {@code "city-center.xml"}.
+ *
* @since 1.5
*/
public static final OptionKey<Path> METADATA_PATH =
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/PRJDataStore.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/PRJDataStore.java
index 5bd4edcebd..3249a57314 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/PRJDataStore.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/PRJDataStore.java
@@ -204,7 +204,7 @@ public abstract class PRJDataStore extends URIDataStore {
stream = Files.newInputStream(path);
source = path;
} else try {
- final URI uri = IOUtilities.toAuxiliaryURI(location, extension);
+ final URI uri = IOUtilities.toAuxiliaryURI(location, extension, true);
if (uri == null) {
return null;
}
@@ -437,8 +437,8 @@ public abstract class PRJDataStore extends URIDataStore {
*/
private static String getBaseFilename(final Path path) {
final String base = path.getFileName().toString();
- final int s = base.lastIndexOf('.');
- return (s >= 0) ? base.substring(0, s+1) : base + '.';
+ final int s = base.lastIndexOf(IOUtilities.EXTENSION_SEPARATOR);
+ return (s >= 0) ? base.substring(0, s+1) : base + IOUtilities.EXTENSION_SEPARATOR;
}
/**
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/URIDataStore.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/URIDataStore.java
index df7f1141ae..856b34f6f8 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/URIDataStore.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/base/URIDataStore.java
@@ -16,6 +16,7 @@
*/
package org.apache.sis.storage.base;
+import java.util.Arrays;
import java.util.Optional;
import java.io.DataInput;
import java.io.DataOutput;
@@ -28,6 +29,7 @@ import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.nio.charset.Charset;
+import java.net.URISyntaxException;
import jakarta.xml.bind.JAXBException;
import org.opengis.util.GenericName;
import org.opengis.parameter.ParameterValueGroup;
@@ -75,6 +77,8 @@ public abstract class URIDataStore extends DataStore implements StoreResource, R
/**
* Path to an auxiliary file providing metadata as path, or {@code null} if none or not applicable.
* Unless absolute, this path is relative to the {@link #location} or to the {@link #locationAsPath}.
+ * The path may contain the {@code '*'} character, which need to be replaced by the main file name
+ * without suffix at reading time.
*/
private final Path metadataPath;
@@ -134,31 +138,100 @@ public abstract class URIDataStore extends DataStore implements StoreResource, R
}
/**
- * Returns the path to the auxiliary metadata file, or {@code null} if none.
- * This is a path build from the "metadata path" option if present.
+ * {@return the path to the auxiliary metadata file, or {@code null} if none}.
+ * This is a path built from the {@link DataOptionKey#METADATA_PATH} value if present.
+ * Note that the metadata may be unavailable as a {@link Path} but available as an {@link URI}.
*/
- private Path getMetadataPath() {
- if (metadataPath != null && locationAsPath != null) {
- Path path = locationAsPath.getParent();
+ private Path getMetadataPath() throws IOException {
+ Path path = replaceWildcard(metadataPath);
+ if (path != null) {
+ Path parent = locationAsPath;
+ if (parent != null) {
+ parent = parent.getParent();
+ if (parent != null) {
+ path = parent.resolve(path);
+ }
+ }
+ if (Files.isSameFile(path, locationAsPath)) {
+ return null;
+ }
+ }
+ return path;
+ }
+
+ /**
+ * {@return the URI to the auxiliary metadata file, or {@code null} if none}.
+ * This is a path built from the {@link DataOptionKey#METADATA_PATH} value if present.
+ * Note that the metadata may be unavailable as an {@link URI} but available as a {@link Path}.
+ */
+ private URI getMetadataURI() throws URISyntaxException {
+ URI uri = location;
+ if (uri != null) {
+ final Path path = replaceWildcard(metadataPath);
if (path != null) {
- return path.resolve(metadataPath);
+ uri = IOUtilities.toAuxiliaryURI(uri, path.toString(), false);
+ if (!uri.equals(location)) {
+ return uri;
+ }
}
}
return null;
}
+ /**
+ * Returns the given path with the wildcard character replaced by the name of the main file.
+ *
+ * @param path path in which to replace wildcard character, or {@code null}.
+ * @return path with wildcard character replaced, or {@code path} if no replacement was done,
+ * or {@code null} if a replacement was required but couldn't be done.
+ */
+ private Path replaceWildcard(Path path) {
+ if (path != null) {
+ boolean changed = false;
+ String filename = null; // Determined when first needed.
+ final var names = new String[path.getNameCount()];
+ int count = 0;
+ for (final Path p : path) {
+ String name = p.toString();
+ if (name.indexOf('*') >= 0) {
+ if (filename == null) {
+ filename = IOUtilities.filename(locationAsPath != null ? locationAsPath : location);
+ if (filename == null) {
+ return null;
+ }
+ final int s = filename.lastIndexOf(IOUtilities.EXTENSION_SEPARATOR);
+ if (s >= 0) {
+ filename = filename.substring(0, s);
+ }
+ }
+ name = name.replace("*", filename);
+ changed = true;
+ }
+ names[count++] = name;
+ }
+ if (changed) {
+ path = path.getFileSystem().getPath(names[0], Arrays.copyOfRange(names, 1, count));
+ }
+ }
+ return path;
+ }
+
/**
* Returns the main and metadata locations as {@code Path} components, or an empty array if none.
* The default implementation returns the storage specified at construction time converted to a {@link Path}
* if such conversion was possible, or {@code null} otherwise.
*
* @return the URI as a path, or an empty array if unknown.
- * @throws DataStoreException if the URI cannot be converted to a {@link Path}.
+ * @throws DataStoreException if an error occurred while getting the paths.
*/
@Override
public Path[] getComponentFiles() throws DataStoreException {
- final var paths = new Path[] {locationAsPath, getMetadataPath()};
- return ArraysExt.resize(paths, ArraysExt.removeDuplicated(paths, ArraysExt.removeNulls(paths)));
+ try {
+ final var paths = new Path[] {locationAsPath, getMetadataPath()};
+ return ArraysExt.resize(paths, ArraysExt.removeDuplicated(paths, ArraysExt.removeNulls(paths)));
+ } catch (IOException e) {
+ throw new DataStoreException(e);
+ }
}
/**
@@ -404,12 +477,28 @@ public abstract class URIDataStore extends DataStore implements StoreResource, R
* @param builder where to merge the metadata.
*/
protected final void mergeAuxiliaryMetadata(final MetadataBuilder builder) {
- final Path path = getMetadataPath();
- if (path != null) try {
- builder.mergeMetadata(XML.unmarshal(path), getLocale());
+ Object metadata = null;
+ Exception error = null;
+ try {
+ final Path path = getMetadataPath();
+ if (path != null) {
+ metadata = XML.unmarshal(path);
+ } else {
+ final URI uri = getMetadataURI();
+ if (uri != null) {
+ metadata = XML.unmarshal(uri.toURL());
+ }
+ }
+ } catch (URISyntaxException | IOException e) {
+ error = e;
} catch (JAXBException e) {
final Throwable cause = e.getCause();
- listeners.warning(cannotReadAuxiliaryFile("xml"), (cause instanceof IOException) ? (Exception) cause : e);
+ error = (cause instanceof IOException) ? (Exception) cause : e;
+ }
+ if (metadata != null) {
+ builder.mergeMetadata(metadata, getLocale());
+ } else if (error != null) {
+ listeners.warning(cannotReadAuxiliaryFile("xml"), error);
}
}
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/esri/RawRasterStoreProvider.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/esri/RawRasterStoreProvider.java
index f5b2a05a14..f46423d2dc 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/esri/RawRasterStoreProvider.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/esri/RawRasterStoreProvider.java
@@ -28,6 +28,7 @@ import org.apache.sis.storage.DataStore;
import org.apache.sis.storage.base.Capability;
import org.apache.sis.storage.base.StoreMetadata;
import org.apache.sis.storage.base.PRJDataStore;
+import org.apache.sis.io.stream.IOUtilities;
/**
@@ -92,8 +93,8 @@ public final class RawRasterStoreProvider extends PRJDataStore.Provider {
Path path = connector.getStorageAs(Path.class);
if (path != null) {
String filename = path.getFileName().toString();
- final int s = filename.lastIndexOf('.');
- filename = ((s >= 0) ? filename.substring(0, s+1) : filename.concat(".")).concat(HDR);
+ final int s = filename.lastIndexOf(IOUtilities.EXTENSION_SEPARATOR);
+ filename = ((s >= 0) ? filename.substring(0, s+1) : filename + IOUtilities.EXTENSION_SEPARATOR) + HDR;
path = path.resolveSibling(filename);
if (Files.isRegularFile(path)) {
// TODO: maybe we should do more tests here (open the file?)
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/folder/WritableStore.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/folder/WritableStore.java
index 1f9f65e780..d89e0b6641 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/folder/WritableStore.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/folder/WritableStore.java
@@ -37,6 +37,7 @@ import org.apache.sis.storage.ReadOnlyStorageException;
import org.apache.sis.storage.base.StoreUtilities;
import org.apache.sis.storage.base.ResourceOnFileSystem;
import org.apache.sis.storage.internal.Resources;
+import org.apache.sis.io.stream.IOUtilities;
import org.apache.sis.util.ArgumentChecks;
@@ -99,7 +100,7 @@ final class WritableStore extends Store implements WritableAggregate {
String filename = identifier.toString();
final String[] suffixes = StoreUtilities.getFileSuffixes(componentProvider.getClass());
if (suffixes.length != 0) {
- filename += '.' + suffixes[0];
+ filename += IOUtilities.EXTENSION_SEPARATOR + suffixes[0];
}
/*
* Create new store/resource for write access, provided that no store already exist for the path.
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/image/WorldFileStore.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/image/WorldFileStore.java
index 5293f2c541..d6014cee80 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/image/WorldFileStore.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/image/WorldFileStore.java
@@ -38,6 +38,7 @@ import org.opengis.metadata.maintenance.ScopeCode;
import org.opengis.referencing.datum.PixelInCell;
import org.apache.sis.coverage.grid.GridExtent;
import org.apache.sis.coverage.grid.GridGeometry;
+import org.apache.sis.io.stream.IOUtilities;
import org.apache.sis.storage.Resource;
import org.apache.sis.storage.Aggregate;
import org.apache.sis.storage.StorageConnector;
@@ -50,10 +51,10 @@ import org.apache.sis.storage.UnsupportedStorageException;
import org.apache.sis.storage.base.PRJDataStore;
import org.apache.sis.storage.base.MetadataBuilder;
import org.apache.sis.referencing.util.j2d.AffineTransform2D;
+import org.apache.sis.metadata.sql.MetadataStoreException;
import org.apache.sis.util.CharSequences;
import org.apache.sis.util.ArraysExt;
import org.apache.sis.util.internal.ListOfUnknownSize;
-import org.apache.sis.metadata.sql.MetadataStoreException;
import org.apache.sis.util.collection.BackingStoreException;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.setup.OptionKey;
@@ -397,7 +398,7 @@ loop: for (int convention=0;; convention++) {
throw new EOFException(errors().getString(Errors.Keys.UnexpectedEndOfFile_1, filename));
}
if (filename != null) {
- final int s = filename.lastIndexOf('.');
+ final int s = filename.lastIndexOf(IOUtilities.EXTENSION_SEPARATOR);
if (s >= 0) {
suffixWLD = filename.substring(s+1);
}
diff --git a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/io/stream/IOUtilitiesTest.java b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/io/stream/IOUtilitiesTest.java
index 44b759565a..e1ebfc728b 100644
--- a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/io/stream/IOUtilitiesTest.java
+++ b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/io/stream/IOUtilitiesTest.java
@@ -26,7 +26,7 @@ import org.apache.sis.util.CharSequences;
// Test dependencies
import org.junit.Test;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
import org.apache.sis.test.DependsOnMethod;
import org.apache.sis.test.TestCase;
@@ -122,15 +122,16 @@ public final class IOUtilitiesTest extends TestCase {
}
/**
- * Tests {@link IOUtilities#toAuxiliaryURI(URI, int)}.
+ * Tests {@link IOUtilities#toAuxiliaryURI(URI, String, boolean)}.
*
* @throws URISyntaxException if a URI cannot be parsed.
* @throws MalformedURLException if a URL cannot be parsed.
*/
@Test
public void testAuxiliaryURI() throws URISyntaxException, MalformedURLException {
- assertEquals(new URI("http://localhost/directory/image.tfw"),
- IOUtilities.toAuxiliaryURI(new URI("http://localhost/directory/image.tiff"), "tfw"));
+ final var src = new URI("http://localhost/directory/image.tiff?request=ignore.me");
+ assertEquals(new URI("http://localhost/directory/image.tfw"), IOUtilities.toAuxiliaryURI(src, "tfw", true));
+ assertEquals(new URI("http://localhost/directory/metadata.xml"), IOUtilities.toAuxiliaryURI(src, "metadata.xml", false));
}
/**
@@ -210,11 +211,11 @@ public final class IOUtilitiesTest extends TestCase {
* @throws IOException if a URL cannot be parsed.
*/
private void testToFile(final String encoding, final String plus) throws IOException {
- assertEquals("Unix absolute path.", new File("/Users/name/Map.png"),
+ assertEquals(new File("/Users/name/Map.png"), // Unix absolute path.
IOUtilities.toFile(IOUtilities.toURL("file:/Users/name/Map.png", encoding)));
- assertEquals("Path with space.", new File("/Users/name/Map with spaces.png"),
+ assertEquals(new File("/Users/name/Map with spaces.png"), // Path with space.
IOUtilities.toFile(IOUtilities.toURL("file:/Users/name/Map with spaces.png", encoding)));
- assertEquals("Path with + sign.", new File("/Users/name/++t--++est.shp"),
+ assertEquals(new File("/Users/name/++t--++est.shp"), // Path with + sign.
IOUtilities.toFile(IOUtilities.toURL(
CharSequences.replace("file:/Users/name/++t--++est.shp", "+", plus).toString(), encoding)));
}
(sis) 04/04: Add a "--metadata" option to the command line for making possible to specify the auxiliary metadata.
Posted by de...@apache.org.
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 558c65499f8996695ab7d8e62a3da459432f54fb
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Dec 9 16:40:30 2023 +0100
Add a "--metadata" option to the command line for making possible to specify the auxiliary metadata.
---
.../main/org/apache/sis/console/Command.java | 11 ++--
.../main/org/apache/sis/console/CommandRunner.java | 61 +++++++++++++++++++++-
.../apache/sis/console/FormattedOutputCommand.java | 15 +++---
.../main/org/apache/sis/console/InfoCommand.java | 2 +-
.../org/apache/sis/console/MetadataCommand.java | 5 +-
.../main/org/apache/sis/console/Option.java | 5 ++
.../main/org/apache/sis/console/Options.properties | 7 +--
.../org/apache/sis/console/Options_fr.properties | 7 +--
.../main/org/apache/sis/console/SIS.java | 26 +++++++++
.../org/apache/sis/console/TransformCommand.java | 4 +-
.../org/apache/sis/console/TranslateCommand.java | 4 +-
.../main/org/apache/sis/setup/OptionKey.java | 2 +-
12 files changed, 121 insertions(+), 28 deletions(-)
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Command.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Command.java
index e6062a0e9b..530ad9a788 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Command.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Command.java
@@ -41,10 +41,12 @@ import org.apache.sis.util.logging.MonolineFormatter;
* <tr><td>{@code help} </td><td>Show a help overview.</td></tr>
* <tr><td>{@code about} </td><td>Show information about Apache SIS and system configuration.</td></tr>
* <tr><td>{@code mime-type} </td><td>Show MIME type for the given file.</td></tr>
+ * <tr><td>{@code identifier} </td><td>Show identifiers for metadata and referencing systems in the given file.</td></tr>
* <tr><td>{@code metadata} </td><td>Show metadata information for the given file.</td></tr>
* <tr><td>{@code crs} </td><td>Show Coordinate Reference System information for the given file or code.</td></tr>
- * <tr><td>{@code identifier} </td><td>Show identifiers for metadata and referencing systems in the given file.</td></tr>
+ * <tr><td>{@code info} </td><td>Show resource-specific information (e.g., grid geometry).</td></tr>
* <tr><td>{@code transform} </td><td>Convert or transform coordinates from given source CRS to target CRS.</td></tr>
+ * <tr><td>{@code translate} </td><td>Rewrite a data file in another format.</td></tr>
* </table></blockquote>
*
* Each command can accepts some of the following options:
@@ -53,10 +55,11 @@ import org.apache.sis.util.logging.MonolineFormatter;
* <caption>Supported command-line options</caption>
* <tr><td>{@code --sourceCRS} </td><td>The Coordinate Reference System of input data.</td></tr>
* <tr><td>{@code --targetCRS} </td><td>The Coordinate Reference System of output data.</td></tr>
+ * <tr><td>{@code --metadata} </td><td>Relative path to an auxiliary metadata file.</td></tr>
* <tr><td>{@code --format} </td><td>The output format: {@code xml}, {@code wkt}, {@code wkt1} or {@code text}.</td></tr>
- * <tr><td>{@code --locale} </td><td>The locale to use for the command output.</td></tr>
- * <tr><td>{@code --timezone} </td><td>The timezone for the dates to be formatted.</td></tr>
- * <tr><td>{@code --encoding} </td><td>The encoding to use for the command outputs and some inputs.</td></tr>
+ * <tr><td>{@code --locale} </td><td>The locale to use for the console output.</td></tr>
+ * <tr><td>{@code --timezone} </td><td>The timezone for the dates printed to the console output.</td></tr>
+ * <tr><td>{@code --encoding} </td><td>The encoding to use for some text inputs and for console output.</td></tr>
* <tr><td>{@code --colors} </td><td>Whether colorized output shall be enabled.</td></tr>
* <tr><td>{@code --brief} </td><td>Whether the output should contain only brief information.</td></tr>
* <tr><td>{@code --verbose} </td><td>Whether the output should contain more detailed information.</td></tr>
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/CommandRunner.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/CommandRunner.java
index 581834dfa6..8de71c01f9 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/CommandRunner.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/CommandRunner.java
@@ -29,12 +29,16 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
+import java.nio.file.Path;
+import java.nio.file.InvalidPathException;
import org.apache.sis.util.Locales;
import org.apache.sis.util.Exceptions;
import org.apache.sis.util.Workaround;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.internal.X364;
import org.apache.sis.pending.jdk.JDK17;
+import org.apache.sis.storage.DataOptionKey;
+import org.apache.sis.storage.StorageConnector;
/**
@@ -302,8 +306,29 @@ abstract class CommandRunner {
if (value instanceof CharSequence) {
return value.toString();
}
- final String name = key.label();
- throw new InvalidOptionException(Errors.format(Errors.Keys.IllegalOptionValue_2, name, value), name);
+ throw invalidOption(key, value, null);
+ }
+
+ /**
+ * Returns the value of the specified option as a path.
+ *
+ * @param key the option for which to get a value.
+ * @return the requested option, or {@code null} if not present.
+ * @throws InvalidOptionException if the value is not convertible to a path.
+ */
+ final Path getOptionAsPath(final Option key) throws InvalidOptionException {
+ final Object value = options.get(key);
+ if (value == null) return null;
+ if (value instanceof Path) {
+ return (Path) value;
+ }
+ Throwable cause = null;
+ if (value instanceof CharSequence) try {
+ return Path.of(value.toString());
+ } catch (InvalidPathException e) {
+ cause = e;
+ }
+ throw invalidOption(key, value, cause);
}
/**
@@ -376,6 +401,38 @@ abstract class CommandRunner {
return files.isEmpty() && System.console() == null;
}
+ /**
+ * Returns a storage connector for the specified input.
+ * This method should be invoked only for the input, not for the output, because it uses input-specific options.
+ * Conversely, this storage connector does <strong>not</strong> include the options intended for console output
+ * such as {@linkplain #locale}, {@link #timezone} and {@linkplain #encoding}.
+ *
+ * @param input the storage input.
+ * @return storage connector for the specified input.
+ * @throws InvalidOptionException if an option has an invalid value.
+ */
+ final StorageConnector inputConnector(final Object input) throws InvalidOptionException {
+ final var connector = new StorageConnector(input);
+ final Path p = getOptionAsPath(Option.METADATA);
+ if (p != null) {
+ connector.setOption(DataOptionKey.METADATA_PATH, p);
+ }
+ return connector;
+ }
+
+ /**
+ * Returns the exception to throw for an invalid option.
+ *
+ * @param key the requested option.
+ * @param value the value associated to the specified option.
+ * @param cause cause of the invalidity, or {@code null} if none.
+ * @return the exception to throw.
+ */
+ private static InvalidOptionException invalidOption(Option key, Object value, Throwable cause) {
+ final String name = key.label();
+ return new InvalidOptionException(Errors.format(Errors.Keys.IllegalOptionValue_2, name, value), cause, name);
+ }
+
/**
* Prints the <q>Cannot open …</q> error message followed by the message in the given exception.
*
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/FormattedOutputCommand.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/FormattedOutputCommand.java
index f268dbbff2..e65fd49cce 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/FormattedOutputCommand.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/FormattedOutputCommand.java
@@ -178,14 +178,14 @@ abstract class FormattedOutputCommand extends CommandRunner {
* The input format is detected automatically (this is <strong>not</strong> {@link #outputFormat}).
*
* @return a {@link Metadata} or {@link CoordinateReferenceSystem} instance, or {@code null} if none.
+ * @throws InvalidOptionException if an option has an invalid value.
* @throws DataStoreException if an error occurred while reading the file.
* @throws FactoryException if an error occurred while looking for a CRS identifier.
*/
- final Object readMetadataOrCRS() throws DataStoreException, FactoryException {
+ final Object readMetadataOrCRS() throws InvalidOptionException, DataStoreException, FactoryException {
+ final Object input;
if (useStandardInput()) {
- try (DataStore store = DataStores.open(System.in)) {
- return store.getMetadata();
- }
+ input = System.in;
} else if (hasUnexpectedFileCount(1, 1)) {
hasUnexpectedFileCount = true;
return null;
@@ -197,9 +197,10 @@ abstract class FormattedOutputCommand extends CommandRunner {
return CRS.forCode(c);
}
}
- try (DataStore store = DataStores.open(file)) {
- return store.getMetadata();
- }
+ input = file;
+ }
+ try (DataStore store = DataStores.open(inputConnector(input))) {
+ return store.getMetadata();
}
}
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/InfoCommand.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/InfoCommand.java
index b09ab42c2f..286a7e8bc2 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/InfoCommand.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/InfoCommand.java
@@ -48,7 +48,7 @@ final class InfoCommand extends FormattedOutputCommand {
* Returns valid options for the {@code "metadata"} command.
*/
static EnumSet<Option> options() {
- return EnumSet.of(Option.LOCALE, Option.TIMEZONE, Option.COLORS, Option.VERBOSE, Option.HELP, Option.DEBUG);
+ return EnumSet.of(Option.METADATA, Option.LOCALE, Option.TIMEZONE, Option.COLORS, Option.VERBOSE, Option.HELP, Option.DEBUG);
}
/**
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/MetadataCommand.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/MetadataCommand.java
index b12a6b4010..365cf47610 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/MetadataCommand.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/MetadataCommand.java
@@ -32,7 +32,8 @@ import org.apache.sis.util.collection.TreeTable;
* Some available options are:
*
* <ul>
- * <li>{@code --format}: the output format (text, XML or GPX).</li>
+ * <li>{@code --metadata}: relative path to auxiliary metadata to combine with the main metadata.</li>
+ * <li>{@code --format}: the output format (text, XML or GPX).</li>
* </ul>
*
* @author Martin Desruisseaux (Geomatys)
@@ -42,7 +43,7 @@ final class MetadataCommand extends FormattedOutputCommand {
* Returns valid options for the {@code "metadata"} command.
*/
static EnumSet<Option> options() {
- return EnumSet.of(Option.FORMAT, Option.LOCALE, Option.TIMEZONE,
+ return EnumSet.of(Option.METADATA, Option.FORMAT, Option.LOCALE, Option.TIMEZONE,
Option.ENCODING, Option.COLORS, Option.VERBOSE, Option.HELP, Option.DEBUG);
}
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Option.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Option.java
index 23e6f2f1f3..8fdead8c96 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Option.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Option.java
@@ -37,6 +37,11 @@ enum Option {
*/
TARGET_CRS(true),
+ /**
+ * Relative path to an auxiliary metadata file.
+ */
+ METADATA(true),
+
/**
* The file to write.
*/
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Options.properties b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Options.properties
index e8c984cb5d..ae275090d8 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Options.properties
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Options.properties
@@ -2,11 +2,12 @@
# and to You under the Apache License, Version 2.0.
sourceCRS=The Coordinate Reference System of input data.
targetCRS=The Coordinate Reference System of output data.
+metadata=Relative path to an auxiliary metadata file.
output=The output file.
format=The output format. Examples: xml, wkt, wkt1 or text.
-locale=The locale to use for the command output.
-timezone=The timezone for the dates to be formatted.
-encoding=The encoding to use for the command outputs and some inputs.
+locale=The locale to use for the console output.
+timezone=The timezone for the dates printed to the console output.
+encoding=The encoding to use for some text inputs and for console output.
colors=Whether colorized output shall be enabled.
brief=Reduce the output to only brief information.
verbose=Request the output to contain more detailed information.
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Options_fr.properties b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Options_fr.properties
index 1828e7be7d..6e7bf7227f 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Options_fr.properties
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/Options_fr.properties
@@ -2,11 +2,12 @@
# and to You under the Apache License, Version 2.0.
sourceCRS=Le syst\u00e8me de r\u00e9f\u00e9rence des coordonn\u00e9es source.
targetCRS=Le syst\u00e8me de r\u00e9f\u00e9rence des coordonn\u00e9es destination.
+metadata=Chemin relatif vers un fichier auxiliaire de m\u00e9ta-donn\u00e9es.
output=Le fichier de sortie.
format=Le format de sortie. Exemples: xml, wkt, wkt1 ou text.
-locale=Les param\u00e8tres r\u00e9gionaux \u00e0 utiliser pour la sortie de la commande.
-timezone=Le fuseau horaire des dates \u00e0 \u00e9crire.
-encoding=L\u2019encodage des caract\u00e8res \u00e0 utiliser pour la sortie de la commande et certaines entr\u00e9es.
+locale=Les param\u00e8tres r\u00e9gionaux \u00e0 utiliser pour la sortie vers la console.
+timezone=Le fuseau horaire des dates \u00e0 \u00e9crire dans la sortie console.
+encoding=L\u2019encodage des caract\u00e8res \u00e0 utiliser pour certaines entr\u00e9es textuels et la sortie vers la console.
colors=Indique si l\u2019affichage peut \u00eatre en couleurs.
brief=Indique que la sortie de la commande ne doit contenir que de br\u00e8ves informations.
verbose=Indique que la sortie de la commande doit contenir des informations plus d\u00e9taill\u00e9es.
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/SIS.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/SIS.java
index 2f78e06b7e..b6f6d15934 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/SIS.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/SIS.java
@@ -432,6 +432,19 @@ public final class SIS extends Static {
/** Creates the instance for metadata or CRS. */
Metadata(String command) {super(command);}
+ /**
+ * Sets the path to auxiliary metadata, relative to the main file.
+ * The {@code '*'} character stands for the name of the main file.
+ * For example if the main file is {@code "city-center.tiff"},
+ * then {@code "*.xml"} stands for {@code "city-center.xml"}.
+ *
+ * @param value relative path to auxiliary metadata.
+ * @return a new builder or {@code this}, for method call chaining.
+ */
+ public Metadata metadata(String value) {
+ return set(Option.METADATA, value);
+ }
+
/**
* Sets the output format.
*
@@ -699,6 +712,19 @@ public final class SIS extends Static {
/** Creates the unique instance. */
Translate() {super("translate");}
+ /**
+ * Sets the path to auxiliary metadata, relative to the main file.
+ * The {@code '*'} character stands for the name of the main file.
+ * For example if the main file is {@code "city-center.tiff"},
+ * then {@code "*.xml"} stands for {@code "city-center.xml"}.
+ *
+ * @param value relative path to auxiliary metadata.
+ * @return a new builder or {@code this}, for method call chaining.
+ */
+ public Translate metadata(String value) {
+ return set(Option.METADATA, value);
+ }
+
/**
* Sets the destination file.
*
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/TransformCommand.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/TransformCommand.java
index ea45f0ee88..698ee27bfa 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/TransformCommand.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/TransformCommand.java
@@ -77,7 +77,6 @@ import org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox;
import org.apache.sis.util.resources.Vocabulary;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.logging.Logging;
-import org.apache.sis.setup.OptionKey;
// Specific to the geoapi-3.1 and geoapi-4.0 branches:
import org.opengis.referencing.ObjectDomain;
@@ -219,8 +218,7 @@ final class TransformCommand extends FormattedOutputCommand {
}
} else {
for (final Object file : files) {
- final var c = new StorageConnector(file);
- c.setOption(OptionKey.ENCODING, encoding);
+ final StorageConnector c = inputConnector(file);
try (BufferedReader in = IOUtilities.toBuffered(c.commit(Reader.class, "transform"))) {
points = readCoordinates(in, file);
}
diff --git a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/TranslateCommand.java b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/TranslateCommand.java
index 38864f912e..142fe59bbe 100644
--- a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/TranslateCommand.java
+++ b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/TranslateCommand.java
@@ -49,7 +49,7 @@ final class TranslateCommand extends CommandRunner {
* @throws InvalidOptionException if an illegal option has been provided, or the option has an illegal value.
*/
TranslateCommand(final int commandIndex, final Object[] arguments) throws InvalidOptionException {
- super(commandIndex, arguments, EnumSet.of(Option.OUTPUT, Option.FORMAT, Option.HELP, Option.DEBUG));
+ super(commandIndex, arguments, EnumSet.of(Option.METADATA, Option.OUTPUT, Option.FORMAT, Option.HELP, Option.DEBUG));
}
/**
@@ -72,7 +72,7 @@ final class TranslateCommand extends CommandRunner {
});
try (DataStore target = DataStores.openWritable(connector, format)) {
for (final Object file : files) {
- try (DataStore source = DataStores.open(file)) {
+ try (DataStore source = DataStores.open(inputConnector(file))) {
if (target instanceof WritableAggregate) {
((WritableAggregate) target).add(source);
} else if (target instanceof WritableGridCoverageResource) {
diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/OptionKey.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/OptionKey.java
index 19b1f40705..03377c606d 100644
--- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/OptionKey.java
+++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/OptionKey.java
@@ -108,7 +108,7 @@ public class OptionKey<T> implements Serializable {
/**
* The character encoding of document content.
- * This option can be used when the file to read does not describe itself its encoding.
+ * This option can be used when the file to read or write does not describe itself its encoding.
* For example, this option can be used when reading plain text files, but is ignored when
* reading XML files having a {@code <?xml version="1.0" encoding="…"?>} declaration.
*
(sis) 01/04: Deprecate `OptionKey.BYTE_BUFFER` because it forces unconditional allocation of byte buffer.
Posted by de...@apache.org.
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 11025a742e68f3d578532ef90851d25ee77ebc3d
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Dec 9 11:48:29 2023 +0100
Deprecate `OptionKey.BYTE_BUFFER` because it forces unconditional allocation of byte buffer.
---
.../main/org/apache/sis/storage/DataOptionKey.java | 24 +++++++++++
.../org/apache/sis/storage/StorageConnector.java | 6 ++-
.../org/apache/sis/storage/DataOptionKeyTest.java | 46 ++++++++++++++++++++++
.../main/org/apache/sis/setup/OptionKey.java | 12 ++++--
.../test/org/apache/sis/setup/OptionKeyTest.java | 4 +-
5 files changed, 86 insertions(+), 6 deletions(-)
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataOptionKey.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataOptionKey.java
index 89ef371110..44d069e4e1 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataOptionKey.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataOptionKey.java
@@ -17,6 +17,10 @@
package org.apache.sis.storage;
import java.nio.file.Path;
+import java.io.ObjectStreamException;
+import static java.util.logging.Logger.getLogger;
+import org.apache.sis.util.logging.Logging;
+import org.apache.sis.system.Modules;
import org.apache.sis.setup.OptionKey;
import org.apache.sis.storage.event.StoreListeners;
import org.apache.sis.feature.FoliationRepresentation;
@@ -76,4 +80,24 @@ public final class DataOptionKey<T> extends OptionKey<T> {
private DataOptionKey(final String name, final Class<T> type) {
super(name, type);
}
+
+ /**
+ * Resolves this option key on deserialization. This method is invoked only
+ * for instance of the exact {@code DataOptionKey} class, not subclasses.
+ *
+ * @return the unique {@code DataOptionKey} instance.
+ * @throws ObjectStreamException required by specification but should never be thrown.
+ */
+ private Object readResolve() throws ObjectStreamException {
+ try {
+ return DataOptionKey.class.getField(getName()).get(null);
+ } catch (ReflectiveOperationException e) {
+ /*
+ * This may happen if we are deserializing a stream produced by a more recent SIS library
+ * than the one running in this JVM.
+ */
+ Logging.recoverableException(getLogger(Modules.STORAGE), DataOptionKey.class, "readResolve", e);
+ return this;
+ }
+ }
}
diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/StorageConnector.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/StorageConnector.java
index 708d431156..1c568e4694 100644
--- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/StorageConnector.java
+++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/StorageConnector.java
@@ -641,7 +641,8 @@ public class StorageConnector implements Serializable {
}
/**
- * Sets the option value for the given key. The default implementation recognizes the following options:
+ * Sets the option value for the given key.
+ * The {@code StorageConnector} class uses the following options, if present:
*
* <ul>
* <li>{@link OptionKey#ENCODING} for decoding characters in an input stream, if needed.</li>
@@ -650,6 +651,8 @@ public class StorageConnector implements Serializable {
* <li>{@link OptionKey#BYTE_BUFFER} for allowing users to control the byte buffer to be created.</li>
* </ul>
*
+ * In addition, each {@link DataStore} implementation may use more options.
+ *
* @param <T> the type of option value.
* @param key the option for which to set the value.
* @param value the new value for the given option, or {@code null} for removing the value.
@@ -1191,6 +1194,7 @@ public class StorageConnector implements Serializable {
* @return the byte buffer to use with {@link ChannelDataInput} or {@link ChannelDataOutput}.
*/
private ByteBuffer getChannelBuffer(final ChannelFactory factory) {
+ @SuppressWarnings("deprecated")
ByteBuffer buffer = getOption(OptionKey.BYTE_BUFFER); // User-supplied buffer.
if (buffer == null) {
if (factory.suggestDirectBuffer) {
diff --git a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/DataOptionKeyTest.java b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/DataOptionKeyTest.java
new file mode 100644
index 0000000000..7313a72bc5
--- /dev/null
+++ b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/DataOptionKeyTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.storage;
+
+// Test dependencies
+import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.*;
+import org.apache.sis.test.TestCase;
+import static org.apache.sis.test.Assertions.assertSerializedEquals;
+
+
+/**
+ * Tests {@link DataOptionKey}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ */
+public final class DataOptionKeyTest extends TestCase {
+ /**
+ * Creates a new test case.
+ */
+ public DataOptionKeyTest() {
+ }
+
+ /**
+ * Tests the serialization of constants.
+ * Those constants shall be resolved to their singleton instance on deserialization.
+ */
+ @Test
+ public void testSerialization() {
+ assertSame(DataOptionKey.METADATA_PATH, assertSerializedEquals(DataOptionKey.METADATA_PATH));
+ }
+}
diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/OptionKey.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/OptionKey.java
index 0fc616d6f8..19b1f40705 100644
--- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/OptionKey.java
+++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/OptionKey.java
@@ -154,8 +154,8 @@ public class OptionKey<T> implements Serializable {
public static final OptionKey<String> URL_ENCODING = new OptionKey<>("URL_ENCODING", String.class);
/**
- * Whether a storage object (e.g. a {@link org.apache.sis.storage.DataStore}) shall be opened in read,
- * write, append or other modes. The main options that can be provided are:
+ * Whether a storage object shall be opened in read, write, append or other modes.
+ * The main options that can be provided are:
*
* <table class="sis">
* <caption>Supported open options</caption>
@@ -179,7 +179,13 @@ public class OptionKey<T> implements Serializable {
* <li>The buffer does not contains any valuable data, as it will be {@linkplain ByteBuffer#clear() cleared}.</li>
* <li>The same buffer is not used concurrently by two different {@code DataStore} instances.</li>
* </ul>
+ *
+ * @deprecated This option forces unconditional allocation of byte buffer, even if the data store does not use it.
+ * It should be replaced by a {@link java.util.function.Supplier} or {@link java.util.function.Function}, but the
+ * exact form has not been determined yet.
*/
+ @Deprecated(since="1.5")
+ // TODO: provide replacement in DataOptionKey, because this option is specific to data stores.
public static final OptionKey<ByteBuffer> BYTE_BUFFER = new OptionKey<>("BYTE_BUFFER", ByteBuffer.class);
/**
@@ -361,7 +367,7 @@ public class OptionKey<T> implements Serializable {
* This may happen if we are deserializing a stream produced by a more recent SIS library
* than the one running in this JVM. This class should be robust to this situation, since
* we override the `equals` and `hashCode` methods. This option is likely to be ignored,
- * but options are expected to be optional...
+ * but options are expected to be optional.
*/
Logging.recoverableException(getLogger(Modules.UTILITIES), OptionKey.class, "readResolve", e);
return this;
diff --git a/endorsed/src/org.apache.sis.util/test/org/apache/sis/setup/OptionKeyTest.java b/endorsed/src/org.apache.sis.util/test/org/apache/sis/setup/OptionKeyTest.java
index ebf68f0924..eea89891a3 100644
--- a/endorsed/src/org.apache.sis.util/test/org/apache/sis/setup/OptionKeyTest.java
+++ b/endorsed/src/org.apache.sis.util/test/org/apache/sis/setup/OptionKeyTest.java
@@ -22,7 +22,7 @@ import static org.apache.sis.setup.OptionKey.*;
// Test dependencies
import org.junit.Test;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
import org.apache.sis.test.TestCase;
import static org.apache.sis.test.TestUtilities.getSingleton;
import static org.apache.sis.test.Assertions.assertSerializedEquals;
@@ -86,7 +86,7 @@ public final class OptionKeyTest extends TestCase {
@Test
public void testSerialization() {
assertSame(URL_ENCODING, assertSerializedEquals(URL_ENCODING));
- assertSame(BYTE_BUFFER, assertSerializedEquals(BYTE_BUFFER ));
+ assertSame(OPEN_OPTIONS, assertSerializedEquals(OPEN_OPTIONS));
}
/**