java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 15 Jun 2014 14:50:49 +0200
changeset 43 058c1c39251e
parent 40 4afb00b7b1a9
child 45 ce2013823604
permissions -rw-r--r--
ouptup modules framework + XML (echo) output module
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@39
    33
import org.xml.sax.ext.LexicalHandler;
franta-hg@2
    34
import org.xml.sax.helpers.DefaultHandler;
franta-hg@2
    35
franta-hg@2
    36
/**
franta-hg@2
    37
 *
franta-hg@19
    38
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    39
 */
franta-hg@2
    40
public class CLI {
franta-hg@2
    41
franta-hg@39
    42
	public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler";
franta-hg@30
    43
	private static final Logger log = Logger.getLogger(CLI.class.getName());
franta-hg@28
    44
franta-hg@30
    45
	public static void main(String[] args) {
franta-hg@16
    46
franta-hg@30
    47
		try {
franta-hg@30
    48
			File vstup = new File(args[0]);
franta-hg@43
    49
			String actionCode = args[1];
franta-hg@43
    50
			OutputStream outputStream = System.out;
franta-hg@16
    51
franta-hg@43
    52
			Map<String, ActionFactory> actionFactories = new HashMap<>();
franta-hg@30
    53
franta-hg@43
    54
			for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
franta-hg@43
    55
				String code = f.getActionCode();
franta-hg@43
    56
				actionFactories.put(code, f);
franta-hg@43
    57
				log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
franta-hg@39
    58
			}
franta-hg@39
    59
franta-hg@43
    60
			ActionFactory actionFactory = actionFactories.get(actionCode);
franta-hg@43
    61
franta-hg@43
    62
			if (actionFactory == null) {
franta-hg@43
    63
				log.log(Level.SEVERE, "No such output action with code: {0}", actionCode);
franta-hg@43
    64
			} else {
franta-hg@43
    65
franta-hg@43
    66
				ActionContext actionContext = new ActionContext(outputStream);
franta-hg@43
    67
				Action action = actionFactory.getAction(actionContext);
franta-hg@43
    68
franta-hg@43
    69
				DefaultHandler defaultHandler = action.getDefaultHandler();
franta-hg@43
    70
				LexicalHandler lexicalHandler = action.getLexicalHandler();
franta-hg@43
    71
franta-hg@43
    72
				SAXParserFactory t = SAXParserFactory.newInstance();
franta-hg@43
    73
				SAXParser p = t.newSAXParser();
franta-hg@43
    74
franta-hg@43
    75
				if (lexicalHandler == null) {
franta-hg@43
    76
					log.log(Level.FINE, "No LexicalHandler provided.");
franta-hg@43
    77
				} else {
franta-hg@43
    78
					try {
franta-hg@43
    79
						p.setProperty(LEXICAL_HANDLER_PROPERTY, defaultHandler);
franta-hg@43
    80
					} catch (Exception e) {
franta-hg@43
    81
						log.log(Level.WARNING, "LexicalHandler registration exception:", e);
franta-hg@43
    82
						log.log(Level.WARNING,
franta-hg@43
    83
								"Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
franta-hg@43
    84
								new Object[]{defaultHandler, p});
franta-hg@43
    85
					}
franta-hg@43
    86
				}
franta-hg@43
    87
franta-hg@43
    88
				p.parse(vstup, defaultHandler);
franta-hg@43
    89
			}
franta-hg@43
    90
franta-hg@30
    91
		} catch (Exception e) {
franta-hg@30
    92
			log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e);
franta-hg@30
    93
		}
franta-hg@21
    94
franta-hg@2
    95
	}
franta-hg@2
    96
}