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.xmlgraphics.util.MimeConstants;
import org.mustangproject.ClasspathResolverURIAdapter;
import org.mustangproject.EStandard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -107,6 +108,42 @@ public class ZUGFeRDVisualizer {
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)
throws FileNotFoundException, TransformerException, IOException, SAXException, ParserConfigurationException {
@@ -148,35 +185,24 @@ public class ZUGFeRDVisualizer {
ByteArrayOutputStream iaos = new ByteArrayOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String zf1Signature = "CrossIndustryDocument";
String zf2Signature = "CrossIndustryInvoice";
String ublSignature = "Invoice";
String ublCreditNoteSignature = "CreditNote";
String cioSignature = "SCRDMCCBDACIOMessageStructure" +
"";
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
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);
} else if (root.getLocalName().equals(zf2Signature)) {
} else if (thestandard == EStandard.facturx) {
//zf2 or fx
applyZF2XSLT(fis, iaos);
doPostProcessing = true;
} else if (root.getLocalName().equals(ublSignature)) {
} else if (thestandard == EStandard.ubl) {
//zf2 or fx
applyUBL2XSLT(fis, iaos);
doPostProcessing = true;
} else if (root.getLocalName().equals(ublCreditNoteSignature)) {
//zf2 or fx
applyUBLCreditNote2XSLT(fis, iaos);
doPostProcessing = true;
} else if (root.getLocalName().equals(cioSignature)) {
} else if (thestandard == EStandard.orderx) {
//zf2 or fx
applyCIO2XSLT(fis, iaos);
doPostProcessing = true;
@@ -219,11 +245,9 @@ public class ZUGFeRDVisualizer {
protected String toFOP(String xmlFilename)
throws FileNotFoundException, TransformerException {
FileInputStream fis = new FileInputStream(xmlFilename);
EStandard theStandard= findOutStandardFromRootNode(fis);
try {
if (mXsltXRTemplate == null) {
mXsltXRTemplate = mFactory.newTemplates(
new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/cii-xr.xsl")));
}
if (mXsltPDFTemplate == null) {
mXsltPDFTemplate = mFactory.newTemplates(
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);
}
FileInputStream fis = new FileInputStream(xmlFilename);
ByteArrayOutputStream iaos = new ByteArrayOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//zf2 or fx
applyZF2XSLT(fis, iaos);
if (theStandard==EStandard.facturx) {
applyZF2XSLT(fis, iaos);
} else if (theStandard==EStandard.ubl) {
applyUBL2XSLT(fis, iaos);
}
PipedInputStream in = new PipedInputStream();
PipedOutputStream out;
@@ -272,7 +300,7 @@ public class ZUGFeRDVisualizer {
public void toPDF(String xmlFilename, String pdfFilename) {
// the writing part
File CIIinputFile = new File(xmlFilename);
File XMLinputFile = new File(xmlFilename);
String result = null;
@@ -280,7 +308,7 @@ public class ZUGFeRDVisualizer {
out from git with arbitrary options (which may include CSRF changes)
*/
try {
result = this.toFOP(CIIinputFile.getAbsolutePath());
result = this.toFOP(XMLinputFile.getAbsolutePath());
} catch (FileNotFoundException | TransformerException e) {
LOGGER.error("Failed to apply FOP", e);
}

View File

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