java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 16 Jun 2014 13:16:45 +0200
changeset 45 ce2013823604
parent 43 058c1c39251e
child 50 3af3ef97f33f
permissions -rw-r--r--
AbstractAction → AbstractHandlerAction; akce dostanou rovnou SAXParser a InputSource
franta-hg@11
     1
/**
franta-hg@11
     2
 * Alt2XML
franta-hg@11
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@11
     4
 *
franta-hg@11
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@11
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@11
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@11
     8
 * (at your option) any later version.
franta-hg@11
     9
 *
franta-hg@11
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@11
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11
    13
 * GNU General Public License for more details.
franta-hg@11
    14
 *
franta-hg@11
    15
 * You should have received a copy of the GNU General Public License
franta-hg@11
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@11
    17
 */
franta-hg@16
    18
package cz.frantovo.alt2xml.cli;
franta-hg@2
    19
franta-hg@43
    20
import cz.frantovo.alt2xml.out.Action;
franta-hg@43
    21
import cz.frantovo.alt2xml.out.ActionContext;
franta-hg@43
    22
import cz.frantovo.alt2xml.out.ActionFactory;
franta-hg@21
    23
import java.io.File;
franta-hg@2
    24
import java.io.OutputStream;
franta-hg@30
    25
import java.util.Arrays;
franta-hg@43
    26
import java.util.HashMap;
franta-hg@43
    27
import java.util.Map;
franta-hg@43
    28
import java.util.ServiceLoader;
franta-hg@30
    29
import java.util.logging.Level;
franta-hg@30
    30
import java.util.logging.Logger;
franta-hg@2
    31
import javax.xml.parsers.SAXParser;
franta-hg@2
    32
import javax.xml.parsers.SAXParserFactory;
franta-hg@45
    33
import org.xml.sax.InputSource;
franta-hg@2
    34
franta-hg@2
    35
/**
franta-hg@2
    36
 *
franta-hg@19
    37
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    38
 */
franta-hg@2
    39
public class CLI {
franta-hg@2
    40
franta-hg@30
    41
	private static final Logger log = Logger.getLogger(CLI.class.getName());
franta-hg@28
    42
franta-hg@30
    43
	public static void main(String[] args) {
franta-hg@16
    44
franta-hg@30
    45
		try {
franta-hg@45
    46
			File inputFile = new File(args[0]);
franta-hg@43
    47
			String actionCode = args[1];
franta-hg@43
    48
			OutputStream outputStream = System.out;
franta-hg@16
    49
franta-hg@43
    50
			Map<String, ActionFactory> actionFactories = new HashMap<>();
franta-hg@30
    51
franta-hg@43
    52
			for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
franta-hg@43
    53
				String code = f.getActionCode();
franta-hg@43
    54
				actionFactories.put(code, f);
franta-hg@43
    55
				log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
franta-hg@39
    56
			}
franta-hg@39
    57
franta-hg@43
    58
			ActionFactory actionFactory = actionFactories.get(actionCode);
franta-hg@43
    59
franta-hg@43
    60
			if (actionFactory == null) {
franta-hg@43
    61
				log.log(Level.SEVERE, "No such output action with code: {0}", actionCode);
franta-hg@43
    62
			} else {
franta-hg@43
    63
franta-hg@43
    64
				ActionContext actionContext = new ActionContext(outputStream);
franta-hg@43
    65
				Action action = actionFactory.getAction(actionContext);
franta-hg@43
    66
franta-hg@43
    67
				SAXParserFactory t = SAXParserFactory.newInstance();
franta-hg@43
    68
				SAXParser p = t.newSAXParser();
franta-hg@43
    69
franta-hg@45
    70
				action.run(p, new InputSource(inputFile.toURI().toASCIIString()));
franta-hg@43
    71
			}
franta-hg@43
    72
franta-hg@30
    73
		} catch (Exception e) {
franta-hg@30
    74
			log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e);
franta-hg@30
    75
		}
franta-hg@21
    76
franta-hg@2
    77
	}
franta-hg@2
    78
}