java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 03 Sep 2014 15:50:16 +0200
changeset 58 c11792ae6489
parent 57 88836193c9ba
child 64 e2c691eedf4d
permissions -rw-r--r--
options validation: --input-stdin requires --system-id
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.cli;
    19 
    20 import cz.frantovo.alt2xml.out.Action;
    21 import cz.frantovo.alt2xml.out.ActionContext;
    22 import cz.frantovo.alt2xml.out.ActionFactory;
    23 import cz.frantovo.alt2xml.out.OutputActionException;
    24 import java.util.HashMap;
    25 import java.util.Map;
    26 import java.util.ServiceLoader;
    27 import java.util.logging.Level;
    28 import java.util.logging.LogRecord;
    29 import java.util.logging.Logger;
    30 import javax.xml.parsers.ParserConfigurationException;
    31 import javax.xml.parsers.SAXParser;
    32 import javax.xml.parsers.SAXParserFactory;
    33 import org.xml.sax.SAXException;
    34 
    35 /**
    36  *
    37  * @author Ing. František Kučera (frantovo.cz)
    38  */
    39 public class CLIStarter {
    40 
    41 	// help:exit-codes
    42 	public static final int EXIT_SUCCESS = 0; // doc:success
    43 	public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug)
    44 	// 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
    45 	public static final int EXIT_SAX_ERROR = 3; // doc:SAX error
    46 	public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error
    47 	public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error
    48 	public static final int EXIT_CONFIGURATION_ERROR = 6; // doc:configuration error
    49 	public static final int EXIT_OUTPUT_ACTION_ERROR = 7; // doc:formatting error
    50 	// public static final int EXIT_BATCH_ERROR = 8; // doc:batch error
    51 	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    52 	private static final String PROGRAM_NAME = "alt2xml";
    53 
    54 	public static void main(String[] args) {
    55 		log.log(Level.FINE, "Starting " + PROGRAM_NAME);
    56 		int exitCode;
    57 
    58 		try {
    59 			CLIParser cliParser = new CLIParser();
    60 			CLIOptions cliOptions = cliParser.parseOptions(args);
    61 			
    62 			cliOptions.validate();
    63 
    64 			Map<String, ActionFactory> actionFactories = new HashMap<>();
    65 
    66 			for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
    67 				String code = f.getActionCode();
    68 				actionFactories.put(code, f);
    69 				log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
    70 			}
    71 
    72 			String actionCode = cliOptions.getAction();
    73 			ActionFactory actionFactory = actionFactories.get(actionCode);
    74 
    75 			if (actionFactory == null) {
    76 				InvalidOptionsException e = new InvalidOptionsException();
    77 				e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
    78 				throw e;
    79 			} else {
    80 
    81 				ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
    82 				Action action = actionFactory.getAction(actionContext);
    83 
    84 				SAXParserFactory t = SAXParserFactory.newInstance();
    85 				SAXParser p = t.newSAXParser();
    86 
    87 				action.run(p, cliOptions.getInputSource());
    88 			}
    89 
    90 			log.log(Level.FINE, "All done");
    91 			exitCode = EXIT_SUCCESS;
    92 		} catch (CLIParserException e) {
    93 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    94 			exitCode = EXIT_CLI_PARSE_ERROR;
    95 		} catch (InvalidOptionsException e) {
    96 			log.log(Level.SEVERE, "Invalid CLI options", e);
    97 			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
    98 				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
    99 				r.setThrown(p.getException());
   100 				r.setParameters(new Object[]{p.getDescription()});
   101 				log.log(r);
   102 			}
   103 			exitCode = EXIT_CLI_VALIDATE_ERROR;
   104 		} catch (OutputActionException e) {
   105 			log.log(Level.SEVERE, "Ouput module problem", e);
   106 			exitCode = EXIT_OUTPUT_ACTION_ERROR;
   107 		} catch (ParserConfigurationException | SAXException e) {
   108 			log.log(Level.SEVERE, "Unable to create SAX parser", e);
   109 			exitCode = EXIT_SAX_ERROR;
   110 		}
   111 
   112 		System.exit(exitCode);
   113 	}
   114 }