From 7f57d8744bb3fde9744b2ea8793a30a4c3ccd70c Mon Sep 17 00:00:00 2001 From: "Andreas Penski (init)" Date: Mon, 6 Jan 2020 11:49:42 +0100 Subject: [PATCH] #40 Fix NPE in Result.getReportDocument for malformed xml input Information about parsing errors in fallback-report --- .../impl/EngineInformation.java | 18 +++++++++ .../impl/tasks/CreateReportAction.java | 16 +++++++- src/main/model/xsd/createReportInput.xsd | 37 +++++++++++++------ src/main/resources/app-info.properties | 2 +- .../validationtool/impl/DefaultCheckTest.java | 1 + 5 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/kosit/validationtool/impl/EngineInformation.java b/src/main/java/de/kosit/validationtool/impl/EngineInformation.java index 3774f9b..10692ec 100644 --- a/src/main/java/de/kosit/validationtool/impl/EngineInformation.java +++ b/src/main/java/de/kosit/validationtool/impl/EngineInformation.java @@ -55,4 +55,22 @@ public static String getName() { public static String getFrameworkVersion() { return PROPERTIES.getProperty("framework_version"); } + + /** + * Gibt die Major-Versions-Nummer des eingesetzten Frameworks zurück. + * + * @return die Major-Versions-Nummer + */ + public static String getFrameworkMajorVersion() { + return getFrameworkVersion().substring(0, 1); + } + + /** + * Gibt den Namespace des eingesetzten Frameworks zurück. + * + * @return die Major-Versions-Nummer + */ + public static String getFrameworkNamespace() { + return "http://www.xoev.de/de/validator/framework/" + getFrameworkMajorVersion() + "/createreportinput"; + } } diff --git a/src/main/java/de/kosit/validationtool/impl/tasks/CreateReportAction.java b/src/main/java/de/kosit/validationtool/impl/tasks/CreateReportAction.java index b8bab3d..9809479 100644 --- a/src/main/java/de/kosit/validationtool/impl/tasks/CreateReportAction.java +++ b/src/main/java/de/kosit/validationtool/impl/tasks/CreateReportAction.java @@ -19,19 +19,25 @@ package de.kosit.validationtool.impl.tasks; +import java.util.Collection; +import java.util.stream.Collectors; + import javax.xml.transform.dom.DOMSource; import org.w3c.dom.Document; import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; import lombok.RequiredArgsConstructor; import de.kosit.validationtool.impl.CollectingErrorEventHandler; import de.kosit.validationtool.impl.ContentRepository; import de.kosit.validationtool.impl.ConversionService; +import de.kosit.validationtool.impl.EngineInformation; import de.kosit.validationtool.impl.ObjectFactory; import de.kosit.validationtool.impl.RelativeUriResolver; import de.kosit.validationtool.impl.ScenarioRepository; +import de.kosit.validationtool.model.reportInput.XMLSyntaxError; import de.kosit.validationtool.model.scenarios.ScenarioType; import net.sf.saxon.s9api.BuildingContentHandler; @@ -53,6 +59,7 @@ @RequiredArgsConstructor public class CreateReportAction implements CheckAction { + private static final String ERROR_MESSAGE_ELEMENT = "error-message"; private final Processor processor; private final ConversionService conversionService; @@ -71,7 +78,7 @@ public void check(final Bag results) { try { final XdmNode parsedDocument = results.getParserResult().isValid() ? results.getParserResult().getObject() - : createEmpty(); + : createErrorInformation(results.getParserResult().getErrors()); final Document reportInput = this.conversionService.writeDocument(results.getReportInput()); final XdmNode root = documentBuilder.build(new DOMSource(reportInput)); @@ -95,9 +102,14 @@ public void check(final Bag results) { } } - private static XdmNode createEmpty() throws SaxonApiException, SAXException { + private static XdmNode createErrorInformation(final Collection errors) throws SaxonApiException, SAXException { final BuildingContentHandler contentHandler = ObjectFactory.createProcessor().newDocumentBuilder().newBuildingContentHandler(); contentHandler.startDocument(); + contentHandler.startElement(EngineInformation.getFrameworkNamespace(), ERROR_MESSAGE_ELEMENT, ERROR_MESSAGE_ELEMENT, + new AttributesImpl()); + final String message = errors.stream().map(XMLSyntaxError::getMessage).collect(Collectors.joining()); + contentHandler.characters(message.toCharArray(), 0, message.length()); + contentHandler.endElement(EngineInformation.getFrameworkNamespace(), ERROR_MESSAGE_ELEMENT, ERROR_MESSAGE_ELEMENT); return contentHandler.getDocumentNode(); } diff --git a/src/main/model/xsd/createReportInput.xsd b/src/main/model/xsd/createReportInput.xsd index feb9443..f0377f9 100644 --- a/src/main/model/xsd/createReportInput.xsd +++ b/src/main/model/xsd/createReportInput.xsd @@ -21,23 +21,23 @@ - + - - - - - - - - + + + + + + + + @@ -145,11 +145,24 @@ - + - + + + + + Dieses Element wird als "Input"-Document im Falle von Parsing-Fehlern an den Report-Generator übergeben. + + + + + + + + + diff --git a/src/main/resources/app-info.properties b/src/main/resources/app-info.properties index 897e35f..d3b04cf 100644 --- a/src/main/resources/app-info.properties +++ b/src/main/resources/app-info.properties @@ -1,7 +1,7 @@ # do not edit this file # this properties are overriden by build process project_version=${project.version} -framework_version=1.0.0 +framework_version=1.0.2 engine_name=KoSIT Validator build_timestamp=${build.timestamp} build_number=${build.number} diff --git a/src/test/java/de/kosit/validationtool/impl/DefaultCheckTest.java b/src/test/java/de/kosit/validationtool/impl/DefaultCheckTest.java index 0e7af12..0b6edc6 100644 --- a/src/test/java/de/kosit/validationtool/impl/DefaultCheckTest.java +++ b/src/test/java/de/kosit/validationtool/impl/DefaultCheckTest.java @@ -124,6 +124,7 @@ public void testNotWellFormed() { assertThat(result.getAcceptRecommendation()).isEqualTo(AcceptRecommendation.REJECT); assertThat(result.getReport()).isNotNull(); assertThat(result.getReportDocument()).isNotNull(); + System.out.println(Helper.serialize(result.getReportDocument())); } }