java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 17 Sep 2014 21:00:32 +0200
changeset 99 fadfde5b3e55
parent 64 e2c691eedf4d
child 111 e4900596abdb
permissions -rw-r--r--
cli, lib-input: propagate features and properties to parser/reader
     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, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package cz.frantovo.alt2xml.cli;
    19 
    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;
    25 import java.util.Map;
    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;
    38 
    39 /**
    40  *
    41  * @author Ing. František Kučera (frantovo.cz)
    42  */
    43 public class CLIStarter {
    44 
    45 	// help:exit-codes
    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";
    57 
    58 	public static void main(String[] args) {
    59 		log.log(Level.FINE, "Starting " + PROGRAM_NAME);
    60 		int exitCode;
    61 
    62 		try {
    63 			CLIParser cliParser = new CLIParser();
    64 			CLIOptions cliOptions = cliParser.parseOptions(args);
    65 
    66 			cliOptions.validate();
    67 
    68 			Map<String, ActionFactory> actionFactories = new HashMap<>();
    69 
    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()});
    74 			}
    75 
    76 			String actionCode = cliOptions.getAction();
    77 			ActionFactory actionFactory = actionFactories.get(actionCode);
    78 
    79 			if (actionFactory == null) {
    80 				InvalidOptionsException e = new InvalidOptionsException();
    81 				e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
    82 				throw e;
    83 			} else {
    84 
    85 				ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
    86 
    87 				actionContext.setActionProperties(cliOptions.getActionProperties());
    88 				actionContext.setActionData(cliOptions.getActionData());
    89 
    90 				Action action = actionFactory.getAction(actionContext);
    91 
    92 				SAXParserFactory t = SAXParserFactory.newInstance();
    93 				parametrizeParserFactory(t, cliOptions.getReaderFeatures());
    94 				
    95 				SAXParser p = t.newSAXParser();
    96 				paramtrizeParser(p, cliOptions.getReaderProperties());
    97 
    98 				action.run(p, cliOptions.getInputSource());
    99 			}
   100 
   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()});
   112 				log.log(r);
   113 			}
   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;
   121 		}
   122 
   123 		System.exit(exitCode);
   124 	}
   125 
   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());
   129 		}
   130 	}
   131 
   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));
   135 		}
   136 	}
   137 }