Merge branch '1.8.0': get back XR to HTML stylesheet to be able to visualize ZF2
This commit is contained in:
@@ -412,8 +412,6 @@ public class ZUGFeRD2PullProvider implements IXMLProvider, IProfileProvider {
|
||||
+ " </ram:PayeePartyCreditorFinancialAccount>\n" //$NON-NLS-1$
|
||||
+ " <ram:PayeeSpecifiedCreditorFinancialInstitution>\n" //$NON-NLS-1$
|
||||
+ " <ram:BICID>" + payment.getOwnBIC() + "</ram:BICID>\n" //$NON-NLS-1$ //$NON-NLS-2$
|
||||
+ " <ram:GermanBankleitzahlID>" + payment.getOwnBLZ() //$NON-NLS-1$
|
||||
+ "</ram:GermanBankleitzahlID>\n" //$NON-NLS-1$
|
||||
// + " <ram:Name>"+trans.getOwnBankName()+"</ram:Name>\n" //$NON-NLS-1$
|
||||
// //$NON-NLS-2$
|
||||
+ " </ram:PayeeSpecifiedCreditorFinancialInstitution>\n" //$NON-NLS-1$
|
||||
|
||||
@@ -128,8 +128,9 @@ public class ZUGFeRDImporter {
|
||||
|
||||
private void extractFiles(Map<String, PDComplexFileSpecification> names) throws IOException {
|
||||
for (String alias : names.keySet()) {
|
||||
String filename=names.get(alias).getFilename();
|
||||
|
||||
|
||||
PDComplexFileSpecification fileSpec = names.get(alias);
|
||||
String filename=fileSpec.getFilename();
|
||||
/**
|
||||
* currently (in the release candidate of version 1) only one attached file with
|
||||
* the name ZUGFeRD-invoice.xml is allowed
|
||||
@@ -137,7 +138,6 @@ public class ZUGFeRDImporter {
|
||||
if ((filename.equals("ZUGFeRD-invoice.xml") || (filename.equals("zugferd-invoice.xml")) || filename.equals("factur-x.xml"))) { //$NON-NLS-1$
|
||||
containsMeta = true;
|
||||
|
||||
PDComplexFileSpecification fileSpec = names.get(filename);
|
||||
PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();
|
||||
// String embeddedFilename = filePath + filename;
|
||||
// File file = new File(filePath + filename);
|
||||
@@ -154,7 +154,6 @@ public class ZUGFeRDImporter {
|
||||
}
|
||||
if (filename.startsWith("additional_data")) {
|
||||
|
||||
PDComplexFileSpecification fileSpec = names.get(filename);
|
||||
PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();
|
||||
additionalXMLs.put(filename, embeddedFile.toByteArray());
|
||||
|
||||
|
||||
134
src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDVisualizer.java
Normal file
134
src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDVisualizer.java
Normal file
@@ -0,0 +1,134 @@
|
||||
/** **********************************************************************
|
||||
*
|
||||
* Copyright 2018 Jochen Staerk
|
||||
*
|
||||
* Use is subject to license terms.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy
|
||||
* of the License at http://www.apache.org/licenses/LICENSE-2.0.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*********************************************************************** */
|
||||
package org.mustangproject.ZUGFeRD;
|
||||
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class ZUGFeRDVisualizer {
|
||||
|
||||
static final ClassLoader CLASS_LOADER = ZUGFeRDVisualizer.class.getClassLoader();
|
||||
private static final String RESOURCE_PATH = ""; //$NON-NLS-1$
|
||||
private static final Logger LOG = Logger.getLogger(ZUGFeRDVisualizer.class.getName());
|
||||
// private static File createTempFileResult(final Transformer transformer, final StreamSource toTransform,
|
||||
// final String suffix) throws TransformerException, IOException {
|
||||
// File result = File.createTempFile("ZUV_", suffix); //$NON-NLS-1$
|
||||
// result.deleteOnExit();
|
||||
//
|
||||
// try (FileOutputStream fos = new FileOutputStream(result)) {
|
||||
// transformer.transform(toTransform, new StreamResult(fos));
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
private TransformerFactory mFactory = null;
|
||||
private Templates mXsltXRTemplate = null;
|
||||
private Templates mXsltHTMLTemplate = null;
|
||||
|
||||
public ZUGFeRDVisualizer() {
|
||||
mFactory = new net.sf.saxon.TransformerFactoryImpl();
|
||||
//fact = TransformerFactory.newInstance();
|
||||
mFactory.setURIResolver(new ClasspathResourceURIResolver());
|
||||
try {
|
||||
mXsltXRTemplate = mFactory.newTemplates(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")));
|
||||
} catch (TransformerConfigurationException ex) {
|
||||
LOG.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
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.ferd-net.de/upload/Dokumente/FACTUR-X_ZUGFeRD_2p0_Teil1_Profil_EN16931_1p03.pdf
|
||||
* http://countwordsfree.com/xmlviewer
|
||||
*/
|
||||
ByteArrayOutputStream iaos = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
applySchematronXsl(new FileInputStream(xmlFilename), iaos);
|
||||
// take the copy of the stream and re-write it to an InputStream
|
||||
PipedInputStream in = new PipedInputStream();
|
||||
PipedOutputStream out;
|
||||
try {
|
||||
out = new PipedOutputStream(in);
|
||||
new Thread(new Runnable() {
|
||||
public void run () {
|
||||
try {
|
||||
// write the original OutputStream to the PipedOutputStream
|
||||
// note that in order for the below method to work, you need
|
||||
// to ensure that the data has finished writing to the
|
||||
// ByteArrayOutputStream
|
||||
iaos.writeTo(out);
|
||||
}
|
||||
catch (IOException e) {
|
||||
// logging and exception handling should go here
|
||||
}
|
||||
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
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
applySchematronXsl2(in, baos);
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
return baos.toString("UTF-8");
|
||||
}
|
||||
|
||||
public void applySchematronXsl(final InputStream xmlFile,
|
||||
final OutputStream HTMLOutstream) throws TransformerException {
|
||||
Transformer transformer = mXsltXRTemplate.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.transform(new StreamSource(xmlFile), new StreamResult(HTMLOutstream));
|
||||
}
|
||||
|
||||
private static class ClasspathResourceURIResolver implements URIResolver {
|
||||
ClasspathResourceURIResolver() {
|
||||
// Do nothing, just prevents synthetic access warning.
|
||||
}
|
||||
|
||||
@Override
|
||||
public Source resolve(String href, String base) throws TransformerException {
|
||||
return new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + href));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,8 +106,7 @@ public class Toecount {
|
||||
+ "\t\t[--source-xml <filename>]: set input XML file\r\n"
|
||||
+ "\t\t[--out <filename>]: set output PDF file\r\n"
|
||||
+ "\t\t[--format <fx|zf>]: enable factur-x or ZUGFeRD\r\n"
|
||||
+ "\t\t[--version <1|2>]: set ZUGFeRD version\r\n"
|
||||
+ "\t\t[--profile <...>]: set ZUGFeRD profile\r\n"
|
||||
+ "\t\t[--version <1|2>]: set ZUGFeRD version\r\n" + "\t\t[--profile <...>]: set ZUGFeRD profile\r\n"
|
||||
+ "\t\t\tFor ZUGFeRD v1: <B>ASIC, <C>OMFORT or <E>XTENDED\r\n"
|
||||
+ "\t\t\tFor ZUGFeRD v2: <M>INIMUM, BASIC <W>L, <B>ASIC, <C>IUS, <E>N16931, E<X>TENDED\r\n");
|
||||
}
|
||||
@@ -116,11 +115,15 @@ public class Toecount {
|
||||
* Asks the user (repeatedly, if neccessary) on the command line for a String
|
||||
* (offering a defaultValue) conforming to a Regex pattern
|
||||
*
|
||||
* @param prompt the question to be asked to the user
|
||||
* @param defaultValue the default return value if user hits enter
|
||||
* @param pattern a regex of acceptable values
|
||||
* @param prompt
|
||||
* the question to be asked to the user
|
||||
* @param defaultValue
|
||||
* the default return value if user hits enter
|
||||
* @param pattern
|
||||
* a regex of acceptable values
|
||||
* @return the user answer conforming to pattern
|
||||
* @throws Exception if pattern not compielable or IOexception on input
|
||||
* @throws Exception
|
||||
* if pattern not compielable or IOexception on input
|
||||
*/
|
||||
protected static String getStringFromUser(String prompt, String defaultValue, String pattern) throws Exception {
|
||||
String input = "";
|
||||
@@ -159,15 +162,20 @@ public class Toecount {
|
||||
/**
|
||||
* Prompts the user for a input or output filename
|
||||
*
|
||||
* @param the text the user is asked
|
||||
* @param a default Filename
|
||||
* @param expectedExtension will warn if filename does not match expected file extension
|
||||
* @param ensureFileExists will warn if file does NOT exist (for input files)
|
||||
* @param ensureFileNotExists will warn if file DOES exist (for output files)
|
||||
* @param the
|
||||
* text the user is asked
|
||||
* @param a
|
||||
* default Filename
|
||||
* @param expectedExtension
|
||||
* will warn if filename does not match expected file extension
|
||||
* @param ensureFileExists
|
||||
* will warn if file does NOT exist (for input files)
|
||||
* @param ensureFileNotExists
|
||||
* will warn if file DOES exist (for output files)
|
||||
* @return String
|
||||
*/
|
||||
protected static String getFilenameFromUser(String prompt, String defaultFilename, String expectedExtension,
|
||||
boolean ensureFileExists, boolean ensureFileNotExists) {
|
||||
boolean ensureFileExists, boolean ensureFileNotExists) {
|
||||
boolean fileExistenceOK = false;
|
||||
String selectedName = "";
|
||||
do {
|
||||
@@ -324,19 +332,62 @@ public class Toecount {
|
||||
performConvert(sourceName, outName);
|
||||
} else if (upgradeRequested) {
|
||||
performUpgrade(sourceName, outName);
|
||||
} else if (action.equals("visualize")) {
|
||||
performVisualization(sourceName, outName);
|
||||
} else {
|
||||
// no argument or argument unknown
|
||||
printUsage();
|
||||
System.exit(2);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
|
||||
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, e);
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void performVisualization(String sourceName, String outName) {
|
||||
// Get params from user if not already defined
|
||||
if (sourceName == null) {
|
||||
sourceName = getFilenameFromUser("ZUGFeRD XML source", "zugferd-invoice.xml", "xml", true, false);
|
||||
} else {
|
||||
System.out.println("ZUGFeRD XML source set to " + sourceName);
|
||||
}
|
||||
if (outName == null) {
|
||||
outName = getFilenameFromUser("ZUGFeRD 2.0 XML target", "factur-x.html", "html", false, true);
|
||||
} else {
|
||||
System.out.println("ZUGFeRD 1.0 XML source set to " + outName);
|
||||
}
|
||||
|
||||
// Verify params
|
||||
try {
|
||||
ensureFileExists(sourceName);
|
||||
ensureFileNotExists(outName);
|
||||
|
||||
// stylesheets/ZUGFeRD_1p0_c1p0_s1p0.xslt
|
||||
} catch (IOException e) {
|
||||
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, e);
|
||||
}
|
||||
|
||||
ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer();
|
||||
String xml = null;
|
||||
try {
|
||||
xml = zvi.visualize(sourceName);
|
||||
Files.write(Paths.get(outName), xml.getBytes());
|
||||
} catch (FileNotFoundException e) {
|
||||
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, e);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, e);
|
||||
} catch (TransformerException e) {
|
||||
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, e);
|
||||
} catch (IOException e) {
|
||||
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, e);
|
||||
}
|
||||
System.out.println("Written to " + outName);
|
||||
|
||||
}
|
||||
|
||||
private static void performUpgrade(String xmlName, String outName) throws IOException, TransformerException {
|
||||
|
||||
// Get params from user if not already defined
|
||||
@@ -426,7 +477,7 @@ public class Toecount {
|
||||
}
|
||||
|
||||
private static void performCombine(String pdfName, String xmlName, String outName, String format, String zfVersion,
|
||||
String zfProfile) throws Exception {
|
||||
String zfProfile) throws Exception {
|
||||
/*
|
||||
* ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory()
|
||||
* .setProducer("toecount") .setCreator(System.getProperty("user.name"))
|
||||
@@ -464,7 +515,6 @@ public class Toecount {
|
||||
System.out.println("Format set to " + format);
|
||||
}
|
||||
|
||||
|
||||
if (zfVersion == null) {
|
||||
try {
|
||||
zfVersion = getStringFromUser("Version (1 or 2)", "1", "1|2");
|
||||
@@ -478,13 +528,12 @@ public class Toecount {
|
||||
|
||||
if (zfProfile == null) {
|
||||
try {
|
||||
if (format.equals("zf")&&(zfIntVersion == 1)) {
|
||||
zfProfile = getStringFromUser("Profile b)asic, c)omfort or e)xtended", "e",
|
||||
"B|b|C|c|E|e");
|
||||
if (format.equals("zf") && (zfIntVersion == 1)) {
|
||||
zfProfile = getStringFromUser("Profile b)asic, c)omfort or e)xtended", "e", "B|b|C|c|E|e");
|
||||
} else {
|
||||
zfProfile = getStringFromUser(
|
||||
"Profile [M]INIMUM, BASIC [W]L, [B]ASIC,\n" + "[C]IUS, [E]N16931, E[X]TENDED",
|
||||
"E", "M|m|W|w|B|b|C|c|E|e|X|x|");
|
||||
"Profile [M]INIMUM, BASIC [W]L, [B]ASIC,\n" + "[C]IUS, [E]N16931, E[X]TENDED", "E",
|
||||
"M|m|W|w|B|b|C|c|E|e|X|x|");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, e);
|
||||
@@ -499,12 +548,12 @@ public class Toecount {
|
||||
ensureFileExists(pdfName);
|
||||
ensureFileExists(xmlName);
|
||||
ensureFileNotExists(outName);
|
||||
|
||||
if ((format.equals("fx"))&&(zfIntVersion>1)) {
|
||||
|
||||
if ((format.equals("fx")) && (zfIntVersion > 1)) {
|
||||
throw new Exception("Factur-X is only available in version 1 (roughly corresponding to ZF2)");
|
||||
}
|
||||
|
||||
if ((format.equals("zf"))&&(zfIntVersion == 1)) {
|
||||
if ((format.equals("zf")) && (zfIntVersion == 1)) {
|
||||
if (zfProfile.equals("b")) {
|
||||
zfConformanceLevelProfile = ZUGFeRDConformanceLevel.BASIC;
|
||||
} else if (zfProfile.equals("c")) {
|
||||
@@ -514,7 +563,7 @@ public class Toecount {
|
||||
} else {
|
||||
throw new Exception(String.format("Unknown ZUGFeRD profile '%s'", zfProfile));
|
||||
}
|
||||
} else if (((format.equals("zf"))&&(zfIntVersion == 2))||(format.equals("fx"))) {
|
||||
} else if (((format.equals("zf")) && (zfIntVersion == 2)) || (format.equals("fx"))) {
|
||||
if (zfProfile.equals("m")) {
|
||||
zfConformanceLevelProfile = ZUGFeRDConformanceLevel.MINIMUM;
|
||||
} else if (zfProfile.equals("w")) {
|
||||
@@ -543,7 +592,6 @@ public class Toecount {
|
||||
if (format.equals("fx")) {
|
||||
ze.setFacturX();
|
||||
}
|
||||
|
||||
|
||||
ze.setZUGFeRDXMLData(Files.readAllBytes(Paths.get(xmlName)));
|
||||
|
||||
|
||||
2202
src/main/resources/stylesheets/cii-xr.xsl
Normal file
2202
src/main/resources/stylesheets/cii-xr.xsl
Normal file
File diff suppressed because it is too large
Load Diff
1188
src/main/resources/stylesheets/xrechnung-html.xsl
Normal file
1188
src/main/resources/stylesheets/xrechnung-html.xsl
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user