Java tutorial
/** * \cond LICENSE * Sucuri -- the localization tool for arara * Copyright (c) 2012, Paulo Roberto Massa Cereda * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the project's author nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * \endcond */ // package definition package com.github.cereda.sucuri.utils; // needed imports import java.io.File; import org.apache.commons.cli.BasicParser; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; /** * Analyzes the command line arguments. * * @author Paulo Roberto Massa Cereda * @version 1.0 * @since 1.0 */ public class CommandLineAnalyzer { /** * command line options. */ private Options commandLineOptions; /** * command line arguments. */ private String[] args; /** * The template file. */ private File template; /** * The input file. */ private File input; /** * The output file. */ private File output; /** * A flag holding the report option. */ private boolean report; /** * The input file encoding. */ private String encoding = ""; /** * Constructor. * * @param args The command line arguments. */ public CommandLineAnalyzer(String[] args) { // set everything that matters commandLineOptions = new Options(); this.args = args; } /** * Parses the command line arguments. * * @return A boolean indicating if the parsing was successful. */ public boolean parse() { // create the options Option optVersion = new Option("v", "version", false, "print the application version"); Option optHelp = new Option("h", "help", false, "print the help message"); Option optReport = new Option("r", "report", false, "generate report"); Option optTemplate = new Option("t", "template", true, "set the master template"); Option optInput = new Option("i", "input", true, "set the input file"); Option optOutput = new Option("o", "output", true, "set the output file"); Option optEncoding = new Option("e", "encoding", true, "set the input encoding"); // add them commandLineOptions.addOption(optVersion); commandLineOptions.addOption(optHelp); commandLineOptions.addOption(optReport); commandLineOptions.addOption(optTemplate); commandLineOptions.addOption(optInput); commandLineOptions.addOption(optOutput); commandLineOptions.addOption(optEncoding); // create a new parser CommandLineParser parser = new BasicParser(); try { // parse the line CommandLine line = parser.parse(commandLineOptions, args); // check for help if (line.hasOption("help")) { printVersion(); printUsage(); return false; } else { // check for version if (line.hasOption("version")) { printVersion(); return false; } // check if there are anything else if (line.getArgs().length != 0) { printVersion(); printUsage(); return false; } else { // check for template if (line.hasOption("template")) { // check for input if (line.hasOption("input")) { // check for output if (line.hasOption("output")) { // get the report flag report = line.hasOption("report"); // check for encoding if (line.hasOption("encoding")) { encoding = line.getOptionValue("encoding"); } // set everything input = new File(line.getOptionValue("input")); output = new File(line.getOptionValue("output")); template = new File(line.getOptionValue("template")); return true; } else { printVersion(); System.out.println("The output file is required.".concat("\n")); printUsage(); return false; } } else { printVersion(); System.out.println("The input file is required.".concat("\n")); printUsage(); return false; } } else { printVersion(); System.out.println("The template file is required.".concat("\n")); printUsage(); return false; } } } } catch (ParseException parseException) { printVersion(); printUsage(); return false; } } /** * Prints the application version. */ private void printVersion() { System.out.println( "sucuri ".concat(SucuriConstants.VERSION).concat(" - ").concat("the localization tool for arara")); System.out.println( "Copyright (c) ".concat(SucuriConstants.COPYRIGHTYEAR).concat(", Paulo Roberto Massa Cereda")); System.out.println("All rights reserved.".concat("\n")); } /** * Prints the application usage. */ private void printUsage() { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp( "sucuri [--template T --input I --output O [--encoding E] [--report R] | --help | --version]", commandLineOptions); } /** * Getter for template. * * @return The template. */ public File getTemplate() { return template; } /** * Getter for input. * * @return The input. */ public File getInput() { return input; } /** * Getter for output. * * @return The output. */ public File getOutput() { return output; } /** * Getter for report. * * @return A flag indicating the report. */ public boolean hasReport() { return report; } /** * Getter for encoding. * * @return The encoding. */ public String getEncoding() { return encoding; } }