#21 Umsetzung der API Rückgabe, erste version

This commit is contained in:
Andreas Penski (init) 2019-05-17 11:21:22 +02:00 committed by Andreas Penski
parent a424fbbcfe
commit ab31ed71b1
21 changed files with 532 additions and 147 deletions

View file

@ -28,7 +28,9 @@ import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.api.Check;
import de.kosit.validationtool.api.CheckConfiguration;
import de.kosit.validationtool.api.Input;
import de.kosit.validationtool.api.Result;
import de.kosit.validationtool.impl.tasks.CheckAction;
import de.kosit.validationtool.impl.tasks.ComputeAcceptanceAction;
import de.kosit.validationtool.impl.tasks.CreateReportAction;
import de.kosit.validationtool.impl.tasks.DocumentParseAction;
import de.kosit.validationtool.impl.tasks.ScenarioSelectionAction;
@ -41,7 +43,6 @@ import de.kosit.validationtool.model.reportInput.EngineType;
import de.kosit.validationtool.model.reportInput.ProcessingError;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.XdmNode;
/**
* Die Referenz-Implementierung für den Prüfprozess. Nach initialer Konfiguration ist diese Klasse threadsafe und kann
@ -64,7 +65,6 @@ public class DefaultCheck implements Check {
private final ConversionService conversionService;
@Getter
private final List<CheckAction> checkSteps;
@ -88,6 +88,7 @@ public class DefaultCheck implements Check {
this.checkSteps.add(new ValidateReportInputAction(this.conversionService, this.contentRepository.getReportInputSchema()));
this.checkSteps
.add(new CreateReportAction(processor, this.conversionService, this.repository, configuration.getScenarioRepository()));
this.checkSteps.add(new ComputeAcceptanceAction());
}
protected static CreateReportInput createReport() {
@ -101,12 +102,12 @@ public class DefaultCheck implements Check {
}
@Override
public XdmNode checkInput(final Input input) {
public Result checkInput(final Input input) {
final CheckAction.Bag t = new CheckAction.Bag(input, createReport());
return runCheckInternal(t);
}
protected XdmNode runCheckInternal(final CheckAction.Bag t) {
protected Result runCheckInternal(final CheckAction.Bag t) {
final long started = System.currentTimeMillis();
log.info("Checking content of {}", t.getInput().getName());
for (final CheckAction action : this.checkSteps) {
@ -124,10 +125,10 @@ public class DefaultCheck implements Check {
}
t.setFinished(true);
log.info("Finished check of {} in {}ms\n", t.getInput().getName(), System.currentTimeMillis() - started);
return t.getReport();
return new Result(t.getReport(), t.getAcceptStatus());
}
private static boolean createDocumentIdentification(final CheckAction.Bag transporter) {
private static void createDocumentIdentification(final CheckAction.Bag transporter) {
final DocumentIdentificationType i = new DocumentIdentificationType();
final DocumentIdentificationType.DocumentHash h = new DocumentIdentificationType.DocumentHash();
h.setHashAlgorithm(transporter.getInput().getDigestAlgorithm());
@ -135,6 +136,5 @@ public class DefaultCheck implements Check {
i.setDocumentHash(h);
i.setDocumentReference(transporter.getInput().getName());
transporter.getReportInput().setDocumentIdentification(i);
return true;
}
}

View file

@ -36,7 +36,11 @@ import lombok.Setter;
import de.kosit.validationtool.impl.ContentRepository;
import de.kosit.validationtool.impl.ScenarioRepository;
import de.kosit.validationtool.model.scenarios.*;
import de.kosit.validationtool.model.scenarios.CreateReportType;
import de.kosit.validationtool.model.scenarios.NamespaceType;
import de.kosit.validationtool.model.scenarios.ResourceType;
import de.kosit.validationtool.model.scenarios.ValidateWithSchematron;
import de.kosit.validationtool.model.scenarios.ValidateWithXmlSchema;
import net.sf.saxon.s9api.XPathExecutable;
import net.sf.saxon.s9api.XPathSelector;
@ -50,50 +54,63 @@ import net.sf.saxon.s9api.XsltExecutable;
*/
public abstract class BaseScenario {
private XPathExecutable xPathExecutable;
/**
* Laufzeitinformationen über eine Transformation.
*/
@Getter
@Setter
@AllArgsConstructor
public class Transformation {
private XsltExecutable executable;
private ResourceType resourceType;
}
private XPathExecutable matchExecutable;
private XPathExecutable acceptExecutable;
private Schema schema;
private List<Transformation> schematronValidations;
private ContentRepository repository;
private Transformation reportTransformation;
/**
* Gibt eine Transformation zurück.
*
* @return initialisierte Transformation
*/
public Transformation getReportTransformation() {
if (reportTransformation == null) {
if (this.reportTransformation == null) {
final ResourceType resource = getCreateReport().getResource();
final XsltExecutable executable = repository.loadXsltScript(URI.create(resource.getLocation()));
reportTransformation = new Transformation(executable, resource);
final XsltExecutable executable = this.repository.loadXsltScript(URI.create(resource.getLocation()));
this.reportTransformation = new Transformation(executable, resource);
}
return reportTransformation;
return this.reportTransformation;
}
/**
* Lieferrt das Schema zu diesem Szenario.
*
* @return das passende Schema
*/
public Schema getSchema() {
if (schema == null) {
if (this.schema == null) {
final List<String> schemaResources = getValidateWithXmlSchema().getResource().stream().map(ResourceType::getLocation)
.collect(Collectors.toList());
schema = repository.createSchema(schemaResources);
this.schema = this.repository.createSchema(schemaResources);
}
return schema;
return this.schema;
}
/**
* Initialisiert das Szenario auf Basis eines [@link ContentRepository}
*
* @param repository das Repository mit den Szenario-Artefakten
* @param lazy optionales lazy loading der XML-Artefakte
* @return true wenn erfolgreich
*/
public boolean initialize(ContentRepository repository, boolean lazy) {
public boolean initialize(final ContentRepository repository, final boolean lazy) {
this.repository = repository;
if (!lazy) {
getSchema();
@ -106,20 +123,21 @@ public abstract class BaseScenario {
/**
* Liefer eine Liste mit Schematron Validierungs-Transformationen
*
* @return liste mit initialisierten Transformationsinformationen
*/
public List<Transformation> getSchematronValidations() {
if (schematronValidations == null) {
schematronValidations = new ArrayList<>();
if (this.schematronValidations == null) {
this.schematronValidations = new ArrayList<>();
getValidateWithSchematron().forEach(v -> {
if (v.isPsvi()) {
throw new NotImplementedException("This implemenation does not support PSVI usage");
}
final XsltExecutable xsltExecutable = repository.loadXsltScript(URI.create(v.getResource().getLocation()));
schematronValidations.add(new Transformation(xsltExecutable, v.getResource()));
final XsltExecutable xsltExecutable = this.repository.loadXsltScript(URI.create(v.getResource().getLocation()));
this.schematronValidations.add(new Transformation(xsltExecutable, v.getResource()));
});
}
return schematronValidations;
return this.schematronValidations;
}
/**
@ -129,11 +147,28 @@ public abstract class BaseScenario {
* @see {@link ScenarioRepository#selectScenario(Document)}.
*/
public XPathSelector getSelector() {
if (xPathExecutable == null) {
final Map<String, String> namespaces = getNamespace().stream().collect(Collectors.toMap(NamespaceType::getPrefix, NamespaceType::getValue));
xPathExecutable = repository.createXPath(getMatch(), namespaces);
if (this.matchExecutable == null) {
this.matchExecutable = this.repository.createXPath(getMatch(), prepareNamespaces());
}
return xPathExecutable.load();
return this.matchExecutable.load();
}
/**
* Liefert einen neuen XPath-Selector zur Evaluierung der {@link de.kosit.validationtool.api.AcceptRecommendation}.
*
* @return neuer Selector
*/
public XPathSelector getAcceptSelector() {
if (this.acceptExecutable == null) {
System.out.println(getAcceptMatch());
System.out.println(prepareNamespaces());
this.acceptExecutable = this.repository.createXPath(getAcceptMatch(), prepareNamespaces());
}
return this.acceptExecutable.load();
}
private Map<String, String> prepareNamespaces() {
return getNamespace().stream().collect(Collectors.toMap(NamespaceType::getPrefix, NamespaceType::getValue));
}
/**
@ -143,6 +178,8 @@ public abstract class BaseScenario {
*/
public abstract String getMatch();
public abstract String getAcceptMatch();
/**
* Getter aus dem schema.
*
@ -171,17 +208,4 @@ public abstract class BaseScenario {
*/
public abstract CreateReportType getCreateReport();
/**
* Laufzeitinformationen über eine Transformation.
*/
@Getter
@Setter
@AllArgsConstructor
public class Transformation {
private XsltExecutable executable;
private ResourceType resourceType;
}
}

View file

@ -25,6 +25,7 @@ import java.util.regex.Pattern;
import lombok.Getter;
import lombok.Setter;
import de.kosit.validationtool.api.AcceptRecommendation;
import de.kosit.validationtool.api.Input;
import de.kosit.validationtool.impl.model.Result;
import de.kosit.validationtool.model.reportInput.CreateReportInput;
@ -43,25 +44,6 @@ import net.sf.saxon.s9api.XdmNode;
@FunctionalInterface
public interface CheckAction {
/**
* Ausfürhung des Prüfschrittes und Erweiterung der gesammelten Informationen.
*
* @param results die Informationssammlung
*/
void check(Bag results);
/**
* Ermittlung, ob ein Schritt u.U. ausgelassen werden kann. Die Funktion wird vor der eigentlichen Prüfaktion aufgerufen
* und kann somit eine Ausführung des Prüfschrittes verhindern. Entwickler können diese Funktion überschreiben, um den
* Prüfschritt bedingt auszuführen.
*
* @param results die bisher gesammelten Information
* @return <tt>true</tt> wenn der Schritt ausgelassen werden soll
*/
default boolean isSkipped(Bag results) {
return false;
}
/**
* Transport-Klasse für Eingabe und Ausgabe-Objekte für die einzelnen Prüfschritte.
*/
@ -80,6 +62,8 @@ public interface CheckAction {
private boolean stopped;
private AcceptRecommendation acceptStatus = AcceptRecommendation.UNDEFINED;
/** Das zu prüfende Dokument */
private Input input;
@ -89,15 +73,16 @@ public interface CheckAction {
private Result<Boolean, XMLSyntaxError> schemaValidationResult;
public Bag(Input input) {
public Bag(final Input input) {
this(input, new CreateReportInput());
}
public Bag(Input input, CreateReportInput reportInput) {
public Bag(final Input input, final CreateReportInput reportInput) {
this.input = input;
this.reportInput = reportInput;
}
/**
* Signalisiert einen vorzeitigen Stop der Vearbeitung.
*/
@ -107,7 +92,7 @@ public interface CheckAction {
/**
* Gibt den Namen des Prüflings zurück, dabei werden etwaige Pfadinformationen abgeschnitten.
*
*
* @return der Name des Prüflings
*/
public String getName() {
@ -119,4 +104,24 @@ public interface CheckAction {
return fileName;
}
}
/**
* Ausfürhung des Prüfschrittes und Erweiterung der gesammelten Informationen.
*
* @param results die Informationssammlung
*/
void check(Bag results);
/**
* Ermittlung, ob ein Schritt u.U. ausgelassen werden kann. Die Funktion wird vor der eigentlichen Prüfaktion aufgerufen
* und kann somit eine Ausführung des Prüfschrittes verhindern. Entwickler können diese Funktion überschreiben, um den
* Prüfschritt bedingt auszuführen.
*
* @param results die bisher gesammelten Information
* @return <tt>true</tt> wenn der Schritt ausgelassen werden soll
*/
default boolean isSkipped(final Bag results) {
return false;
}
}

View file

@ -0,0 +1,42 @@
package de.kosit.validationtool.impl.tasks;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.api.AcceptRecommendation;
import net.sf.saxon.s9api.XPathSelector;
/**
* Berechnet die Akzeptanz-Empfehlung gemäß konfigurierten 'acceptMatch' des aktuellen Szenarios.
*
* @author Andreas Penski
*/
@RequiredArgsConstructor
@Slf4j
public class ComputeAcceptanceAction implements CheckAction {
@Override
public void check(final Bag results) {
final String acceptMatch = results.getScenarioSelectionResult().getObject().getAcceptMatch();
if (isNotBlank(acceptMatch)) {
try {
final XPathSelector selector = results.getScenarioSelectionResult().getObject().getAcceptSelector();
selector.setContextItem(results.getReport());
results.setAcceptStatus(selector.effectiveBooleanValue() ? AcceptRecommendation.ACCEPTABLE : AcceptRecommendation.REJECT);
} catch (final Exception e) {
log.error("Fehler bei Evaluierung des Accept-Status: {}", e.getMessage(), e);
}
}
}
@Override
public boolean isSkipped(final Bag results) {
return results.getScenarioSelectionResult().isInvalid();
}
}