Patches courtesy by Ivan Vaklinov (with one little modification), direct

XML access
This commit is contained in:
jstaerk
2014-08-08 19:34:20 +02:00
parent 7336d2a319
commit 2a1dfc54ef
13 changed files with 915 additions and 926 deletions

View File

@@ -4,7 +4,7 @@
<groupId>org.mustangproject.ZUGFeRD</groupId> <groupId>org.mustangproject.ZUGFeRD</groupId>
<artifactId>mustang</artifactId> <artifactId>mustang</artifactId>
<version>1.1.0</version> <version>1.1.1alpha</version>
<repositories> <repositories>
<repository> <repository>

View File

@@ -64,6 +64,13 @@ public class ZUGFeRDExporter {
private String conformanceLevel="U"; private String conformanceLevel="U";
private String versionStr="1.0"; private String versionStr="1.0";
/**
* Data (XML invoice) to be added to the ZUGFeRD PDF. It may be externally set, in which case passing a
* IZUGFeRDExportableTransaction is not necessary. By default it is null meaning the caller needs to
* pass a IZUGFeRDExportableTransaction for the XML to be populated.
*/
byte[] zugferdData = null;
private String currencyFormat(BigDecimal value, char decimalDelimiter) { private String currencyFormat(BigDecimal value, char decimalDelimiter) {
/* /*
* I needed 123,45, locale independent.I tried * I needed 123,45, locale independent.I tried
@@ -422,8 +429,12 @@ public class ZUGFeRDExporter {
} }
/** /**
* embed the Zugferd XML structure in a file named ZUGFeRD-invoice.xml * Embeds the Zugferd XML structure in a file named ZUGFeRD-invoice.xml.
* */ *
* @param doc PDDocument to attach an XML invoice to
* @param trans a IZUGFeRDExportableTransaction that provides the data-model to populate the XML.
* This parameter may be null, if so the XML data should hav ebeen set via <code>setZUGFeRDXMLData(byte[] zugferdData)</code>
*/
public void PDFattachZugferdFile(PDDocument doc, IZUGFeRDExportableTransaction trans) throws IOException { public void PDFattachZugferdFile(PDDocument doc, IZUGFeRDExportableTransaction trans) throws IOException {
// embedded files are stored in a named tree // embedded files are stored in a named tree
@@ -440,15 +451,13 @@ public class ZUGFeRDExporter {
dict.setString("UF", filename); //$NON-NLS-1$ dict.setString("UF", filename); //$NON-NLS-1$
if (zugferdData == null) // XML ZUGFeRD data not set externally, needs to be built
{
// create a dummy file stream, this would probably normally be a // create a dummy file stream, this would probably normally be a
// FileInputStream // FileInputStream
byte[] zugferdRaw = getZugferdXMLForTransaction(trans).getBytes("UTF-8"); //$NON-NLS-1$ byte[] zugferdRaw = getZugferdXMLForTransaction(trans).getBytes("UTF-8"); //$NON-NLS-1$
byte[] zugferdData;
if ((zugferdRaw[0]==(byte)0xEF)&&(zugferdRaw[1]==(byte)0xBB)&&(zugferdRaw[2]==(byte)0xBF)) { if ((zugferdRaw[0]==(byte)0xEF)&&(zugferdRaw[1]==(byte)0xBB)&&(zugferdRaw[2]==(byte)0xBF)) {
// I don't like BOMs, lets remove it // I don't like BOMs, lets remove it
zugferdData=new byte[zugferdRaw.length-3]; zugferdData=new byte[zugferdRaw.length-3];
@@ -456,6 +465,7 @@ public class ZUGFeRDExporter {
} else { } else {
zugferdData=zugferdRaw; zugferdData=zugferdRaw;
} }
}
ByteArrayInputStream fakeFile = new ByteArrayInputStream(zugferdData); ByteArrayInputStream fakeFile = new ByteArrayInputStream(zugferdData);
PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile); PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile);
@@ -481,6 +491,17 @@ public class ZUGFeRDExporter {
} }
/**
* Sets the ZUGFeRD XML data to be attached as a single byte array. This is useful for
* use-cases where the XML has already been produced by some external API or component.
*
* @param zugferdData XML data to be set as a byte array (XML file in raw form).
*/
public void setZUGFeRDXMLData(byte[] zugferdData)
{
this.zugferdData = zugferdData;
}
/*** /***
* This will add both the RDF-indication which embedded file is Zugferd and the * This will add both the RDF-indication which embedded file is Zugferd and the
* neccessary PDF/A schema extension description to be able to add this information to RDF * neccessary PDF/A schema extension description to be able to add this information to RDF

View File

@@ -11,6 +11,8 @@ package org.mustangproject.ZUGFeRD;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Map; import java.util.Map;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@@ -46,17 +48,34 @@ public class ZUGFeRDImporter {
private String IBAN; private String IBAN;
private String holder; private String holder;
private String amount; private String amount;
private String meta; /** Raw XML form of the extracted data - may be directly obtained. */
private byte[] rawXML=null;
private String bankName; private String bankName;
private boolean amountFound; private boolean amountFound;
/**
* Extracts a ZUGFeRD invoice from a PDF document represented by a file name.
* Errors are just logged to STDOUT.
*/
public void extract(String pdfFilename)
{
try
{
extractLowLevel(new BufferedInputStream(new FileInputStream(pdfFilename)));
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
/**
* Extracts a ZUGFeRD invoice from a PDF document represented by an input stream.
public void extract(String pdfFilename) { * Errors are reported via exception handling.
*/
public void extractLowLevel(InputStream pdfStream) throws IOException {
PDDocument doc = null; PDDocument doc = null;
try { try {
doc = PDDocument.load(pdfFilename); doc = PDDocument.load(pdfStream);
// PDDocumentInformation info = doc.getDocumentInformation(); // PDDocumentInformation info = doc.getDocumentInformation();
PDDocumentNameDictionary names = new PDDocumentNameDictionary( PDDocumentNameDictionary names = new PDDocumentNameDictionary(
doc.getDocumentCatalog()); doc.getDocumentCatalog());
@@ -85,16 +104,15 @@ public class ZUGFeRDImporter {
// ByteArrayOutputStream fileBytes=new // ByteArrayOutputStream fileBytes=new
// ByteArrayOutputStream(); // ByteArrayOutputStream();
// FileOutputStream fos = new FileOutputStream(file); // FileOutputStream fos = new FileOutputStream(file);
rawXML = embeddedFile.getByteArray();
setMeta(new String(embeddedFile.getByteArray())); setMeta(new String(rawXML));
// fos.write(embeddedFile.getByteArray()); // fos.write(embeddedFile.getByteArray());
// fos.close(); // fos.close();
} }
} }
} catch (IOException e1) { } catch (IOException e1) {
// TODO Auto-generated catch block throw e1;
e1.printStackTrace();
} }
finally { finally {
try { try {
@@ -121,7 +139,7 @@ public class ZUGFeRDImporter {
} }
try { try {
InputStream bais = new ByteArrayInputStream(meta.getBytes()); InputStream bais = new ByteArrayInputStream(rawXML);
document = builder.parse(bais); document = builder.parse(bais);
} catch (SAXException ex1) { } catch (SAXException ex1) {
ex1.printStackTrace(); ex1.printStackTrace();
@@ -339,11 +357,24 @@ public class ZUGFeRDImporter {
} }
public void setMeta(String meta) { public void setMeta(String meta) {
this.meta=meta; this.rawXML=meta.getBytes();
} }
public String getMeta() { public String getMeta() {
return meta; if (rawXML==null){
return null;
} else {
return new String(rawXML);
}
}
/**
* Returns the raw XML data as extracted from the ZUGFeRD PDF file.
*/
public byte[] getRawXML()
{
return rawXML;
} }
/** /**
@@ -353,6 +384,7 @@ public class ZUGFeRDImporter {
//SpecifiedExchangedDocumentContext is in the schema, so a relatively good indication if zugferd is present - better than just invoice //SpecifiedExchangedDocumentContext is in the schema, so a relatively good indication if zugferd is present - better than just invoice
String meta=getMeta();
return (meta!=null)&&( meta.length()>0)&&( meta.contains("SpecifiedExchangedDocumentContext")); //$NON-NLS-1$ return (meta!=null)&&( meta.length()>0)&&( meta.contains("SpecifiedExchangedDocumentContext")); //$NON-NLS-1$
} }
} }

View File

@@ -1,5 +1,5 @@
#Generated by Maven #Generated by Maven
#Sat Jun 28 19:58:49 CEST 2014 #Fri Aug 08 19:33:01 CEST 2014
version=1.1.0.alpha version=1.1.1alpha
groupId=org.mustangproject.ZUGFeRD groupId=org.mustangproject.ZUGFeRD
artifactId=mustang artifactId=mustang

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,60 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite failures="0" time="0.224" errors="0" skipped="0" tests="1" name="org.mustangproject.ZUGFeRD.AppTest">
<properties>
<property name="java.runtime.name" value="OpenJDK Runtime Environment"/>
<property name="sun.boot.library.path" value="/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64"/>
<property name="java.vm.version" value="23.25-b01"/>
<property name="java.vm.vendor" value="Sun Microsystems Inc."/>
<property name="java.vendor.url" value="http://java.sun.com/"/>
<property name="path.separator" value=":"/>
<property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/>
<property name="file.encoding.pkg" value="sun.io"/>
<property name="user.country" value="DE"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="sun.os.patch.level" value="unknown"/>
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
<property name="user.dir" value="/home/jstaerk/workspace/PDFA3/mustang"/>
<property name="java.runtime.version" value="1.6.0_31-b31"/>
<property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
<property name="java.endorsed.dirs" value="/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/endorsed"/>
<property name="os.arch" value="amd64"/>
<property name="java.io.tmpdir" value="/tmp"/>
<property name="line.separator" value="
"/>
<property name="java.vm.specification.vendor" value="Sun Microsystems Inc."/>
<property name="os.name" value="Linux"/>
<property name="classworlds.conf" value="/usr/share/maven2/bin/m2.conf"/>
<property name="sun.jnu.encoding" value="UTF-8"/>
<property name="java.library.path" value="/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64:/usr/lib/jvm/java-6-openjdk-amd64/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib/jni:/lib:/usr/lib"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.class.version" value="50.0"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="os.version" value="3.5.0-52-generic"/>
<property name="user.home" value="/home/jstaerk"/>
<property name="user.timezone" value="Europe/Berlin"/>
<property name="java.awt.printerjob" value="sun.print.PSPrinterJob"/>
<property name="file.encoding" value="UTF-8"/>
<property name="java.specification.version" value="1.6"/>
<property name="user.name" value="jstaerk"/>
<property name="java.class.path" value="/usr/share/maven2/boot/classworlds.jar"/>
<property name="java.vm.specification.version" value="1.0"/>
<property name="sun.arch.data.model" value="64"/>
<property name="java.home" value="/usr/lib/jvm/java-6-openjdk-amd64/jre"/>
<property name="sun.java.command" value="org.codehaus.classworlds.Launcher &quot;test&quot;"/>
<property name="java.specification.vendor" value="Sun Microsystems Inc."/>
<property name="user.language" value="de"/>
<property name="java.vm.info" value="mixed mode"/>
<property name="java.version" value="1.6.0_31"/>
<property name="java.ext.dirs" value="/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/ext:/usr/java/packages/lib/ext"/>
<property name="securerandom.source" value="file:/dev/./urandom"/>
<property name="sun.boot.class.path" value="/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/rt.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/jfr.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/netx.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/plugin.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/rhino.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/classes"/>
<property name="java.vendor" value="Sun Microsystems Inc."/>
<property name="maven.home" value="/usr/share/maven2"/>
<property name="file.separator" value="/"/>
<property name="java.vendor.url.bug" value="http://java.sun.com/cgi-bin/bugreport.cgi"/>
<property name="sun.cpu.endian" value="little"/>
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
<property name="sun.cpu.isalist" value=""/>
</properties>
<testcase time="0.213" classname="org.mustangproject.ZUGFeRD.AppTest" name="testImport"/>
</testsuite>

View File

@@ -1,4 +0,0 @@
-------------------------------------------------------------------------------
Test set: org.mustangproject.ZUGFeRD.AppTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.223 sec