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@39: import org.xml.sax.ext.LexicalHandler; franta-hg@2: import org.xml.sax.helpers.DefaultHandler; 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@2: public class CLI { franta-hg@2: franta-hg@39: public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler"; franta-hg@30: private static final Logger log = Logger.getLogger(CLI.class.getName()); franta-hg@28: franta-hg@30: public static void main(String[] args) { franta-hg@16: franta-hg@30: try { franta-hg@30: File vstup = 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@43: 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: DefaultHandler defaultHandler = action.getDefaultHandler(); franta-hg@43: LexicalHandler lexicalHandler = action.getLexicalHandler(); franta-hg@43: franta-hg@43: SAXParserFactory t = SAXParserFactory.newInstance(); franta-hg@43: SAXParser p = t.newSAXParser(); franta-hg@43: franta-hg@43: if (lexicalHandler == null) { franta-hg@43: log.log(Level.FINE, "No LexicalHandler provided."); franta-hg@43: } else { franta-hg@43: try { franta-hg@43: p.setProperty(LEXICAL_HANDLER_PROPERTY, defaultHandler); franta-hg@43: } catch (Exception e) { franta-hg@43: log.log(Level.WARNING, "LexicalHandler registration exception:", e); franta-hg@43: log.log(Level.WARNING, franta-hg@43: "Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}", franta-hg@43: new Object[]{defaultHandler, p}); franta-hg@43: } franta-hg@43: } franta-hg@43: franta-hg@43: p.parse(vstup, defaultHandler); 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: }