java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 99 fadfde5b3e55
permissions -rw-r--r--
fix license version: GNU GPLv3
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@111
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@11
     8
 *
franta-hg@11
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@11
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11
    12
 * GNU General Public License for more details.
franta-hg@11
    13
 *
franta-hg@11
    14
 * You should have received a copy of the GNU General Public License
franta-hg@11
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@11
    16
 */
franta-hg@16
    17
package cz.frantovo.alt2xml.cli;
franta-hg@2
    18
franta-hg@43
    19
import cz.frantovo.alt2xml.out.Action;
franta-hg@43
    20
import cz.frantovo.alt2xml.out.ActionContext;
franta-hg@43
    21
import cz.frantovo.alt2xml.out.ActionFactory;
franta-hg@57
    22
import cz.frantovo.alt2xml.out.OutputActionException;
franta-hg@43
    23
import java.util.HashMap;
franta-hg@43
    24
import java.util.Map;
franta-hg@99
    25
import java.util.Map.Entry;
franta-hg@99
    26
import java.util.Properties;
franta-hg@43
    27
import java.util.ServiceLoader;
franta-hg@30
    28
import java.util.logging.Level;
franta-hg@57
    29
import java.util.logging.LogRecord;
franta-hg@30
    30
import java.util.logging.Logger;
franta-hg@57
    31
import javax.xml.parsers.ParserConfigurationException;
franta-hg@2
    32
import javax.xml.parsers.SAXParser;
franta-hg@2
    33
import javax.xml.parsers.SAXParserFactory;
franta-hg@57
    34
import org.xml.sax.SAXException;
franta-hg@99
    35
import org.xml.sax.SAXNotRecognizedException;
franta-hg@99
    36
import org.xml.sax.SAXNotSupportedException;
franta-hg@2
    37
franta-hg@2
    38
/**
franta-hg@2
    39
 *
franta-hg@19
    40
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    41
 */
franta-hg@53
    42
public class CLIStarter {
franta-hg@2
    43
franta-hg@57
    44
	// help:exit-codes
franta-hg@57
    45
	public static final int EXIT_SUCCESS = 0; // doc:success
franta-hg@57
    46
	public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug)
franta-hg@57
    47
	// 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
franta-hg@57
    48
	public static final int EXIT_SAX_ERROR = 3; // doc:SAX error
franta-hg@57
    49
	public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error
franta-hg@57
    50
	public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error
franta-hg@57
    51
	public static final int EXIT_CONFIGURATION_ERROR = 6; // doc:configuration error
franta-hg@57
    52
	public static final int EXIT_OUTPUT_ACTION_ERROR = 7; // doc:formatting error
franta-hg@57
    53
	// public static final int EXIT_BATCH_ERROR = 8; // doc:batch error
franta-hg@53
    54
	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
franta-hg@57
    55
	private static final String PROGRAM_NAME = "alt2xml";
franta-hg@28
    56
franta-hg@30
    57
	public static void main(String[] args) {
franta-hg@57
    58
		log.log(Level.FINE, "Starting " + PROGRAM_NAME);
franta-hg@57
    59
		int exitCode;
franta-hg@16
    60
franta-hg@30
    61
		try {
franta-hg@55
    62
			CLIParser cliParser = new CLIParser();
franta-hg@55
    63
			CLIOptions cliOptions = cliParser.parseOptions(args);
franta-hg@99
    64
franta-hg@58
    65
			cliOptions.validate();
franta-hg@16
    66
franta-hg@43
    67
			Map<String, ActionFactory> actionFactories = new HashMap<>();
franta-hg@30
    68
franta-hg@43
    69
			for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
franta-hg@43
    70
				String code = f.getActionCode();
franta-hg@43
    71
				actionFactories.put(code, f);
franta-hg@52
    72
				log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
franta-hg@39
    73
			}
franta-hg@39
    74
franta-hg@55
    75
			String actionCode = cliOptions.getAction();
franta-hg@43
    76
			ActionFactory actionFactory = actionFactories.get(actionCode);
franta-hg@43
    77
franta-hg@43
    78
			if (actionFactory == null) {
franta-hg@57
    79
				InvalidOptionsException e = new InvalidOptionsException();
franta-hg@57
    80
				e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
franta-hg@57
    81
				throw e;
franta-hg@43
    82
			} else {
franta-hg@43
    83
franta-hg@55
    84
				ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
franta-hg@99
    85
franta-hg@64
    86
				actionContext.setActionProperties(cliOptions.getActionProperties());
franta-hg@64
    87
				actionContext.setActionData(cliOptions.getActionData());
franta-hg@99
    88
franta-hg@43
    89
				Action action = actionFactory.getAction(actionContext);
franta-hg@43
    90
franta-hg@43
    91
				SAXParserFactory t = SAXParserFactory.newInstance();
franta-hg@99
    92
				parametrizeParserFactory(t, cliOptions.getReaderFeatures());
franta-hg@99
    93
				
franta-hg@43
    94
				SAXParser p = t.newSAXParser();
franta-hg@99
    95
				paramtrizeParser(p, cliOptions.getReaderProperties());
franta-hg@43
    96
franta-hg@55
    97
				action.run(p, cliOptions.getInputSource());
franta-hg@43
    98
			}
franta-hg@43
    99
franta-hg@57
   100
			log.log(Level.FINE, "All done");
franta-hg@57
   101
			exitCode = EXIT_SUCCESS;
franta-hg@57
   102
		} catch (CLIParserException e) {
franta-hg@57
   103
			log.log(Level.SEVERE, "Unable to parse CLI options", e);
franta-hg@57
   104
			exitCode = EXIT_CLI_PARSE_ERROR;
franta-hg@57
   105
		} catch (InvalidOptionsException e) {
franta-hg@57
   106
			log.log(Level.SEVERE, "Invalid CLI options", e);
franta-hg@57
   107
			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
franta-hg@57
   108
				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
franta-hg@57
   109
				r.setThrown(p.getException());
franta-hg@57
   110
				r.setParameters(new Object[]{p.getDescription()});
franta-hg@57
   111
				log.log(r);
franta-hg@57
   112
			}
franta-hg@57
   113
			exitCode = EXIT_CLI_VALIDATE_ERROR;
franta-hg@57
   114
		} catch (OutputActionException e) {
franta-hg@57
   115
			log.log(Level.SEVERE, "Ouput module problem", e);
franta-hg@57
   116
			exitCode = EXIT_OUTPUT_ACTION_ERROR;
franta-hg@57
   117
		} catch (ParserConfigurationException | SAXException e) {
franta-hg@57
   118
			log.log(Level.SEVERE, "Unable to create SAX parser", e);
franta-hg@57
   119
			exitCode = EXIT_SAX_ERROR;
franta-hg@30
   120
		}
franta-hg@21
   121
franta-hg@57
   122
		System.exit(exitCode);
franta-hg@2
   123
	}
franta-hg@99
   124
franta-hg@99
   125
	private static void parametrizeParserFactory(SAXParserFactory factory, Map<String, Boolean> features) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@99
   126
		for (Entry<String, Boolean> feature : features.entrySet()) {
franta-hg@99
   127
			factory.setFeature(feature.getKey(), feature.getValue());
franta-hg@99
   128
		}
franta-hg@99
   129
	}
franta-hg@99
   130
franta-hg@99
   131
	private static void paramtrizeParser(SAXParser parser, Properties readerProperties) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@99
   132
		for (String name : readerProperties.stringPropertyNames()) {
franta-hg@99
   133
			parser.setProperty(name, readerProperties.getProperty(name));
franta-hg@99
   134
		}
franta-hg@99
   135
	}
franta-hg@2
   136
}