# HG changeset patch # User František Kučera # Date 1402917405 -7200 # Node ID ce20138236049386770f05f07c5ce9ed02f74e50 # Parent 7bd195a5fa2ac06bfdc7236ce42feccc0b983198 AbstractAction → AbstractHandlerAction; akce dostanou rovnou SAXParser a InputSource diff -r 7bd195a5fa2a -r ce2013823604 java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java Mon Jun 16 11:11:01 2014 +0200 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java Mon Jun 16 13:16:45 2014 +0200 @@ -30,8 +30,7 @@ import java.util.logging.Logger; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; -import org.xml.sax.ext.LexicalHandler; -import org.xml.sax.helpers.DefaultHandler; +import org.xml.sax.InputSource; /** * @@ -39,13 +38,12 @@ */ public class CLI { - public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler"; private static final Logger log = Logger.getLogger(CLI.class.getName()); public static void main(String[] args) { try { - File vstup = new File(args[0]); + File inputFile = new File(args[0]); String actionCode = args[1]; OutputStream outputStream = System.out; @@ -66,26 +64,10 @@ ActionContext actionContext = new ActionContext(outputStream); Action action = actionFactory.getAction(actionContext); - DefaultHandler defaultHandler = action.getDefaultHandler(); - LexicalHandler lexicalHandler = action.getLexicalHandler(); - SAXParserFactory t = SAXParserFactory.newInstance(); SAXParser p = t.newSAXParser(); - if (lexicalHandler == null) { - log.log(Level.FINE, "No LexicalHandler provided."); - } else { - try { - p.setProperty(LEXICAL_HANDLER_PROPERTY, defaultHandler); - } catch (Exception e) { - log.log(Level.WARNING, "LexicalHandler registration exception:", e); - log.log(Level.WARNING, - "Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}", - new Object[]{defaultHandler, p}); - } - } - - p.parse(vstup, defaultHandler); + action.run(p, new InputSource(inputFile.toURI().toASCIIString())); } } catch (Exception e) { diff -r 7bd195a5fa2a -r ce2013823604 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java --- a/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java Mon Jun 16 11:11:01 2014 +0200 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java Mon Jun 16 13:16:45 2014 +0200 @@ -17,8 +17,6 @@ */ package cz.frantovo.alt2xml.out; -import org.xml.sax.ext.LexicalHandler; - /** * Recommended base class for Actions. * @@ -36,12 +34,4 @@ return actionContext; } - /** - * @return null – lexical events are not supported - */ - @Override - public LexicalHandler getLexicalHandler() { - return null; - } - } diff -r 7bd195a5fa2a -r ce2013823604 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractHandlerAction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractHandlerAction.java Mon Jun 16 13:16:45 2014 +0200 @@ -0,0 +1,80 @@ +/** + * 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.out; + +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.parsers.SAXParser; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.ext.LexicalHandler; +import org.xml.sax.helpers.DefaultHandler; + +/** + * Recommended base class for Actions based on handlers. + * + * @author Ing. František Kučera (frantovo.cz) + */ +public abstract class AbstractHandlerAction extends AbstractAction { + + private static final Logger log = Logger.getLogger(AbstractHandlerAction.class.getName()); + public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler"; + + public AbstractHandlerAction(ActionContext actionContext) { + super(actionContext); + } + + public abstract DefaultHandler getDefaultHandler(); + + /** + * For e.g. comment nodes. + * + * @return may return null if this output module does not support lexical events + */ + public LexicalHandler getLexicalHandler() { + return null; + } + + @Override + public void run(SAXParser parser, InputSource source) throws OutputActionException { + DefaultHandler defaultHandler = getDefaultHandler(); + LexicalHandler lexicalHandler = getLexicalHandler(); + + if (lexicalHandler == null) { + log.log(Level.FINE, "No LexicalHandler provided → no comment nodes etc. on output."); + } else { + try { + parser.setProperty(LEXICAL_HANDLER_PROPERTY, lexicalHandler); + } catch (SAXNotRecognizedException | SAXNotSupportedException e) { + log.log(Level.WARNING, "LexicalHandler registration exception:", e); + log.log(Level.WARNING, + "Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}", + new Object[]{lexicalHandler, parser}); + } + } + + try { + parser.parse(source, defaultHandler); + } catch (IOException | SAXException e) { + throw new OutputActionException("Error while parsing document: " + source, e); + } + } +} diff -r 7bd195a5fa2a -r ce2013823604 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java --- a/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java Mon Jun 16 11:11:01 2014 +0200 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java Mon Jun 16 13:16:45 2014 +0200 @@ -17,8 +17,8 @@ */ package cz.frantovo.alt2xml.out; -import org.xml.sax.ext.LexicalHandler; -import org.xml.sax.helpers.DefaultHandler; +import javax.xml.parsers.SAXParser; +import org.xml.sax.InputSource; /** * Executive class of an output module. @@ -27,13 +27,6 @@ */ public interface Action { - public DefaultHandler getDefaultHandler(); - - /** - * For e.g. comment nodes. - * - * @return may return null if this output module does not support lexical events - */ - public LexicalHandler getLexicalHandler(); + public void run(SAXParser parser, InputSource source) throws OutputActionException; } diff -r 7bd195a5fa2a -r ce2013823604 java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLAction.java --- a/java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLAction.java Mon Jun 16 11:11:01 2014 +0200 +++ b/java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLAction.java Mon Jun 16 13:16:45 2014 +0200 @@ -17,7 +17,7 @@ */ package cz.frantovo.alt2xml.out.xml; -import cz.frantovo.alt2xml.out.AbstractAction; +import cz.frantovo.alt2xml.out.AbstractHandlerAction; import cz.frantovo.alt2xml.out.ActionContext; import javax.xml.stream.XMLStreamWriter; import org.xml.sax.helpers.DefaultHandler; @@ -26,7 +26,7 @@ * * @author Ing. František Kučera (frantovo.cz) */ -public class XMLAction extends AbstractAction { +public class XMLAction extends AbstractHandlerAction { private final XMLHandler handler;