From b244f73d3ada47f22048a5e60b72364bbd4c7f1e Mon Sep 17 00:00:00 2001 From: apenski Date: Wed, 9 Nov 2022 09:02:15 +0100 Subject: [PATCH] (Fix) https://projekte.kosit.org/kosit/validator/-/issues/95 NPE when using empty repository definition (-r "") --- CHANGELOG.md | 14 ++-- .../cmd/CommandLineApplication.java | 4 +- .../validationtool/cmd/TypeConverter.java | 70 +++++++++---------- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 416fca6..3f4e5c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,19 +9,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- [#93](https://projekte.kosit.org/kosit/validator/-/issues/93) Remove usage information, when validation failed (CLI) +- [CLI][#93](https://projekte.kosit.org/kosit/validator/-/issues/93) Remove usage information, when validation failed +- [CLI][#95](https://projekte.kosit.org/kosit/validator/-/issues/95) NPE when using empty repository definition (-r "") ### Added -- Support for multiple configurations and multiple repositories. See [cli documentation](docs/cli.md) for details -- Possibility to use preconfigured Saxon `Processor` instance for validation +- [CLI] Support for multiple configurations and multiple repositories. See [cli documentation](docs/cli.md) for details +- [API ]Possibility to use preconfigured Saxon `Processor` instance for validation ### Changed -- [ResolvingConfigurationStrategy.java#getProcessor()](de/kosit/validationtool/api/ResolvingConfigurationStrategy) is +- [API] [ResolvingConfigurationStrategy.java#getProcessor()](de/kosit/validationtool/api/ResolvingConfigurationStrategy) + is removed. -- Bump [Saxon HE](https://www.saxonica.com/documentation11/documentation.xml) to 11.4 -- Bump [jaxb-ri](https://github.com/eclipse-ee4j/jaxb-ri) to 2.3.7 +- [INTERNAL] Bump [Saxon HE](https://www.saxonica.com/documentation11/documentation.xml) to 11.4 +- [INTERNAL] Bump [jaxb-ri](https://github.com/eclipse-ee4j/jaxb-ri) to 2.3.7 - [INTERNAL] CLI parsing based on pico-cli, commons-cli is removed diff --git a/src/main/java/de/kosit/validationtool/cmd/CommandLineApplication.java b/src/main/java/de/kosit/validationtool/cmd/CommandLineApplication.java index c99f63e..45bfd84 100644 --- a/src/main/java/de/kosit/validationtool/cmd/CommandLineApplication.java +++ b/src/main/java/de/kosit/validationtool/cmd/CommandLineApplication.java @@ -17,6 +17,7 @@ package de.kosit.validationtool.cmd; import static de.kosit.validationtool.impl.Printer.writeErr; +import static org.apache.commons.lang3.StringUtils.isNotEmpty; import org.apache.commons.lang3.ObjectUtils; import org.fusesource.jansi.AnsiConsole; @@ -96,7 +97,8 @@ public class CommandLineApplication { } private static int logExecutionException(final Exception ex, final CommandLine cli, final ParseResult parseResult) { - Printer.writeErr(ex, ex.getMessage()); + final String message = isNotEmpty(ex.getMessage()) ? ex.getMessage() : "Es ist eine Fehler aufgetreten"; + Printer.writeErr(ex, message); return 1; } diff --git a/src/main/java/de/kosit/validationtool/cmd/TypeConverter.java b/src/main/java/de/kosit/validationtool/cmd/TypeConverter.java index 9f9c8c1..cedf29d 100644 --- a/src/main/java/de/kosit/validationtool/cmd/TypeConverter.java +++ b/src/main/java/de/kosit/validationtool/cmd/TypeConverter.java @@ -16,7 +16,7 @@ package de.kosit.validationtool.cmd; -import static org.apache.commons.lang3.StringUtils.isNotBlank; +import static org.apache.commons.lang3.StringUtils.defaultIfBlank; import java.nio.file.Paths; import java.util.HashMap; @@ -37,9 +37,41 @@ import picocli.CommandLine.ITypeConverter; */ class TypeConverter { + final static Map, AtomicInteger> counter = new HashMap<>(); + + private static String getDefaultName(final Class type) { + final AtomicInteger current = counter.computeIfAbsent(type, a -> new AtomicInteger(1)); + return ScenarioRepository.DEFAULT + "_" + current.getAndIncrement(); + } + + private static T convert(final Class type, final String value) { + final T def; + final String[] splitted = defaultIfBlank(value, "").split("="); + if (splitted.length == 1) { + def = createNewInstance(type); + def.setName(getDefaultName(type)); + def.setPath(Paths.get(splitted[0].trim())); + } else if (splitted.length == 2) { + def = createNewInstance(type); + def.setName(splitted[0].trim()); + def.setPath(Paths.get(splitted[1].trim())); + } else { + throw new IllegalArgumentException("Not a valid repository specification " + value); + } + return def; + } + + private static T createNewInstance(final Class type) { + try { + return type.getConstructor().newInstance(); + } catch (final ReflectiveOperationException e) { + throw new IllegalStateException("Error creating instance of type " + type); + } + } + /** * Type converter for a repository definition specification e.g. '-r somelocation.xml OR -r myid=somelocation.xml' - * + * * @author Andreas Penski */ public static class RepositoryConverter implements ITypeConverter { @@ -62,38 +94,4 @@ class TypeConverter { return TypeConverter.convert(ScenarioDefinition.class, value); } } - - final static Map, AtomicInteger> counter = new HashMap<>(); - - private static String getDefaultName(final Class type) { - final AtomicInteger current = counter.computeIfAbsent(type, a -> new AtomicInteger(1)); - return ScenarioRepository.DEFAULT + "_" + current.getAndIncrement(); - } - - private static T convert(final Class type, final String value) { - T def = null; - if (isNotBlank(value)) { - final String[] splitted = value.split("="); - if (splitted.length == 1) { - def = createNewInstance(type); - def.setName(getDefaultName(type)); - def.setPath(Paths.get(splitted[0].trim())); - } else if (splitted.length == 2) { - def = createNewInstance(type); - def.setName(splitted[0].trim()); - def.setPath(Paths.get(splitted[1].trim())); - } else { - throw new IllegalArgumentException("Not a valid repository specification " + value); - } - } - return def; - } - - private static T createNewInstance(final Class type) { - try { - return type.getConstructor().newInstance(); - } catch (final ReflectiveOperationException e) { - throw new IllegalStateException("Error creating instance of type " + type); - } - } }