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,7 +34,8 @@ 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
// StreamSource toTransform,
// final String suffix) throws TransformerException, IOException { // final String suffix) throws TransformerException, IOException {
// File result = File.createTempFile("ZUV_", suffix); //$NON-NLS-1$ // File result = File.createTempFile("ZUV_", suffix); //$NON-NLS-1$
// result.deleteOnExit(); // result.deleteOnExit();
@@ -43,29 +48,50 @@ public class ZUGFeRDVisualizer {
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);
String zf1Signature = "rsm:CrossIndustryDocument";
if (fileContent.contains(zf1Signature)) {
applyZF1SchematronXsl(fis, baos);
} else {
//zf2 or fx
applySchematronXsl(fis, iaos);
// take the copy of the stream and re-write it to an InputStream // take the copy of the stream and re-write it to an InputStream
PipedInputStream in = new PipedInputStream(); PipedInputStream in = new PipedInputStream();
PipedOutputStream out; PipedOutputStream out;
@@ -79,11 +105,9 @@ public class ZUGFeRDVisualizer {
// to ensure that the data has finished writing to the // to ensure that the data has finished writing to the
// ByteArrayOutputStream // ByteArrayOutputStream
iaos.writeTo(out); iaos.writeTo(out);
} } catch (IOException e) {
catch (IOException e) { LOG.log(Level.SEVERE, null, e);
// logging and exception handling should go here } finally {
}
finally {
// close the PipedOutputStream here because we're done writing data // close the PipedOutputStream here because we're done writing data
// once this thread has completed its run // once this thread has completed its run
if (out != null) { if (out != null) {
@@ -92,7 +116,7 @@ public class ZUGFeRDVisualizer {
out.close(); out.close();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); LOG.log(Level.SEVERE, null, e);
} }
} }
} }
@@ -100,22 +124,29 @@ public class ZUGFeRDVisualizer {
}).start(); }).start();
applySchematronXsl2(in, baos); applySchematronXsl2(in, baos);
} catch (IOException e1) { } catch (IOException e1) {
// TODO Auto-generated catch block LOG.log(Level.SEVERE, null, e1);
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"/>