3 * Copyright © 2014 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package cz.frantovo.alt2xml.cli;
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;
26 import java.util.Map.Entry;
27 import java.util.Properties;
28 import java.util.ServiceLoader;
29 import java.util.logging.Level;
30 import java.util.logging.LogRecord;
31 import java.util.logging.Logger;
32 import javax.xml.parsers.ParserConfigurationException;
33 import javax.xml.parsers.SAXParser;
34 import javax.xml.parsers.SAXParserFactory;
35 import org.xml.sax.SAXException;
36 import org.xml.sax.SAXNotRecognizedException;
37 import org.xml.sax.SAXNotSupportedException;
41 * @author Ing. František Kučera (frantovo.cz)
43 public class CLIStarter {
46 public static final int EXIT_SUCCESS = 0; // doc:success
47 public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug)
48 // 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
49 public static final int EXIT_SAX_ERROR = 3; // doc:SAX error
50 public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error
51 public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error
52 public static final int EXIT_CONFIGURATION_ERROR = 6; // doc:configuration error
53 public static final int EXIT_OUTPUT_ACTION_ERROR = 7; // doc:formatting error
54 // public static final int EXIT_BATCH_ERROR = 8; // doc:batch error
55 private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
56 private static final String PROGRAM_NAME = "alt2xml";
58 public static void main(String[] args) {
59 log.log(Level.FINE, "Starting " + PROGRAM_NAME);
63 CLIParser cliParser = new CLIParser();
64 CLIOptions cliOptions = cliParser.parseOptions(args);
66 cliOptions.validate();
68 Map<String, ActionFactory> actionFactories = new HashMap<>();
70 for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
71 String code = f.getActionCode();
72 actionFactories.put(code, f);
73 log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
76 String actionCode = cliOptions.getAction();
77 ActionFactory actionFactory = actionFactories.get(actionCode);
79 if (actionFactory == null) {
80 InvalidOptionsException e = new InvalidOptionsException();
81 e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
85 ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
87 actionContext.setActionProperties(cliOptions.getActionProperties());
88 actionContext.setActionData(cliOptions.getActionData());
90 Action action = actionFactory.getAction(actionContext);
92 SAXParserFactory t = SAXParserFactory.newInstance();
93 parametrizeParserFactory(t, cliOptions.getReaderFeatures());
95 SAXParser p = t.newSAXParser();
96 paramtrizeParser(p, cliOptions.getReaderProperties());
98 action.run(p, cliOptions.getInputSource());
101 log.log(Level.FINE, "All done");
102 exitCode = EXIT_SUCCESS;
103 } catch (CLIParserException e) {
104 log.log(Level.SEVERE, "Unable to parse CLI options", e);
105 exitCode = EXIT_CLI_PARSE_ERROR;
106 } catch (InvalidOptionsException e) {
107 log.log(Level.SEVERE, "Invalid CLI options", e);
108 for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
109 LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
110 r.setThrown(p.getException());
111 r.setParameters(new Object[]{p.getDescription()});
114 exitCode = EXIT_CLI_VALIDATE_ERROR;
115 } catch (OutputActionException e) {
116 log.log(Level.SEVERE, "Ouput module problem", e);
117 exitCode = EXIT_OUTPUT_ACTION_ERROR;
118 } catch (ParserConfigurationException | SAXException e) {
119 log.log(Level.SEVERE, "Unable to create SAX parser", e);
120 exitCode = EXIT_SAX_ERROR;
123 System.exit(exitCode);
126 private static void parametrizeParserFactory(SAXParserFactory factory, Map<String, Boolean> features) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
127 for (Entry<String, Boolean> feature : features.entrySet()) {
128 factory.setFeature(feature.getKey(), feature.getValue());
132 private static void paramtrizeParser(SAXParser parser, Properties readerProperties) throws SAXNotRecognizedException, SAXNotSupportedException {
133 for (String name : readerProperties.stringPropertyNames()) {
134 parser.setProperty(name, readerProperties.getProperty(name));