java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java
changeset 53 6bcb20e856fe
parent 52 e539cae27747
child 55 c703fb7f088f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java	Tue Jun 17 18:19:01 2014 +0200
     1.3 @@ -0,0 +1,78 @@
     1.4 +/**
     1.5 + * Alt2XML
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.alt2xml.cli;
    1.22 +
    1.23 +import cz.frantovo.alt2xml.out.Action;
    1.24 +import cz.frantovo.alt2xml.out.ActionContext;
    1.25 +import cz.frantovo.alt2xml.out.ActionFactory;
    1.26 +import java.io.File;
    1.27 +import java.io.OutputStream;
    1.28 +import java.util.Arrays;
    1.29 +import java.util.HashMap;
    1.30 +import java.util.Map;
    1.31 +import java.util.ServiceLoader;
    1.32 +import java.util.logging.Level;
    1.33 +import java.util.logging.Logger;
    1.34 +import javax.xml.parsers.SAXParser;
    1.35 +import javax.xml.parsers.SAXParserFactory;
    1.36 +import org.xml.sax.InputSource;
    1.37 +
    1.38 +/**
    1.39 + *
    1.40 + * @author Ing. František Kučera (frantovo.cz)
    1.41 + */
    1.42 +public class CLIStarter {
    1.43 +
    1.44 +	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    1.45 +
    1.46 +	public static void main(String[] args) {
    1.47 +
    1.48 +		try {
    1.49 +			File inputFile = new File(args[0]);
    1.50 +			String actionCode = args[1];
    1.51 +			OutputStream outputStream = System.out;
    1.52 +
    1.53 +			Map<String, ActionFactory> actionFactories = new HashMap<>();
    1.54 +
    1.55 +			for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
    1.56 +				String code = f.getActionCode();
    1.57 +				actionFactories.put(code, f);
    1.58 +				log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
    1.59 +			}
    1.60 +
    1.61 +			ActionFactory actionFactory = actionFactories.get(actionCode);
    1.62 +
    1.63 +			if (actionFactory == null) {
    1.64 +				log.log(Level.SEVERE, "No such output action with code: {0}", actionCode);
    1.65 +			} else {
    1.66 +
    1.67 +				ActionContext actionContext = new ActionContext(outputStream);
    1.68 +				Action action = actionFactory.getAction(actionContext);
    1.69 +
    1.70 +				SAXParserFactory t = SAXParserFactory.newInstance();
    1.71 +				SAXParser p = t.newSAXParser();
    1.72 +
    1.73 +				action.run(p, new InputSource(inputFile.toURI().toASCIIString()));
    1.74 +			}
    1.75 +
    1.76 +		} catch (Exception e) {
    1.77 +			log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e);
    1.78 +		}
    1.79 +
    1.80 +	}
    1.81 +}