diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java index 6687297e7..275da4454 100755 --- a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java +++ b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java @@ -19,6 +19,7 @@ package org.mustangproject.commandline; import org.apache.commons.cli.*; +import org.apache.commons.io.FilenameUtils; import org.mustangproject.CII.CIIToUBL; import org.mustangproject.EStandard; import org.mustangproject.FileAttachment; @@ -84,6 +85,7 @@ private static String getUsage() { + " [--logAppend ]: text to be added to log line\n" + " Additional parameters (optional - user will be prompted if not defined)\n" + " [--source ]: input PDF or XML file\n" + + " [--log-as-pdf]: save log output as pdf\n" + " --action validateExpectInvalid validate directory expecting negative results \n" + " [--no-notices]: refrain from reporting notices\n" + " Additional parameters (optional - user will be prompted if not defined)\n" @@ -357,6 +359,7 @@ public static void main(String[] args) { options.addOption(new Option("d", "directory", true, "which directory to operate on")); options.addOption(new Option("i", "ignorefileextension", false, "ignore non-matching file extensions")); options.addOption(new Option("l", "listfromstdin", false, "take list of files from commandline")); + options.addOption(new Option("log-as-pdf", "log-as-pdf", false, "saving log output to pdf file")); boolean optionsRecognized = false; String action = ""; @@ -380,6 +383,7 @@ public static void main(String[] args) { String format = cmd.getOptionValue("format"); String lang = cmd.getOptionValue("language"); Boolean noNotices = cmd.hasOption("no-notices"); + Boolean LogAsPDF = cmd.hasOption("log-as-pdf"); String zugferdVersion = cmd.getOptionValue("version"); String zugferdProfile = cmd.getOptionValue("profile"); @@ -427,7 +431,7 @@ public static void main(String[] args) { performUBL(sourceName, outName); optionsRecognized = true; } else if ((action != null) && (action.equals("validate"))) { - optionsRecognized = performValidate(sourceName, noNotices, cmd.getOptionValue("logAppend")); + optionsRecognized = performValidate(sourceName, noNotices, cmd.getOptionValue("logAppend"), LogAsPDF); } else if ((action != null) && (action.equals("validateExpectValid"))) { optionsRecognized = performValidateExpect(true, directoryName); } else if ((action != null) && (action.equals("validateExpectInvalid"))) { @@ -454,7 +458,7 @@ public static void main(String[] args) { } - private static boolean performValidate(String sourceName, boolean noNotices, String logAppend) { + private static boolean performValidate(String sourceName, boolean noNotices, String logAppend, boolean createLogAsPDF) { boolean optionsRecognized; if (sourceName == null) { sourceName = getFilenameFromUser("Source PDF or XML", "invoice.pdf", "pdf|xml", true, false); @@ -466,7 +470,16 @@ private static boolean performValidate(String sourceName, boolean noNotices, Str if (noNotices) { zfv.disableNotices(); } - System.out.println(zfv.validate(sourceName)); + + String validationResultXML = zfv.validate(sourceName); + System.out.println(validationResultXML); + + if( createLogAsPDF) { + ValidationLogVisualizer vlvi = new ValidationLogVisualizer(); + String fileBasename = FilenameUtils.getBaseName(sourceName); + + vlvi.toPDF(validationResultXML, fileBasename + "_result.pdf"); + } optionsRecognized = !zfv.hasOptionsError(); if (!zfv.wasCompletelyValid()) { System.exit(-1); diff --git a/library/pom.xml b/library/pom.xml index 22f516f75..073661c1f 100644 --- a/library/pom.xml +++ b/library/pom.xml @@ -200,21 +200,51 @@ - FreeSans.ttf + SourceSansPro-Regular.ttf font/ttf - FreeSerif.ttf + SourceSansPro-It.ttf - application/x-font + font/ttf + + + + SourceSansPro-Bold.ttf + + font/ttf + + + + SourceSansPro-BoldIt.ttf + + font/ttf - Times-Bold.ttf + SourceSerifPro-Regular.ttf - application/x-font + font/ttf + + + + SourceSerifPro-It.ttf + + font/ttf + + + + SourceSerifPro-Bold.ttf + + font/ttf + + + + SourceSerifPro-BoldIt.ttf + + font/ttf diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ValidationLogVisualizer.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ValidationLogVisualizer.java new file mode 100644 index 000000000..686daa7cc --- /dev/null +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ValidationLogVisualizer.java @@ -0,0 +1,148 @@ +package org.mustangproject.ZUGFeRD; + +import org.apache.fop.apps.*; +import org.apache.fop.apps.io.ResourceResolverFactory; +import org.apache.fop.configuration.Configuration; +import org.apache.fop.configuration.ConfigurationException; +import org.apache.fop.configuration.DefaultConfigurationBuilder; +import org.apache.xmlgraphics.util.MimeConstants; +import org.mustangproject.ClasspathResolverURIAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.xml.transform.*; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import java.io.*; +import java.nio.charset.StandardCharsets; + +public class ValidationLogVisualizer { + public enum Language { + EN, + FR, + DE + } + + static final ClassLoader CLASS_LOADER = ValidationLogVisualizer.class.getClassLoader(); + private static final String RESOURCE_PATH = ""; + private static final Logger LOGGER = LoggerFactory.getLogger(ValidationLogVisualizer.class); + + private TransformerFactory mFactory = null; + private Templates mXsltPDFTemplate = null; + + + public ValidationLogVisualizer() { + mFactory = new net.sf.saxon.TransformerFactoryImpl(); + // fact = TransformerFactory.newInstance(); + mFactory.setURIResolver(new ValidationLogVisualizer.ClasspathResourceURIResolver()); + } + + protected void applyXSLTToPDF(final String xmlContent, final OutputStream PDFOutstream) + throws TransformerException { + Transformer transformer = mXsltPDFTemplate.newTransformer(); + + transformer.transform(new StreamSource(new StringReader(xmlContent)), new StreamResult(PDFOutstream)); + } + + protected String toFOP(final String xmlContent) + throws TransformerException { + + try { + if (mXsltPDFTemplate == null) { + mXsltPDFTemplate = mFactory.newTemplates( + new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/result-pdf.xsl"))); + } + } catch (TransformerConfigurationException ex) { + LOGGER.error("Failed to init XSLT templates", ex); + } + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + try { + + applyXSLTToPDF(xmlContent, baos); + + } catch (Exception e1) { + LOGGER.error("Failed to create PDF", e1); + } + + return baos.toString(StandardCharsets.UTF_8); + } + + public void toPDF(String xmlLogfileContent, String pdfFilename) { + + // the writing part + + String result = null; + + /* remove file endings so that tests can also pass after checking + out from git with arbitrary options (which may include CSRF changes) + */ + try { + result = this.toFOP(xmlLogfileContent); + } catch ( TransformerException e) { + LOGGER.error("Failed to apply FOP", e); + } + DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); + + Configuration cfg = null; + try { + cfg = cfgBuilder.build(CLASS_LOADER.getResourceAsStream("fop-config.xconf")); + } catch (ConfigurationException e) { + throw new RuntimeException(e); + } + + FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI(), new ClasspathResolverURIAdapter()).setConfiguration(cfg); +// Step 1: Construct a FopFactory by specifying a reference to the configuration file +// (reuse if you plan to render multiple documents!) + + FopFactory fopFactory = builder.build(); + + fopFactory.getFontManager().setResourceResolver( + ResourceResolverFactory.createInternalResourceResolver( + new File(".").toURI(), + new ClasspathResolverURIAdapter())); + + FOUserAgent userAgent = fopFactory.newFOUserAgent(); + + userAgent.getRendererOptions().put("pdf-a-mode", "PDF/A-3b"); + +// Step 2: Set up output stream. +// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams). + + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(pdfFilename))) { + + // Step 3: Construct fop with desired output format + Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out); + + // Step 4: Setup JAXP using identity transformer + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); // identity transformer + + // Step 5: Setup input and output for XSLT transformation + // Setup input stream + Source src = new StreamSource(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8))); + + // Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new SAXResult(fop.getDefaultHandler()); + + // Step 6: Start XSLT transformation and FOP processing + transformer.transform(src, res); + + } catch (FOPException | IOException | TransformerException e) { + LOGGER.error("Failed to create PDF", e); + } + } + + private static class ClasspathResourceURIResolver implements URIResolver { + ClasspathResourceURIResolver() { + // Do nothing, just prevents synthetic access warning. + } + + @Override + public Source resolve(String href, String base) throws TransformerException { + return new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/" + href)); + } + } +} diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index 5189bee66..6ff0a4b87 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -659,6 +659,7 @@ private PostalTradeAddress getAddressFromNodeList(NodeList nl) { * * @return a List of LineItem instances */ + @Deprecated public List getLineItemList() { final List nodeList = getLineItemNodes(); final List lineItemList = new ArrayList<>(); diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java index da10d8dd5..ceda9530e 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java @@ -1036,6 +1036,7 @@ public String getUTF8() { * for PDF embedded files in FX use getFileAttachmentsPDF() * @deprecated use invoice.getAdditionalReferencedDocuments */ + @Deprecated public List getFileAttachmentsXML() { return new ArrayList<>(Arrays.asList(importedInvoice.getAdditionalReferencedDocuments())); } diff --git a/library/src/main/resources/fonts/SourceSansPro-Bold.ttf b/library/src/main/resources/fonts/SourceSansPro-Bold.ttf new file mode 100644 index 000000000..a253fdc20 Binary files /dev/null and b/library/src/main/resources/fonts/SourceSansPro-Bold.ttf differ diff --git a/library/src/main/resources/fonts/SourceSansPro-BoldIt.ttf b/library/src/main/resources/fonts/SourceSansPro-BoldIt.ttf new file mode 100644 index 000000000..61505f237 Binary files /dev/null and b/library/src/main/resources/fonts/SourceSansPro-BoldIt.ttf differ diff --git a/library/src/main/resources/fonts/SourceSansPro-It.ttf b/library/src/main/resources/fonts/SourceSansPro-It.ttf new file mode 100644 index 000000000..51e634210 Binary files /dev/null and b/library/src/main/resources/fonts/SourceSansPro-It.ttf differ diff --git a/library/src/main/resources/fonts/SourceSansPro-Regular.ttf b/library/src/main/resources/fonts/SourceSansPro-Regular.ttf new file mode 100644 index 000000000..d9344ce81 Binary files /dev/null and b/library/src/main/resources/fonts/SourceSansPro-Regular.ttf differ diff --git a/library/src/main/resources/fonts/SourceSerifPro-Bold.ttf b/library/src/main/resources/fonts/SourceSerifPro-Bold.ttf new file mode 100644 index 000000000..5814a6a03 Binary files /dev/null and b/library/src/main/resources/fonts/SourceSerifPro-Bold.ttf differ diff --git a/library/src/main/resources/fonts/SourceSerifPro-BoldIt.ttf b/library/src/main/resources/fonts/SourceSerifPro-BoldIt.ttf new file mode 100644 index 000000000..8a23518cc Binary files /dev/null and b/library/src/main/resources/fonts/SourceSerifPro-BoldIt.ttf differ diff --git a/library/src/main/resources/fonts/SourceSerifPro-It.ttf b/library/src/main/resources/fonts/SourceSerifPro-It.ttf new file mode 100644 index 000000000..be263bee2 Binary files /dev/null and b/library/src/main/resources/fonts/SourceSerifPro-It.ttf differ diff --git a/library/src/main/resources/fonts/SourceSerifPro-Regular.ttf b/library/src/main/resources/fonts/SourceSerifPro-Regular.ttf new file mode 100644 index 000000000..0a05b4dcd Binary files /dev/null and b/library/src/main/resources/fonts/SourceSerifPro-Regular.ttf differ diff --git a/library/src/main/resources/fop-config.xconf b/library/src/main/resources/fop-config.xconf index cf01edd3b..4d93f97ec 100644 --- a/library/src/main/resources/fop-config.xconf +++ b/library/src/main/resources/fop-config.xconf @@ -1,37 +1,57 @@ - - - - true - - - true - - - - - - - 96 - - 96 - - - - - - - - - - - - - - - - classpath:AdobeCompat-v2.icc - - - - + + + + true + + + true + + + + + + + 96 + + 96 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + classpath:AdobeCompat-v2.icc + + + diff --git a/library/src/main/resources/stylesheets/result-pdf.xsl b/library/src/main/resources/stylesheets/result-pdf.xsl new file mode 100644 index 000000000..fc6554306 --- /dev/null +++ b/library/src/main/resources/stylesheets/result-pdf.xsl @@ -0,0 +1,227 @@ + + + + + + + + + + + + + + + + + green + + + red + + + + + + green + + + red + + + + + + Das ZUGFeRD-PDF ist valide. + + + Das ZUGFeRD-PDF ist nicht valide. + + + + + + Es wird empfohlen das Dokument anzunehmen und weiter zu verarbeiten. + + + Es wird empfohlen das Dokument zurückzuweisen. + + + + + + + + + + + + + + + Prüfbericht + + + + + + + + + + + + + + + Referenz: + + + + + + + + + + Zeitpunkt der Prüfung: + + + + + + + + + + Erkannter Dokumenttyp: + + + + + + + + + + + + + Validierungsergebnisse im Detail: + + + + + + + + + + Type + + + Code + + + Schwere + + + Text + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + red + Fehler + + + Hinweis + + + + + + + + red + + + + + + + + + + red + + Pfad: + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/library/src/main/resources/stylesheets/xr-pdf/lib/konstanten.xsl b/library/src/main/resources/stylesheets/xr-pdf/lib/konstanten.xsl index de3b5459a..bcd846da7 100644 --- a/library/src/main/resources/stylesheets/xr-pdf/lib/konstanten.xsl +++ b/library/src/main/resources/stylesheets/xr-pdf/lib/konstanten.xsl @@ -1,235 +1,235 @@ - - - - - - SourceSerifPro - SourceSerifPro - - - - - - - - - - - 9pt - 12pt - 3 - true - de - - - - - 18pt - - false - all - - - - - 18pt - - bold - 4mm - false - all - always - - - - - 0.5pt solid - 2.5pt - all - 4mm - always - - - - - 9pt - - bold - 0.5pt solid - 4pt 6pt - always - 0 - - - - - 10pt - - bold - 1mm - 2mm - 2mm - all - always - - - - - 2mm - 3mm - 0.5pt dotted - #999999 - always - - - - #eeeeee - 28 - - - 7pt - 10pt - 1mm - 1mm - 2mm - 1mm - - - - 9pt - 10pt - - 1mm - 1mm - 2mm - 2mm - always - - - - - 9pt - left - 0.2mm - 0.2mm - 0mm - 1mm - 0mm - always - - - - 9pt - left - 0.2mm - 0.2mm - 0mm - 1mm - 0mm - always - - - - 9pt - right - 0.2mm - 0.2mm - 0mm - 0mm - 0mm - always - - - - 9pt - left - 0.4mm - 0.2mm - 0mm - 1mm - 0.1pt solid #999999 - always - - - - 9pt - left - 0.4mm - 0.2mm - 0mm - 1mm - 0.1pt solid #999999 - always - - - - 9pt - bold - right - 0.4mm - 0.2mm - 0mm - 0mm - 0.1pt solid #999999 - always - - - - - 10mm - - - - 2mm - always - all - - - - 2mm - - - - - 2pt - 2pt - 100% - fixed - 2mm - - - - bold - - - - - - - - - 80% - italic - - - - 2pt - 2pt - 100% - fixed - 80% - italic - - - \ No newline at end of file + + + + + + SourceSansPro + SourceSerifPro + + + + + + + + + + + 9pt + 12pt + 3 + true + de + + + + + 18pt + + false + all + + + + + 18pt + + bold + 4mm + false + all + always + + + + + 0.5pt solid + 2.5pt + all + 4mm + always + + + + + 9pt + + bold + 0.5pt solid + 4pt 6pt + always + 0 + + + + + 10pt + + bold + 1mm + 2mm + 2mm + all + always + + + + + 2mm + 3mm + 0.5pt dotted + #999999 + always + + + + #eeeeee + 28 + + + 7pt + 10pt + 1mm + 1mm + 2mm + 1mm + + + + 9pt + 10pt + + 1mm + 1mm + 2mm + 2mm + always + + + + + 9pt + left + 0.2mm + 0.2mm + 0mm + 1mm + 0mm + always + + + + 9pt + left + 0.2mm + 0.2mm + 0mm + 1mm + 0mm + always + + + + 9pt + right + 0.2mm + 0.2mm + 0mm + 0mm + 0mm + always + + + + 9pt + left + 0.4mm + 0.2mm + 0mm + 1mm + 0.1pt solid #999999 + always + + + + 9pt + left + 0.4mm + 0.2mm + 0mm + 1mm + 0.1pt solid #999999 + always + + + + 9pt + bold + right + 0.4mm + 0.2mm + 0mm + 0mm + 0.1pt solid #999999 + always + + + + + 10mm + + + + 2mm + always + all + + + + 2mm + + + + + 2pt + 2pt + 100% + fixed + 2mm + + + + bold + + + + + + + + + 80% + italic + + + + 2pt + 2pt + 100% + fixed + 80% + italic + + + diff --git a/library/src/test/java/org/mustangproject/ZUGFeRD/VisualizationTest.java b/library/src/test/java/org/mustangproject/ZUGFeRD/VisualizationTest.java index a59199f43..3ab73e7d7 100644 --- a/library/src/test/java/org/mustangproject/ZUGFeRD/VisualizationTest.java +++ b/library/src/test/java/org/mustangproject/ZUGFeRD/VisualizationTest.java @@ -1,289 +1,313 @@ -/** - * ********************************************************************* - *

- * Copyright 2019 Jochen Staerk - *

- * Use is subject to license terms. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy - * of the License at http://www.apache.org/licenses/LICENSE-2.0. - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - *

- * See the License for the specific language governing permissions and - * limitations under the License. - *

- * ********************************************************************** - */ -package org.mustangproject.ZUGFeRD; - -import org.junit.FixMethodOrder; -import org.junit.runners.MethodSorters; -import org.mustangproject.util.ByteArraySearcher; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class VisualizationTest extends ResourceCase { - - final String TARGET_PDF_CII = "./target/testout-Visualization-cii.pdf"; - final String TARGET_PDF_UBL = "./target/testout-Visualization-cii.pdf"; - - public void testCIIVisualizationBasic() { - - // 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) - */ - result = zvi.visualize(CIIinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.FR) - .replace("\r", "") - .replace("\n", "") - .replace("\t", "") - .replace(" ", ""); - - File expectedResult = getResourceAsFile("factur-x-vis.fr.html"); - expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8) - .replace("\r", "") - .replace("\n", "") - .replace("\t", "") - .replace(" ", ""); - // remove linebreaks as well... - - } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: " + e.getMessage()); - } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: " + e.getMessage()); - } catch (TransformerException e) { - fail("TransformerException should not happen: " + e.getMessage()); - } catch (IOException e) { - fail("IOException should not happen: " + e.getMessage()); - } catch (ParserConfigurationException e) { - fail("ParserConfigurationException should not happen: " + e.getMessage()); - } catch (SAXException e) { - fail("SAXException should not happen: " + e.getMessage()); - } - - - assertNotNull(result); - // Reading ZUGFeRD - assertEquals(expected, result); - } - - public void testCIIVisualizationExtended() { - - // the writing part - String sourceFilename = "factur-x-extended.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) - */ - result = zvi.visualize(CIIinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.DE).replace("\r", "").replace("\n", "") - .replace("\t", "") - .replace(" ", ""); - - File expectedResult = getResourceAsFile("factur-x-vis-extended.de.html"); - expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r", "").replace("\n", "") - .replace("\t", "") - .replace(" ", ""); - // remove linebreaks as well... - - } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: " + e.getMessage()); - } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: " + e.getMessage()); - } catch (TransformerException e) { - fail("TransformerException should not happen: " + e.getMessage()); - } catch (IOException e) { - fail("IOException should not happen: " + e.getMessage()); - } catch (ParserConfigurationException e) { - fail("ParserConfigurationException should not happen: " + e.getMessage()); - } catch (SAXException e) { - fail("SAXException should not happen: " + e.getMessage()); - } - - - assertNotNull(result); - // Reading ZUGFeRD - assertEquals(expected, result); - } - - public void testUBLCreditNoteVisualizationBasic() { - - // the writing part - File UBLinputFile = getResourceAsFile("ubl-creditnote.xml"); - - 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) - */ - result = zvi.visualize(UBLinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.EN).replace("\r", "").replace("\n", "").replace("\t", "").replace(" ", ""); - - File expectedResult = getResourceAsFile("factur-x-vis-ubl-creditnote.en.html"); - expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r", "").replace("\n", "").replace("\t", "").replace(" ", ""); - // remove linebreaks as well... - - } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: " + e.getMessage()); - } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: " + e.getMessage()); - } catch (TransformerException e) { - fail("TransformerException should not happen: " + e.getMessage()); - } catch (IOException e) { - fail("IOException should not happen: " + e.getMessage()); - } catch (ParserConfigurationException e) { - fail("ParserConfigurationException should not happen: " + e.getMessage()); - } catch (SAXException e) { - fail("SAXException should not happen: " + e.getMessage()); - } - - - assertNotNull(result); - // Reading ZUGFeRD - assertEquals(expected, result); - } - - - public void testUBLVisualizationBasic() { - - // the writing part - File UBLinputFile = getResourceAsFile("ubl/01.01a-INVOICE.ubl.xml"); - - 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) - */ - result = zvi.visualize(UBLinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.EN).replace("\r", "").replace("\n", "") - .replace("\t", "") - .replace(" ", ""); - - File expectedResult = getResourceAsFile("factur-x-vis-ubl.en.html"); - expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r", "").replace("\n", "") - .replace("\t", "") - .replace(" ", ""); - // remove linebreaks as well... - - } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: " + e.getMessage()); - } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: " + e.getMessage()); - } catch (TransformerException e) { - fail("TransformerException should not happen: " + e.getMessage()); - } catch (IOException e) { - fail("IOException should not happen: " + e.getMessage()); - } catch (ParserConfigurationException e) { - fail("ParserConfigurationException should not happen: " + e.getMessage()); - } catch (SAXException e) { - fail("SAXException should not happen: " + e.getMessage()); - } - - - assertNotNull(result); - // Reading ZUGFeRD - assertEquals(expected, result); - } - - public void testPDFVisualizationCII() { - - File CIIinputFile = getResourceAsFile("cii/01.01a-INVOICE.cii.xml"); - - // the writing part - - String expected = null; - String result = null; - try { - ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); - zvi.toPDF(CIIinputFile.getAbsolutePath(), TARGET_PDF_CII); - } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: " + e.getMessage()); - } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: " + e.getMessage()); - } - - try { - assertTrue(ByteArraySearcher.startsWith(Files.readAllBytes(Paths.get(TARGET_PDF_CII)), new byte[]{'%', 'P', 'D', 'F'})); - } catch (IOException e) { - fail("IOException should not occur"); - } - } - - public void testPDFVisualizationUBL() { - - File UBLinputFile = getResourceAsFile("ubl/01.01a-INVOICE.ubl.xml"); - - // the writing part - String sourceFilename = "factur-x.xml"; - - String expected = null; - String result = null; - try { - ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); - zvi.toPDF(UBLinputFile.getAbsolutePath(), TARGET_PDF_UBL); - } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: " + e.getMessage()); - } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: " + e.getMessage()); - } - - - try { - assertTrue(ByteArraySearcher.startsWith(Files.readAllBytes(Paths.get(TARGET_PDF_CII)), new byte[]{'%', 'P', 'D', 'F'})); - } catch (IOException e) { - fail("IOException should not occur"); - } - } - - public void testPDFVisualizationUBLCreditNote() { - - File UBLinputFile = getResourceAsFile("ubl/UBL-CreditNote-2.1-Example.ubl.xml"); - - // the writing part - String sourceFilename = "factur-x.xml"; - - String expected = null; - String result = null; - try { - ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); - zvi.toPDF(UBLinputFile.getAbsolutePath(), TARGET_PDF_UBL); - } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: " + e.getMessage()); - } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: " + e.getMessage()); - } - - - try { - assertTrue(ByteArraySearcher.startsWith(Files.readAllBytes(Paths.get(TARGET_PDF_CII)), new byte[]{'%', 'P', 'D', 'F'})); - } catch (IOException e) { - fail("IOException should not occur"); - } - } - -} +/** + * ********************************************************************* + *

+ * Copyright 2019 Jochen Staerk + *

+ * Use is subject to license terms. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0. + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + *

+ * See the License for the specific language governing permissions and + * limitations under the License. + *

+ * ********************************************************************** + */ +package org.mustangproject.ZUGFeRD; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; +import org.mustangproject.util.ByteArraySearcher; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class VisualizationTest extends ResourceCase { + + final String TARGET_PDF_CII = "./target/testout-Visualization-cii.pdf"; + final String TARGET_PDF_UBL = "./target/testout-Visualization-cii.pdf"; + + public void testCIIVisualizationBasic() { + + // 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) + */ + result = zvi.visualize(CIIinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.FR); + Files.write(Paths.get("./target/testout-factur-x-vis.fr.html"), result.getBytes(StandardCharsets.UTF_8)); + result = result + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + + File expectedResult = getResourceAsFile("factur-x-vis.fr.html"); + expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8) + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + // remove linebreaks as well... + + } catch (UnsupportedOperationException e) { + fail("UnsupportedOperationException should not happen: " + e.getMessage()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should not happen: " + e.getMessage()); + } catch (TransformerException e) { + fail("TransformerException should not happen: " + e.getMessage()); + } catch (IOException e) { + fail("IOException should not happen: " + e.getMessage()); + } catch (ParserConfigurationException e) { + fail("ParserConfigurationException should not happen: " + e.getMessage()); + } catch (SAXException e) { + fail("SAXException should not happen: " + e.getMessage()); + } + + + assertNotNull(result); + // Reading ZUGFeRD + assertEquals(expected, result); + } + + public void testCIIVisualizationExtended() { + + // the writing part + String sourceFilename = "factur-x-extended.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) + */ + result = zvi.visualize(CIIinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.DE); + Files.write(Paths.get("./target/testout-factur-x-vis-extended.de.html"), result.getBytes(StandardCharsets.UTF_8)); + result = result + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + + File expectedResult = getResourceAsFile("factur-x-vis-extended.de.html"); + expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8) + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + // remove linebreaks as well... + + } catch (UnsupportedOperationException e) { + fail("UnsupportedOperationException should not happen: " + e.getMessage()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should not happen: " + e.getMessage()); + } catch (TransformerException e) { + fail("TransformerException should not happen: " + e.getMessage()); + } catch (IOException e) { + fail("IOException should not happen: " + e.getMessage()); + } catch (ParserConfigurationException e) { + fail("ParserConfigurationException should not happen: " + e.getMessage()); + } catch (SAXException e) { + fail("SAXException should not happen: " + e.getMessage()); + } + + + assertNotNull(result); + // Reading ZUGFeRD + assertEquals(expected, result); + } + + public void testUBLCreditNoteVisualizationBasic() { + + // the writing part + File UBLinputFile = getResourceAsFile("ubl-creditnote.xml"); + + 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) + */ + result = zvi.visualize(UBLinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.EN); + Files.write(Paths.get("./target/testout-factur-x-vis-ubl-creditnote.en.html"), result.getBytes(StandardCharsets.UTF_8)); + result = result + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + + File expectedResult = getResourceAsFile("factur-x-vis-ubl-creditnote.en.html"); + expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8) + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + // remove linebreaks as well... + + } catch (UnsupportedOperationException e) { + fail("UnsupportedOperationException should not happen: " + e.getMessage()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should not happen: " + e.getMessage()); + } catch (TransformerException e) { + fail("TransformerException should not happen: " + e.getMessage()); + } catch (IOException e) { + fail("IOException should not happen: " + e.getMessage()); + } catch (ParserConfigurationException e) { + fail("ParserConfigurationException should not happen: " + e.getMessage()); + } catch (SAXException e) { + fail("SAXException should not happen: " + e.getMessage()); + } + + + assertNotNull(result); + // Reading ZUGFeRD + assertEquals(expected, result); + } + + + public void testUBLVisualizationBasic() { + + // the writing part + File UBLinputFile = getResourceAsFile("ubl/01.01a-INVOICE.ubl.xml"); + + 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) + */ + result = zvi.visualize(UBLinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.EN); + Files.write(Paths.get("./target/testout-factur-x-vis-ubl.en.html"), result.getBytes(StandardCharsets.UTF_8)); + result = result + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + + File expectedResult = getResourceAsFile("factur-x-vis-ubl.en.html"); + expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8) + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + // remove linebreaks as well... + + } catch (UnsupportedOperationException e) { + fail("UnsupportedOperationException should not happen: " + e.getMessage()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should not happen: " + e.getMessage()); + } catch (TransformerException e) { + fail("TransformerException should not happen: " + e.getMessage()); + } catch (IOException e) { + fail("IOException should not happen: " + e.getMessage()); + } catch (ParserConfigurationException e) { + fail("ParserConfigurationException should not happen: " + e.getMessage()); + } catch (SAXException e) { + fail("SAXException should not happen: " + e.getMessage()); + } + + + assertNotNull(result); + // Reading ZUGFeRD + assertEquals(expected, result); + } + + public void testPDFVisualizationCII() { + + File CIIinputFile = getResourceAsFile("cii/01.01a-INVOICE.cii.xml"); + + // the writing part + + String expected = null; + String result = null; + try { + ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); + zvi.toPDF(CIIinputFile.getAbsolutePath(), TARGET_PDF_CII); + } catch (UnsupportedOperationException e) { + fail("UnsupportedOperationException should not happen: " + e.getMessage()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should not happen: " + e.getMessage()); + } + + try { + assertTrue(ByteArraySearcher.startsWith(Files.readAllBytes(Paths.get(TARGET_PDF_CII)), new byte[]{'%', 'P', 'D', 'F'})); + } catch (IOException e) { + fail("IOException should not occur"); + } + } + + public void testPDFVisualizationUBL() { + + File UBLinputFile = getResourceAsFile("ubl/01.01a-INVOICE.ubl.xml"); + + // the writing part + String sourceFilename = "factur-x.xml"; + + String expected = null; + String result = null; + try { + ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); + zvi.toPDF(UBLinputFile.getAbsolutePath(), TARGET_PDF_UBL); + } catch (UnsupportedOperationException e) { + fail("UnsupportedOperationException should not happen: " + e.getMessage()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should not happen: " + e.getMessage()); + } + + + try { + assertTrue(ByteArraySearcher.startsWith(Files.readAllBytes(Paths.get(TARGET_PDF_CII)), new byte[]{'%', 'P', 'D', 'F'})); + } catch (IOException e) { + fail("IOException should not occur"); + } + } + + public void testPDFVisualizationUBLCreditNote() { + + File UBLinputFile = getResourceAsFile("ubl/UBL-CreditNote-2.1-Example.ubl.xml"); + + // the writing part + String sourceFilename = "factur-x.xml"; + + String expected = null; + String result = null; + try { + ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); + zvi.toPDF(UBLinputFile.getAbsolutePath(), TARGET_PDF_UBL); + } catch (UnsupportedOperationException e) { + fail("UnsupportedOperationException should not happen: " + e.getMessage()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should not happen: " + e.getMessage()); + } + + + try { + assertTrue(ByteArraySearcher.startsWith(Files.readAllBytes(Paths.get(TARGET_PDF_CII)), new byte[]{'%', 'P', 'D', 'F'})); + } catch (IOException e) { + fail("IOException should not occur"); + } + } + +}