diff --git a/README.adoc b/README.adoc index 4313ac30..efcaa09d 100644 --- a/README.adoc +++ b/README.adoc @@ -14,14 +14,14 @@ You will need the `maven` build tool and `make`. [source,sh] ---- -java -jar target/mn2pdf-1.3.jar +java -jar target/mn2pdf-1.4.jar --xml-file --xsl-file --pdf-file ---- e.g. [source,sh] ---- -java -jar target/mn2pdf-1.3.jar tests/pdf_fonts_config.xml tests/G.191.xml tests/itu.recommendation.xsl tests/G.191.pdf +java -jar target/mn2pdf-1.4.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf ---- @@ -41,7 +41,7 @@ Update version in `pom.xml`, e.g.: ---- com.metanorma.fop mn2pdf -1.3 +1.4 Metanorma XML to PDF converter ---- @@ -52,8 +52,8 @@ Tag the same version in Git: [source,xml] ---- -git tag v1.3 -git push origin v1.3 +git tag v1.4 +git push origin v1.4 ---- Then the corresponding GitHub release will be automatically created at: diff --git a/pom.xml b/pom.xml index 8b2a7572..a82726d0 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.metanorma.fop mn2pdf - 1.3 + 1.4 Metanorma XML to PDF converter jar https://www.metanorma.com @@ -61,8 +61,14 @@ + + commons-cli + commons-cli + 1.4 + - + + org.apache.xmlgraphics fop 2.4 diff --git a/src/main/java/com/metanorma/fop/mn2pdf.java b/src/main/java/com/metanorma/fop/mn2pdf.java index 8773d292..93d1ee0c 100644 --- a/src/main/java/com/metanorma/fop/mn2pdf.java +++ b/src/main/java/com/metanorma/fop/mn2pdf.java @@ -5,6 +5,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; @@ -25,27 +26,71 @@ import org.apache.fop.apps.MimeConstants; import net.sourceforge.jeuclid.fop.plugin.JEuclidFopFactoryConfigurator; -import org.apache.fop.apps.FopFactoryBuilder; -import org.apache.fop.apps.FopFactoryConfig; -import org.xml.sax.SAXException; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +import org.xml.sax.SAXException; /** * This class for the conversion of an XML file to PDF using FOP and JEuclid */ public class mn2pdf { - static final String USAGE = "Usage: java -jar mn2pdf.jar "; + + static final String CMD = "java -jar mn2pdf.jar"; static final String INPUT_NOT_FOUND = "Error: %s file '%s' not found!"; - //static final String FOP_CONFIG_INPUT = "FOP config"; static final String FONTS_FOLDER_INPUT = "Fonts path"; static final String XML_INPUT = "XML"; static final String XSL_INPUT = "XSL"; static final String INPUT_LOG = "Input: %s (%s)"; + static final String DEFAULT_FONT_PATH = "~/.metanorma/fonts"; + + static final Options options = new Options() { + { + addOption(Option.builder("f") + .longOpt("font-path") + .desc("optional path to fonts folder") + .hasArg() + .argName("folder") + .required(false) + .build()); + addOption(Option.builder("x") + .longOpt("xml-file") + .desc("path to source XML file") + .hasArg() + .argName("file") + .required(true) + .build()); + addOption(Option.builder("s") + .longOpt("xsl-file") + .desc("path to XSL file") + .hasArg() + .argName("file") + .required(true) + .build()); + addOption(Option.builder("o") + .longOpt("pdf-file") + .desc("path to output PDF file") + .hasArg() + .argName("file") + .required(true) + .build()); + } + }; + + static final String USAGE = getUsage(); + static final int ERROR_EXIT_CODE = -1; /** * Converts an XML file to a PDF file using FOP + * * @param config the FOP config file * @param xml the XML source file * @param xsl the XSL file @@ -75,9 +120,9 @@ public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws // Step 1: Construct a FopFactory by specifying a reference to the configuration file fontConfig fontcfg = new fontConfig(fontPath); - + FopFactory fopFactory = FopFactory.newInstance(fontcfg.getUpdatedConfig()); - + JEuclidFopFactoryConfigurator.configure(fopFactory); FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); // configure foUserAgent as desired @@ -113,53 +158,68 @@ public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws } } - /** * Main method. + * * @param args command-line arguments */ - public static void main(String[] args) { - if (args.length != 4) { - System.out.println(USAGE); - System.exit(ERROR_EXIT_CODE); - } - - System.out.println("mn2pdf\n"); - System.out.println("Preparing..."); + public static void main(String[] args) throws ParseException { + + CommandLineParser parser = new DefaultParser(); + + + try { + CommandLine cmd = parser.parse(options, args); + + System.out.println("mn2pdf\n"); + System.out.println("Preparing..."); - //Setup font path, input and output files - final String argFontsPath = args[0]; - final String argXML = args[1]; - File fXML = new File(argXML); - if (!fXML.exists()) { - System.out.println(String.format(INPUT_NOT_FOUND, XML_INPUT, fXML)); - System.exit(ERROR_EXIT_CODE); - } - final String argXSL = args[2]; - File fXSL = new File(argXSL); - if (!fXSL.exists()) { - System.out.println(String.format(INPUT_NOT_FOUND, XSL_INPUT, fXSL)); - System.exit(ERROR_EXIT_CODE); - } - final String argPDF = args[3]; - File fPDF = new File(argPDF); - - System.out.println(String.format(INPUT_LOG, FONTS_FOLDER_INPUT, argFontsPath)); - System.out.println(String.format(INPUT_LOG, XML_INPUT, fXML)); - System.out.println(String.format(INPUT_LOG, XSL_INPUT, fXSL)); - System.out.println("Output: PDF (" + fPDF + ")"); - System.out.println(); - System.out.println("Transforming..."); + //Setup font path, input and output files + final String argFontsPath = (cmd.hasOption("font-path") ? cmd.getOptionValue("font-path") : DEFAULT_FONT_PATH); - try { - mn2pdf app = new mn2pdf(); - app.convertmn2pdf(argFontsPath, fXML, fXSL, fPDF); - System.out.println("Success!"); - } catch (Exception e) { - e.printStackTrace(System.err); + final String argXML = cmd.getOptionValue("xml-file"); + File fXML = new File(argXML); + if (!fXML.exists()) { + System.out.println(String.format(INPUT_NOT_FOUND, XML_INPUT, fXML)); + System.exit(ERROR_EXIT_CODE); + } + final String argXSL = cmd.getOptionValue("xsl-file"); + File fXSL = new File(argXSL); + if (!fXSL.exists()) { + System.out.println(String.format(INPUT_NOT_FOUND, XSL_INPUT, fXSL)); + System.exit(ERROR_EXIT_CODE); + } + final String argPDF = cmd.getOptionValue("pdf-file"); + File fPDF = new File(argPDF); + + System.out.println(String.format(INPUT_LOG, FONTS_FOLDER_INPUT, argFontsPath)); + System.out.println(String.format(INPUT_LOG, XML_INPUT, fXML)); + System.out.println(String.format(INPUT_LOG, XSL_INPUT, fXSL)); + System.out.println("Output: PDF (" + fPDF + ")"); + System.out.println(); + System.out.println("Transforming..."); + + try { + mn2pdf app = new mn2pdf(); + app.convertmn2pdf(argFontsPath, fXML, fXSL, fPDF); + System.out.println("Success!"); + } catch (Exception e) { + e.printStackTrace(System.err); + System.exit(ERROR_EXIT_CODE); + } + } + catch( ParseException exp ) { + System.out.println(USAGE); System.exit(ERROR_EXIT_CODE); } - } + private static String getUsage() { + StringWriter stringWriter = new StringWriter(); + PrintWriter pw = new PrintWriter(stringWriter); + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp(pw, 80, CMD, "", options, 0, 0, ""); + pw.flush(); + return stringWriter.toString(); + } } diff --git a/src/test/java/com/metanorma/fop/mn2pdfTests.java b/src/test/java/com/metanorma/fop/mn2pdfTests.java index e7eb8123..975bffc3 100644 --- a/src/test/java/com/metanorma/fop/mn2pdfTests.java +++ b/src/test/java/com/metanorma/fop/mn2pdfTests.java @@ -4,6 +4,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import org.apache.commons.cli.ParseException; import org.junit.Rule; import org.junit.Test; @@ -25,7 +26,7 @@ public class mn2pdfTests { public final EnvironmentVariables envVarRule = new EnvironmentVariables(); @Test - public void notEnoughArguments() { + public void notEnoughArguments() throws ParseException { exitRule.expectSystemExitWithStatus(-1); String[] args = new String[]{"1", "2", "3"}; mn2pdf.main(args); @@ -48,12 +49,12 @@ public void fopConfingNotExists() { }*/ @Test - public void xmlNotExists() { + public void xmlNotExists() throws ParseException { exitRule.expectSystemExitWithStatus(-1); String fontpath = System.getProperty("buildDirectory") + File.separator + ".." + File.separator + "fonts"; - String[] args = new String[]{fontpath, "2", "3", "4"}; + String[] args = new String[]{"--xml-file", "1", "--xsl-file", "2", "--pdf-file", "3"}; mn2pdf.main(args); assertTrue(systemOutRule.getLog().contains( @@ -61,14 +62,14 @@ public void xmlNotExists() { } @Test - public void xslNotExists() { + public void xslNotExists() throws ParseException { exitRule.expectSystemExitWithStatus(-1); ClassLoader classLoader = getClass().getClassLoader(); String fontpath = System.getProperty("buildDirectory") + File.separator + ".." + File.separator + "fonts"; String xml = classLoader.getResource("G.191.xml").getFile(); - String[] args = new String[]{fontpath, xml, "3", "4"}; + String[] args = new String[]{"--xml-file", xml, "--xsl-file", "3", "--pdf-file", "4"}; mn2pdf.main(args); assertTrue(systemOutRule.getLog().contains( @@ -91,14 +92,14 @@ public void missingEnvVariable() { assertTrue(systemOutRule.getLog().contains(fontConfig.ENV_FONT_PATH)); }*/ @Test - public void success() { + public void success() throws ParseException { ClassLoader classLoader = getClass().getClassLoader(); - String fontpath = ((System.getenv("MN_PDF_FONT_PATH") == null) ? System.getProperty("buildDirectory") + File.separator + ".." + File.separator + "fonts" : System.getenv("MN_PDF_FONT_PATH")); + String fontpath = Paths.get(System.getProperty("buildDirectory"), ".." , "fonts").toString(); String xml = classLoader.getResource("G.191.xml").getFile(); String xsl = classLoader.getResource("itu.recommendation.xsl").getFile(); Path pdf = Paths.get(System.getProperty("buildDirectory"), "G.191.pdf"); - String[] args = new String[]{fontpath, xml, xsl, pdf.toAbsolutePath().toString()}; + String[] args = new String[]{"--font-path", fontpath, "--xml-file", xml, "--xsl-file", xsl, "--pdf-file", pdf.toAbsolutePath().toString()}; mn2pdf.main(args); assertTrue(Files.exists(pdf));