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.ServiceLoader;
27 import java.util.logging.Level;
28 import java.util.logging.LogRecord;
29 import java.util.logging.Logger;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.parsers.SAXParser;
32 import javax.xml.parsers.SAXParserFactory;
33 import org.xml.sax.SAXException;
37 * @author Ing. František Kučera (frantovo.cz)
39 public class CLIStarter {
42 public static final int EXIT_SUCCESS = 0; // doc:success
43 public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug)
44 // 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
45 public static final int EXIT_SAX_ERROR = 3; // doc:SAX error
46 public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error
47 public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error
48 public static final int EXIT_CONFIGURATION_ERROR = 6; // doc:configuration error
49 public static final int EXIT_OUTPUT_ACTION_ERROR = 7; // doc:formatting error
50 // public static final int EXIT_BATCH_ERROR = 8; // doc:batch error
51 private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
52 private static final String PROGRAM_NAME = "alt2xml";
54 public static void main(String[] args) {
55 log.log(Level.FINE, "Starting " + PROGRAM_NAME);
59 CLIParser cliParser = new CLIParser();
60 CLIOptions cliOptions = cliParser.parseOptions(args);
62 Map<String, ActionFactory> actionFactories = new HashMap<>();
64 for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
65 String code = f.getActionCode();
66 actionFactories.put(code, f);
67 log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
70 String actionCode = cliOptions.getAction();
71 ActionFactory actionFactory = actionFactories.get(actionCode);
73 if (actionFactory == null) {
74 InvalidOptionsException e = new InvalidOptionsException();
75 e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
79 ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
80 Action action = actionFactory.getAction(actionContext);
82 SAXParserFactory t = SAXParserFactory.newInstance();
83 SAXParser p = t.newSAXParser();
85 action.run(p, cliOptions.getInputSource());
88 log.log(Level.FINE, "All done");
89 exitCode = EXIT_SUCCESS;
90 } catch (CLIParserException e) {
91 log.log(Level.SEVERE, "Unable to parse CLI options", e);
92 exitCode = EXIT_CLI_PARSE_ERROR;
93 } catch (InvalidOptionsException e) {
94 log.log(Level.SEVERE, "Invalid CLI options", e);
95 for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
96 LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
97 r.setThrown(p.getException());
98 r.setParameters(new Object[]{p.getDescription()});
101 exitCode = EXIT_CLI_VALIDATE_ERROR;
102 } catch (OutputActionException e) {
103 log.log(Level.SEVERE, "Ouput module problem", e);
104 exitCode = EXIT_OUTPUT_ACTION_ERROR;
105 } catch (ParserConfigurationException | SAXException e) {
106 log.log(Level.SEVERE, "Unable to create SAX parser", e);
107 exitCode = EXIT_SAX_ERROR;
110 System.exit(exitCode);