java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 09 Jul 2014 00:20:49 +0200
changeset 57 88836193c9ba
parent 55 c703fb7f088f
child 58 c11792ae6489
permissions -rw-r--r--
CLIStarter: exceptions handling
     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 			Map<String, ActionFactory> actionFactories = new HashMap<>();
    63 
    64 			for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
    65 				String code = f.getActionCode();
    66 				actionFactories.put(code, f);
    67 				log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
    68 			}
    69 
    70 			String actionCode = cliOptions.getAction();
    71 			ActionFactory actionFactory = actionFactories.get(actionCode);
    72 
    73 			if (actionFactory == null) {
    74 				InvalidOptionsException e = new InvalidOptionsException();
    75 				e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
    76 				throw e;
    77 			} else {
    78 
    79 				ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
    80 				Action action = actionFactory.getAction(actionContext);
    81 
    82 				SAXParserFactory t = SAXParserFactory.newInstance();
    83 				SAXParser p = t.newSAXParser();
    84 
    85 				action.run(p, cliOptions.getInputSource());
    86 			}
    87 
    88 			log.log(Level.FINE, "All done");
    89 			exitCode = EXIT_SUCCESS;
    90 		} catch (CLIParserException e) {
    91 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    92 			exitCode = EXIT_CLI_PARSE_ERROR;
    93 		} catch (InvalidOptionsException e) {
    94 			log.log(Level.SEVERE, "Invalid CLI options", e);
    95 			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
    96 				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
    97 				r.setThrown(p.getException());
    98 				r.setParameters(new Object[]{p.getDescription()});
    99 				log.log(r);
   100 			}
   101 			exitCode = EXIT_CLI_VALIDATE_ERROR;
   102 		} catch (OutputActionException e) {
   103 			log.log(Level.SEVERE, "Ouput module problem", e);
   104 			exitCode = EXIT_OUTPUT_ACTION_ERROR;
   105 		} catch (ParserConfigurationException | SAXException e) {
   106 			log.log(Level.SEVERE, "Unable to create SAX parser", e);
   107 			exitCode = EXIT_SAX_ERROR;
   108 		}
   109 
   110 		System.exit(exitCode);
   111 	}
   112 }