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
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@58
    61
			
franta-hg@58
    62
			cliOptions.validate();
franta-hg@16
    63
franta-hg@43
    64
			Map<String, ActionFactory> actionFactories = new HashMap<>();
franta-hg@30
    65
franta-hg@43
    66
			for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
franta-hg@43
    67
				String code = f.getActionCode();
franta-hg@43
    68
				actionFactories.put(code, f);
franta-hg@52
    69
				log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
franta-hg@39
    70
			}
franta-hg@39
    71
franta-hg@55
    72
			String actionCode = cliOptions.getAction();
franta-hg@43
    73
			ActionFactory actionFactory = actionFactories.get(actionCode);
franta-hg@43
    74
franta-hg@43
    75
			if (actionFactory == null) {
franta-hg@57
    76
				InvalidOptionsException e = new InvalidOptionsException();
franta-hg@57
    77
				e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
franta-hg@57
    78
				throw e;
franta-hg@43
    79
			} else {
franta-hg@43
    80
franta-hg@55
    81
				ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
franta-hg@43
    82
				Action action = actionFactory.getAction(actionContext);
franta-hg@43
    83
franta-hg@43
    84
				SAXParserFactory t = SAXParserFactory.newInstance();
franta-hg@43
    85
				SAXParser p = t.newSAXParser();
franta-hg@43
    86
franta-hg@55
    87
				action.run(p, cliOptions.getInputSource());
franta-hg@43
    88
			}
franta-hg@43
    89
franta-hg@57
    90
			log.log(Level.FINE, "All done");
franta-hg@57
    91
			exitCode = EXIT_SUCCESS;
franta-hg@57
    92
		} catch (CLIParserException e) {
franta-hg@57
    93
			log.log(Level.SEVERE, "Unable to parse CLI options", e);
franta-hg@57
    94
			exitCode = EXIT_CLI_PARSE_ERROR;
franta-hg@57
    95
		} catch (InvalidOptionsException e) {
franta-hg@57
    96
			log.log(Level.SEVERE, "Invalid CLI options", e);
franta-hg@57
    97
			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
franta-hg@57
    98
				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
franta-hg@57
    99
				r.setThrown(p.getException());
franta-hg@57
   100
				r.setParameters(new Object[]{p.getDescription()});
franta-hg@57
   101
				log.log(r);
franta-hg@57
   102
			}
franta-hg@57
   103
			exitCode = EXIT_CLI_VALIDATE_ERROR;
franta-hg@57
   104
		} catch (OutputActionException e) {
franta-hg@57
   105
			log.log(Level.SEVERE, "Ouput module problem", e);
franta-hg@57
   106
			exitCode = EXIT_OUTPUT_ACTION_ERROR;
franta-hg@57
   107
		} catch (ParserConfigurationException | SAXException e) {
franta-hg@57
   108
			log.log(Level.SEVERE, "Unable to create SAX parser", e);
franta-hg@57
   109
			exitCode = EXIT_SAX_ERROR;
franta-hg@30
   110
		}
franta-hg@21
   111
franta-hg@57
   112
		System.exit(exitCode);
franta-hg@2
   113
	}
franta-hg@2
   114
}