From c69b9fe815ccf577cdc7f99471002c7125caeee7 Mon Sep 17 00:00:00 2001 From: Yannik Hampe Date: Tue, 16 May 2017 17:32:37 +0200 Subject: [PATCH] Use enum values for PDF/A conformance levels and ZUGFeRD conformance levels --- .../ZUGFeRD/PDFAConformanceLevel.java | 24 +++++ .../ZUGFeRD/XMPSchemaZugferd.java | 6 +- .../ZUGFeRD/ZUGFeRDConformanceLevel.java | 5 + .../ZUGFeRD/ZUGFeRDExporter.java | 92 +++++++++++++------ 4 files changed, 97 insertions(+), 30 deletions(-) create mode 100644 mustang/src/main/java/org/mustangproject/ZUGFeRD/PDFAConformanceLevel.java create mode 100644 mustang/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDConformanceLevel.java diff --git a/mustang/src/main/java/org/mustangproject/ZUGFeRD/PDFAConformanceLevel.java b/mustang/src/main/java/org/mustangproject/ZUGFeRD/PDFAConformanceLevel.java new file mode 100644 index 00000000..cb51e151 --- /dev/null +++ b/mustang/src/main/java/org/mustangproject/ZUGFeRD/PDFAConformanceLevel.java @@ -0,0 +1,24 @@ +package org.mustangproject.ZUGFeRD; + +public enum PDFAConformanceLevel { + ACCESSIBLE("A"), BASIC("B"), UNICODE("U"); + + private final String letter; + + PDFAConformanceLevel(String letter) { + this.letter = letter; + } + + public String getLetter() { + return letter; + } + + public static PDFAConformanceLevel findByLetter(String letter) { + for (PDFAConformanceLevel candidate : values()) { + if (candidate.letter.equals(letter)) { + return candidate; + } + } + throw new IllegalArgumentException("PDF conformance level <" + letter + "> is unknown."); + } +} diff --git a/mustang/src/main/java/org/mustangproject/ZUGFeRD/XMPSchemaZugferd.java b/mustang/src/main/java/org/mustangproject/ZUGFeRD/XMPSchemaZugferd.java index 0c9b2347..c5c5ab76 100644 --- a/mustang/src/main/java/org/mustangproject/ZUGFeRD/XMPSchemaZugferd.java +++ b/mustang/src/main/java/org/mustangproject/ZUGFeRD/XMPSchemaZugferd.java @@ -15,14 +15,14 @@ public class XMPSchemaZugferd extends XMPSchema { /** * This is what needs to be added to the RDF metadata - basically the name of the embedded Zugferd file */ - public XMPSchemaZugferd(XMPMetadata metadata, String conformanceLevel) { + public XMPSchemaZugferd(XMPMetadata metadata, ZUGFeRDConformanceLevel conformanceLevel) { super(metadata, "urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#", "zf", "ZUGFeRD Schema"); setAboutAsSimple(""); - setTextPropertyValue("ConformanceLevel", conformanceLevel); + setTextPropertyValue("ConformanceLevel", conformanceLevel.name()); setTextPropertyValue("DocumentType", "INVOICE"); setTextPropertyValue("DocumentFileName", "ZUGFeRD-invoice.xml"); setTextPropertyValue("Version", "1.0"); } -} \ No newline at end of file +} diff --git a/mustang/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDConformanceLevel.java b/mustang/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDConformanceLevel.java new file mode 100644 index 00000000..2f2c7fd3 --- /dev/null +++ b/mustang/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDConformanceLevel.java @@ -0,0 +1,5 @@ +package org.mustangproject.ZUGFeRD; + +public enum ZUGFeRDConformanceLevel { + BASIC, COMFORT, EXTENDED; +} diff --git a/mustang/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDExporter.java b/mustang/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDExporter.java index aff6c824..de696523 100644 --- a/mustang/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDExporter.java +++ b/mustang/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDExporter.java @@ -26,8 +26,6 @@ import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.activation.FileDataSource; import javax.xml.bind.JAXBContext; @@ -226,11 +224,11 @@ public class ZUGFeRDExporter implements Closeable { } // // MAIN CLASS - private String conformanceLevel = "U"; + private PDFAConformanceLevel conformanceLevel = PDFAConformanceLevel.UNICODE; private String versionStr = "1.4.0"; // BASIC, COMFORT etc - may be set from outside. - private String ZUGFeRDConformanceLevel = null; + private ZUGFeRDConformanceLevel zUGFeRDConformanceLevel = ZUGFeRDConformanceLevel.EXTENDED; /** * Data (XML invoice) to be added to the ZUGFeRD PDF. It may be externally @@ -308,7 +306,7 @@ public class ZUGFeRDExporter implements Closeable { /** * All files are PDF/A-3, setConformance refers to the level conformance. * - * PDF/A-3 has three coformance levels, called "A", "U" and "B". + * PDF/A-3 has three conformance levels, called "A", "U" and "B". * * PDF/A-3-B where B means only visually preservable, U -standard for * Mustang- means visually and unicode preservable and A means full @@ -319,10 +317,32 @@ public class ZUGFeRDExporter implements Closeable { * * */ - public void setConformanceLevel(String newLevel) { + public void setConformanceLevel(PDFAConformanceLevel newLevel) { + if (newLevel == null) { + throw new NullPointerException("pdf conformance level"); + } conformanceLevel = newLevel; } + /** + * All files are PDF/A-3, setConformance refers to the level conformance. + * + * PDF/A-3 has three conformance levels, called "A", "U" and "B". + * + * PDF/A-3-B where B means only visually preservable, U -standard for + * Mustang- means visually and unicode preservable and A means full + * compliance, i.e. visually, unicode and structurally preservable and + * tagged PDF, i.e. useful metainformation for blind people. + * + * Feel free to pass "A" as new level if you know what you are doing :-) + * + * @deprecated Use {@link #setConformanceLevel(org.mustangproject.ZUGFeRD.PDFAConformanceLevel)} instead + */ + @Deprecated + public void setConformanceLevel(String newLevel) { + conformanceLevel = PDFAConformanceLevel.findByLetter(newLevel); + } + /** * enables the flag to indicate a test invoice in the XML structure * @@ -506,23 +526,25 @@ public class ZUGFeRDExporter implements Closeable { AdobePDFSchema pdf = xmp.createAndAddAdobePDFSchema(); pdf.setProducer(fullProducer); - /* - * // Mandatory: PDF/A3-a is tagged PDF which has to be expressed using - * a // MarkInfo dictionary (PDF A/3 Standard sec. 6.7.2.2) PDMarkInfo - * markinfo = new PDMarkInfo(); markinfo.setMarked(true); - * doc.getDocumentCatalog().setMarkInfo(markinfo); - */ - /* - * - * To be on the safe side, we use level B without Markinfo because we - * can not guarantee that the user correctly tagged the templates for - * the PDF. - */ - try { - pdfaid.setConformance(conformanceLevel);//$NON-NLS-1$ //$NON-NLS-1$ - } catch (BadFieldValueException ex) { - Logger.getLogger(ZUGFeRDExporter.class.getName()).log(Level.SEVERE, null, ex); - } + /* + * // Mandatory: PDF/A3-a is tagged PDF which has to be expressed using + * a // MarkInfo dictionary (PDF A/3 Standard sec. 6.7.2.2) PDMarkInfo + * markinfo = new PDMarkInfo(); markinfo.setMarked(true); + * doc.getDocumentCatalog().setMarkInfo(markinfo); + */ + /* + * + * To be on the safe side, we use level B without Markinfo because we + * can not guarantee that the user correctly tagged the templates for + * the PDF. + */ + try { + pdfaid.setConformance(conformanceLevel.getLetter());//$NON-NLS-1$ //$NON-NLS-1$ + } catch (BadFieldValueException ex) { + // This should be impossible, because it would occur only if an illegal conformance level is + // supplied, however the enum enforces that the conformance level is valid. + throw new Error(ex); + } pdfaid.setPart(3); @@ -1533,11 +1555,27 @@ public class ZUGFeRDExporter implements Closeable { /** * Sets the ZUGFeRD conformance level (override). * - * @param ZUGFeRDConformanceLevel + * @param zUGFeRDConformanceLevel * the new conformance level */ - public void setZUGFeRDConformanceLevel(String ZUGFeRDConformanceLevel) { - this.ZUGFeRDConformanceLevel = ZUGFeRDConformanceLevel; + public void setZUGFeRDConformanceLevel(ZUGFeRDConformanceLevel zUGFeRDConformanceLevel) { + if (zUGFeRDConformanceLevel == null) { + throw new NullPointerException("ZUGFeRD conformance level"); + } + this.zUGFeRDConformanceLevel = zUGFeRDConformanceLevel; + } + + /** + * Sets the ZUGFeRD conformance level (override). + * + * @param zUGFeRDConformanceLevel + * the new conformance level + * + * @deprecated Use {@link #setConformanceLevel(PDFAConformanceLevel)} instead + */ + @Deprecated + public void setZUGFeRDConformanceLevel(String zUGFeRDConformanceLevel) { + this.zUGFeRDConformanceLevel = ZUGFeRDConformanceLevel.valueOf(zUGFeRDConformanceLevel); } /** @@ -1550,7 +1588,7 @@ public class ZUGFeRDExporter implements Closeable { private void addZugferdXMP(XMPMetadata metadata) { XMPSchemaZugferd zf = new XMPSchemaZugferd(metadata, - this.ZUGFeRDConformanceLevel); + this.zUGFeRDConformanceLevel); metadata.addSchema(zf);