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
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 package cz.frantovo.alt2xml.cli;
    18 
    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;
    24 import java.util.Map;
    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;
    37 
    38 /**
    39  *
    40  * @author Ing. František Kučera (frantovo.cz)
    41  */
    42 public class CLIStarter {
    43 
    44 	// help:exit-codes
    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";
    56 
    57 	public static void main(String[] args) {
    58 		log.log(Level.FINE, "Starting " + PROGRAM_NAME);
    59 		int exitCode;
    60 
    61 		try {
    62 			CLIParser cliParser = new CLIParser();
    63 			CLIOptions cliOptions = cliParser.parseOptions(args);
    64 
    65 			cliOptions.validate();
    66 
    67 			Map<String, ActionFactory> actionFactories = new HashMap<>();
    68 
    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()});
    73 			}
    74 
    75 			String actionCode = cliOptions.getAction();
    76 			ActionFactory actionFactory = actionFactories.get(actionCode);
    77 
    78 			if (actionFactory == null) {
    79 				InvalidOptionsException e = new InvalidOptionsException();
    80 				e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
    81 				throw e;
    82 			} else {
    83 
    84 				ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
    85 
    86 				actionContext.setActionProperties(cliOptions.getActionProperties());
    87 				actionContext.setActionData(cliOptions.getActionData());
    88 
    89 				Action action = actionFactory.getAction(actionContext);
    90 
    91 				SAXParserFactory t = SAXParserFactory.newInstance();
    92 				parametrizeParserFactory(t, cliOptions.getReaderFeatures());
    93 				
    94 				SAXParser p = t.newSAXParser();
    95 				paramtrizeParser(p, cliOptions.getReaderProperties());
    96 
    97 				action.run(p, cliOptions.getInputSource());
    98 			}
    99 
   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()});
   111 				log.log(r);
   112 			}
   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;
   120 		}
   121 
   122 		System.exit(exitCode);
   123 	}
   124 
   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());
   128 		}
   129 	}
   130 
   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));
   134 		}
   135 	}
   136 }