Skip to content

Commit

Permalink
#40 Fix NPE in Result.getReportDocument for malformed xml input
Browse files Browse the repository at this point in the history
Information about parsing errors in fallback-report
  • Loading branch information
apenski committed Jan 6, 2020
1 parent 0ca79fc commit 7f57d87
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
18 changes: 18 additions & 0 deletions src/main/java/de/kosit/validationtool/impl/EngineInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -95,9 +102,14 @@ public void check(final Bag results) {
}
}

private static XdmNode createEmpty() throws SaxonApiException, SAXException {
private static XdmNode createErrorInformation(final Collection<XMLSyntaxError> 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();
}

Expand Down
37 changes: 25 additions & 12 deletions src/main/model/xsd/createReportInput.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
<!-- $Id$ -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:in="http://www.xoev.de/de/validator/framework/1/createreportinput"
xmlns:s="http://www.xoev.de/de/validator/framework/1/scenarios" xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
targetNamespace="http://www.xoev.de/de/validator/framework/1/createreportinput" version="1.0.0"
targetNamespace="http://www.xoev.de/de/validator/framework/1/createreportinput" version="1.0.2"
elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:import namespace="http://www.xoev.de/de/validator/framework/1/scenarios" schemaLocation="scenarios.xsd"/>
<xs:import namespace="http://www.xoev.de/de/validator/framework/1/scenarios" schemaLocation="scenarios.xsd" />
<xs:import namespace="http://purl.oclc.org/dsdl/svrl" schemaLocation="svrl-kosit.xsd" />

<xs:element name="createReportInput">
<xs:complexType>
<xs:sequence>
<xs:element name="engine" type="in:EngineType"/>
<xs:element name="timestamp" type="xs:dateTime"/>
<xs:element name="documentIdentification" type="in:DocumentIdentificationType"/>
<xs:element ref="s:scenario" minOccurs="0"/>
<xs:element name="validationResultsWellformedness" type="in:ValidationResultsWellformedness" minOccurs="0"/>
<xs:element name="validationResultsXmlSchema" type="in:ValidationResultsXmlSchema" minOccurs="0"/>
<xs:element name="validationResultsSchematron" type="in:ValidationResultsSchematron" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="processingError" type="in:ProcessingError" minOccurs="0"/>
<xs:element name="engine" type="in:EngineType" />
<xs:element name="timestamp" type="xs:dateTime" />
<xs:element name="documentIdentification" type="in:DocumentIdentificationType" />
<xs:element ref="s:scenario" minOccurs="0" />
<xs:element name="validationResultsWellformedness" type="in:ValidationResultsWellformedness" minOccurs="0" />
<xs:element name="validationResultsXmlSchema" type="in:ValidationResultsXmlSchema" minOccurs="0" />
<xs:element name="validationResultsSchematron" type="in:ValidationResultsSchematron" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="processingError" type="in:ProcessingError" minOccurs="0" />
</xs:sequence>
<xs:attribute name="frameworkVersion" type="xs:string" use="required"/>
</xs:complexType>
Expand Down Expand Up @@ -145,11 +145,24 @@
</xs:element>
</xs:sequence>
</xs:complexType>

<xs:complexType name="ProcessingError">
<xs:sequence>
<xs:element name="error" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="error" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:element name="error-message">
<xs:annotation>
<xs:documentation>
Dieses Element wird als "Input"-Document im Falle von Parsing-Fehlern an den Report-Generator übergeben.
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string" />
</xs:simpleContent>
</xs:complexType>
</xs:element>

</xs:schema>
2 changes: 1 addition & 1 deletion src/main/resources/app-info.properties
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

}

0 comments on commit 7f57d87

Please sign in to comment.