Skip to content

Commit

Permalink
Merge pull request #16 from metanorma/cmd_params
Browse files Browse the repository at this point in the history
mn2pdf command line parameters changed
  • Loading branch information
Intelligent2013 authored Mar 23, 2020
2 parents 47abca5 + 5d392cd commit 075c407
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 61 deletions.
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ You will need the `maven` build tool and `make`.

[source,sh]
----
java -jar target/mn2pdf-1.3.jar <PDF-Font-Config> <XML-FileName> <XSLT-FileName> <Output-PDF-FileName>
java -jar target/mn2pdf-1.4.jar --xml-file <XML-FileName> --xsl-file <XSLT-FileName> --pdf-file <Output-PDF-FileName>
----

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
----


Expand All @@ -41,7 +41,7 @@ Update version in `pom.xml`, e.g.:
----
<groupId>com.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.3</version>
<version>1.4</version>
<name>Metanorma XML to PDF converter</name>
----

Expand All @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.3</version>
<version>1.4</version>
<name>Metanorma XML to PDF converter</name>
<packaging>jar</packaging>
<url>https://www.metanorma.com</url>
Expand Down Expand Up @@ -61,8 +61,14 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->
<dependency>

<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.4</version>
Expand Down
152 changes: 106 additions & 46 deletions src/main/java/com/metanorma/fop/mn2pdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 <path to fonts folder> <path to source XML file> <path to source XSLT file> <path to output PDF>";

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
17 changes: 9 additions & 8 deletions src/test/java/com/metanorma/fop/mn2pdfTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -48,27 +49,27 @@ 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(
String.format(mn2pdf.INPUT_NOT_FOUND, mn2pdf.XML_INPUT, args[1])));
}

@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(
Expand All @@ -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));
Expand Down

0 comments on commit 075c407

Please sign in to comment.