Skip to content

Commit

Permalink
support languages
Browse files Browse the repository at this point in the history
  • Loading branch information
jstaerk committed Apr 13, 2023
1 parent 7c5a6e3 commit 2d9b68d
Show file tree
Hide file tree
Showing 27 changed files with 22,101 additions and 20 deletions.
2 changes: 2 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
support multiple languages in visualization

2.6.2 "Happy Easter"
=======
2023-04-06
Expand Down
26 changes: 23 additions & 3 deletions Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private static String getUsage() {
+ " Additional parameters (optional - user will be prompted if not defined)\n"
+ " -d, --directory to check recursively \n"
+ " --action visualize convert XML to HTML \n"
+ " [--language <lang>]: set output lang (en, fr or de)\n"
+ " [--source <filename>]: set input XML file\n"
+ " [--out <filename>]: set output HTML file\n"
;
Expand Down Expand Up @@ -351,6 +352,7 @@ public static void main(String[] args) {
options.addOption(attachmentOpt);
options.addOption(new Option("", "source",true, "which source file to use"));
options.addOption(new Option("", "source-xml",true, "which source file to use"));
options.addOption(new Option("", "language",true, "output language (en, de or fr)"));
options.addOption(new Option("", "out",true, "which output file to write to"));
options.addOption(new Option("", "no-notices",false, "suppress non-fatal errors"));
options.addOption(new Option("", "logAppend",true, "freeform text to be appended to log messages"));
Expand All @@ -373,6 +375,7 @@ public static void main(String[] args) {
String sourceXMLName = cmd.getOptionValue("source-xml");
String outName = cmd.getOptionValue("out");
String format = cmd.getOptionValue("format");
String lang = cmd.getOptionValue("language");
Boolean noNotices = cmd.hasOption("no-notices");

String zugferdVersion = cmd.getOptionValue("version");
Expand Down Expand Up @@ -403,7 +406,7 @@ public static void main(String[] args) {
performConvert(sourceName, outName);
optionsRecognized=true;
} else if ((action!=null)&&(action.equals("visualize"))) {
performVisualization(sourceName, outName);
performVisualization(sourceName, lang, outName);
optionsRecognized=true;
} else if ((action!=null)&&(action.equals("upgrade"))) {
performUpgrade(sourceName, outName);
Expand Down Expand Up @@ -766,13 +769,23 @@ private static void performMetrics(String directoryName, Boolean filesFromStdIn,
System.out.println(sr.getSummaryLine());
}

private static void performVisualization(String sourceName, String outName) {
private static void performVisualization(String sourceName, String lang, String outName) {
// Get params from user if not already defined
if (sourceName == null) {
sourceName = getFilenameFromUser("ZUGFeRD XML source", "factur-x.xml", "xml", true, false);
} else {
System.out.println("ZUGFeRD XML source set to " + sourceName);
}
if (lang == null) {
try {
lang = getStringFromUser("Output language", "en", "en|de|fr");
} catch(Exception e) {
LOGGER.error(e.getMessage(), e);
}
} else {
System.out.println("Output language set to " + lang);
}

if (outName == null) {
outName = getFilenameFromUser("HTML target file", "factur-x.html", "html", false, true);
} else {
Expand All @@ -792,7 +805,14 @@ private static void performVisualization(String sourceName, String outName) {
ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer();
String xml = null;
try {
xml = zvi.visualize(sourceName);
ZUGFeRDVisualizer.Language langCode=ZUGFeRDVisualizer.Language.EN;
if (lang.equalsIgnoreCase("de")) {
langCode=ZUGFeRDVisualizer.Language.DE;
}
if (lang.equalsIgnoreCase("fr")) {
langCode=ZUGFeRDVisualizer.Language.FR;
}
xml = zvi.visualize(sourceName, langCode);
Files.write(Paths.get(outName), xml.getBytes());
} catch (FileNotFoundException e) {
LOGGER.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ZUGFeRDVisualizer {

public enum Language {
EN,
FR,
DE
}
static final ClassLoader CLASS_LOADER = ZUGFeRDVisualizer.class.getClassLoader();
private static final String RESOURCE_PATH = "";
private static final Logger LOG = Logger.getLogger(ZUGFeRDVisualizer.class.getName());
Expand All @@ -55,20 +59,26 @@ public ZUGFeRDVisualizer() {
mFactory = new net.sf.saxon.TransformerFactoryImpl();
// fact = TransformerFactory.newInstance();
mFactory.setURIResolver(new ClasspathResourceURIResolver());
}

public String visualize(String xmlFilename, Language lang)
throws FileNotFoundException, TransformerException, UnsupportedEncodingException {

try {
mXsltXRTemplate = mFactory.newTemplates(
new StreamSource(CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/cii-xr.xsl")));
if (mXsltXRTemplate==null) {
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")));
mXsltZF1HTMLTemplate = mFactory.newTemplates(new StreamSource(
CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/ZUGFeRD_1p0_c1p0_s1p0.xslt")));
CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/xrechnung-html."+lang.name().toLowerCase()+".xsl")));
if (mXsltZF1HTMLTemplate==null) {
mXsltZF1HTMLTemplate = mFactory.newTemplates(new StreamSource(
CLASS_LOADER.getResourceAsStream(RESOURCE_PATH + "stylesheets/ZUGFeRD_1p0_c1p0_s1p0.xslt")));
}
} 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
Expand All @@ -88,11 +98,11 @@ public String visualize(String xmlFilename)

String zf1Signature = "rsm:CrossIndustryDocument";
if (fileContent.contains(zf1Signature)) {
applyZF1SchematronXsl(fis, baos);
applyZF1XSLT(fis, baos);

} else {
//zf2 or fx
applySchematronXsl(fis, iaos);
applyZF2XSLT(fis, iaos);
// take the copy of the stream and re-write it to an InputStream
PipedInputStream in = new PipedInputStream();
PipedOutputStream out;
Expand Down Expand Up @@ -123,7 +133,7 @@ public void run() {
}
}
}).start();
applySchematronXsl2(in, baos);
applyXSLTToHTML(in, baos);
} catch (IOException e1) {
LOG.log(Level.SEVERE, null, e1);
}
Expand All @@ -132,21 +142,21 @@ public void run() {
return baos.toString("UTF-8");
}

public void applySchematronXsl(final InputStream xmlFile, final OutputStream HTMLOutstream)
public void applyZF2XSLT(final InputStream xmlFile, final OutputStream HTMLOutstream)
throws TransformerException {
Transformer transformer = mXsltXRTemplate.newTransformer();

transformer.transform(new StreamSource(xmlFile), new StreamResult(HTMLOutstream));
}

public void applyZF1SchematronXsl(final InputStream xmlFile, final OutputStream HTMLOutstream)
public void applyZF1XSLT(final InputStream xmlFile, final OutputStream HTMLOutstream)
throws TransformerException {
Transformer transformer = mXsltZF1HTMLTemplate.newTransformer();

transformer.transform(new StreamSource(xmlFile), new StreamResult(HTMLOutstream));
}

public void applySchematronXsl2(final InputStream xmlFile, final OutputStream HTMLOutstream)
public void applyXSLTToHTML(final InputStream xmlFile, final OutputStream HTMLOutstream)
throws TransformerException {
Transformer transformer = mXsltHTMLTemplate.newTransformer();

Expand Down
Loading

0 comments on commit 2d9b68d

Please sign in to comment.