mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-26 01:05:38 +00:00
#41 No schematron validation after schema errors
This commit is contained in:
parent
96e82e0009
commit
b6141bce23
4 changed files with 45 additions and 27 deletions
|
|
@ -102,6 +102,10 @@ public interface CheckAction {
|
||||||
this.reportInput.getProcessingError().getError().addAll(errors);
|
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.
|
* Gibt den Namen des Prüflings zurück, dabei werden etwaige Pfadinformationen abgeschnitten.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,8 @@ import de.kosit.validationtool.model.scenarios.ScenarioType;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SchemaValidationAction implements CheckAction {
|
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());
|
log.debug("Validating document using scenario {}", scenarioType.getName());
|
||||||
final CollectingErrorEventHandler errorHandler = new CollectingErrorEventHandler();
|
final CollectingErrorEventHandler errorHandler = new CollectingErrorEventHandler();
|
||||||
try ( final InputStream input = new ByteArrayInputStream(document) ) {
|
try ( final InputStream input = new ByteArrayInputStream(document) ) {
|
||||||
|
|
@ -55,7 +56,10 @@ public class SchemaValidationAction implements CheckAction {
|
||||||
validator.validate(new StreamSource(input));
|
validator.validate(new StreamSource(input));
|
||||||
return new Result<>(!errorHandler.hasErrors(), errorHandler.getErrors());
|
return new Result<>(!errorHandler.hasErrors(), errorHandler.getErrors());
|
||||||
} catch (final SAXException | IOException e) {
|
} 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) {
|
public void check(final Bag results) {
|
||||||
final CreateReportInput report = results.getReportInput();
|
final CreateReportInput report = results.getReportInput();
|
||||||
final ScenarioType scenario = results.getScenarioSelectionResult().getObject();
|
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);
|
results.setSchemaValidationResult(validateResult);
|
||||||
final ValidationResultsXmlSchema result = new ValidationResultsXmlSchema();
|
final ValidationResultsXmlSchema result = new ValidationResultsXmlSchema();
|
||||||
report.setValidationResultsXmlSchema(result);
|
report.setValidationResultsXmlSchema(result);
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import org.oclc.purl.dsdl.svrl.SchematronOutput;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import de.kosit.validationtool.impl.CollectingErrorEventHandler;
|
import de.kosit.validationtool.impl.CollectingErrorEventHandler;
|
||||||
import de.kosit.validationtool.impl.ContentRepository;
|
import de.kosit.validationtool.impl.ContentRepository;
|
||||||
|
|
@ -50,17 +51,20 @@ import net.sf.saxon.s9api.XsltTransformer;
|
||||||
* @author Andreas Penski
|
* @author Andreas Penski
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class SchematronValidationAction implements CheckAction {
|
public class SchematronValidationAction implements CheckAction {
|
||||||
|
|
||||||
private final ContentRepository repository;
|
private final ContentRepository repository;
|
||||||
|
|
||||||
private final ConversionService conversionService;
|
private final ConversionService conversionService;
|
||||||
|
|
||||||
private List<ValidationResultsSchematron> validate(final XdmNode document, final ScenarioType scenario) {
|
private List<ValidationResultsSchematron> validate(final Bag results, final XdmNode document, final ScenarioType scenario) {
|
||||||
return scenario.getSchematronValidations().stream().map(v -> validate(document, v)).collect(Collectors.toList());
|
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 {
|
try {
|
||||||
final XsltTransformer transformer = validation.getExecutable().load();
|
final XsltTransformer transformer = validation.getExecutable().load();
|
||||||
// resolving nur relative zum Repository
|
// resolving nur relative zum Repository
|
||||||
|
|
@ -73,29 +77,34 @@ public class SchematronValidationAction implements CheckAction {
|
||||||
transformer.setDestination(new DOMDestination(result));
|
transformer.setDestination(new DOMDestination(result));
|
||||||
transformer.setInitialContextNode(document);
|
transformer.setInitialContextNode(document);
|
||||||
transformer.transform();
|
transformer.transform();
|
||||||
final ValidationResultsSchematron s = new ValidationResultsSchematron();
|
|
||||||
s.setResource(validation.getResourceType());
|
|
||||||
final ValidationResultsSchematron.Results r = new ValidationResultsSchematron.Results();
|
final ValidationResultsSchematron.Results r = new ValidationResultsSchematron.Results();
|
||||||
r.setSchematronOutput(this.conversionService.readDocument(new DOMSource(result), SchematronOutput.class));
|
r.setSchematronOutput(this.conversionService.readDocument(new DOMSource(result), SchematronOutput.class));
|
||||||
s.setResults(r);
|
s.setResults(r);
|
||||||
return s;
|
|
||||||
|
|
||||||
} catch (final SaxonApiException e) {
|
} 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
|
@Override
|
||||||
public void check(final Bag results) {
|
public void check(final Bag results) {
|
||||||
final CreateReportInput report = results.getReportInput();
|
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());
|
results.getScenarioSelectionResult().getObject());
|
||||||
report.getValidationResultsSchematron().addAll(validationResult);
|
report.getValidationResultsSchematron().addAll(validationResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isSkipped(final Bag results) {
|
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) {
|
private static boolean hasNoSchematrons(final ScenarioType object) {
|
||||||
|
|
|
||||||
|
|
@ -61,24 +61,24 @@ public class SchemaValidatorActionTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
service = new SchemaValidationAction();
|
this.service = new SchemaValidationAction();
|
||||||
repository = new ContentRepository(ObjectFactory.createProcessor(), Helper.REPOSITORY);
|
this.repository = new ContentRepository(ObjectFactory.createProcessor(), Helper.REPOSITORY);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimple() {
|
public void testSimple() {
|
||||||
CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(VALID_EXAMPLE), new CreateReportInput());
|
final CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(VALID_EXAMPLE), new CreateReportInput());
|
||||||
ScenarioType t = new ScenarioType();
|
final ScenarioType t = new ScenarioType();
|
||||||
ValidateWithXmlSchema v = new ValidateWithXmlSchema();
|
final ValidateWithXmlSchema v = new ValidateWithXmlSchema();
|
||||||
ResourceType r = new ResourceType();
|
final ResourceType r = new ResourceType();
|
||||||
r.setLocation("resources/eRechnung/UBL-2.1/xsdrt/maindoc/UBL-Invoice-2.1.xsd");
|
r.setLocation("resources/eRechnung/UBL-2.1/xsdrt/maindoc/UBL-Invoice-2.1.xsd");
|
||||||
r.setName("invoice");
|
r.setName("invoice");
|
||||||
v.getResource().add(r);
|
v.getResource().add(r);
|
||||||
t.setValidateWithXmlSchema(v);
|
t.setValidateWithXmlSchema(v);
|
||||||
t.initialize(repository, true);
|
t.initialize(this.repository, true);
|
||||||
bag.setScenarioSelectionResult(new Result<>(t, Collections.emptyList()));
|
bag.setScenarioSelectionResult(new Result<>(t, Collections.emptyList()));
|
||||||
service.check(bag);
|
this.service.check(bag);
|
||||||
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
|
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
|
||||||
assertThat(bag.getSchemaValidationResult()).isNotNull();
|
assertThat(bag.getSchemaValidationResult()).isNotNull();
|
||||||
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
|
assertThat(bag.getSchemaValidationResult().isValid()).isTrue();
|
||||||
|
|
@ -86,17 +86,17 @@ public class SchemaValidatorActionTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testValidationFailure() throws MalformedURLException {
|
public void testValidationFailure() throws MalformedURLException {
|
||||||
CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(INVALID_EXAMPLE.toURL()), new CreateReportInput());
|
final CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(INVALID_EXAMPLE.toURL()), new CreateReportInput());
|
||||||
ScenarioType t = new ScenarioType();
|
final ScenarioType t = new ScenarioType();
|
||||||
ValidateWithXmlSchema v = new ValidateWithXmlSchema();
|
final ValidateWithXmlSchema v = new ValidateWithXmlSchema();
|
||||||
ResourceType r = new ResourceType();
|
final ResourceType r = new ResourceType();
|
||||||
r.setLocation(Helper.REPOSITORY.relativize(Helper.SCENARIO_SCHEMA).getRawPath());
|
r.setLocation(Helper.REPOSITORY.relativize(Helper.SCENARIO_SCHEMA).getRawPath());
|
||||||
r.setName("invoice");
|
r.setName("invoice");
|
||||||
v.getResource().add(r);
|
v.getResource().add(r);
|
||||||
t.setValidateWithXmlSchema(v);
|
t.setValidateWithXmlSchema(v);
|
||||||
t.initialize(repository, true);
|
t.initialize(this.repository, true);
|
||||||
bag.setScenarioSelectionResult(new Result<>(t, Collections.emptyList()));
|
bag.setScenarioSelectionResult(new Result<>(t, Collections.emptyList()));
|
||||||
service.check(bag);
|
this.service.check(bag);
|
||||||
assertThat(bag.getSchemaValidationResult().isValid()).isFalse();
|
assertThat(bag.getSchemaValidationResult().isValid()).isFalse();
|
||||||
bag.getSchemaValidationResult().getErrors().forEach(e->{
|
bag.getSchemaValidationResult().getErrors().forEach(e->{
|
||||||
assertThat(e.getRowNumber()).isGreaterThan(0);
|
assertThat(e.getRowNumber()).isGreaterThan(0);
|
||||||
|
|
@ -104,9 +104,10 @@ public class SchemaValidatorActionTest {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSchemaReferences() {
|
public void testSchemaReferences() {
|
||||||
final Schema reportInputSchema = repository.getReportInputSchema();
|
final Schema reportInputSchema = this.repository.getReportInputSchema();
|
||||||
assertThat(reportInputSchema).isNotNull();
|
assertThat(reportInputSchema).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue