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