Skip to content

Commit

Permalink
prototype of schema validation, only commenting out 1 test case for that
Browse files Browse the repository at this point in the history
  • Loading branch information
Jochen Stärk authored and Jochen Stärk committed Jun 27, 2019
1 parent 0033c98 commit c5afbe7
Show file tree
Hide file tree
Showing 14 changed files with 6,106 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/main/java/ZUV/XMLValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -14,9 +15,13 @@
import java.util.Date;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.XPathConstants;
import javax.xml.xpath.XPathExpression;
Expand All @@ -31,6 +36,7 @@
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.helger.schematron.ISchematronResource;
import com.helger.schematron.xslt.SchematronResourceXSLT;
Expand Down Expand Up @@ -107,6 +113,8 @@ public void validate() {
int firedRules=0;
int failedRules=0;

validateSchema();

ByteArrayInputStream xmlByteInputStream = new ByteArrayInputStream(zfXML.getBytes(StandardCharsets.UTF_8));

if (zfXML.isEmpty()) {
Expand Down Expand Up @@ -314,5 +322,22 @@ public void validate() {
"</profile><validator version=\""+Main.class.getPackage().getImplementationVersion()+"\"></validator><validation datetime=\""+isoDF.format(date)+"\"><rules><fired>"+firedRules+"</fired><failed>"+failedRules+"</failed></rules>" + "<duration unit='ms'>" + (endTime - startXMLTime) + "</duration></validation></info>");

}
protected void validateSchema() {
URL schemaFile = ClassLoader.getSystemResource("schema/EN16931/zugferd2p0_en16931.xsd");
Source xmlFile = new StreamSource(new StringReader(zfXML));
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
Schema schema = schemaFactory.newSchema(schemaFile);
javax.xml.validation.Validator validator = schema.newValidator();
validator.validate(xmlFile);
} catch (SAXException e) {
context.addResultItem(new ValidationResultItem(ESeverity.error, "schema validation fails:"+e).setSection(18)
.setPart(EPart.xml));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}

}

}
20 changes: 20 additions & 0 deletions src/main/resources/schema/BASIC/zugferd2p0_basicwl_minimum.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100"
targetNamespace="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
elementFormDefault="qualified">
<xs:import namespace="urn:un:unece:uncefact:data:standard:QualifiedDataType:100" schemaLocation="zugferd2p0_basicwl_minimum_urn_un_unece_uncefact_data_standard_QualifiedDataType_100.xsd"/>
<xs:import namespace="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" schemaLocation="zugferd2p0_basicwl_minimum_urn_un_unece_uncefact_data_standard_ReusableAggregateBusinessInformationEntity_100.xsd"/>
<xs:import namespace="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" schemaLocation="zugferd2p0_basicwl_minimum_urn_un_unece_uncefact_data_standard_UnqualifiedDataType_100.xsd"/>
<xs:element name="CrossIndustryInvoice" type="rsm:CrossIndustryInvoiceType"/>
<xs:complexType name="CrossIndustryInvoiceType">
<xs:sequence>
<xs:element name="ExchangedDocumentContext" type="ram:ExchangedDocumentContextType"/>
<xs:element name="ExchangedDocument" type="ram:ExchangedDocumentType"/>
<xs:element name="SupplyChainTradeTransaction" type="ram:SupplyChainTradeTransactionType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Loading

0 comments on commit c5afbe7

Please sign in to comment.