Replace ' with " in xml attributes
Some automatically changes by the IDE: - remove unused imports - add final where possible
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package org.mustangproject.validator;
|
package org.mustangproject.validator;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -8,7 +7,6 @@ import java.io.PrintWriter;
|
|||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@@ -16,14 +14,9 @@ import java.util.EnumSet;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.xml.XMLConstants;
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
import javax.xml.transform.Source;
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
|
||||||
import javax.xml.validation.Schema;
|
|
||||||
import javax.xml.validation.SchemaFactory;
|
|
||||||
import javax.xml.xpath.XPath;
|
import javax.xml.xpath.XPath;
|
||||||
import javax.xml.xpath.XPathConstants;
|
import javax.xml.xpath.XPathConstants;
|
||||||
import javax.xml.xpath.XPathExpression;
|
import javax.xml.xpath.XPathExpression;
|
||||||
@@ -75,46 +68,47 @@ public class PDFValidator extends Validator {
|
|||||||
return Arrays.asList(arr).contains(targetValue);
|
return Arrays.asList(arr).contains(targetValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validate() throws IrrecoverableValidationError {
|
@Override
|
||||||
|
public void validate() throws IrrecoverableValidationError {
|
||||||
|
|
||||||
zfXML = null;
|
zfXML = null;
|
||||||
File file = new File(pdfFilename);
|
final File file = new File(pdfFilename);
|
||||||
// file existence must have been checked before
|
// file existence must have been checked before
|
||||||
BigFileSearcher searcher = new BigFileSearcher();
|
final BigFileSearcher searcher = new BigFileSearcher();
|
||||||
|
|
||||||
byte[] pdfSignature = { '%', 'P', 'D', 'F' };
|
final byte[] pdfSignature = { '%', 'P', 'D', 'F' };
|
||||||
if (searcher.indexOf(file, pdfSignature) != 0) {
|
if (searcher.indexOf(file, pdfSignature) != 0) {
|
||||||
context.addResultItem(
|
context.addResultItem(
|
||||||
new ValidationResultItem(ESeverity.fatal, "Not a PDF file "+pdfFilename).setSection(20).setPart(EPart.pdf));
|
new ValidationResultItem(ESeverity.fatal, "Not a PDF file "+pdfFilename).setSection(20).setPart(EPart.pdf));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long startPDFTime = Calendar.getInstance().getTimeInMillis();
|
final long startPDFTime = Calendar.getInstance().getTimeInMillis();
|
||||||
|
|
||||||
// Step 1 Validate PDF
|
// Step 1 Validate PDF
|
||||||
|
|
||||||
VeraGreenfieldFoundryProvider.initialise();
|
VeraGreenfieldFoundryProvider.initialise();
|
||||||
// Default validator config
|
// Default validator config
|
||||||
ValidatorConfig validatorConfig = ValidatorFactory.defaultConfig();
|
final ValidatorConfig validatorConfig = ValidatorFactory.defaultConfig();
|
||||||
// Default features config
|
// Default features config
|
||||||
FeatureExtractorConfig featureConfig = FeatureFactory.defaultConfig();
|
final FeatureExtractorConfig featureConfig = FeatureFactory.defaultConfig();
|
||||||
// Default plugins config
|
// Default plugins config
|
||||||
PluginsCollectionConfig pluginsConfig = PluginsCollectionConfig.defaultConfig();
|
final PluginsCollectionConfig pluginsConfig = PluginsCollectionConfig.defaultConfig();
|
||||||
// Default fixer config
|
// Default fixer config
|
||||||
MetadataFixerConfig fixerConfig = FixerFactory.defaultConfig();
|
final MetadataFixerConfig fixerConfig = FixerFactory.defaultConfig();
|
||||||
// Tasks configuring
|
// Tasks configuring
|
||||||
EnumSet tasks = EnumSet.noneOf(TaskType.class);
|
final EnumSet tasks = EnumSet.noneOf(TaskType.class);
|
||||||
tasks.add(TaskType.VALIDATE);
|
tasks.add(TaskType.VALIDATE);
|
||||||
// tasks.add(TaskType.EXTRACT_FEATURES);
|
// tasks.add(TaskType.EXTRACT_FEATURES);
|
||||||
// tasks.add(TaskType.FIX_METADATA);
|
// tasks.add(TaskType.FIX_METADATA);
|
||||||
// Creating processor config
|
// Creating processor config
|
||||||
ProcessorConfig processorConfig = ProcessorFactory.fromValues(validatorConfig, featureConfig, pluginsConfig,
|
final ProcessorConfig processorConfig = ProcessorFactory.fromValues(validatorConfig, featureConfig, pluginsConfig,
|
||||||
fixerConfig, tasks);
|
fixerConfig, tasks);
|
||||||
// Creating processor and output stream.
|
// Creating processor and output stream.
|
||||||
ByteArrayOutputStream reportStream = new ByteArrayOutputStream();
|
final ByteArrayOutputStream reportStream = new ByteArrayOutputStream();
|
||||||
try (BatchProcessor processor = ProcessorFactory.fileBatchProcessor(processorConfig)) {
|
try (BatchProcessor processor = ProcessorFactory.fileBatchProcessor(processorConfig)) {
|
||||||
// Generating list of files for processing
|
// Generating list of files for processing
|
||||||
List<File> files = new ArrayList<>();
|
final List<File> files = new ArrayList<>();
|
||||||
files.add(new File(pdfFilename));
|
files.add(new File(pdfFilename));
|
||||||
// starting the processor
|
// starting the processor
|
||||||
processor.process(files, ProcessorFactory.getHandler(FormatOption.MRR, true, reportStream, 100,
|
processor.process(files, ProcessorFactory.getHandler(FormatOption.MRR, true, reportStream, 100,
|
||||||
@@ -122,25 +116,25 @@ public class PDFValidator extends Validator {
|
|||||||
|
|
||||||
pdfReport = reportStream.toString("utf-8").replaceAll("<\\?xml version=\"1\\.0\" encoding=\"utf-8\"\\?>",
|
pdfReport = reportStream.toString("utf-8").replaceAll("<\\?xml version=\"1\\.0\" encoding=\"utf-8\"\\?>",
|
||||||
"");
|
"");
|
||||||
} catch (VeraPDFException e) {
|
} catch (final VeraPDFException e) {
|
||||||
ValidationResultItem vri = new ValidationResultItem(ESeverity.exception, e.getMessage()).setSection(6)
|
final ValidationResultItem vri = new ValidationResultItem(ESeverity.exception, e.getMessage()).setSection(6)
|
||||||
.setPart(EPart.pdf);
|
.setPart(EPart.pdf);
|
||||||
StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
PrintWriter pw = new PrintWriter(sw);
|
final PrintWriter pw = new PrintWriter(sw);
|
||||||
e.printStackTrace(pw);
|
e.printStackTrace(pw);
|
||||||
vri.setStacktrace(sw.toString());
|
vri.setStacktrace(sw.toString());
|
||||||
context.addResultItem(vri);
|
context.addResultItem(vri);
|
||||||
} catch (IOException excep) {
|
} catch (final IOException excep) {
|
||||||
context.addResultItem(new ValidationResultItem(ESeverity.exception, excep.getMessage()).setSection(7)
|
context.addResultItem(new ValidationResultItem(ESeverity.exception, excep.getMessage()).setSection(7)
|
||||||
.setPart(EPart.pdf).setStacktrace(excep.getStackTrace().toString()));
|
.setPart(EPart.pdf).setStacktrace(excep.getStackTrace().toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// step 2 validate XMP
|
// step 2 validate XMP
|
||||||
ZUGFeRDImporter zi = new ZUGFeRDImporter(pdfFilename);
|
final ZUGFeRDImporter zi = new ZUGFeRDImporter(pdfFilename);
|
||||||
String xmp = zi.getXMP();
|
final String xmp = zi.getXMP();
|
||||||
|
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
Document docXMP;
|
final Document docXMP;
|
||||||
|
|
||||||
if (xmp.length() == 0) {
|
if (xmp.length() == 0) {
|
||||||
context.addResultItem(new ValidationResultItem(ESeverity.error, "Invalid XMP Metadata not found")
|
context.addResultItem(new ValidationResultItem(ESeverity.error, "Invalid XMP Metadata not found")
|
||||||
@@ -153,15 +147,15 @@ public class PDFValidator extends Validator {
|
|||||||
* <zf:Version>1.0</zf:Version>
|
* <zf:Version>1.0</zf:Version>
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
final DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
InputSource is = new InputSource(new StringReader(xmp));
|
final InputSource is = new InputSource(new StringReader(xmp));
|
||||||
docXMP = builder.parse(is);
|
docXMP = builder.parse(is);
|
||||||
|
|
||||||
XPathFactory xpathFactory = XPathFactory.newInstance();
|
final XPathFactory xpathFactory = XPathFactory.newInstance();
|
||||||
|
|
||||||
// Create XPath object XPath xpath = xpathFactory.newXPath(); XPathExpression
|
// Create XPath object XPath xpath = xpathFactory.newXPath(); XPathExpression
|
||||||
|
|
||||||
XPath xpath = xpathFactory.newXPath();
|
final XPath xpath = xpathFactory.newXPath();
|
||||||
// xpath.compile("//*[local-name()=\"GuidelineSpecifiedDocumentContextParameter\"]/[local-name()=\"ID\"]");
|
// xpath.compile("//*[local-name()=\"GuidelineSpecifiedDocumentContextParameter\"]/[local-name()=\"ID\"]");
|
||||||
// evaluate expression result on XML document ndList = (NodeList)
|
// evaluate expression result on XML document ndList = (NodeList)
|
||||||
|
|
||||||
@@ -179,7 +173,7 @@ public class PDFValidator extends Validator {
|
|||||||
boolean conformanceLevelValid=false;
|
boolean conformanceLevelValid=false;
|
||||||
for (int i = 0; i < nodes.getLength(); i++) {
|
for (int i = 0; i < nodes.getLength(); i++) {
|
||||||
|
|
||||||
String[] valueArray = { "BASIC WL", "BASIC", "MINIMUM", "EN 16931", "COMFORT", "CIUS", "EXTENDED", "XRECHNUNG" };
|
final String[] valueArray = { "BASIC WL", "BASIC", "MINIMUM", "EN 16931", "COMFORT", "CIUS", "EXTENDED", "XRECHNUNG" };
|
||||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||||
conformanceLevelValid=true;
|
conformanceLevelValid=true;
|
||||||
}
|
}
|
||||||
@@ -220,7 +214,7 @@ public class PDFValidator extends Validator {
|
|||||||
}
|
}
|
||||||
boolean documentFilenameValid=false;
|
boolean documentFilenameValid=false;
|
||||||
for (int i = 0; i < nodes.getLength(); i++) {
|
for (int i = 0; i < nodes.getLength(); i++) {
|
||||||
String[] valueArray = { "factur-x.xml", "ZUGFeRD-invoice.xml", "zugferd-invoice.xml", "xrechnung.xml" };
|
final String[] valueArray = { "factur-x.xml", "ZUGFeRD-invoice.xml", "zugferd-invoice.xml", "xrechnung.xml" };
|
||||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||||
documentFilenameValid=true;
|
documentFilenameValid=true;
|
||||||
}
|
}
|
||||||
@@ -246,7 +240,7 @@ public class PDFValidator extends Validator {
|
|||||||
|
|
||||||
boolean versionValid=false;
|
boolean versionValid=false;
|
||||||
for (int i = 0; i < nodes.getLength(); i++) {
|
for (int i = 0; i < nodes.getLength(); i++) {
|
||||||
String[] valueArray = { "1.0", "2p0", "1.2", "2.0" }; //1.2 and 2.0 are for xrechnung 1.2, 2p0 can be ZF 2.0, 2.1, 2.1.1
|
final String[] valueArray = { "1.0", "2p0", "1.2", "2.0" }; //1.2 and 2.0 are for xrechnung 1.2, 2p0 can be ZF 2.0, 2.1, 2.1.1
|
||||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||||
versionValid=true;
|
versionValid=true;
|
||||||
} // e.g. 1.0
|
} // e.g. 1.0
|
||||||
@@ -258,26 +252,26 @@ public class PDFValidator extends Validator {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SAXException e) {
|
} catch (final SAXException e) {
|
||||||
LOGGER.error(e.getMessage(), e);
|
LOGGER.error(e.getMessage(), e);
|
||||||
} catch (IOException e) {
|
} catch (final IOException e) {
|
||||||
LOGGER.error(e.getMessage(), e);
|
LOGGER.error(e.getMessage(), e);
|
||||||
} catch (ParserConfigurationException e) {
|
} catch (final ParserConfigurationException e) {
|
||||||
LOGGER.error(e.getMessage(), e);
|
LOGGER.error(e.getMessage(), e);
|
||||||
} catch (XPathExpressionException e) {
|
} catch (final XPathExpressionException e) {
|
||||||
LOGGER.error(e.getMessage(), e);
|
LOGGER.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
zfXML = zi.getUTF8();
|
zfXML = zi.getUTF8();
|
||||||
|
|
||||||
// step 3 find signatures
|
// step 3 find signatures
|
||||||
try {
|
try {
|
||||||
byte[] symtraxSignature = "Symtrax".getBytes("UTF-8");
|
final byte[] symtraxSignature = "Symtrax".getBytes("UTF-8");
|
||||||
byte[] mustangSignature = "via mustangproject".getBytes("UTF-8");
|
final byte[] mustangSignature = "via mustangproject".getBytes("UTF-8");
|
||||||
byte[] facturxpythonSignature = "by Alexis de Lattre".getBytes("UTF-8");
|
final byte[] facturxpythonSignature = "by Alexis de Lattre".getBytes("UTF-8");
|
||||||
byte[] intarsysSignature = "intarsys ".getBytes("UTF-8");
|
final byte[] intarsysSignature = "intarsys ".getBytes("UTF-8");
|
||||||
byte[] konikSignature = "Konik".getBytes("UTF-8");
|
final byte[] konikSignature = "Konik".getBytes("UTF-8");
|
||||||
byte[] pdfMachineSignature = "pdfMachine from Broadgun Software".getBytes("UTF-8");
|
final byte[] pdfMachineSignature = "pdfMachine from Broadgun Software".getBytes("UTF-8");
|
||||||
byte[] ghostscriptSignature = "%%Invocation:".getBytes("UTF-8");
|
final byte[] ghostscriptSignature = "%%Invocation:".getBytes("UTF-8");
|
||||||
|
|
||||||
if (searcher.indexOf(file, symtraxSignature) != -1) {
|
if (searcher.indexOf(file, symtraxSignature) != -1) {
|
||||||
Signature = "Symtrax";
|
Signature = "Symtrax";
|
||||||
@@ -297,13 +291,13 @@ public class PDFValidator extends Validator {
|
|||||||
|
|
||||||
context.setSignature(Signature);
|
context.setSignature(Signature);
|
||||||
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (final UnsupportedEncodingException e) {
|
||||||
LOGGER.error(e.getMessage(), e);
|
LOGGER.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// step 4:validate additional data
|
// step 4:validate additional data
|
||||||
HashMap<String, byte[]> additionalData=zi.getAdditionalData();
|
final HashMap<String, byte[]> additionalData=zi.getAdditionalData();
|
||||||
for (String filename : additionalData.keySet()) {
|
for (final String filename : additionalData.keySet()) {
|
||||||
// validating xml in byte[] additionalData.get(filename)
|
// validating xml in byte[] additionalData.get(filename)
|
||||||
LOGGER.info("validating additionalData " + filename);
|
LOGGER.info("validating additionalData " + filename);
|
||||||
validateSchema(additionalData.get(filename), "ad/basic/additional_data_base_schema.xsd", 2, EPart.pdf);
|
validateSchema(additionalData.get(filename), "ad/basic/additional_data_base_schema.xsd", 2, EPart.pdf);
|
||||||
@@ -312,7 +306,7 @@ public class PDFValidator extends Validator {
|
|||||||
|
|
||||||
//end
|
//end
|
||||||
|
|
||||||
long endTime = Calendar.getInstance().getTimeInMillis();
|
final long endTime = Calendar.getInstance().getTimeInMillis();
|
||||||
if (!pdfReport.contains("validationReports compliant=\"1\"")) {
|
if (!pdfReport.contains("validationReports compliant=\"1\"")) {
|
||||||
context.setInvalid();
|
context.setInvalid();
|
||||||
}
|
}
|
||||||
@@ -323,7 +317,7 @@ public class PDFValidator extends Validator {
|
|||||||
}
|
}
|
||||||
context.addCustomXML(pdfReport + "<info><signature>"
|
context.addCustomXML(pdfReport + "<info><signature>"
|
||||||
+ ((context.getSignature() != null) ? context.getSignature() : "unknown")
|
+ ((context.getSignature() != null) ? context.getSignature() : "unknown")
|
||||||
+ "</signature><duration unit='ms'>" + (endTime - startPDFTime) + "</duration></info>");
|
+ "</signature><duration unit=\"ms\">" + (endTime - startPDFTime) + "</duration></info>");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public class ValidationContext {
|
|||||||
|
|
||||||
public ValidationContext(Logger log) {
|
public ValidationContext(Logger log) {
|
||||||
logger = log;
|
logger = log;
|
||||||
results = new Vector<ValidationResultItem>();
|
results = new Vector<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addResultItem(ValidationResultItem vr) throws IrrecoverableValidationError {
|
public void addResultItem(ValidationResultItem vr) throws IrrecoverableValidationError {
|
||||||
@@ -105,14 +105,14 @@ public class ValidationContext {
|
|||||||
res += "<messages>";
|
res += "<messages>";
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ValidationResultItem validationResultItem : results) {
|
for (final ValidationResultItem validationResultItem : results) {
|
||||||
// xml and pdf are handled in their respective sections
|
// xml and pdf are handled in their respective sections
|
||||||
res += validationResultItem.getXMLOnce() + "\n";
|
res += validationResultItem.getXMLOnce() + "\n";
|
||||||
}
|
}
|
||||||
if (results.size() > 0) {
|
if (results.size() > 0) {
|
||||||
res += "</messages>";
|
res += "</messages>";
|
||||||
}
|
}
|
||||||
res += "<summary status='" + (isValid ? "valid" : "invalid") + "'/>";
|
res += "<summary status=\"" + (isValid ? "valid" : "invalid") + "\"/>";
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,9 +121,9 @@ public class ValidationContext {
|
|||||||
* @return the unique error types as comma separated string
|
* @return the unique error types as comma separated string
|
||||||
*/
|
*/
|
||||||
public String getCSVResult() {
|
public String getCSVResult() {
|
||||||
ArrayList<String> errorcodes = new ArrayList<String>();
|
final ArrayList<String> errorcodes = new ArrayList<>();
|
||||||
for (ValidationResultItem validationResultItem : results) {
|
for (final ValidationResultItem validationResultItem : results) {
|
||||||
String errorCodeStr=Integer.toString(validationResultItem.getSection());
|
final String errorCodeStr=Integer.toString(validationResultItem.getSection());
|
||||||
errorcodes.add(errorCodeStr);
|
errorcodes.add(errorCodeStr);
|
||||||
}
|
}
|
||||||
return String.join(",", errorcodes);
|
return String.join(",", errorcodes);
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package org.mustangproject.validator;
|
package org.mustangproject.validator;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.print.attribute.standard.Severity;
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.transform.*;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
import javax.xml.transform.stream.StreamSource;
|
||||||
import javax.xml.xpath.XPath;
|
import javax.xml.xpath.XPath;
|
||||||
import javax.xml.xpath.XPathConstants;
|
import javax.xml.xpath.XPathConstants;
|
||||||
@@ -25,13 +25,14 @@ import org.w3c.dom.Document;
|
|||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
|
|
||||||
|
import com.helger.schematron.ISchematronResource;
|
||||||
|
import com.helger.schematron.svrl.SVRLHelper;
|
||||||
import com.helger.schematron.svrl.jaxb.FailedAssert;
|
import com.helger.schematron.svrl.jaxb.FailedAssert;
|
||||||
import com.helger.schematron.svrl.jaxb.FiredRule;
|
import com.helger.schematron.svrl.jaxb.FiredRule;
|
||||||
import com.helger.schematron.svrl.jaxb.SchematronOutputType;
|
import com.helger.schematron.svrl.jaxb.SchematronOutputType;
|
||||||
import com.helger.schematron.ISchematronResource;
|
|
||||||
import com.helger.schematron.xslt.SchematronResourceXSLT;
|
import com.helger.schematron.xslt.SchematronResourceXSLT;
|
||||||
import com.helger.schematron.svrl.SVRLHelper;
|
|
||||||
import org.xml.sax.InputSource;
|
|
||||||
|
|
||||||
public class XMLValidator extends Validator {
|
public class XMLValidator extends Validator {
|
||||||
|
|
||||||
@@ -57,18 +58,19 @@ public class XMLValidator extends Validator {
|
|||||||
* @param name
|
* @param name
|
||||||
* @throws IrrecoverableValidationError
|
* @throws IrrecoverableValidationError
|
||||||
*/
|
*/
|
||||||
public void setFilename(String name) throws IrrecoverableValidationError { // from XML Filename
|
@Override
|
||||||
|
public void setFilename(String name) throws IrrecoverableValidationError { // from XML Filename
|
||||||
filename = name;
|
filename = name;
|
||||||
// file existence must have been checked before
|
// file existence must have been checked before
|
||||||
|
|
||||||
try {
|
try {
|
||||||
zfXML = new String(XMLTools.removeBOM(Files.readAllBytes(Paths.get(name))), StandardCharsets.UTF_8);
|
zfXML = new String(XMLTools.removeBOM(Files.readAllBytes(Paths.get(name))), StandardCharsets.UTF_8);
|
||||||
} catch (IOException e) {
|
} catch (final IOException e) {
|
||||||
|
|
||||||
ValidationResultItem vri = new ValidationResultItem(ESeverity.exception, e.getMessage()).setSection(9)
|
final ValidationResultItem vri = new ValidationResultItem(ESeverity.exception, e.getMessage()).setSection(9)
|
||||||
.setPart(EPart.fx);
|
.setPart(EPart.fx);
|
||||||
StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
PrintWriter pw = new PrintWriter(sw);
|
final PrintWriter pw = new PrintWriter(sw);
|
||||||
e.printStackTrace(pw);
|
e.printStackTrace(pw);
|
||||||
vri.setStacktrace(sw.toString());
|
vri.setStacktrace(sw.toString());
|
||||||
context.addResultItem(vri);
|
context.addResultItem(vri);
|
||||||
@@ -108,13 +110,13 @@ public class XMLValidator extends Validator {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void validate() throws IrrecoverableValidationError {
|
public void validate() throws IrrecoverableValidationError {
|
||||||
long startXMLTime = Calendar.getInstance().getTimeInMillis();
|
final long startXMLTime = Calendar.getInstance().getTimeInMillis();
|
||||||
firedRules = 0;
|
firedRules = 0;
|
||||||
failedRules = 0;
|
failedRules = 0;
|
||||||
|
|
||||||
|
|
||||||
if (zfXML.isEmpty()) {
|
if (zfXML.isEmpty()) {
|
||||||
ValidationResultItem res = new ValidationResultItem(ESeverity.exception,
|
final ValidationResultItem res = new ValidationResultItem(ESeverity.exception,
|
||||||
"XML data not found in " + filename
|
"XML data not found in " + filename
|
||||||
+ ": did you specify a pdf or xml file and does the xml file contain an embedded XML file?")
|
+ ": did you specify a pdf or xml file and does the xml file contain an embedded XML file?")
|
||||||
.setSection(3);
|
.setSection(3);
|
||||||
@@ -139,33 +141,33 @@ public class XMLValidator extends Validator {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||||
dbf.setNamespaceAware(true); // otherwise we can not act namespace independently, i.e. use
|
dbf.setNamespaceAware(true); // otherwise we can not act namespace independently, i.e. use
|
||||||
// document.getElementsByTagNameNS("*",...
|
// document.getElementsByTagNameNS("*",...
|
||||||
|
|
||||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
final DocumentBuilder db = dbf.newDocumentBuilder();
|
||||||
InputSource is = new InputSource(new StringReader(zfXML));
|
final InputSource is = new InputSource(new StringReader(zfXML));
|
||||||
Document doc = db.parse(is);
|
final Document doc = db.parse(is);
|
||||||
|
|
||||||
Element root = doc.getDocumentElement();
|
final Element root = doc.getDocumentElement();
|
||||||
|
|
||||||
NodeList ndList;
|
final NodeList ndList;
|
||||||
|
|
||||||
// rootNode = document.getDocumentElement();
|
// rootNode = document.getDocumentElement();
|
||||||
// ApplicableSupplyChainTradeSettlement
|
// ApplicableSupplyChainTradeSettlement
|
||||||
|
|
||||||
// Create XPathFactory object
|
// Create XPathFactory object
|
||||||
XPathFactory xpathFactory = XPathFactory.newInstance();
|
final XPathFactory xpathFactory = XPathFactory.newInstance();
|
||||||
|
|
||||||
// Create XPath object
|
// Create XPath object
|
||||||
XPath xpath = xpathFactory.newXPath();
|
final XPath xpath = xpathFactory.newXPath();
|
||||||
XPathExpression expr = xpath.compile(
|
final XPathExpression expr = xpath.compile(
|
||||||
"//*[local-name()=\"GuidelineSpecifiedDocumentContextParameter\"]/*[local-name()=\"ID\"]/text()");
|
"//*[local-name()=\"GuidelineSpecifiedDocumentContextParameter\"]/*[local-name()=\"ID\"]/text()");
|
||||||
// evaluate expression result on XML document
|
// evaluate expression result on XML document
|
||||||
ndList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
|
ndList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
|
||||||
|
|
||||||
for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) {
|
for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) {
|
||||||
Node booking = ndList.item(bookingIndex);
|
final Node booking = ndList.item(bookingIndex);
|
||||||
// if there is a attribute in the tag number:value
|
// if there is a attribute in the tag number:value
|
||||||
// urn:ferd:CrossIndustryDocument:invoice:1p0:extended
|
// urn:ferd:CrossIndustryDocument:invoice:1p0:extended
|
||||||
// setForeignReference(booking.getTextContent());
|
// setForeignReference(booking.getTextContent());
|
||||||
@@ -301,24 +303,24 @@ public class XMLValidator extends Validator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} catch (IrrecoverableValidationError er) {
|
} catch (final IrrecoverableValidationError er) {
|
||||||
throw er;
|
throw er;
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
ValidationResultItem vri = new ValidationResultItem(ESeverity.exception, e.getMessage()).setSection(22)
|
final ValidationResultItem vri = new ValidationResultItem(ESeverity.exception, e.getMessage()).setSection(22)
|
||||||
.setPart(EPart.fx);
|
.setPart(EPart.fx);
|
||||||
StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
PrintWriter pw = new PrintWriter(sw);
|
final PrintWriter pw = new PrintWriter(sw);
|
||||||
e.printStackTrace(pw);
|
e.printStackTrace(pw);
|
||||||
vri.setStacktrace(sw.toString());
|
vri.setStacktrace(sw.toString());
|
||||||
context.addResultItem(vri);
|
context.addResultItem(vri);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
long endTime = Calendar.getInstance().getTimeInMillis();
|
final long endTime = Calendar.getInstance().getTimeInMillis();
|
||||||
|
|
||||||
context.addCustomXML("<info><version>" + ((context.getVersion() != null) ? context.getVersion() : "invalid")
|
context.addCustomXML("<info><version>" + ((context.getVersion() != null) ? context.getVersion() : "invalid")
|
||||||
+ "</version><profile>" + ((context.getProfile() != null) ? context.getProfile() : "invalid") +
|
+ "</version><profile>" + ((context.getProfile() != null) ? context.getProfile() : "invalid") +
|
||||||
"</profile><validator version=\"" + XMLValidator.class.getPackage().getImplementationVersion() + "\"></validator><rules><fired>" + firedRules + "</fired><failed>" + failedRules + "</failed></rules>" + "<duration unit='ms'>" + (endTime - startXMLTime) + "</duration></info>");
|
"</profile><validator version=\"" + XMLValidator.class.getPackage().getImplementationVersion() + "\"></validator><rules><fired>" + firedRules + "</fired><failed>" + failedRules + "</failed></rules>" + "<duration unit=\"ms\">" + (endTime - startXMLTime) + "</duration></info>");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,20 +354,20 @@ public class XMLValidator extends Validator {
|
|||||||
throw new IllegalArgumentException(xsltFilename + " is invalid Schematron!");
|
throw new IllegalArgumentException(xsltFilename + " is invalid Schematron!");
|
||||||
}
|
}
|
||||||
|
|
||||||
SchematronOutputType sout;
|
final SchematronOutputType sout;
|
||||||
try {
|
try {
|
||||||
sout = aResSCH
|
sout = aResSCH
|
||||||
.applySchematronValidationToSVRL(new StreamSource(new StringReader(xml)));
|
.applySchematronValidationToSVRL(new StreamSource(new StringReader(xml)));
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
throw new IrrecoverableValidationError(e.getMessage());
|
throw new IrrecoverableValidationError(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Object> failedAsserts = sout.getActivePatternAndFiredRuleAndFailedAssert();
|
final List<Object> failedAsserts = sout.getActivePatternAndFiredRuleAndFailedAssert();
|
||||||
if (failedAsserts.size() > 0) {
|
if (failedAsserts.size() > 0) {
|
||||||
for (Object object : failedAsserts) {
|
for (final Object object : failedAsserts) {
|
||||||
if (object instanceof FailedAssert) {
|
if (object instanceof FailedAssert) {
|
||||||
|
|
||||||
FailedAssert failedAssert = (FailedAssert) object;
|
final FailedAssert failedAssert = (FailedAssert) object;
|
||||||
LOGGER.info("FailedAssert ", failedAssert);
|
LOGGER.info("FailedAssert ", failedAssert);
|
||||||
|
|
||||||
context.addResultItem(new ValidationResultItem(severity, SVRLHelper.getAsString(failedAssert.getText()))
|
context.addResultItem(new ValidationResultItem(severity, SVRLHelper.getAsString(failedAssert.getText()))
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ZUGFeRDValidator.class.getCanonicalName()); // log
|
private static final Logger LOGGER = LoggerFactory.getLogger(ZUGFeRDValidator.class.getCanonicalName()); // log
|
||||||
|
|
||||||
public void testPDFValidation() {
|
public void testPDFValidation() {
|
||||||
ValidationContext vc = new ValidationContext(null);
|
final ValidationContext vc = new ValidationContext(null);
|
||||||
PDFValidator pv = new PDFValidator(vc);
|
final PDFValidator pv = new PDFValidator(vc);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@@ -25,10 +25,10 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
pv.setFilename(tempFile.getAbsolutePath());
|
pv.setFilename(tempFile.getAbsolutePath());
|
||||||
pv.validate();
|
pv.validate();
|
||||||
String actual = pv.getXMLResult();
|
String actual = pv.getXMLResult();
|
||||||
assertEquals(true, actual.contains("summary status='valid"));
|
assertEquals(true, actual.contains("summary status=\"valid"));
|
||||||
assertEquals(false, actual.contains("summary status='invalid"));
|
assertEquals(false, actual.contains("summary status=\"invalid"));
|
||||||
|
|
||||||
XMLValidator xv = new XMLValidator(vc);
|
final XMLValidator xv = new XMLValidator(vc);
|
||||||
xv.setStringContent(pv.getRawXML());
|
xv.setStringContent(pv.getRawXML());
|
||||||
xv.validate();
|
xv.validate();
|
||||||
actual = vc.getXMLResult();
|
actual = vc.getXMLResult();
|
||||||
@@ -58,16 +58,16 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
actual.contains("validationReports compliant=\"1\" nonCompliant=\"0\" failedJobs=\"0\">"));
|
actual.contains("validationReports compliant=\"1\" nonCompliant=\"0\" failedJobs=\"0\">"));
|
||||||
|
|
||||||
assertEquals(false, actual.contains("<error"));
|
assertEquals(false, actual.contains("<error"));
|
||||||
} catch (IrrecoverableValidationError e) {
|
} catch (final IrrecoverableValidationError e) {
|
||||||
// ignore, will be in XML output anyway
|
// ignore, will be in XML output anyway
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPDFXMLValidation() {
|
public void testPDFXMLValidation() {
|
||||||
ValidationContext vc = new ValidationContext(null);
|
final ValidationContext vc = new ValidationContext(null);
|
||||||
try {
|
try {
|
||||||
PDFValidator pv = new PDFValidator(vc);
|
final PDFValidator pv = new PDFValidator(vc);
|
||||||
|
|
||||||
File tempFile = getResourceAsFile("attributeBasedXMP_zugferd_2p0_EN16931_Einfach.pdf");// need a more
|
File tempFile = getResourceAsFile("attributeBasedXMP_zugferd_2p0_EN16931_Einfach.pdf");// need a more
|
||||||
// invalid file here
|
// invalid file here
|
||||||
@@ -99,7 +99,7 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
xmlvres = xv.getXMLResult();
|
xmlvres = xv.getXMLResult();
|
||||||
assertEquals(true, pdfvres.contains("valid") && !pdfvres.contains("invalid"));
|
assertEquals(true, pdfvres.contains("valid") && !pdfvres.contains("invalid"));
|
||||||
assertEquals(true, xmlvres.contains("valid") && !xmlvres.contains("invalid"));
|
assertEquals(true, xmlvres.contains("valid") && !xmlvres.contains("invalid"));
|
||||||
} catch (IrrecoverableValidationError e) {
|
} catch (final IrrecoverableValidationError e) {
|
||||||
// ignore, will be in XML output anyway
|
// ignore, will be in XML output anyway
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,8 +107,8 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
|
|
||||||
public void testXMPValidation() {
|
public void testXMPValidation() {
|
||||||
|
|
||||||
ValidationContext vc = new ValidationContext(null);
|
final ValidationContext vc = new ValidationContext(null);
|
||||||
PDFValidator pv = new PDFValidator(vc);
|
final PDFValidator pv = new PDFValidator(vc);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
File tempFile = getResourceAsFile("invalidXMP.pdf");
|
File tempFile = getResourceAsFile("invalidXMP.pdf");
|
||||||
@@ -130,7 +130,7 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
|
|
||||||
assertEquals(false, actual.contains("<error"));// issue 18: "ConformanceLevel not found" should not be
|
assertEquals(false, actual.contains("<error"));// issue 18: "ConformanceLevel not found" should not be
|
||||||
// reported since it's actually there
|
// reported since it's actually there
|
||||||
} catch (IrrecoverableValidationError e) {
|
} catch (final IrrecoverableValidationError e) {
|
||||||
// ignore, will be in XML output anyway
|
// ignore, will be in XML output anyway
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user