Use enum values for PDF/A conformance levels and ZUGFeRD conformance levels

This commit is contained in:
Yannik Hampe
2017-05-16 17:32:37 +02:00
parent c196fae12d
commit c69b9fe815
4 changed files with 97 additions and 30 deletions

View File

@@ -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.");
}
}

View File

@@ -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");
}
}
}

View File

@@ -0,0 +1,5 @@
package org.mustangproject.ZUGFeRD;
public enum ZUGFeRDConformanceLevel {
BASIC, COMFORT, EXTENDED;
}

View File

@@ -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);