Skip to content

Commit

Permalink
#79 Actions generate XVRL output (WiP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed Maza committed Jun 2, 2021
1 parent 35129b9 commit 89c1485
Show file tree
Hide file tree
Showing 21 changed files with 774 additions and 81 deletions.
21 changes: 9 additions & 12 deletions src/main/java/de/kosit/validationtool/cmd/InternalCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@

package de.kosit.validationtool.cmd;

import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.fusesource.jansi.AnsiRenderer.Code;

import lombok.extern.slf4j.Slf4j;

import de.kosit.validationtool.api.Configuration;
import de.kosit.validationtool.api.Input;
import de.kosit.validationtool.api.Result;
Expand All @@ -36,8 +26,15 @@
import de.kosit.validationtool.cmd.report.Line;
import de.kosit.validationtool.impl.DefaultCheck;
import de.kosit.validationtool.impl.tasks.CheckAction;

import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.s9api.Processor;
import org.fusesource.jansi.AnsiRenderer.Code;

import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
* Simple Erweiterung der Klasse {@link DefaultCheck} um das Ergebnis der Assertion-Prüfung auszuwerten und auszugeben.
Expand Down Expand Up @@ -69,7 +66,7 @@ class InternalCheck extends DefaultCheck {
*/
@Override
public Result checkInput(final Input input) {
final CheckAction.Process process = new CheckAction.Process(input, createReport());
final CheckAction.Process process = new CheckAction.Process(input, createReport(), createXVRLXvrlReportSummary());
final Result result = runCheckInternal(process);
if (process.getAssertionResult() != null) {
this.checkAssertions += process.getAssertionResult().getObject();
Expand Down
61 changes: 47 additions & 14 deletions src/main/java/de/kosit/validationtool/impl/DefaultCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
import java.util.Map;
import java.util.stream.Collectors;

import de.kosit.validationtool.model.xvrl.Timestamp;
import de.kosit.validationtool.model.xvrl.XVRLMetadata;
import de.kosit.validationtool.model.xvrl.XVRLReportSummary;
import de.kosit.validationtool.impl.tasks.*;
import de.kosit.validationtool.impl.xvrl.XVRLUtil;
import de.kosit.validationtool.impl.xvrl.XmlErrorImpl;
import de.kosit.validationtool.model.xvrl.*;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -36,16 +37,7 @@
import de.kosit.validationtool.api.Input;
import de.kosit.validationtool.api.Result;
import de.kosit.validationtool.api.XmlError;
import de.kosit.validationtool.impl.tasks.CheckAction;
import de.kosit.validationtool.impl.tasks.CheckAction.Process;
import de.kosit.validationtool.impl.tasks.ComputeAcceptanceAction;
import de.kosit.validationtool.impl.tasks.CreateDocumentIdentificationAction;
import de.kosit.validationtool.impl.tasks.CreateReportAction;
import de.kosit.validationtool.impl.tasks.DocumentParseAction;
import de.kosit.validationtool.impl.tasks.ScenarioSelectionAction;
import de.kosit.validationtool.impl.tasks.SchemaValidationAction;
import de.kosit.validationtool.impl.tasks.SchematronValidationAction;
import de.kosit.validationtool.impl.tasks.ValidateReportInputAction;
import de.kosit.validationtool.impl.xml.ProcessorProvider;
import de.kosit.validationtool.model.reportInput.CreateReportInput;
import de.kosit.validationtool.model.reportInput.EngineType;
Expand Down Expand Up @@ -95,6 +87,7 @@ public DefaultCheck(final Processor processor, final Configuration... configurat
this.checkSteps.add(new SchemaValidationAction(processor));
this.checkSteps.add(new SchematronValidationAction(this.conversionService));
this.checkSteps.add(new ValidateReportInputAction(this.conversionService, SchemaProvider.getReportInputSchema()));
this.checkSteps.add(new ValidateXVRLReportSummaryAction(this.conversionService, SchemaProvider.getXVRLSchema()));
this.checkSteps.add(new CreateReportAction(processor, this.conversionService));
this.checkSteps.add(new ComputeAcceptanceAction());
}
Expand All @@ -112,9 +105,16 @@ protected static CreateReportInput createReport() {
protected static XVRLReportSummary createXVRLXvrlReportSummary() {
final XVRLReportSummary xvrlReportSummary = new XVRLReportSummary();
XVRLMetadata metadata = new XVRLMetadata();

Timestamp timestamp = new Timestamp();
timestamp.setValue(createTimestamp());
metadata.getTimestamps().add(timestamp);

Validator validator = new Validator();
validator.setName(EngineInformation.getName());
validator.setVersion(EngineInformation.getVersion());
metadata.getValidators().add(validator);

xvrlReportSummary.setMetadata(metadata);
return xvrlReportSummary;
}
Expand All @@ -125,7 +125,7 @@ protected boolean isSuccessful(final Map<String, Result> results) {

@Override
public Result checkInput(final Input input) {
final Process checkProcess = new Process(input, createReport());
final Process checkProcess = new Process(input, createReport(), createXVRLXvrlReportSummary());
return runCheckInternal(checkProcess);
}

Expand All @@ -141,7 +141,9 @@ protected Result runCheckInternal(final Process checkProcess) {
}
checkProcess.setFinished(true);
log.info("Finished check of {} in {}ms\n", checkProcess.getInput().getName(), System.currentTimeMillis() - started);
return createResult(checkProcess);
Result result = createResult(checkProcess);
Result xvrlResult = createXVRLResult(checkProcess);
return result;
}

private Result createResult(final Process t) {
Expand All @@ -157,9 +159,40 @@ private Result createResult(final Process t) {
return result;
}

private Result createXVRLResult(final Process process) {

final XVRLResult xvrlResult = new XVRLResult(process.getXvrlFinalReport(), process.getAcceptStatus(),
new HtmlExtractor(this.processor));

XVRLReport parserReport = process.getReport(DocumentParseAction.class);
xvrlResult.setWellformed("true".equals(parserReport.getDigest().getValid()));

xvrlResult.setReportSummary(process.getXvrlReportSummary());
xvrlResult.setSchemaViolations(convertErrorsFromXVRLDetections(XVRLUtil.getAllErrorDetections(process.getXvrlReportSummary())));

xvrlResult.setProcessingSuccessful(!process.isStopped() && process.isFinished());

// TODO MM add SchematronResult
// XVRLReport schematronValidationReport = process.getReport(SchematronValidationAction.class);
// xvrlResult.setSchematronResult(t.getReportInput().getValidationResultsSchematron().stream()
// .filter(e -> e.getResults() != null).map(e ->
// e.getResults().getSchematronOutput()).collect(Collectors.toList()));

return xvrlResult;
}

private static List<XmlError> convertErrors(final Collection<XMLSyntaxError> errors) {
// noinspection unchecked
return (List<XmlError>) (List<?>) errors;
}

private static List<XmlError> convertErrorsFromXVRLDetections(final Collection<XVRLDetection> errors) {
List<XmlError> xmlErrorList = new ArrayList<>();
errors.forEach(xvrlDetection -> {
XmlError xmlError = new XmlErrorImpl(xvrlDetection);
xmlErrorList.add(xmlError);
});
return xmlErrorList;
}

}
16 changes: 16 additions & 0 deletions src/main/java/de/kosit/validationtool/impl/SchemaProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class SchemaProvider {

private static Schema reportInputSchema;

private static Schema xvrlSchema;

/**
* Liefert das definierte Schema für die Validierung des [@link CreateReportInput}
*
Expand All @@ -51,6 +53,20 @@ public static Schema getReportInputSchema() {
return reportInputSchema;
}

/**
* Liefert das definierte Schema für die Validierung des [@link CreateReportInput}
*
* @return ReportInput-Schema
*/
public static Schema getXVRLSchema() {
if (xvrlSchema == null) {
final SchemaFactory sf = ResolvingMode.STRICT_RELATIVE.getStrategy().createSchemaFactory();
final Source source = resolve(SchemaProvider.class.getResource("/xsd/xvrl.xsd"));
xvrlSchema = createSchema(sf, new Source[] { source }, new ClassPathResourceResolver("/xsd"));
}
return xvrlSchema;
}

private static Schema createSchema(final SchemaFactory sf, final Source[] schemaSources, final LSResourceResolver resourceResolver) {
try {
sf.setResourceResolver(resourceResolver);
Expand Down
158 changes: 158 additions & 0 deletions src/main/java/de/kosit/validationtool/impl/XVRLResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright 2017-2021 Koordinierungsstelle für IT-Standards (KoSIT)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.kosit.validationtool.impl;

import de.kosit.validationtool.api.AcceptRecommendation;
import de.kosit.validationtool.api.Result;
import de.kosit.validationtool.api.XmlError;
import de.kosit.validationtool.impl.HtmlExtractor;
import de.kosit.validationtool.impl.xvrl.XVRLUtil;
import de.kosit.validationtool.model.reportInput.CreateReportInput;
import de.kosit.validationtool.model.xvrl.XVRLReportSummary;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import net.sf.saxon.dom.NodeOverNodeInfo;
import net.sf.saxon.s9api.XdmNode;
import org.oclc.purl.dsdl.svrl.FailedAssert;
import org.oclc.purl.dsdl.svrl.SchematronOutput;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class XVRLResult implements Result {

/** Der generierte Report. */
@Getter
private final XdmNode report;

/** Die vom Validator erstelle interne Berichts-'Vorstufe' */
@Getter
@Setter(AccessLevel.PACKAGE)
private XVRLReportSummary reportSummary;

/** Das evaluierte Ergebnis. */
@Getter
private final AcceptRecommendation acceptRecommendation;

private final HtmlExtractor htmlExtraction;

@Setter(AccessLevel.PACKAGE)
@Getter
private List<XmlError> schemaViolations;

@Getter
@Setter(AccessLevel.PACKAGE)
private List<SchematronOutput> schematronResult;

@Getter
@Setter
private boolean processingSuccessful;

@Getter
@Setter
private boolean wellformed;

public XVRLResult(final XdmNode report, final AcceptRecommendation recommendation, final HtmlExtractor htmlExtractor) {
this.report = report;
this.acceptRecommendation = recommendation;
this.htmlExtraction = htmlExtractor;
}

@Override
public List<String> getProcessingErrors() {
return XVRLUtil.getAllErrors(reportSummary);
}

/**
* Gibt den Report als W3C-{@link Document} zurück.
*
* @return der Report
*/
@Override
public Document getReportDocument() {
return (Document) NodeOverNodeInfo.wrap(getReport().getUnderlyingNode());
}

/**
* Schnellzugriff auf die Empfehlung zur Weiterverarbeitung des Dokuments.
*
* @return true wenn {@link AcceptRecommendation#ACCEPTABLE}
*/
@Override
public boolean isAcceptable() {
return isProcessingSuccessful() && AcceptRecommendation.ACCEPTABLE.equals(this.acceptRecommendation);
}

@Override
public boolean isSchemaValid() {
return getSchemaViolations() != null && getSchemaViolations().isEmpty();
}

/**
* Extrahiert evtl. im Report vorhandene HTML-Fragmente als String.
*
* @return Liste mit HTML Strings.
*/
public List<String> extractHtmlAsString() {
return this.htmlExtraction.extractAsString(getReport());
}

/**
* Extrahiert evtl. im Report vorhandene HTML-Fragmente.
*
* @return Liste mit HTML Nodes.
*/
public List<XdmNode> extractHtml() {
return this.htmlExtraction.extract(getReport());
}

/**
* Extrahiert evtl. im Report vorhandene HTML-Fragmente als {@link Element}.
*
* @return Liste mit HTML Elementen.
*/
public List<Element> extractHtmlAsElement() {
return this.htmlExtraction.extractAsElement(getReport());
}

/**
* Gibt alle Schematron-Ergebnisse vom Typ {@link FailedAssert} zurück.
*
* @return die {@link FailedAssert}
*/
@Override
public List<FailedAssert> getFailedAsserts() {
return getSchematronResult() != null
? getSchematronResult().stream().flatMap(e -> e.getActivePatternAndFiredRuleAndFailedAssert().stream())
.filter(FailedAssert.class::isInstance).map(FailedAssert.class::cast).collect(Collectors.toList())
: Collections.emptyList();
}

private boolean isSchematronEvaluated() {
return getSchematronResult() != null
&& getSchematronResult().stream().noneMatch(e -> e.getActivePatternAndFiredRuleAndFailedAssert().isEmpty());
}

@Override
public boolean isSchematronValid() {
return isSchematronEvaluated() && getFailedAsserts().isEmpty();
}
}
Loading

0 comments on commit 89c1485

Please sign in to comment.