zf1 visualization, recognition if file is zf1 or zf2

This commit is contained in:
Jochen Stärk
2019-07-10 18:41:06 +02:00
parent d045073c78
commit fb53501c61
2 changed files with 87 additions and 56 deletions

View File

@@ -21,7 +21,11 @@ package org.mustangproject.ZUGFeRD;
import javax.xml.transform.*; import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -30,94 +34,121 @@ public class ZUGFeRDVisualizer {
static final ClassLoader CLASS_LOADER = ZUGFeRDVisualizer.class.getClassLoader(); static final ClassLoader CLASS_LOADER = ZUGFeRDVisualizer.class.getClassLoader();
private static final String RESOURCE_PATH = ""; //$NON-NLS-1$ private static final String RESOURCE_PATH = ""; //$NON-NLS-1$
private static final Logger LOG = Logger.getLogger(ZUGFeRDVisualizer.class.getName()); private static final Logger LOG = Logger.getLogger(ZUGFeRDVisualizer.class.getName());
// private static File createTempFileResult(final Transformer transformer, final StreamSource toTransform, // private static File createTempFileResult(final Transformer transformer, final
// final String suffix) throws TransformerException, IOException { // StreamSource toTransform,
// File result = File.createTempFile("ZUV_", suffix); //$NON-NLS-1$ // final String suffix) throws TransformerException, IOException {
// result.deleteOnExit(); // File result = File.createTempFile("ZUV_", suffix); //$NON-NLS-1$
// // result.deleteOnExit();
// try (FileOutputStream fos = new FileOutputStream(result)) { //
// transformer.transform(toTransform, new StreamResult(fos)); // try (FileOutputStream fos = new FileOutputStream(result)) {
// } // transformer.transform(toTransform, new StreamResult(fos));
// return result; // }
// } // return result;
// }
private TransformerFactory mFactory = null; private TransformerFactory mFactory = null;
private Templates mXsltXRTemplate = null; private Templates mXsltXRTemplate = null;
private Templates mXsltHTMLTemplate = null; private Templates mXsltHTMLTemplate = null;
private Templates mXsltZF1HTMLTemplate = null;
public ZUGFeRDVisualizer() { public ZUGFeRDVisualizer() {
mFactory = new net.sf.saxon.TransformerFactoryImpl(); mFactory = new net.sf.saxon.TransformerFactoryImpl();
//fact = TransformerFactory.newInstance(); // fact = TransformerFactory.newInstance();
mFactory.setURIResolver(new ClasspathResourceURIResolver()); mFactory.setURIResolver(new ClasspathResourceURIResolver());
try { try {
mXsltXRTemplate = mFactory.newTemplates(new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/cii-xr.xsl"))); mXsltXRTemplate = mFactory.newTemplates(
mXsltHTMLTemplate = mFactory.newTemplates(new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/xrechnung-html.xsl"))); new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/cii-xr.xsl")));
mXsltHTMLTemplate = mFactory.newTemplates(new StreamSource(
CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/xrechnung-html.xsl")));
mXsltZF1HTMLTemplate = mFactory.newTemplates(new StreamSource(
CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/ZUGFeRD_1p0_c1p0_s1p0.xslt")));
} catch (TransformerConfigurationException ex) { } catch (TransformerConfigurationException ex) {
LOG.log(Level.SEVERE, null, ex); LOG.log(Level.SEVERE, null, ex);
} }
} }
public String visualize(String xmlFilename) throws FileNotFoundException, TransformerException, UnsupportedEncodingException { public String visualize(String xmlFilename)
throws FileNotFoundException, TransformerException, UnsupportedEncodingException {
/** /**
* * * *
* http://www.unece.org/fileadmin/DAM/cefact/xml/XML-Naming-And-Design-Rules-V2_1.pdf * http://www.unece.org/fileadmin/DAM/cefact/xml/XML-Naming-And-Design-Rules-V2_1.pdf
* http://www.ferd-net.de/upload/Dokumente/FACTUR-X_ZUGFeRD_2p0_Teil1_Profil_EN16931_1p03.pdf * http://www.ferd-net.de/upload/Dokumente/FACTUR-X_ZUGFeRD_2p0_Teil1_Profil_EN16931_1p03.pdf
* http://countwordsfree.com/xmlviewer * http://countwordsfree.com/xmlviewer
*/ */
FileInputStream fis=new FileInputStream(xmlFilename);
String fileContent="";
try {
fileContent = new String(Files.readAllBytes(Paths.get(xmlFilename)));
} catch (IOException e2) {
LOG.log(Level.SEVERE, null, e2);
}
ByteArrayOutputStream iaos = new ByteArrayOutputStream(); ByteArrayOutputStream iaos = new ByteArrayOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
applySchematronXsl(new FileInputStream(xmlFilename), iaos);
// take the copy of the stream and re-write it to an InputStream String zf1Signature = "rsm:CrossIndustryDocument";
PipedInputStream in = new PipedInputStream(); if (fileContent.contains(zf1Signature)) {
PipedOutputStream out; applyZF1SchematronXsl(fis, baos);
try {
out = new PipedOutputStream(in); } else {
new Thread(new Runnable() { //zf2 or fx
public void run () { applySchematronXsl(fis, iaos);
try { // take the copy of the stream and re-write it to an InputStream
// write the original OutputStream to the PipedOutputStream PipedInputStream in = new PipedInputStream();
// note that in order for the below method to work, you need PipedOutputStream out;
// to ensure that the data has finished writing to the try {
// ByteArrayOutputStream out = new PipedOutputStream(in);
iaos.writeTo(out); new Thread(new Runnable() {
} public void run() {
catch (IOException e) { try {
// logging and exception handling should go here // write the original OutputStream to the PipedOutputStream
} // note that in order for the below method to work, you need
finally { // to ensure that the data has finished writing to the
// close the PipedOutputStream here because we're done writing data // ByteArrayOutputStream
// once this thread has completed its run iaos.writeTo(out);
if (out != null) {
// close the PipedOutputStream cleanly
try {
out.close();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block LOG.log(Level.SEVERE, null, e);
e.printStackTrace(); } finally {
// close the PipedOutputStream here because we're done writing data
// once this thread has completed its run
if (out != null) {
// close the PipedOutputStream cleanly
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
LOG.log(Level.SEVERE, null, e);
}
}
} }
} }
} }).start();
} applySchematronXsl2(in, baos);
}).start(); } catch (IOException e1) {
applySchematronXsl2(in, baos); LOG.log(Level.SEVERE, null, e1);
} catch (IOException e1) { }
// TODO Auto-generated catch block
e1.printStackTrace();
} }
return baos.toString("UTF-8"); return baos.toString("UTF-8");
} }
public void applySchematronXsl(final InputStream xmlFile, public void applySchematronXsl(final InputStream xmlFile, final OutputStream HTMLOutstream)
final OutputStream HTMLOutstream) throws TransformerException { throws TransformerException {
Transformer transformer = mXsltXRTemplate.newTransformer(); Transformer transformer = mXsltXRTemplate.newTransformer();
transformer.transform(new StreamSource(xmlFile), new StreamResult(HTMLOutstream)); transformer.transform(new StreamSource(xmlFile), new StreamResult(HTMLOutstream));
} }
public void applySchematronXsl2(final InputStream xmlFile, public void applyZF1SchematronXsl(final InputStream xmlFile, final OutputStream HTMLOutstream)
final OutputStream HTMLOutstream) throws TransformerException { throws TransformerException {
Transformer transformer = mXsltZF1HTMLTemplate.newTransformer();
transformer.transform(new StreamSource(xmlFile), new StreamResult(HTMLOutstream));
}
public void applySchematronXsl2(final InputStream xmlFile, final OutputStream HTMLOutstream)
throws TransformerException {
Transformer transformer = mXsltHTMLTemplate.newTransformer(); Transformer transformer = mXsltHTMLTemplate.newTransformer();
transformer.transform(new StreamSource(xmlFile), new StreamResult(HTMLOutstream)); transformer.transform(new StreamSource(xmlFile), new StreamResult(HTMLOutstream));
} }

View File

@@ -57,7 +57,7 @@ Kosten, Verluste bzw. Schäden hätte normalerweise vorhergesehen werden können
<xsl:variable name="XML" select="/"/> <xsl:variable name="XML" select="/"/>
<xsl:variable name="altova:nPxPerIn" select="96"/> <xsl:variable name="altova:nPxPerIn" select="96"/>
<xsl:decimal-format name="format1" grouping-separator="." decimal-separator=","/> <xsl:decimal-format name="format1" grouping-separator="." decimal-separator=","/>
<xsl:import-schema schema-location="ZUGFeRD_1p0.xsd" use-when="system-property('xsl:is-schema-aware')='yes'" namespace="urn:ferd:CrossIndustryDocument:invoice:1p0"/> <xsl:import-schema schema-location="../schema/ZUGFeRD_1p0.xsd" use-when="system-property('xsl:is-schema-aware')='yes'" namespace="urn:ferd:CrossIndustryDocument:invoice:1p0"/>
<xsl:variable name="altova:CssImages" select="()"/> <xsl:variable name="altova:CssImages" select="()"/>
<xsl:template match="/"> <xsl:template match="/">
<xsl:call-template name="altova:Root"/> <xsl:call-template name="altova:Root"/>