From de8f96fccd464caa4a938e53a9852df2326749b3 Mon Sep 17 00:00:00 2001 From: Baxter Date: Tue, 9 Jul 2024 11:25:36 +0200 Subject: [PATCH 1/2] Fix CSS --- data/xsl/xrechnung-viewer.css | 3 ++- gradle.properties | 2 +- version-badge.svg | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/data/xsl/xrechnung-viewer.css b/data/xsl/xrechnung-viewer.css index accb929..46b101c 100644 --- a/data/xsl/xrechnung-viewer.css +++ b/data/xsl/xrechnung-viewer.css @@ -644,11 +644,12 @@ button { display: block; float: left; - width: calc(100vw - 170px); + width: 100%; padding: 11px 10px !important; line-height: 1.3; min-height: 38px; height: auto; + max-width: calc(100vw - 100px); overflow-x: auto; } diff --git a/gradle.properties b/gradle.properties index 247a0d6..c95d58e 100755 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ -currentVersion=0.1.2 +currentVersion=0.1.3 mainClassName=io.github.easybill.xrviz.App diff --git a/version-badge.svg b/version-badge.svg index 73d0a33..1f0e0f7 100644 --- a/version-badge.svg +++ b/version-badge.svg @@ -1,5 +1,5 @@ - + @@ -15,7 +15,7 @@ Version - - 0.1.2 + + 0.1.3 From 4fc4584329fc4748df92d0eb6318024e83de7163 Mon Sep 17 00:00:00 2001 From: Baxter Date: Thu, 18 Jul 2024 12:22:49 +0200 Subject: [PATCH 2/2] Add UBL invoice and credit note XML support --- gradle.properties | 2 +- .../github/easybill/xrviz/XslTransformer.java | 52 ++- .../xrviz/handler/XmlRequestExtractor.java | 10 +- src/test/http/api-test.http | 30 +- src/test/http/ubl-creditnote.xml | 215 +++++++++ src/test/http/ubl-invoice.xml | 410 ++++++++++++++++++ version-badge.svg | 6 +- 7 files changed, 712 insertions(+), 13 deletions(-) create mode 100644 src/test/http/ubl-creditnote.xml create mode 100644 src/test/http/ubl-invoice.xml diff --git a/gradle.properties b/gradle.properties index c95d58e..bb9040c 100755 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ -currentVersion=0.1.3 +currentVersion=0.1.4 mainClassName=io.github.easybill.xrviz.App diff --git a/src/main/java/io/github/easybill/xrviz/XslTransformer.java b/src/main/java/io/github/easybill/xrviz/XslTransformer.java index 6d242b5..e94d9a3 100644 --- a/src/main/java/io/github/easybill/xrviz/XslTransformer.java +++ b/src/main/java/io/github/easybill/xrviz/XslTransformer.java @@ -20,16 +20,53 @@ import java.io.*; import java.nio.file.Paths; import java.util.Arrays; +import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; public class XslTransformer { static final Logger logger = Logger.getGlobal(); static final String BASE_PATH = Config.getValue(Config.Keys.DATA_PATH); + public static final String CII_VALIDATION_STRING = " detectDocumentType(String xmlContent) { + if (xmlContent == null || xmlContent.isEmpty()) { + return Optional.empty(); + } + + if (xmlContent.contains(CII_VALIDATION_STRING)) { + return Optional.of(CII); + } else if (xmlContent.contains(UBL_I_VALIDATION_STRING)) { + return Optional.of(UBL_I); + } else if (xmlContent.contains(UBL_C_VALIDATION_STRING)) { + return Optional.of(UBL_C); + } + + return Optional.empty(); + } + } public static void validateFiles() { String[] files = { "xsl/cii-xr.xsl", + "xsl/ubl-invoice-xr.xsl", "xsl/xrechnung-html.xsl", "xsl/xr-pdf.xsl", "fop/fop.xconf" @@ -47,24 +84,27 @@ public static void validateFiles() { } } - private static DOMSource transformCiiToXr(String inputXml) throws TransformerException { + private static DOMSource transformXmlToXr(String inputXml, DocumentType type) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); - StreamSource xslCiiXr = new StreamSource("data/xsl/cii-xr.xsl"); - Transformer ciiXrTransformer = factory.newTransformer(xslCiiXr); + StreamSource source = new StreamSource("data/xsl/" + type.getXslName()); + Transformer transformer = factory.newTransformer(source); Source xml = new StreamSource(new StringReader(inputXml)); DOMResult domResult = new DOMResult(); - ciiXrTransformer.transform(xml, domResult); + transformer.transform(xml, domResult); return new DOMSource(domResult.getNode()); } public static String transformToHtml(String inputXml, String language) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); StreamSource xslXrHtml = new StreamSource("data/xsl/xrechnung-html.xsl"); + Transformer xslXrTransformer = factory.newTransformer(xslXrHtml); xslXrTransformer.setParameter("lang", language); - DOMSource transformedSource = transformCiiToXr(inputXml); + + DOMSource transformedSource = transformXmlToXr(inputXml, DocumentType.detectDocumentType(inputXml).orElseThrow()); StringWriter outputString = new StringWriter(); xslXrTransformer.transform(transformedSource, new StreamResult(outputString)); + return outputString.toString(); } @@ -74,7 +114,7 @@ public static byte[] transformToPdf(String inputXml, String language) throws Tra Transformer xslXrTransformer = factory.newTransformer(xslXrPdf); xslXrTransformer.setParameter("lang", language); - DOMSource transformedSource = transformCiiToXr(inputXml); + DOMSource transformedSource = transformXmlToXr(inputXml, DocumentType.detectDocumentType(inputXml).orElseThrow()); StringWriter outputString = new StringWriter(); xslXrTransformer.transform(transformedSource, new StreamResult(outputString)); String foXmlString = outputString.toString(); diff --git a/src/main/java/io/github/easybill/xrviz/handler/XmlRequestExtractor.java b/src/main/java/io/github/easybill/xrviz/handler/XmlRequestExtractor.java index 415b986..6f01fba 100644 --- a/src/main/java/io/github/easybill/xrviz/handler/XmlRequestExtractor.java +++ b/src/main/java/io/github/easybill/xrviz/handler/XmlRequestExtractor.java @@ -9,9 +9,10 @@ import java.net.HttpURLConnection; import java.util.logging.Logger; +import static io.github.easybill.xrviz.XslTransformer.*; + public abstract class XmlRequestExtractor { static final Logger logger = Logger.getGlobal(); - private static final String CII_VALIDATION_STRING = " validate(HttpExchange exchange) throws IOException { if (!exchange.getRequestMethod().equalsIgnoreCase("POST")) { @@ -23,7 +24,7 @@ Optional validate(HttpExchange exchange) throws IOException { String xml = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8); - if (xml.isBlank() || !xml.contains(CII_VALIDATION_STRING)) { + if (!isXMLValid(xml)) { logger.severe("Invalid XML content!"); exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_REQUEST, -1); @@ -38,4 +39,9 @@ String getLanguage(HttpExchange exchange) { return acceptLanguage != null && acceptLanguage.toLowerCase().contains("en") ? "en" : "de"; } + private boolean isXMLValid(String xml) { + return !xml.isBlank() && (xml.contains(CII_VALIDATION_STRING) || + xml.contains(UBL_I_VALIDATION_STRING) || + xml.contains(UBL_C_VALIDATION_STRING)); + } } diff --git a/src/test/http/api-test.http b/src/test/http/api-test.http index 98fae59..76af653 100644 --- a/src/test/http/api-test.http +++ b/src/test/http/api-test.http @@ -10,9 +10,37 @@ Accept-Language: de < ./EN16931_Einfach.xml +### Generate a HTML file from a UBL Invoice file +POST {{baseUrl}}/convert.html +Content-Type: application/xml +Accept-Language: de + +< ./ubl-invoice.xml + +### Generate a HTML file from a UBL CreditNote file +POST {{baseUrl}}/convert.html +Content-Type: application/xml +Accept-Language: de + +< ./ubl-creditnote.xml + ### Generate a PDF file from a XML file POST {{baseUrl}}/convert.pdf Content-Type: application/xml Accept-Language: de -< ./EN16931_Einfach.xml \ No newline at end of file +< ./EN16931_Einfach.xml + +### Generate a PDF file from a UBL Invoice file +POST {{baseUrl}}/convert.pdf +Content-Type: application/xml +Accept-Language: de + +< ./ubl-invoice.xml + +### Generate a PDF file from a UBL CreditNote file +POST {{baseUrl}}/convert.pdf +Content-Type: application/xml +Accept-Language: de + +< ./ubl-creditnote.xml diff --git a/src/test/http/ubl-creditnote.xml b/src/test/http/ubl-creditnote.xml new file mode 100644 index 0000000..3eb0813 --- /dev/null +++ b/src/test/http/ubl-creditnote.xml @@ -0,0 +1,215 @@ + + + urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0 + urn:fdc:peppol.eu:2017:poacc:billing:01:1.0 + Snippet1 + 2017-11-13 + 381 + Please note we have a new phone number: 22 22 22 22 + EUR + 4025:123:4343 + 0150abc + + + Snippet1 + + + + + 9482348239847239874 + + 99887766 + + + SupplierTradingName Ltd. + + + Main street 1 + Postbox 123 + London + GB 123 EW + + GB + + + + GB1232434 + + VAT + + + + SupplierOfficialName Ltd + GB983294 + + + + + + FR23342 + + FR23342 + + + BuyerTradingName AS + + + Hovedgatan 32 + Po box 878 + Stockholm + 456 34 + + SE + + + + SE4598375937 + + VAT + + + + Buyer Official Name + 39937423947 + + + Lisa Johnson + 23434234 + lj@buyer.se + + + + + 2017-11-01 + + 9483759475923478 + + Delivery street 2 + Building 56 + Stockholm + 21234 + + SE + + + + + + Delivery party Name + + + + + 30 + Snippet1 + + IBAN32423940 + AccountName + + BIC324098 + + + + + Payment within 10 days, 2% discount + + + true + Insurance + 25 + + S + 25.0 + + VAT + + + + + 331.25 + + 1325 + 331.25 + + S + 25.0 + + VAT + + + + + + 1300 + 1325 + 1656.25 + 25 + 1656.25 + + + + 1 + 7 + 2800 + Konteringsstreng + + 123 + + + Description of item + item name + + 21382183120983 + + + NO + + + 09348023 + + + S + 25.0 + + VAT + + + + + 400 + + + + 2 + -3 + -1500 + + 123 + + + Description 2 + item name 2 + + 21382183120983 + + + NO + + + 09348023 + + + S + 25.0 + + VAT + + + + + 500 + + + diff --git a/src/test/http/ubl-invoice.xml b/src/test/http/ubl-invoice.xml new file mode 100644 index 0000000..5b53c4f --- /dev/null +++ b/src/test/http/ubl-invoice.xml @@ -0,0 +1,410 @@ + + + + urn:cen.eu:en16931:2017 + 1 + TOSL110 + 2013-04-10 + 2013-05-10 + 380 + Ordered through our website#Ordering information + DKK + EUR + 67543 + qwerty + + 2013-03-10 + 2013-04-10 + + + PO4711 + 123 + + + + TOSL109 + 2013-03-10 + + + + 5433 + + + 3544 + + + Lot567 + + + 2013-05 + + + OBJ999 + ATS + + + sales slip + your sales slip + + VGVzdGluZyBCYXNlNjQgZW5jb2Rpbmc= + + + + Project345 + + + + info@selco.nl + + 5790000436101 + + + SelCo + + + Hoofdstraat 4 + Om de hoek + Grootstad + 54321 + Overijssel + + NL + + + + NL16356706 + + VAT + + + + NL16356706 + + LOC + + + + SellerCompany + NL16356706 + Export + + + Anthon Larsen + +3198989898 + Anthon@Selco.nl + + + + + + info@buyercompany.dk + + 5790000436057 + + + Buyco + + + Anystreet, Building 1 + 5th floor + Anytown + 101 + Jutland + + DK + + + + DK16356607 + + VAT + + + + Buyercompany ltd + DK16356607 + + + John Hansen + +4598989898 + john.hansen@buyercompany.dk + + + + + + DK16356608 + + + Dagobert Duck + + + DK16356608 + + + + + Dick Panama + + + Anystreet, Building 1 + 6th floor + Anytown + 101 + Jutland + + DK + + + + DK16356609 + + VAT + + + + + 2013-04-15 + + 5790000436068 + + Deliverystreet + Gate 15 + Deliverycity + 9000 + Jutland + + DK + + + + + + Logistic service Ltd + + + + + 49 + Half prepaid + Payref1 + + 123456 + + DK1212341234123412 + + + + + 50% prepaid, 50% within one month + + + false + 100 + Loyal customer + 10 + 150.00 + 1500.00 + + S + 25 + + VAT + + + + + true + ABL + Packaging + 10 + 150.00 + 1500.00 + + S + 25 + + VAT + + + + + 675.00 + + 1500.00 + 375.00 + + S + 25 + + VAT + + + + + 2500.00 + 300.00 + + S + 12 + + VAT + + + + + + 628.62 + + + 4000.00 + 4000.00 + 4675.00 + 150.00 + 150.00 + 2337.50 + 2337.50 + + + 1 + first line + 1000 + 1000.00 + ACC7654 + + 2013-03-10 + 2013-04-10 + + + 1 + + + Object2 + + + false + 100 + Loyal customer + 10 + 100.00 + 1000.00 + + + true + ABL + Packaging + 10 + 100.00 + 1000.00 + + + Printing paper, 2mm + Printing paper + + BUY123 + + + JB007 + + + 1234567890128 + + + NL + + + 12344321 + + + S + 25 + + VAT + + + + Thickness + 2 mm + + + + 1.00 + 1 + + false + 0.10 + 1.10 + + + + + 2 + second line + 100 + 500.00 + ACC7654 + + 2013-03-10 + 2013-04-10 + + + 2 + + + Object2 + + + Parker Pen, Black, model Sansa + Parker Pen + + JB008 + + + NL + + + S + 25 + + VAT + + + + + 5.00 + + + + 3 + 500 + 2500.00 + + American Cookies + + JB009 + + + S + 12 + + VAT + + + + + 5.00 + + + diff --git a/version-badge.svg b/version-badge.svg index 1f0e0f7..7d07e5e 100644 --- a/version-badge.svg +++ b/version-badge.svg @@ -1,5 +1,5 @@ - + @@ -15,7 +15,7 @@ Version - - 0.1.3 + + 0.1.4