franta-hg@11: /** franta-hg@11: * Alt2XML franta-hg@11: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@11: * franta-hg@11: * This program is free software: you can redistribute it and/or modify franta-hg@11: * it under the terms of the GNU General Public License as published by franta-hg@111: * the Free Software Foundation, version 3 of the License. franta-hg@11: * franta-hg@11: * This program is distributed in the hope that it will be useful, franta-hg@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@11: * GNU General Public License for more details. franta-hg@11: * franta-hg@11: * You should have received a copy of the GNU General Public License franta-hg@11: * along with this program. If not, see . franta-hg@11: */ franta-hg@16: package cz.frantovo.alt2xml.cli; franta-hg@2: franta-hg@43: import cz.frantovo.alt2xml.out.Action; franta-hg@43: import cz.frantovo.alt2xml.out.ActionContext; franta-hg@43: import cz.frantovo.alt2xml.out.ActionFactory; franta-hg@57: import cz.frantovo.alt2xml.out.OutputActionException; franta-hg@43: import java.util.HashMap; franta-hg@43: import java.util.Map; franta-hg@99: import java.util.Map.Entry; franta-hg@99: import java.util.Properties; franta-hg@43: import java.util.ServiceLoader; franta-hg@30: import java.util.logging.Level; franta-hg@57: import java.util.logging.LogRecord; franta-hg@30: import java.util.logging.Logger; franta-hg@57: import javax.xml.parsers.ParserConfigurationException; franta-hg@2: import javax.xml.parsers.SAXParser; franta-hg@2: import javax.xml.parsers.SAXParserFactory; franta-hg@57: import org.xml.sax.SAXException; franta-hg@99: import org.xml.sax.SAXNotRecognizedException; franta-hg@99: import org.xml.sax.SAXNotSupportedException; franta-hg@2: franta-hg@2: /** franta-hg@2: * franta-hg@19: * @author Ing. František Kučera (frantovo.cz) franta-hg@2: */ franta-hg@53: public class CLIStarter { franta-hg@2: franta-hg@57: // help:exit-codes franta-hg@57: public static final int EXIT_SUCCESS = 0; // doc:success franta-hg@57: public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug) franta-hg@57: // 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF franta-hg@57: public static final int EXIT_SAX_ERROR = 3; // doc:SAX error franta-hg@57: public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error franta-hg@57: public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error franta-hg@57: public static final int EXIT_CONFIGURATION_ERROR = 6; // doc:configuration error franta-hg@57: public static final int EXIT_OUTPUT_ACTION_ERROR = 7; // doc:formatting error franta-hg@57: // public static final int EXIT_BATCH_ERROR = 8; // doc:batch error franta-hg@53: private static final Logger log = Logger.getLogger(CLIStarter.class.getName()); franta-hg@57: private static final String PROGRAM_NAME = "alt2xml"; franta-hg@28: franta-hg@30: public static void main(String[] args) { franta-hg@57: log.log(Level.FINE, "Starting " + PROGRAM_NAME); franta-hg@57: int exitCode; franta-hg@16: franta-hg@30: try { franta-hg@55: CLIParser cliParser = new CLIParser(); franta-hg@55: CLIOptions cliOptions = cliParser.parseOptions(args); franta-hg@99: franta-hg@58: cliOptions.validate(); franta-hg@16: franta-hg@43: Map actionFactories = new HashMap<>(); franta-hg@30: franta-hg@43: for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) { franta-hg@43: String code = f.getActionCode(); franta-hg@43: actionFactories.put(code, f); franta-hg@52: log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()}); franta-hg@39: } franta-hg@39: franta-hg@55: String actionCode = cliOptions.getAction(); franta-hg@43: ActionFactory actionFactory = actionFactories.get(actionCode); franta-hg@43: franta-hg@43: if (actionFactory == null) { franta-hg@57: InvalidOptionsException e = new InvalidOptionsException(); franta-hg@57: e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode)); franta-hg@57: throw e; franta-hg@43: } else { franta-hg@43: franta-hg@55: ActionContext actionContext = new ActionContext(cliOptions.getOutputStream()); franta-hg@99: franta-hg@64: actionContext.setActionProperties(cliOptions.getActionProperties()); franta-hg@64: actionContext.setActionData(cliOptions.getActionData()); franta-hg@99: franta-hg@43: Action action = actionFactory.getAction(actionContext); franta-hg@43: franta-hg@43: SAXParserFactory t = SAXParserFactory.newInstance(); franta-hg@99: parametrizeParserFactory(t, cliOptions.getReaderFeatures()); franta-hg@99: franta-hg@43: SAXParser p = t.newSAXParser(); franta-hg@99: paramtrizeParser(p, cliOptions.getReaderProperties()); franta-hg@43: franta-hg@55: action.run(p, cliOptions.getInputSource()); franta-hg@43: } franta-hg@43: franta-hg@57: log.log(Level.FINE, "All done"); franta-hg@57: exitCode = EXIT_SUCCESS; franta-hg@57: } catch (CLIParserException e) { franta-hg@57: log.log(Level.SEVERE, "Unable to parse CLI options", e); franta-hg@57: exitCode = EXIT_CLI_PARSE_ERROR; franta-hg@57: } catch (InvalidOptionsException e) { franta-hg@57: log.log(Level.SEVERE, "Invalid CLI options", e); franta-hg@57: for (InvalidOptionsException.OptionProblem p : e.getProblems()) { franta-hg@57: LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}"); franta-hg@57: r.setThrown(p.getException()); franta-hg@57: r.setParameters(new Object[]{p.getDescription()}); franta-hg@57: log.log(r); franta-hg@57: } franta-hg@57: exitCode = EXIT_CLI_VALIDATE_ERROR; franta-hg@57: } catch (OutputActionException e) { franta-hg@57: log.log(Level.SEVERE, "Ouput module problem", e); franta-hg@57: exitCode = EXIT_OUTPUT_ACTION_ERROR; franta-hg@57: } catch (ParserConfigurationException | SAXException e) { franta-hg@57: log.log(Level.SEVERE, "Unable to create SAX parser", e); franta-hg@57: exitCode = EXIT_SAX_ERROR; franta-hg@30: } franta-hg@21: franta-hg@57: System.exit(exitCode); franta-hg@2: } franta-hg@99: franta-hg@99: private static void parametrizeParserFactory(SAXParserFactory factory, Map features) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException { franta-hg@99: for (Entry feature : features.entrySet()) { franta-hg@99: factory.setFeature(feature.getKey(), feature.getValue()); franta-hg@99: } franta-hg@99: } franta-hg@99: franta-hg@99: private static void paramtrizeParser(SAXParser parser, Properties readerProperties) throws SAXNotRecognizedException, SAXNotSupportedException { franta-hg@99: for (String name : readerProperties.stringPropertyNames()) { franta-hg@99: parser.setProperty(name, readerProperties.getProperty(name)); franta-hg@99: } franta-hg@99: } franta-hg@2: }