1.1 --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java Sat Jun 14 22:56:51 2014 +0200
1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java Sun Jun 15 14:50:49 2014 +0200
1.3 @@ -17,16 +17,19 @@
1.4 */
1.5 package cz.frantovo.alt2xml.cli;
1.6
1.7 -import cz.frantovo.alt2xml.out.EchoContentHandler;
1.8 +import cz.frantovo.alt2xml.out.Action;
1.9 +import cz.frantovo.alt2xml.out.ActionContext;
1.10 +import cz.frantovo.alt2xml.out.ActionFactory;
1.11 import java.io.File;
1.12 import java.io.OutputStream;
1.13 import java.util.Arrays;
1.14 +import java.util.HashMap;
1.15 +import java.util.Map;
1.16 +import java.util.ServiceLoader;
1.17 import java.util.logging.Level;
1.18 import java.util.logging.Logger;
1.19 import javax.xml.parsers.SAXParser;
1.20 import javax.xml.parsers.SAXParserFactory;
1.21 -import javax.xml.stream.XMLOutputFactory;
1.22 -import javax.xml.stream.XMLStreamWriter;
1.23 import org.xml.sax.ext.LexicalHandler;
1.24 import org.xml.sax.helpers.DefaultHandler;
1.25
1.26 @@ -43,37 +46,48 @@
1.27
1.28 try {
1.29 File vstup = new File(args[0]);
1.30 - OutputStream výstup = System.out;
1.31 + String actionCode = args[1];
1.32 + OutputStream outputStream = System.out;
1.33
1.34 - /**
1.35 - * Serializujeme data do XML.
1.36 - * To normálně vůbec není potřeba – data se do tvaru proudu obsahujícího ostré závorky
1.37 - * vůbec nedostanou – zpracováváme události (volání javovských metod – začátky a konce
1.38 - * elementů atd.)
1.39 - * a z nich např. deserializujeme nějaké naše objekty, provádíme nějaké akce, nebo třeba
1.40 - * stavíme DOM.
1.41 - */
1.42 - XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
1.43 - XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(výstup);
1.44 - DefaultHandler h = new EchoContentHandler(w);
1.45 + Map<String, ActionFactory> actionFactories = new HashMap<>();
1.46
1.47 - SAXParserFactory t = SAXParserFactory.newInstance();
1.48 - SAXParser p = t.newSAXParser();
1.49 -
1.50 - if (h instanceof LexicalHandler
1.51 - // TODO: add option/feature to disable LexicalHandler registration
1.52 - && false) {
1.53 - try {
1.54 - p.setProperty(LEXICAL_HANDLER_PROPERTY, h);
1.55 - } catch (Exception e) {
1.56 - log.log(Level.WARNING, "LexicalHandler registration exception:", e);
1.57 - log.log(Level.WARNING,
1.58 - "Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
1.59 - new Object[]{h, p});
1.60 - }
1.61 + for (ActionFactory f : ServiceLoader.load(ActionFactory.class)) {
1.62 + String code = f.getActionCode();
1.63 + actionFactories.put(code, f);
1.64 + log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
1.65 }
1.66
1.67 - p.parse(vstup, h);
1.68 + ActionFactory actionFactory = actionFactories.get(actionCode);
1.69 +
1.70 + if (actionFactory == null) {
1.71 + log.log(Level.SEVERE, "No such output action with code: {0}", actionCode);
1.72 + } else {
1.73 +
1.74 + ActionContext actionContext = new ActionContext(outputStream);
1.75 + Action action = actionFactory.getAction(actionContext);
1.76 +
1.77 + DefaultHandler defaultHandler = action.getDefaultHandler();
1.78 + LexicalHandler lexicalHandler = action.getLexicalHandler();
1.79 +
1.80 + SAXParserFactory t = SAXParserFactory.newInstance();
1.81 + SAXParser p = t.newSAXParser();
1.82 +
1.83 + if (lexicalHandler == null) {
1.84 + log.log(Level.FINE, "No LexicalHandler provided.");
1.85 + } else {
1.86 + try {
1.87 + p.setProperty(LEXICAL_HANDLER_PROPERTY, defaultHandler);
1.88 + } catch (Exception e) {
1.89 + log.log(Level.WARNING, "LexicalHandler registration exception:", e);
1.90 + log.log(Level.WARNING,
1.91 + "Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
1.92 + new Object[]{defaultHandler, p});
1.93 + }
1.94 + }
1.95 +
1.96 + p.parse(vstup, defaultHandler);
1.97 + }
1.98 +
1.99 } catch (Exception e) {
1.100 log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e);
1.101 }