AbstractAction → AbstractHandlerAction; akce dostanou rovnou SAXParser a InputSource
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 16 Jun 2014 13:16:45 +0200
changeset 45ce2013823604
parent 44 7bd195a5fa2a
child 46 7f9df0b6b20f
AbstractAction → AbstractHandlerAction; akce dostanou rovnou SAXParser a InputSource
java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java
java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java
java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractHandlerAction.java
java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java
java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLAction.java
     1.1 --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java	Mon Jun 16 11:11:01 2014 +0200
     1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java	Mon Jun 16 13:16:45 2014 +0200
     1.3 @@ -30,8 +30,7 @@
     1.4  import java.util.logging.Logger;
     1.5  import javax.xml.parsers.SAXParser;
     1.6  import javax.xml.parsers.SAXParserFactory;
     1.7 -import org.xml.sax.ext.LexicalHandler;
     1.8 -import org.xml.sax.helpers.DefaultHandler;
     1.9 +import org.xml.sax.InputSource;
    1.10  
    1.11  /**
    1.12   *
    1.13 @@ -39,13 +38,12 @@
    1.14   */
    1.15  public class CLI {
    1.16  
    1.17 -	public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler";
    1.18  	private static final Logger log = Logger.getLogger(CLI.class.getName());
    1.19  
    1.20  	public static void main(String[] args) {
    1.21  
    1.22  		try {
    1.23 -			File vstup = new File(args[0]);
    1.24 +			File inputFile = new File(args[0]);
    1.25  			String actionCode = args[1];
    1.26  			OutputStream outputStream = System.out;
    1.27  
    1.28 @@ -66,26 +64,10 @@
    1.29  				ActionContext actionContext = new ActionContext(outputStream);
    1.30  				Action action = actionFactory.getAction(actionContext);
    1.31  
    1.32 -				DefaultHandler defaultHandler = action.getDefaultHandler();
    1.33 -				LexicalHandler lexicalHandler = action.getLexicalHandler();
    1.34 -
    1.35  				SAXParserFactory t = SAXParserFactory.newInstance();
    1.36  				SAXParser p = t.newSAXParser();
    1.37  
    1.38 -				if (lexicalHandler == null) {
    1.39 -					log.log(Level.FINE, "No LexicalHandler provided.");
    1.40 -				} else {
    1.41 -					try {
    1.42 -						p.setProperty(LEXICAL_HANDLER_PROPERTY, defaultHandler);
    1.43 -					} catch (Exception e) {
    1.44 -						log.log(Level.WARNING, "LexicalHandler registration exception:", e);
    1.45 -						log.log(Level.WARNING,
    1.46 -								"Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
    1.47 -								new Object[]{defaultHandler, p});
    1.48 -					}
    1.49 -				}
    1.50 -
    1.51 -				p.parse(vstup, defaultHandler);
    1.52 +				action.run(p, new InputSource(inputFile.toURI().toASCIIString()));
    1.53  			}
    1.54  
    1.55  		} catch (Exception e) {
     2.1 --- a/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java	Mon Jun 16 11:11:01 2014 +0200
     2.2 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java	Mon Jun 16 13:16:45 2014 +0200
     2.3 @@ -17,8 +17,6 @@
     2.4   */
     2.5  package cz.frantovo.alt2xml.out;
     2.6  
     2.7 -import org.xml.sax.ext.LexicalHandler;
     2.8 -
     2.9  /**
    2.10   * Recommended base class for Actions.
    2.11   *
    2.12 @@ -36,12 +34,4 @@
    2.13  		return actionContext;
    2.14  	}
    2.15  
    2.16 -	/**
    2.17 -	 * @return null – lexical events are not supported
    2.18 -	 */
    2.19 -	@Override
    2.20 -	public LexicalHandler getLexicalHandler() {
    2.21 -		return null;
    2.22 -	}
    2.23 -
    2.24  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractHandlerAction.java	Mon Jun 16 13:16:45 2014 +0200
     3.3 @@ -0,0 +1,80 @@
     3.4 +/**
     3.5 + * Alt2XML
     3.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, either version 3 of the License, or
    3.11 + * (at your option) any later version.
    3.12 + *
    3.13 + * This program is distributed in the hope that it will be useful,
    3.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    3.16 + * GNU General Public License for more details.
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License
    3.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    3.20 + */
    3.21 +package cz.frantovo.alt2xml.out;
    3.22 +
    3.23 +import java.io.IOException;
    3.24 +import java.util.logging.Level;
    3.25 +import java.util.logging.Logger;
    3.26 +import javax.xml.parsers.SAXParser;
    3.27 +import org.xml.sax.InputSource;
    3.28 +import org.xml.sax.SAXException;
    3.29 +import org.xml.sax.SAXNotRecognizedException;
    3.30 +import org.xml.sax.SAXNotSupportedException;
    3.31 +import org.xml.sax.ext.LexicalHandler;
    3.32 +import org.xml.sax.helpers.DefaultHandler;
    3.33 +
    3.34 +/**
    3.35 + * Recommended base class for Actions based on handlers.
    3.36 + *
    3.37 + * @author Ing. František Kučera (frantovo.cz)
    3.38 + */
    3.39 +public abstract class AbstractHandlerAction extends AbstractAction {
    3.40 +
    3.41 +	private static final Logger log = Logger.getLogger(AbstractHandlerAction.class.getName());
    3.42 +	public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler";
    3.43 +
    3.44 +	public AbstractHandlerAction(ActionContext actionContext) {
    3.45 +		super(actionContext);
    3.46 +	}
    3.47 +
    3.48 +	public abstract DefaultHandler getDefaultHandler();
    3.49 +
    3.50 +	/**
    3.51 +	 * For e.g. comment nodes.
    3.52 +	 *
    3.53 +	 * @return may return null if this output module does not support lexical events
    3.54 +	 */
    3.55 +	public LexicalHandler getLexicalHandler() {
    3.56 +		return null;
    3.57 +	}
    3.58 +
    3.59 +	@Override
    3.60 +	public void run(SAXParser parser, InputSource source) throws OutputActionException {
    3.61 +		DefaultHandler defaultHandler = getDefaultHandler();
    3.62 +		LexicalHandler lexicalHandler = getLexicalHandler();
    3.63 +
    3.64 +		if (lexicalHandler == null) {
    3.65 +			log.log(Level.FINE, "No LexicalHandler provided → no comment nodes etc. on output.");
    3.66 +		} else {
    3.67 +			try {
    3.68 +				parser.setProperty(LEXICAL_HANDLER_PROPERTY, lexicalHandler);
    3.69 +			} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
    3.70 +				log.log(Level.WARNING, "LexicalHandler registration exception:", e);
    3.71 +				log.log(Level.WARNING,
    3.72 +						"Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
    3.73 +						new Object[]{lexicalHandler, parser});
    3.74 +			}
    3.75 +		}
    3.76 +
    3.77 +		try {
    3.78 +			parser.parse(source, defaultHandler);
    3.79 +		} catch (IOException | SAXException e) {
    3.80 +			throw new OutputActionException("Error while parsing document: " + source, e);
    3.81 +		}
    3.82 +	}
    3.83 +}
     4.1 --- a/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java	Mon Jun 16 11:11:01 2014 +0200
     4.2 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java	Mon Jun 16 13:16:45 2014 +0200
     4.3 @@ -17,8 +17,8 @@
     4.4   */
     4.5  package cz.frantovo.alt2xml.out;
     4.6  
     4.7 -import org.xml.sax.ext.LexicalHandler;
     4.8 -import org.xml.sax.helpers.DefaultHandler;
     4.9 +import javax.xml.parsers.SAXParser;
    4.10 +import org.xml.sax.InputSource;
    4.11  
    4.12  /**
    4.13   * Executive class of an output module.
    4.14 @@ -27,13 +27,6 @@
    4.15   */
    4.16  public interface Action {
    4.17  
    4.18 -	public DefaultHandler getDefaultHandler();
    4.19 -
    4.20 -	/**
    4.21 -	 * For e.g. comment nodes.
    4.22 -	 *
    4.23 -	 * @return may return null if this output module does not support lexical events
    4.24 -	 */
    4.25 -	public LexicalHandler getLexicalHandler();
    4.26 +	public void run(SAXParser parser, InputSource source) throws OutputActionException;
    4.27  
    4.28  }
     5.1 --- a/java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLAction.java	Mon Jun 16 11:11:01 2014 +0200
     5.2 +++ b/java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLAction.java	Mon Jun 16 13:16:45 2014 +0200
     5.3 @@ -17,7 +17,7 @@
     5.4   */
     5.5  package cz.frantovo.alt2xml.out.xml;
     5.6  
     5.7 -import cz.frantovo.alt2xml.out.AbstractAction;
     5.8 +import cz.frantovo.alt2xml.out.AbstractHandlerAction;
     5.9  import cz.frantovo.alt2xml.out.ActionContext;
    5.10  import javax.xml.stream.XMLStreamWriter;
    5.11  import org.xml.sax.helpers.DefaultHandler;
    5.12 @@ -26,7 +26,7 @@
    5.13   *
    5.14   * @author Ing. František Kučera (frantovo.cz)
    5.15   */
    5.16 -public class XMLAction extends AbstractAction {
    5.17 +public class XMLAction extends AbstractHandlerAction {
    5.18  
    5.19  	private final XMLHandler handler;
    5.20