Enhance code quality
This commit is contained in:
@@ -12,7 +12,6 @@ import java.util.Calendar;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
@@ -130,136 +129,137 @@ public class PDFValidator extends Validator {
|
||||
|
||||
final Document docXMP;
|
||||
|
||||
if (xmp == null || xmp.length() == 0) {
|
||||
if (xmp == null || xmp.isEmpty()) {
|
||||
context.addResultItem(new ValidationResultItem(ESeverity.error, "Invalid XMP Metadata not found")
|
||||
.setSection(17).setPart(EPart.pdf));
|
||||
}
|
||||
else
|
||||
/*
|
||||
* checking for sth like <zf:ConformanceLevel>EXTENDED</zf:ConformanceLevel>
|
||||
* <zf:DocumentType>INVOICE</zf:DocumentType>
|
||||
* <zf:DocumentFileName>ZUGFeRD-invoice.xml</zf:DocumentFileName>
|
||||
* <zf:Version>1.0</zf:Version>
|
||||
*/
|
||||
try {
|
||||
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
// and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks"
|
||||
factory.setXIncludeAware(false);
|
||||
else {
|
||||
/*
|
||||
* checking for sth like <zf:ConformanceLevel>EXTENDED</zf:ConformanceLevel>
|
||||
* <zf:DocumentType>INVOICE</zf:DocumentType>
|
||||
* <zf:DocumentFileName>ZUGFeRD-invoice.xml</zf:DocumentFileName>
|
||||
* <zf:Version>1.0</zf:Version>
|
||||
*/
|
||||
try {
|
||||
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
// and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks"
|
||||
factory.setXIncludeAware(false);
|
||||
|
||||
final DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
final InputSource is = new InputSource(new StringReader(xmp));
|
||||
docXMP = builder.parse(is);
|
||||
final DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
final InputSource is = new InputSource(new StringReader(xmp));
|
||||
docXMP = builder.parse(is);
|
||||
|
||||
final 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
|
||||
|
||||
final XPath xpath = xpathFactory.newXPath();
|
||||
// xpath.compile("//*[local-name()=\"GuidelineSpecifiedDocumentContextParameter\"]/[local-name()=\"ID\"]");
|
||||
// evaluate expression result on XML document ndList = (NodeList)
|
||||
final XPath xpath = xpathFactory.newXPath();
|
||||
// xpath.compile("//*[local-name()=\"GuidelineSpecifiedDocumentContextParameter\"]/[local-name()=\"ID\"]");
|
||||
// evaluate expression result on XML document ndList = (NodeList)
|
||||
|
||||
// get the first element
|
||||
XPathExpression xpr = xpath.compile(
|
||||
"//*[local-name()=\"ConformanceLevel\"]|//*[local-name()=\"Description\"]/@ConformanceLevel");
|
||||
NodeList nodes = (NodeList) xpr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
// get the first element
|
||||
XPathExpression xpr = xpath.compile(
|
||||
"//*[local-name()=\"ConformanceLevel\"]|//*[local-name()=\"Description\"]/@ConformanceLevel");
|
||||
NodeList nodes = (NodeList) xpr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
|
||||
if (nodes.getLength() == 0) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.error, "XMP Metadata: ConformanceLevel not found")
|
||||
.setSection(11).setPart(EPart.pdf));
|
||||
}
|
||||
|
||||
boolean conformanceLevelValid = false;
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
|
||||
final String[] valueArray = {"BASIC WL", "BASIC", "MINIMUM", "EN 16931", "COMFORT", "CIUS", "EXTENDED", "XRECHNUNG"};
|
||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||
conformanceLevelValid = true;
|
||||
}
|
||||
}
|
||||
if (!conformanceLevelValid) {
|
||||
context.addResultItem(new ValidationResultItem(
|
||||
ESeverity.error,
|
||||
"XMP Metadata: ConformanceLevel contains invalid value"
|
||||
).setSection(12).setPart(EPart.pdf));
|
||||
|
||||
}
|
||||
xpr = xpath.compile("//*[local-name()=\"DocumentType\"]|//*[local-name()=\"Description\"]/@DocumentType");
|
||||
nodes = (NodeList) xpr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
|
||||
if (nodes.getLength() == 0) {
|
||||
context.addResultItem(new ValidationResultItem(ESeverity.error, "XMP Metadata: DocumentType not found")
|
||||
.setSection(13).setPart(EPart.pdf));
|
||||
}
|
||||
|
||||
boolean documentTypeValid = false;
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
if (nodes.item(i).getTextContent().equals("INVOICE") || nodes.item(i).getTextContent().equals("ORDER")
|
||||
|| nodes.item(i).getTextContent().equals("ORDER_RESPONSE") || nodes.item(i).getTextContent()
|
||||
.equals("ORDER_CHANGE")) {
|
||||
documentTypeValid = true;
|
||||
}
|
||||
}
|
||||
if (!documentTypeValid) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.error, "XMP Metadata: DocumentType invalid")
|
||||
.setSection(14).setPart(EPart.pdf));
|
||||
|
||||
}
|
||||
xpr = xpath.compile(
|
||||
"//*[local-name()=\"DocumentFileName\"]|//*[local-name()=\"Description\"]/@DocumentFileName");
|
||||
nodes = (NodeList) xpr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
|
||||
if (nodes.getLength() == 0) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.error, "XMP Metadata: DocumentFileName not found")
|
||||
.setSection(21).setPart(EPart.pdf));
|
||||
}
|
||||
boolean documentFilenameValid = false;
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
final String[] valueArray = {"factur-x.xml", "ZUGFeRD-invoice.xml", "zugferd-invoice.xml", "xrechnung.xml", "order-x.xml"};
|
||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||
documentFilenameValid = true;
|
||||
if (nodes.getLength() == 0) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.error, "XMP Metadata: ConformanceLevel not found")
|
||||
.setSection(11).setPart(EPart.pdf));
|
||||
}
|
||||
|
||||
// e.g. ZUGFeRD-invoice.xml
|
||||
}
|
||||
if (!documentFilenameValid) {
|
||||
boolean conformanceLevelValid = false;
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
|
||||
context.addResultItem(new ValidationResultItem(
|
||||
ESeverity.error,
|
||||
"XMP Metadata: DocumentFileName contains invalid value"
|
||||
).setSection(19).setPart(EPart.pdf));
|
||||
}
|
||||
xpr = xpath.compile("//*[local-name()=\"Version\"]|//*[local-name()=\"Description\"]/@Version");
|
||||
nodes = (NodeList) xpr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
final String[] valueArray = {"BASIC WL", "BASIC", "MINIMUM", "EN 16931", "COMFORT", "CIUS", "EXTENDED", "XRECHNUNG"};
|
||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||
conformanceLevelValid = true;
|
||||
}
|
||||
}
|
||||
if (!conformanceLevelValid) {
|
||||
context.addResultItem(new ValidationResultItem(
|
||||
ESeverity.error,
|
||||
"XMP Metadata: ConformanceLevel contains invalid value"
|
||||
).setSection(12).setPart(EPart.pdf));
|
||||
|
||||
// get all child nodes
|
||||
// NodeList nodes = element.getChildNodes();
|
||||
// expr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
// print the text content of each child
|
||||
if (nodes.getLength() == 0) {
|
||||
context.addResultItem(new ValidationResultItem(ESeverity.error, "XMP Metadata: Version not found")
|
||||
.setSection(15).setPart(EPart.pdf));
|
||||
}
|
||||
}
|
||||
xpr = xpath.compile("//*[local-name()=\"DocumentType\"]|//*[local-name()=\"Description\"]/@DocumentType");
|
||||
nodes = (NodeList) xpr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
|
||||
boolean versionValid = false;
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
final String[] valueArray = {"1.0", "1p0", "2p0", "1.2", "2.0", "2.1", "2.2", "2.3", "3.0"}; //1.2, 2.0, 2.1, 2.2, 2.3 and 3.0 are for xrechnung 1.2, 2p0 can be ZF 2.0, 2.1, 2.1.1
|
||||
if (nodes.getLength() == 0) {
|
||||
context.addResultItem(new ValidationResultItem(ESeverity.error, "XMP Metadata: DocumentType not found")
|
||||
.setSection(13).setPart(EPart.pdf));
|
||||
}
|
||||
|
||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||
versionValid = true;
|
||||
} // e.g. 1.0
|
||||
}
|
||||
if (!versionValid) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.error, "XMP Metadata: Version contains invalid value")
|
||||
.setSection(16).setPart(EPart.pdf));
|
||||
boolean documentTypeValid = false;
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
if (nodes.item(i).getTextContent().equals("INVOICE") || nodes.item(i).getTextContent().equals("ORDER")
|
||||
|| nodes.item(i).getTextContent().equals("ORDER_RESPONSE") || nodes.item(i).getTextContent()
|
||||
.equals("ORDER_CHANGE")) {
|
||||
documentTypeValid = true;
|
||||
}
|
||||
}
|
||||
if (!documentTypeValid) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.error, "XMP Metadata: DocumentType invalid")
|
||||
.setSection(14).setPart(EPart.pdf));
|
||||
|
||||
}
|
||||
xpr = xpath.compile(
|
||||
"//*[local-name()=\"DocumentFileName\"]|//*[local-name()=\"Description\"]/@DocumentFileName");
|
||||
nodes = (NodeList) xpr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
|
||||
if (nodes.getLength() == 0) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.error, "XMP Metadata: DocumentFileName not found")
|
||||
.setSection(21).setPart(EPart.pdf));
|
||||
}
|
||||
boolean documentFilenameValid = false;
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
final String[] valueArray = {"factur-x.xml", "ZUGFeRD-invoice.xml", "zugferd-invoice.xml", "xrechnung.xml", "order-x.xml"};
|
||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||
documentFilenameValid = true;
|
||||
}
|
||||
|
||||
// e.g. ZUGFeRD-invoice.xml
|
||||
}
|
||||
if (!documentFilenameValid) {
|
||||
|
||||
context.addResultItem(new ValidationResultItem(
|
||||
ESeverity.error,
|
||||
"XMP Metadata: DocumentFileName contains invalid value"
|
||||
).setSection(19).setPart(EPart.pdf));
|
||||
}
|
||||
xpr = xpath.compile("//*[local-name()=\"Version\"]|//*[local-name()=\"Description\"]/@Version");
|
||||
nodes = (NodeList) xpr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
|
||||
// get all child nodes
|
||||
// NodeList nodes = element.getChildNodes();
|
||||
// expr.evaluate(docXMP, XPathConstants.NODESET);
|
||||
// print the text content of each child
|
||||
if (nodes.getLength() == 0) {
|
||||
context.addResultItem(new ValidationResultItem(ESeverity.error, "XMP Metadata: Version not found")
|
||||
.setSection(15).setPart(EPart.pdf));
|
||||
}
|
||||
|
||||
boolean versionValid = false;
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
final String[] valueArray = {"1.0", "1p0", "2p0", "1.2", "2.0", "2.1", "2.2", "2.3", "3.0"}; //1.2, 2.0, 2.1, 2.2, 2.3 and 3.0 are for xrechnung 1.2, 2p0 can be ZF 2.0, 2.1, 2.1.1
|
||||
|
||||
if (stringArrayContains(valueArray, nodes.item(i).getTextContent())) {
|
||||
versionValid = true;
|
||||
} // e.g. 1.0
|
||||
}
|
||||
if (!versionValid) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.error, "XMP Metadata: Version contains invalid value")
|
||||
.setSection(16).setPart(EPart.pdf));
|
||||
|
||||
}
|
||||
} catch (final SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
||||
LOGGER.error(e.getMessage(), e);
|
||||
}
|
||||
} catch (final SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
||||
LOGGER.error(e.getMessage(), e);
|
||||
}
|
||||
zfXML = zi.getUTF8();
|
||||
|
||||
@@ -306,7 +306,7 @@ public class PDFValidator extends Validator {
|
||||
final HashMap<String, byte[]> additionalData = zi.getAdditionalData();
|
||||
for (final String filename : additionalData.keySet()) {
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,13 +32,13 @@ public class ValidationContext {
|
||||
}
|
||||
if (logger != null) {
|
||||
if ((vr.getSeverity() == ESeverity.fatal) || (vr.getSeverity() == ESeverity.exception)) {
|
||||
logger.error("Fatal Error " + vr.getSection() + ": " + vr.getMessage());
|
||||
logger.error("Fatal Error {}: {}", vr.getSection(), vr.getMessage());
|
||||
} else if ((vr.getSeverity() == ESeverity.error)) {
|
||||
logger.error("Error " + vr.getSection() + ": " + vr.getMessage());
|
||||
logger.error("Error {}: {}", vr.getSection(), vr.getMessage());
|
||||
} else if (vr.getSeverity() == ESeverity.warning) {
|
||||
logger.warn("Warning " + vr.getSection() + ": " + vr.getMessage());
|
||||
logger.warn("Warning {}: {}", vr.getSection(), vr.getMessage());
|
||||
} else if (vr.getSeverity() == ESeverity.notice) {
|
||||
logger.info("Notice " + vr.getSection() + ": " + vr.getMessage());
|
||||
logger.info("Notice {}: {}", vr.getSection(), vr.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,20 +106,17 @@ public class ValidationContext {
|
||||
}
|
||||
|
||||
public String getXMLResult() {
|
||||
String res = getCustomXML();
|
||||
if (results.size() > 0) {
|
||||
res += "<messages>";
|
||||
StringBuilder res = new StringBuilder(getCustomXML());
|
||||
if (results != null && !results.isEmpty()) {
|
||||
res.append("<messages>");
|
||||
for (final ValidationResultItem validationResultItem : results) {
|
||||
// xml and pdf are handled in their respective sections
|
||||
res.append(validationResultItem.getXMLOnce()).append("\n");
|
||||
}
|
||||
res.append("</messages>");
|
||||
}
|
||||
|
||||
for (final ValidationResultItem validationResultItem : results) {
|
||||
// xml and pdf are handled in their respective sections
|
||||
res += validationResultItem.getXMLOnce() + "\n";
|
||||
}
|
||||
if (results.size() > 0) {
|
||||
res += "</messages>";
|
||||
}
|
||||
res += "<summary status=\"" + (isValid ? "valid" : "invalid") + "\"/>";
|
||||
return res;
|
||||
res.append("<summary status=\"").append(isValid ? "valid" : "invalid").append("\"/>");
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
/***
|
||||
|
||||
@@ -22,7 +22,6 @@ import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.mustangproject.CalculatedInvoice;
|
||||
import org.mustangproject.Exceptions.ArithmetricException;
|
||||
import org.mustangproject.XMLTools;
|
||||
import org.mustangproject.ZUGFeRD.ZUGFeRDInvoiceImporter;
|
||||
import org.slf4j.Logger;
|
||||
@@ -310,7 +309,7 @@ public class XMLValidator extends Validator {
|
||||
if (!xrVersion.equals("12") && !xrVersion.equals("20") && !xrVersion.equals("21") && !xrVersion.equals("22") && !xrVersion.equals("23") && !xrVersion.equals("30")) {
|
||||
throw new Exception("Unsupported XR version");
|
||||
}
|
||||
LOGGER.debug("is XRechnung v" + xrVersion);
|
||||
LOGGER.debug("is XRechnung v{}", xrVersion);
|
||||
xsltFilename = "/xslt/XR_" + xrVersion + "/XRechnung-UBL-validation.xslt";
|
||||
XrechnungSeverity = ESeverity.error;
|
||||
mainSchematronSectionErrorTypeCode = 27;
|
||||
@@ -534,7 +533,7 @@ public class XMLValidator extends Validator {
|
||||
}
|
||||
}
|
||||
|
||||
LOGGER.info("FailedAssert ", thisFailText);
|
||||
LOGGER.info("FailedAssert {}", thisFailText);
|
||||
|
||||
context.addResultItem(new ValidationResultItem(severity, thisFailText + thisFailIDStr + " from " + xsltFilename + ")")
|
||||
.setLocation(thisFailLocation).setCriterion(thisFailTest).setSection(section).setID(thisFailID)
|
||||
|
||||
Reference in New Issue
Block a user