have a test, start to also handle UBL input

This commit is contained in:
jstaerk
2024-09-28 12:21:16 +02:00
parent 26f0668aa9
commit be8c0be850
2 changed files with 63 additions and 36 deletions

View File

@@ -62,6 +62,7 @@ import org.apache.fop.configuration.ConfigurationException;
import org.apache.fop.configuration.DefaultConfigurationBuilder; import org.apache.fop.configuration.DefaultConfigurationBuilder;
import org.apache.xmlgraphics.util.MimeConstants; import org.apache.xmlgraphics.util.MimeConstants;
import org.mustangproject.ClasspathResolverURIAdapter; import org.mustangproject.ClasspathResolverURIAdapter;
import org.mustangproject.EStandard;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -107,6 +108,42 @@ public class ZUGFeRDVisualizer {
mFactory.setURIResolver(new ClasspathResourceURIResolver()); mFactory.setURIResolver(new ClasspathResourceURIResolver());
} }
/***
* returns which standard is used, CII or UBL
* @param fis inputstream (will be consumed)
* @return (facturx=cii)
*/
public EStandard findOutStandardFromRootNode(InputStream fis) {
String zf1Signature = "CrossIndustryDocument";
String zf2Signature = "CrossIndustryInvoice";
String ublSignature = "Invoice";
String ublCreditNoteSignature = "CreditNote";
String cioSignature = "SCRDMCCBDACIOMessageStructure";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(fis));
Element root = doc.getDocumentElement();
if (root.getLocalName().equals(zf1Signature)) {
return EStandard.zugferd;
} else if (root.getLocalName().equals(zf2Signature)) {
return EStandard.facturx;
} else if (root.getLocalName().equals(ublSignature)) {
return EStandard.ubl;
} else if (root.getLocalName().equals(ublCreditNoteSignature)) {
return EStandard.ubl;
} else if (root.getLocalName().equals(cioSignature)) {
return EStandard.orderx;
}
} catch (Exception e) {
LOGGER.error("Failed to recognize standard", e);
}
return null;
}
public String visualize(String xmlFilename, Language lang) public String visualize(String xmlFilename, Language lang)
throws FileNotFoundException, TransformerException, IOException, SAXException, ParserConfigurationException { throws FileNotFoundException, TransformerException, IOException, SAXException, ParserConfigurationException {
@@ -148,35 +185,24 @@ public class ZUGFeRDVisualizer {
ByteArrayOutputStream iaos = new ByteArrayOutputStream(); ByteArrayOutputStream iaos = new ByteArrayOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
String zf1Signature = "CrossIndustryDocument";
String zf2Signature = "CrossIndustryInvoice";
String ublSignature = "Invoice";
String ublCreditNoteSignature = "CreditNote";
String cioSignature = "SCRDMCCBDACIOMessageStructure" +
"";
boolean doPostProcessing = false; boolean doPostProcessing = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(fis));
Element root = doc.getDocumentElement();
fis = new FileInputStream(xmlFilename); // fis wont reset() so re-read from beginning fis = new FileInputStream(xmlFilename); // fis wont reset() so re-read from beginning
if (root.getLocalName().equals(zf1Signature)) { EStandard thestandard=findOutStandardFromRootNode(fis);
fis = new FileInputStream(xmlFilename); // fis wont reset() so re-read from beginning
if (thestandard == EStandard.zugferd) {
applyZF1XSLT(fis, baos); applyZF1XSLT(fis, baos);
} else if (root.getLocalName().equals(zf2Signature)) { } else if (thestandard == EStandard.facturx) {
//zf2 or fx //zf2 or fx
applyZF2XSLT(fis, iaos); applyZF2XSLT(fis, iaos);
doPostProcessing = true; doPostProcessing = true;
} else if (root.getLocalName().equals(ublSignature)) { } else if (thestandard == EStandard.ubl) {
//zf2 or fx //zf2 or fx
applyUBL2XSLT(fis, iaos); applyUBL2XSLT(fis, iaos);
doPostProcessing = true; doPostProcessing = true;
} else if (root.getLocalName().equals(ublCreditNoteSignature)) { } else if (thestandard == EStandard.orderx) {
//zf2 or fx
applyUBLCreditNote2XSLT(fis, iaos);
doPostProcessing = true;
} else if (root.getLocalName().equals(cioSignature)) {
//zf2 or fx //zf2 or fx
applyCIO2XSLT(fis, iaos); applyCIO2XSLT(fis, iaos);
doPostProcessing = true; doPostProcessing = true;
@@ -219,11 +245,9 @@ public class ZUGFeRDVisualizer {
protected String toFOP(String xmlFilename) protected String toFOP(String xmlFilename)
throws FileNotFoundException, TransformerException { throws FileNotFoundException, TransformerException {
FileInputStream fis = new FileInputStream(xmlFilename);
EStandard theStandard= findOutStandardFromRootNode(fis);
try { try {
if (mXsltXRTemplate == null) {
mXsltXRTemplate = mFactory.newTemplates(
new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/cii-xr.xsl")));
}
if (mXsltPDFTemplate == null) { if (mXsltPDFTemplate == null) {
mXsltPDFTemplate = mFactory.newTemplates( mXsltPDFTemplate = mFactory.newTemplates(
new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/xr-pdf.xsl"))); new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/xr-pdf.xsl")));
@@ -232,12 +256,16 @@ public class ZUGFeRDVisualizer {
LOGGER.error("Failed to init XSLT templates", ex); LOGGER.error("Failed to init XSLT templates", ex);
} }
FileInputStream fis = new FileInputStream(xmlFilename);
ByteArrayOutputStream iaos = new ByteArrayOutputStream(); ByteArrayOutputStream iaos = new ByteArrayOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
//zf2 or fx //zf2 or fx
if (theStandard==EStandard.facturx) {
applyZF2XSLT(fis, iaos); applyZF2XSLT(fis, iaos);
} else if (theStandard==EStandard.ubl) {
applyUBL2XSLT(fis, iaos);
}
PipedInputStream in = new PipedInputStream(); PipedInputStream in = new PipedInputStream();
PipedOutputStream out; PipedOutputStream out;
@@ -272,7 +300,7 @@ public class ZUGFeRDVisualizer {
public void toPDF(String xmlFilename, String pdfFilename) { public void toPDF(String xmlFilename, String pdfFilename) {
// the writing part // the writing part
File CIIinputFile = new File(xmlFilename); File XMLinputFile = new File(xmlFilename);
String result = null; String result = null;
@@ -280,7 +308,7 @@ public class ZUGFeRDVisualizer {
out from git with arbitrary options (which may include CSRF changes) out from git with arbitrary options (which may include CSRF changes)
*/ */
try { try {
result = this.toFOP(CIIinputFile.getAbsolutePath()); result = this.toFOP(XMLinputFile.getAbsolutePath());
} catch (FileNotFoundException | TransformerException e) { } catch (FileNotFoundException | TransformerException e) {
LOGGER.error("Failed to apply FOP", e); LOGGER.error("Failed to apply FOP", e);
} }

View File

@@ -32,6 +32,8 @@ import java.nio.file.Files;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class VisualizationTest extends ResourceCase { public class VisualizationTest extends ResourceCase {
final String TARGET_PDF = "./target/testout-Visualization.pdf";
public void testCIIVisualizationBasic() { public void testCIIVisualizationBasic() {
// the writing part // the writing part
@@ -145,21 +147,18 @@ public class VisualizationTest extends ResourceCase {
assertEquals(expected, result); assertEquals(expected, result);
} }
/* public void testPDFVisualization() { public void testPDFVisualization() {
File UBLinputFile = getResourceAsFile("ubl/01.01a-INVOICE.ubl.xml");
// the writing part // the writing part
CIIToUBL c2u = new CIIToUBL();
String sourceFilename = "factur-x.xml"; String sourceFilename = "factur-x.xml";
File CIIinputFile = getResourceAsFile(sourceFilename);
String expected = null; String expected = null;
String result = null; String result = null;
try { try {
ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer();
/* remove file endings so that tests can also pass after checking zvi.toPDF(UBLinputFile.getAbsolutePath(), TARGET_PDF);
out from git with arbitrary options (which may include CSRF changes)
*
zvi.toPDF(CIIinputFile.getAbsolutePath(), "c:\\users\\jstaerk\\temp\\fopy2.pdf");
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
fail("UnsupportedOperationException should not happen: "+e.getMessage()); fail("UnsupportedOperationException should not happen: "+e.getMessage());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
@@ -169,6 +168,6 @@ public class VisualizationTest extends ResourceCase {
// assertNotNull(result); // assertNotNull(result);
// Reading ZUGFeRD // Reading ZUGFeRD
// assertEquals(expected, result); // assertEquals(expected, result);
}*/ }
} }