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@11: * the Free Software Foundation, either version 3 of the License, or
franta-hg@11: * (at your option) any later version.
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@21: import java.io.File;
franta-hg@2: import java.io.OutputStream;
franta-hg@30: import java.util.Arrays;
franta-hg@43: import java.util.HashMap;
franta-hg@43: import java.util.Map;
franta-hg@43: import java.util.ServiceLoader;
franta-hg@30: import java.util.logging.Level;
franta-hg@30: import java.util.logging.Logger;
franta-hg@2: import javax.xml.parsers.SAXParser;
franta-hg@2: import javax.xml.parsers.SAXParserFactory;
franta-hg@45: import org.xml.sax.InputSource;
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@53: private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
franta-hg@28:
franta-hg@30: public static void main(String[] args) {
franta-hg@16:
franta-hg@30: try {
franta-hg@45: File inputFile = new File(args[0]);
franta-hg@43: String actionCode = args[1];
franta-hg@43: OutputStream outputStream = System.out;
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@43: ActionFactory actionFactory = actionFactories.get(actionCode);
franta-hg@43:
franta-hg@43: if (actionFactory == null) {
franta-hg@43: log.log(Level.SEVERE, "No such output action with code: {0}", actionCode);
franta-hg@43: } else {
franta-hg@43:
franta-hg@43: ActionContext actionContext = new ActionContext(outputStream);
franta-hg@43: Action action = actionFactory.getAction(actionContext);
franta-hg@43:
franta-hg@43: SAXParserFactory t = SAXParserFactory.newInstance();
franta-hg@43: SAXParser p = t.newSAXParser();
franta-hg@43:
franta-hg@45: action.run(p, new InputSource(inputFile.toURI().toASCIIString()));
franta-hg@43: }
franta-hg@43:
franta-hg@30: } catch (Exception e) {
franta-hg@30: log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e);
franta-hg@30: }
franta-hg@21:
franta-hg@2: }
franta-hg@2: }