com.mycompany.localetopdf.CmdParser.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.localetopdf.CmdParser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.localetopdf;

import java.io.File;
import java.io.IOException;
import org.apache.commons.cli.*;
import java.util.Locale;
import javax.xml.transform.TransformerException;
import org.apache.fop.apps.FOPException;
import org.xml.sax.SAXException;

/**
 *
 * @author bigdata
 */
public class CmdParser {
    private final Options options = new Options()
            .addOption(Option.builder("help").desc("prints help message").build())
            .addOption(Option.builder("l").hasArgs().longOpt("locale")
                    .desc("specifies locale parameters (i.e. -locale de DE)").argName("locale ID").build())
            .addOption(Option.builder("x").hasArg().longOpt("xslt").desc("uses given file as an input XSLT")
                    .argName("xslt-file path").build())
            .addOption(Option.builder("p").hasArg().longOpt("pdf").desc("uses given file as an output PDF")
                    .argName("pdf-file path").build())
            .addOption(Option.builder("r").desc("rewrite existing pdf-file").build());

    protected Options getOptions() {
        return options;
    }

    protected void printHelp() {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("xml2pdf-cli", getOptions());
    }

    public static boolean ConfigureAppEnviroment(String[] args, AppEnviroment AppEnvInst)
            throws IOException, FOPException, TransformerException {
        {
            CmdParser instance = new CmdParser();

            CommandLineParser parser = new DefaultParser();

            try {
                CommandLine cmdLine = parser.parse(instance.getOptions(), args, true);

                if (cmdLine.hasOption("help")) {
                    instance.printHelp();
                    return false;
                } else if (cmdLine.hasOption("l")) {
                    String[] localeArg = cmdLine.getOptionValues("locale");
                    AppEnvInst.setLocale(new LocaleObject(localeArg[0], localeArg[1]));

                    File PDF;
                    if (cmdLine.hasOption("p")) {
                        PDF = new File(cmdLine.getOptionValue("p"));
                    } else {
                        PDF = new File("Locale.pdf");
                    }
                    AppEnvInst.setOutputFile(PDF);

                    File XSLT;
                    if (cmdLine.hasOption("x")) {
                        XSLT = new File(cmdLine.getOptionValue("x"));
                    } else {
                        XSLT = null;
                    }

                    boolean rewrite;
                    if (cmdLine.hasOption("r")) {
                        rewrite = true;
                    } else {
                        rewrite = false;
                    }

                    AppEnvInst.setView(new PdfView(XSLT, rewrite));
                    return true;
                } else {
                    instance.printHelp();
                    return false;
                }

            } catch (ParseException e) {
                e.printStackTrace();
                return false;
            }

        }
    }

}