java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractHandlerAction.java
changeset 45 ce2013823604
parent 43 058c1c39251e
child 111 e4900596abdb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractHandlerAction.java	Mon Jun 16 13:16:45 2014 +0200
     1.3 @@ -0,0 +1,80 @@
     1.4 +/**
     1.5 + * Alt2XML
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.alt2xml.out;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.util.logging.Level;
    1.25 +import java.util.logging.Logger;
    1.26 +import javax.xml.parsers.SAXParser;
    1.27 +import org.xml.sax.InputSource;
    1.28 +import org.xml.sax.SAXException;
    1.29 +import org.xml.sax.SAXNotRecognizedException;
    1.30 +import org.xml.sax.SAXNotSupportedException;
    1.31 +import org.xml.sax.ext.LexicalHandler;
    1.32 +import org.xml.sax.helpers.DefaultHandler;
    1.33 +
    1.34 +/**
    1.35 + * Recommended base class for Actions based on handlers.
    1.36 + *
    1.37 + * @author Ing. František Kučera (frantovo.cz)
    1.38 + */
    1.39 +public abstract class AbstractHandlerAction extends AbstractAction {
    1.40 +
    1.41 +	private static final Logger log = Logger.getLogger(AbstractHandlerAction.class.getName());
    1.42 +	public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler";
    1.43 +
    1.44 +	public AbstractHandlerAction(ActionContext actionContext) {
    1.45 +		super(actionContext);
    1.46 +	}
    1.47 +
    1.48 +	public abstract DefaultHandler getDefaultHandler();
    1.49 +
    1.50 +	/**
    1.51 +	 * For e.g. comment nodes.
    1.52 +	 *
    1.53 +	 * @return may return null if this output module does not support lexical events
    1.54 +	 */
    1.55 +	public LexicalHandler getLexicalHandler() {
    1.56 +		return null;
    1.57 +	}
    1.58 +
    1.59 +	@Override
    1.60 +	public void run(SAXParser parser, InputSource source) throws OutputActionException {
    1.61 +		DefaultHandler defaultHandler = getDefaultHandler();
    1.62 +		LexicalHandler lexicalHandler = getLexicalHandler();
    1.63 +
    1.64 +		if (lexicalHandler == null) {
    1.65 +			log.log(Level.FINE, "No LexicalHandler provided → no comment nodes etc. on output.");
    1.66 +		} else {
    1.67 +			try {
    1.68 +				parser.setProperty(LEXICAL_HANDLER_PROPERTY, lexicalHandler);
    1.69 +			} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
    1.70 +				log.log(Level.WARNING, "LexicalHandler registration exception:", e);
    1.71 +				log.log(Level.WARNING,
    1.72 +						"Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
    1.73 +						new Object[]{lexicalHandler, parser});
    1.74 +			}
    1.75 +		}
    1.76 +
    1.77 +		try {
    1.78 +			parser.parse(source, defaultHandler);
    1.79 +		} catch (IOException | SAXException e) {
    1.80 +			throw new OutputActionException("Error while parsing document: " + source, e);
    1.81 +		}
    1.82 +	}
    1.83 +}