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