# HG changeset patch # User František Kučera # Date 1403021941 -7200 # Node ID 6bcb20e856fe8cb91f14bb8af8a45265619b2050 # Parent e539cae277479e0f73861a010498a0dd90f92f06 CLI → CLIStarter diff -r e539cae27747 -r 6bcb20e856fe java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java Mon Jun 16 22:35:44 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -/** - * Alt2XML - * Copyright © 2014 František Kučera (frantovo.cz) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package cz.frantovo.alt2xml.cli; - -import cz.frantovo.alt2xml.out.Action; -import cz.frantovo.alt2xml.out.ActionContext; -import cz.frantovo.alt2xml.out.ActionFactory; -import java.io.File; -import java.io.OutputStream; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import org.xml.sax.InputSource; - -/** - * - * @author Ing. František Kučera (frantovo.cz) - */ -public class CLI { - - private static final Logger log = Logger.getLogger(CLI.class.getName()); - - public static void main(String[] args) { - - try { - File inputFile = new File(args[0]); - String actionCode = args[1]; - OutputStream outputStream = System.out; - - Map actionFactories = new HashMap<>(); - - for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) { - String code = f.getActionCode(); - actionFactories.put(code, f); - log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()}); - } - - ActionFactory actionFactory = actionFactories.get(actionCode); - - if (actionFactory == null) { - log.log(Level.SEVERE, "No such output action with code: {0}", actionCode); - } else { - - ActionContext actionContext = new ActionContext(outputStream); - Action action = actionFactory.getAction(actionContext); - - SAXParserFactory t = SAXParserFactory.newInstance(); - SAXParser p = t.newSAXParser(); - - action.run(p, new InputSource(inputFile.toURI().toASCIIString())); - } - - } catch (Exception e) { - log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e); - } - - } -} diff -r e539cae27747 -r 6bcb20e856fe java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java Tue Jun 17 18:19:01 2014 +0200 @@ -0,0 +1,78 @@ +/** + * Alt2XML + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.alt2xml.cli; + +import cz.frantovo.alt2xml.out.Action; +import cz.frantovo.alt2xml.out.ActionContext; +import cz.frantovo.alt2xml.out.ActionFactory; +import java.io.File; +import java.io.OutputStream; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import org.xml.sax.InputSource; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CLIStarter { + + private static final Logger log = Logger.getLogger(CLIStarter.class.getName()); + + public static void main(String[] args) { + + try { + File inputFile = new File(args[0]); + String actionCode = args[1]; + OutputStream outputStream = System.out; + + Map actionFactories = new HashMap<>(); + + for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) { + String code = f.getActionCode(); + actionFactories.put(code, f); + log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()}); + } + + ActionFactory actionFactory = actionFactories.get(actionCode); + + if (actionFactory == null) { + log.log(Level.SEVERE, "No such output action with code: {0}", actionCode); + } else { + + ActionContext actionContext = new ActionContext(outputStream); + Action action = actionFactory.getAction(actionContext); + + SAXParserFactory t = SAXParserFactory.newInstance(); + SAXParser p = t.newSAXParser(); + + action.run(p, new InputSource(inputFile.toURI().toASCIIString())); + } + + } catch (Exception e) { + log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e); + } + + } +}