#41 No schematron validation after schema errors

This commit is contained in:
Andreas Penski (init) 2020-01-23 15:30:05 +01:00
parent 96e82e0009
commit b6141bce23
4 changed files with 45 additions and 27 deletions

View file

@ -102,6 +102,10 @@ public interface CheckAction {
this.reportInput.getProcessingError().getError().addAll(errors);
}
public void addProcessingError(final String msg) {
stopProcessing(msg);
}
/**
* Gibt den Namen des Prüflings zurück, dabei werden etwaige Pfadinformationen abgeschnitten.
*

View file

@ -46,7 +46,8 @@ import de.kosit.validationtool.model.scenarios.ScenarioType;
@Slf4j
public class SchemaValidationAction implements CheckAction {
private static Result<Boolean, XMLSyntaxError> validate(final byte[] document, final ScenarioType scenarioType) {
private static Result<Boolean, XMLSyntaxError> validate(final byte[] document, final ScenarioType scenarioType,
final Bag results) {
log.debug("Validating document using scenario {}", scenarioType.getName());
final CollectingErrorEventHandler errorHandler = new CollectingErrorEventHandler();
try ( final InputStream input = new ByteArrayInputStream(document) ) {
@ -55,7 +56,10 @@ public class SchemaValidationAction implements CheckAction {
validator.validate(new StreamSource(input));
return new Result<>(!errorHandler.hasErrors(), errorHandler.getErrors());
} catch (final SAXException | IOException e) {
throw new IllegalStateException("Error validating document", e);
final String msg = String.format("Error processing schema validation for scenario %s", scenarioType.getName());
log.error(msg, e);
results.addProcessingError(msg);
return new Result<>(Boolean.FALSE);
}
}
@ -63,7 +67,7 @@ public class SchemaValidationAction implements CheckAction {
public void check(final Bag results) {
final CreateReportInput report = results.getReportInput();
final ScenarioType scenario = results.getScenarioSelectionResult().getObject();
final Result<Boolean, XMLSyntaxError> validateResult = validate(results.getInput().getContent(), scenario);
final Result<Boolean, XMLSyntaxError> validateResult = validate(results.getInput().getContent(), scenario, results);
results.setSchemaValidationResult(validateResult);
final ValidationResultsXmlSchema result = new ValidationResultsXmlSchema();
report.setValidationResultsXmlSchema(result);

View file

@ -28,6 +28,7 @@ import org.oclc.purl.dsdl.svrl.SchematronOutput;
import org.w3c.dom.Document;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.impl.CollectingErrorEventHandler;
import de.kosit.validationtool.impl.ContentRepository;
@ -50,17 +51,20 @@ import net.sf.saxon.s9api.XsltTransformer;
* @author Andreas Penski
*/
@RequiredArgsConstructor
@Slf4j
public class SchematronValidationAction implements CheckAction {
private final ContentRepository repository;
private final ConversionService conversionService;
private List<ValidationResultsSchematron> validate(final XdmNode document, final ScenarioType scenario) {
return scenario.getSchematronValidations().stream().map(v -> validate(document, v)).collect(Collectors.toList());
private List<ValidationResultsSchematron> validate(final Bag results, final XdmNode document, final ScenarioType scenario) {
return scenario.getSchematronValidations().stream().map(v -> validate(results, document, v)).collect(Collectors.toList());
}
private ValidationResultsSchematron validate(final XdmNode document, final BaseScenario.Transformation validation) {
private ValidationResultsSchematron validate(final Bag results, final XdmNode document, final BaseScenario.Transformation validation) {
final ValidationResultsSchematron s = new ValidationResultsSchematron();
s.setResource(validation.getResourceType());
try {
final XsltTransformer transformer = validation.getExecutable().load();
// resolving nur relative zum Repository
@ -73,29 +77,34 @@ public class SchematronValidationAction implements CheckAction {
transformer.setDestination(new DOMDestination(result));
transformer.setInitialContextNode(document);
transformer.transform();
final ValidationResultsSchematron s = new ValidationResultsSchematron();
s.setResource(validation.getResourceType());
final ValidationResultsSchematron.Results r = new ValidationResultsSchematron.Results();
r.setSchematronOutput(this.conversionService.readDocument(new DOMSource(result), SchematronOutput.class));
s.setResults(r);
return s;
} catch (final SaxonApiException e) {
throw new IllegalStateException("Can not run schematron validation", e);
final String msg = String.format("Error processing schematron validation %s", validation.getResourceType().getName());
log.error(msg, e);
results.addProcessingError(msg);
}
return s;
}
@Override
public void check(final Bag results) {
final CreateReportInput report = results.getReportInput();
final List<ValidationResultsSchematron> validationResult = validate(results.getParserResult().getObject(),
final List<ValidationResultsSchematron> validationResult = validate(results, results.getParserResult().getObject(),
results.getScenarioSelectionResult().getObject());
report.getValidationResultsSchematron().addAll(validationResult);
}
@Override
public boolean isSkipped(final Bag results) {
return hasNoSchematrons(results.getScenarioSelectionResult().getObject());
return hasNoSchematrons(results.getScenarioSelectionResult().getObject()) || isSchemaInvalid(results);
}
private static boolean isSchemaInvalid(final Bag results) {
return results.getSchemaValidationResult() == null || results.getSchemaValidationResult().isInvalid();
}
private static boolean hasNoSchematrons(final ScenarioType object) {

View file

@ -61,24 +61,24 @@ public class SchemaValidatorActionTest {
@Before
public void setup() {
service = new SchemaValidationAction();
repository = new ContentRepository(ObjectFactory.createProcessor(), Helper.REPOSITORY);
this.service = new SchemaValidationAction();
this.repository = new ContentRepository(ObjectFactory.createProcessor(), Helper.REPOSITORY);
}
@Test
public void testSimple() {
CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(VALID_EXAMPLE), new CreateReportInput());
ScenarioType t = new ScenarioType();
ValidateWithXmlSchema v = new ValidateWithXmlSchema();
ResourceType r = new ResourceType();
final CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(VALID_EXAMPLE), new CreateReportInput());
final ScenarioType t = new ScenarioType();
final ValidateWithXmlSchema v = new ValidateWithXmlSchema();
final ResourceType r = new ResourceType();
r.setLocation("resources/eRechnung/UBL-2.1/xsdrt/maindoc/UBL-Invoice-2.1.xsd");
r.setName("invoice");
v.getResource().add(r);
t.setValidateWithXmlSchema(v);
t.initialize(repository, true);
t.initialize(this.repository, true);
bag.setScenarioSelectionResult(new Result<>(t, Collections.emptyList()));
service.check(bag);
this.service.check(bag);
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
assertThat(bag.getSchemaValidationResult()).isNotNull();
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
@ -86,17 +86,17 @@ public class SchemaValidatorActionTest {
@Test
public void testValidationFailure() throws MalformedURLException {
CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(INVALID_EXAMPLE.toURL()), new CreateReportInput());
ScenarioType t = new ScenarioType();
ValidateWithXmlSchema v = new ValidateWithXmlSchema();
ResourceType r = new ResourceType();
final CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(INVALID_EXAMPLE.toURL()), new CreateReportInput());
final ScenarioType t = new ScenarioType();
final ValidateWithXmlSchema v = new ValidateWithXmlSchema();
final ResourceType r = new ResourceType();
r.setLocation(Helper.REPOSITORY.relativize(Helper.SCENARIO_SCHEMA).getRawPath());
r.setName("invoice");
v.getResource().add(r);
t.setValidateWithXmlSchema(v);
t.initialize(repository, true);
t.initialize(this.repository, true);
bag.setScenarioSelectionResult(new Result<>(t, Collections.emptyList()));
service.check(bag);
this.service.check(bag);
assertThat(bag.getSchemaValidationResult().isValid()).isFalse();
bag.getSchemaValidationResult().getErrors().forEach(e->{
assertThat(e.getRowNumber()).isGreaterThan(0);
@ -104,9 +104,10 @@ public class SchemaValidatorActionTest {
});
}
@Test
public void testSchemaReferences() {
final Schema reportInputSchema = repository.getReportInputSchema();
final Schema reportInputSchema = this.repository.getReportInputSchema();
assertThat(reportInputSchema).isNotNull();
}