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 4c772ccb11cc8b5bda684e522bff10005050882c Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Mon Dec 25 18:58:48 2023 +0100 Move `DatumShiftGridLoader.toAbsolutePath(URI)` to `DataDirectory`. --- .../operation/provider/DatumShiftGridLoader.java | 34 ----------- .../provider/FranceGeocentricInterpolation.java | 7 ++- .../sis/referencing/operation/provider/NADCON.java | 5 +- .../sis/referencing/operation/provider/NTv2.java | 5 +- .../referencing/operation/provider/NTv2Test.java | 2 +- .../main/org/apache/sis/system/DataDirectory.java | 67 +++++++++++++++++++++- .../org/apache/sis/util/resources/Messages.java | 4 +- .../apache/sis/util/resources/Messages.properties | 2 +- .../sis/util/resources/Messages_fr.properties | 2 +- 9 files changed, 81 insertions(+), 47 deletions(-) diff --git a/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/DatumShiftGridLoader.java b/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/DatumShiftGridLoader.java index eb4b6c6530..d08196ded4 100644 --- a/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/DatumShiftGridLoader.java +++ b/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/DatumShiftGridLoader.java @@ -32,7 +32,6 @@ import java.nio.channels.ReadableByteChannel; import java.nio.channels.Channels; import org.opengis.util.FactoryException; import org.apache.sis.util.resources.Errors; -import org.apache.sis.util.resources.Messages; import org.apache.sis.util.logging.Logging; import org.apache.sis.system.DataDirectory; import org.apache.sis.referencing.internal.Resources; @@ -149,39 +148,6 @@ abstract class DatumShiftGridLoader { buffer.position(p); } - /** - * If the given URI is not absolute, tries to make it absolute - * with a path to the common directory of datum shift grid files. - * - * @param path the URI to make absolute. - * @return an absolute (if possible) URI to the data. - * @throws NoSuchFileException if the path cannot be made absolute. - * This exception is necessary for letting the caller know that the coordinate operation is - * probably valid but cannot be constructed because an optional configuration is missing. - * It is typically because the {@code SIS_DATA} environment variable has not been set. - */ - static URI toAbsolutePath(final URI path) throws NoSuchFileException { - if (path.isAbsolute()) { - return path; - } - String message; - if (path.isOpaque()) { - message = Errors.format(Errors.Keys.CanNotOpen_1, path); - } else { - final Path dir = DataDirectory.DATUM_CHANGES.getDirectory(); - if (dir != null) { - return dir.resolve(path.getPath()).toUri(); - } - final String env = DataDirectory.getenv(); - if (env == null) { - message = Messages.format(Messages.Keys.DataDirectoryNotSpecified_1, DataDirectory.ENV); - } else { - message = Messages.format(Messages.Keys.DataDirectoryNotReadable_2, DataDirectory.ENV, env); - } - } - throw new NoSuchFileException(path.toString(), null, message); - } - /** * Creates a channel for reading bytes from the file at the specified path. * This method tries to open using the file system before to open from the URL. diff --git a/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/FranceGeocentricInterpolation.java b/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/FranceGeocentricInterpolation.java index e45e3bf626..4b92f06fda 100644 --- a/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/FranceGeocentricInterpolation.java +++ b/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/FranceGeocentricInterpolation.java @@ -46,6 +46,8 @@ import org.opengis.referencing.operation.MathTransformFactory; import org.opengis.referencing.operation.NoninvertibleTransformException; import org.opengis.util.FactoryException; import org.apache.sis.referencing.CommonCRS; +import org.apache.sis.referencing.datum.DefaultEllipsoid; +import org.apache.sis.referencing.operation.transform.InterpolatedGeocentricTransform; import org.apache.sis.referencing.util.NilReferencingObject; import org.apache.sis.referencing.internal.Resources; import org.apache.sis.parameter.ParameterBuilder; @@ -54,8 +56,7 @@ import org.apache.sis.measure.Units; import org.apache.sis.util.CharSequences; import org.apache.sis.util.logging.Logging; import org.apache.sis.util.resources.Errors; -import org.apache.sis.referencing.datum.DefaultEllipsoid; -import org.apache.sis.referencing.operation.transform.InterpolatedGeocentricTransform; +import org.apache.sis.system.DataDirectory; import static org.apache.sis.util.internal.Constants.DIM; @@ -376,7 +377,7 @@ public final class FranceGeocentricInterpolation extends GeodeticOperation { static DatumShiftGridFile<Angle,Length> getOrLoad(final URI file, final double[] averages, final double scale) throws Exception { - final URI resolved = DatumShiftGridLoader.toAbsolutePath(file); + final URI resolved = DataDirectory.DATUM_CHANGES.toAbsolutePath(file); return DatumShiftGridFile.getOrLoad(resolved, null, new Loader(resolved, averages, scale)) .castTo(Angle.class, Length.class); } diff --git a/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/NADCON.java b/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/NADCON.java index 1039770b15..45305d1761 100644 --- a/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/NADCON.java +++ b/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/NADCON.java @@ -41,6 +41,7 @@ import org.apache.sis.parameter.Parameters; import org.apache.sis.util.CharSequences; import org.apache.sis.util.resources.Errors; import org.apache.sis.measure.Units; +import org.apache.sis.system.DataDirectory; /** @@ -159,8 +160,8 @@ public final class NADCON extends AbstractProvider { static DatumShiftGridFile<Angle,Angle> getOrLoad(final URI latitudeShifts, final URI longitudeShifts) throws Exception { - final URI rlat = Loader.toAbsolutePath(latitudeShifts); - final URI rlon = Loader.toAbsolutePath(longitudeShifts); + final URI rlat = DataDirectory.DATUM_CHANGES.toAbsolutePath(latitudeShifts); + final URI rlon = DataDirectory.DATUM_CHANGES.toAbsolutePath(longitudeShifts); return DatumShiftGridFile.getOrLoad(rlat, rlon, () -> { final Loader loader; URI file = latitudeShifts; diff --git a/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/NTv2.java b/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/NTv2.java index 1c4911634b..6daee0dff1 100644 --- a/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/NTv2.java +++ b/endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/operation/provider/NTv2.java @@ -45,13 +45,14 @@ import org.opengis.referencing.operation.Transformation; import org.opengis.referencing.operation.NoninvertibleTransformException; import org.apache.sis.referencing.util.Formulas; import org.apache.sis.referencing.internal.Resources; -import org.apache.sis.util.internal.Strings; import org.apache.sis.parameter.ParameterBuilder; import org.apache.sis.parameter.Parameters; import org.apache.sis.util.collection.Containers; +import org.apache.sis.util.internal.Strings; import org.apache.sis.util.resources.Errors; import org.apache.sis.util.resources.Messages; import org.apache.sis.measure.Units; +import org.apache.sis.system.DataDirectory; /** @@ -166,7 +167,7 @@ public final class NTv2 extends AbstractProvider { static DatumShiftGridFile<Angle,Angle> getOrLoad(final Class<? extends AbstractProvider> provider, final URI file, final int version) throws Exception { - final URI resolved = Loader.toAbsolutePath(file); + final URI resolved = DataDirectory.DATUM_CHANGES.toAbsolutePath(file); return DatumShiftGridFile.getOrLoad(resolved, null, () -> { final DatumShiftGridFile<?,?> grid; try (ReadableByteChannel in = DatumShiftGridLoader.newByteChannel(resolved)) { diff --git a/endorsed/src/org.apache.sis.referencing/test/org/apache/sis/referencing/operation/provider/NTv2Test.java b/endorsed/src/org.apache.sis.referencing/test/org/apache/sis/referencing/operation/provider/NTv2Test.java index 7a90ae91b3..37dbe253b9 100644 --- a/endorsed/src/org.apache.sis.referencing/test/org/apache/sis/referencing/operation/provider/NTv2Test.java +++ b/endorsed/src/org.apache.sis.referencing/test/org/apache/sis/referencing/operation/provider/NTv2Test.java @@ -185,7 +185,7 @@ public final class NTv2Test extends DatumShiftTestCase { public void testMultiGrids() throws Exception { assumeTrue(RUN_EXTENSIVE_TESTS); assumeTrue(DataDirectory.getenv() != null); - final URI file = DatumShiftGridLoader.toAbsolutePath(new URI(MULTIGRID_TEST_FILE)); + final URI file = DataDirectory.DATUM_CHANGES.toAbsolutePath(new URI(MULTIGRID_TEST_FILE)); assumeTrue(Files.exists(Path.of(file))); final DatumShiftGridFile<Angle,Angle> grid = NTv2.getOrLoad(NTv2.class, file, 2); assertInstanceOf("Should contain many grids.", DatumShiftGridGroup.class, grid); diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/system/DataDirectory.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/system/DataDirectory.java index 22d964a5df..81daa118a4 100644 --- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/system/DataDirectory.java +++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/system/DataDirectory.java @@ -17,13 +17,16 @@ package org.apache.sis.system; import java.io.IOException; +import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.InvalidPathException; +import java.nio.file.NoSuchFileException; import java.util.logging.Level; import java.util.logging.LogRecord; import org.apache.sis.util.logging.Logging; import org.apache.sis.util.internal.Strings; +import org.apache.sis.util.resources.Errors; import org.apache.sis.util.resources.Messages; @@ -94,6 +97,13 @@ public enum DataDirectory { */ private Path directory; + /** + * The directory as an URI, or {@code null} if none or not yet determined. + * + * @see #getDirectoryAsURI() + */ + private URI directoryAsURI; + /** * Prevents the log message about {@code SIS_DATA} environment variable not set. * This is used for the "About" command line action only. @@ -190,7 +200,7 @@ public enum DataDirectory { * * @return the sub-directory, or {@code null} if unspecified. */ - public synchronized Path getDirectory() { + public final synchronized Path getDirectory() { if (directory == null) { final Path root = getRootDirectory(); if (root != null) { @@ -219,4 +229,59 @@ public enum DataDirectory { } return directory; } + + /** + * Returns the sub-directory as an {@code URI}, or {@code null} if none. + * The URI form is useful when the file to resolve it itself expressed as an URI. + * The use of {@link URI#resolve(URI)} instead of {@link Path#resolve(Path)} preserves + * URI-specific components such as fragment and query. + * + * @return the sub-directory, or {@code null} if unspecified. + */ + private synchronized URI getDirectoryAsURI() { + if (directoryAsURI == null) { + @SuppressWarnings("LocalVariableHidesMemberVariable") + final Path directory = getDirectory(); + if (directory != null) { + directoryAsURI = directory.toUri(); + if (!directoryAsURI.getPath().endsWith("/")) { + directoryAsURI = null; + warning(null, Messages.Keys.DataDirectoryNotAccessible_2, ENV, directory); + } + } + } + return directoryAsURI; + } + + /** + * Returns the given URI as an absolute URI, resolved with this {@code DataDirectory} if the URI is relative. + * If the URI cannot be made absolute, a {@link NoSuchFileException} is thrown. This is necessary for letting + * the caller know that a coordinate operation is probably valid but cannot be constructed because an optional + * configuration is missing. It is typically because the {@code SIS_DATA} environment variable has not been set. + * + * @param path the URI to make absolute. + * @return an absolute URI to the data. + * @throws NoSuchFileException if the path cannot be made absolute. + */ + public final URI toAbsolutePath(URI path) throws NoSuchFileException { + final URI base = getDirectoryAsURI(); + if (base != null) { + path = base.resolve(path); + } + if (path.isAbsolute()) { + return path; + } + final String message; + if (path.isOpaque()) { + message = Errors.format(Errors.Keys.CanNotOpen_1, path); + } else { + final String env = getenv(); + if (env == null) { + message = Messages.format(Messages.Keys.DataDirectoryNotSpecified_1, ENV); + } else { + message = Messages.format(Messages.Keys.DataDirectoryNotAccessible_2, ENV, env); + } + } + throw new NoSuchFileException(path.toString(), null, message); + } } diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages.java index dce51e8ed6..daa1797844 100644 --- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages.java +++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages.java @@ -110,8 +110,8 @@ public class Messages extends IndexedResourceBundle { public static final short DataDirectoryDoesNotExist_2 = 12; /** - * Apache SIS is not authorized to access the “{1}” sub-directory in the directory given by the - * {0} environment variable. + * Cannot access the “{1}” sub-directory in the directory given by the {0} environment + * variable. */ public static final short DataDirectoryNotAccessible_2 = 13; diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages.properties b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages.properties index 87080d174a..055f4ab7d0 100644 --- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages.properties +++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages.properties @@ -28,7 +28,7 @@ CreateDurationFromIdentifier_3 = Created an instance of \u2018{0}\u2019 from t CreatingSchema_2 = Creating {0} schema in the \u201c{1}\u201d database. DataDirectory_2 = Environment variable {0} specifies the \u201c{1}\u201d data directory. DataDirectoryDoesNotExist_2 = The {0} environment variable is defined, but the given \u201c{1}\u201d value is not an existing directory. -DataDirectoryNotAccessible_2 = Apache SIS is not authorized to access the \u201c{1}\u201d sub-directory in the directory given by the {0} environment variable. +DataDirectoryNotAccessible_2 = Cannot access the \u201c{1}\u201d sub-directory in the directory given by the {0} environment variable. DataDirectoryNotAuthorized_1 = Apache SIS is not authorized to read information given by the \u201c{0}\u201d environment variable. DataDirectoryNotReadable_2 = The \u201c{1}\u201d directory specified by the {0} environment variable exists but is not readable. DataDirectoryNotSpecified_1 = The \u201c{0}\u201d environment variable is not set. diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages_fr.properties b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages_fr.properties index e58178c6ed..d34f6dd40d 100644 --- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages_fr.properties +++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/resources/Messages_fr.properties @@ -35,7 +35,7 @@ CreateDurationFromIdentifier_3 = Cr\u00e9ation d\u2019une instance de \u2018{0 CreatingSchema_2 = Cr\u00e9ation du sch\u00e9ma {0} dans la base de donn\u00e9es \u00ab\u202f{1}\u202f\u00bb. DataDirectory_2 = La variable environnementale {0} sp\u00e9cifie le r\u00e9pertoire de donn\u00e9es \u00ab\u202f{1}\u202f\u00bb. DataDirectoryDoesNotExist_2 = La variable environnementale {0} est bien d\u00e9finie, mais sa valeur \u00ab\u202f{1}\u202f\u00bb n\u2019est pas un r\u00e9pertoire existant. -DataDirectoryNotAccessible_2 = Apache SIS n\u2019a pas l\u2019autorisation d\u2019acc\u00e9der au sous-r\u00e9pertoire \u00ab\u202f{1}\u202f\u00bb dans le r\u00e9pertoire donn\u00e9 par la variable environnementale {0}. +DataDirectoryNotAccessible_2 = Ne peut pas acc\u00e9der au sous-r\u00e9pertoire \u00ab\u202f{1}\u202f\u00bb dans le r\u00e9pertoire donn\u00e9 par la variable environnementale {0}. DataDirectoryNotAuthorized_1 = Apache SIS n\u2019a pas l\u2019autorisation de lire des informations donn\u00e9es par la variable environnementale \u00ab\u202f{0}\u202f\u00bb. DataDirectoryNotReadable_2 = Le r\u00e9pertoire \u00ab\u202f{1}\u202f\u00bb sp\u00e9cifi\u00e9 par la variable environnementale {0} existe bien mais ne peut pas \u00eatre lu. DataDirectoryNotSpecified_1 = La variable environnementale \u00ab\u202f{0}\u202f\u00bb n\u2019est pas d\u00e9finie.