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 a09032ea36c8bf76d16f2f166ac55669873c3600 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Fri Dec 9 15:01:51 2022 +0100 `NumberFormatException` thrown when parsing an invalid code should be wrapped in a `NoSuchAuthorityCodeException`. --- .../referencing/factory/CommonAuthorityCode.java | 10 ++++++- .../factory/CommonAuthorityFactory.java | 35 +++++++++++----------- .../java/org/apache/sis/internal/util/Strings.java | 2 +- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/CommonAuthorityCode.java b/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/CommonAuthorityCode.java index 8aa66bd213..7c1650b78d 100644 --- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/CommonAuthorityCode.java +++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/CommonAuthorityCode.java @@ -117,7 +117,7 @@ final class CommonAuthorityCode { int end = CharSequences.skipTrailingWhitespaces(code, s, length); final int startOfParameters = code.indexOf(SEPARATOR, s); if (startOfParameters >= 0) { - complement = code.substring(startOfParameters + 1, end); + complement = code.substring(CharSequences.skipLeadingWhitespaces(code, startOfParameters + 1, end), end); end = CharSequences.skipTrailingWhitespaces(code, s, startOfParameters); } localCode = code.substring(s, end); @@ -135,12 +135,20 @@ final class CommonAuthorityCode { return legacy ? (versionOfAuto == 1) : (versionOfAuto >= 1 && versionOfAuto <= 2); } + /** + * Returns whether there is no parameters. + */ + final boolean isParameterless() { + return (complement == null) || complement.isEmpty(); + } + /** * Returns the result of parsing the comma-separated list of optional parameters after the code. * If there is no parameter, then this method returns an empty array. * Caller should not modify the returned array. * * @return the parameters after the code, or an empty array if none. + * @throws NumberFormatException if at least one number cannot be parsed. */ @SuppressWarnings("ReturnOfCollectionOrArrayField") final double[] parameters() { diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/CommonAuthorityFactory.java b/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/CommonAuthorityFactory.java index fbb893350e..86c9907966 100644 --- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/CommonAuthorityFactory.java +++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/CommonAuthorityFactory.java @@ -422,11 +422,11 @@ public class CommonAuthorityFactory extends GeodeticAuthorityFactory implements @Override public InternationalString getDescriptionText(final String code) throws FactoryException { final CommonAuthorityCode parsed = new CommonAuthorityCode(code); - if (parsed.isNumeric && parsed.parameters().length == 0) { + if (parsed.isNumeric && parsed.isParameterless()) { /* * For codes in the "AUTO(2)" namespace without parameters, we cannot rely on the default implementation * because it would fail to create the ProjectedCRS instance. Instead, we return a generic description. - * Note that we do not execute this block if parametes were specified. If there is parameters, + * Note that we do not execute this block if parametes were specified. If there are parameters, * then we instead rely on the default implementation for a more accurate description text. * Note also that we do not restrict to "AUTOx" namespaces because erroneous namespaces exist * in practice and the numerical codes are non-ambiguous (at least in current version). @@ -498,26 +498,25 @@ public class CommonAuthorityFactory extends GeodeticAuthorityFactory implements private CoordinateReferenceSystem createCoordinateReferenceSystem(final String code, final CommonAuthorityCode parsed) throws FactoryException { - /* - * First, handled the case of non-numerical parameterless codes ("OGC:JulianDate", etc.) - * We accept also SIS-specific codes (e.g. "OGC:ModifiedJulianDate", "OGC:JavaTime") if - * the namespace is the more neutral "CRS" instead of "OGC". The SIS-specific codes are - * never listed in `getAuthorityCodes(…)`. - */ final String localCode = parsed.localCode; - final double[] parameters = parsed.parameters(); - if (!parsed.isNumeric && !parsed.isAuto(false) && parameters.length == 0) try { - return CommonCRS.Temporal.forIdentifier(localCode, parsed.isOGC).crs(); - } catch (IllegalArgumentException e) { - throw noSuchAuthorityCode(localCode, code, e); - } - /* - * In current version, all non-temporal CRS have a numerical code. - */ + final double[] parameters; final int codeValue; try { + parameters = parsed.parameters(); + /* + * First, handled the case of non-numerical parameterless codes ("OGC:JulianDate", etc.) + * We accept also SIS-specific codes (e.g. "OGC:ModifiedJulianDate", "OGC:JavaTime") if + * the namespace is the more neutral "CRS" instead of "OGC". The SIS-specific codes are + * never listed in `getAuthorityCodes(…)`. + */ + if (!parsed.isNumeric && !parsed.isAuto(false) && parameters.length == 0) { + return CommonCRS.Temporal.forIdentifier(localCode, parsed.isOGC).crs(); + } + /* + * In current version, all non-temporal CRS have a numerical code. + */ codeValue = Integer.parseInt(localCode); - } catch (NumberFormatException exception) { + } catch (IllegalArgumentException exception) { // Include `NumberFormatException`. throw noSuchAuthorityCode(localCode, code, exception); } /* diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Strings.java b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Strings.java index 09bf00c464..1a94992f88 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Strings.java +++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Strings.java @@ -70,7 +70,7 @@ public final class Strings extends Static { } /** - * Returns the given text is non-null, or the empty string otherwise. + * Returns the given text if non-null, or the empty string otherwise. * * @param text text or null. * @return given text or empty string (never null).