java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractHandlerAction.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 16 Jun 2014 13:16:45 +0200
changeset 45 ce2013823604
parent 43 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java@058c1c39251e
child 111 e4900596abdb
permissions -rw-r--r--
AbstractAction → AbstractHandlerAction; akce dostanou rovnou SAXParser a InputSource
franta-hg@43
     1
/**
franta-hg@43
     2
 * Alt2XML
franta-hg@43
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@43
     4
 *
franta-hg@43
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@43
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@43
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@43
     8
 * (at your option) any later version.
franta-hg@43
     9
 *
franta-hg@43
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@43
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@43
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@43
    13
 * GNU General Public License for more details.
franta-hg@43
    14
 *
franta-hg@43
    15
 * You should have received a copy of the GNU General Public License
franta-hg@43
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@43
    17
 */
franta-hg@43
    18
package cz.frantovo.alt2xml.out;
franta-hg@43
    19
franta-hg@45
    20
import java.io.IOException;
franta-hg@45
    21
import java.util.logging.Level;
franta-hg@45
    22
import java.util.logging.Logger;
franta-hg@45
    23
import javax.xml.parsers.SAXParser;
franta-hg@45
    24
import org.xml.sax.InputSource;
franta-hg@45
    25
import org.xml.sax.SAXException;
franta-hg@45
    26
import org.xml.sax.SAXNotRecognizedException;
franta-hg@45
    27
import org.xml.sax.SAXNotSupportedException;
franta-hg@43
    28
import org.xml.sax.ext.LexicalHandler;
franta-hg@45
    29
import org.xml.sax.helpers.DefaultHandler;
franta-hg@43
    30
franta-hg@43
    31
/**
franta-hg@45
    32
 * Recommended base class for Actions based on handlers.
franta-hg@43
    33
 *
franta-hg@43
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@43
    35
 */
franta-hg@45
    36
public abstract class AbstractHandlerAction extends AbstractAction {
franta-hg@43
    37
franta-hg@45
    38
	private static final Logger log = Logger.getLogger(AbstractHandlerAction.class.getName());
franta-hg@45
    39
	public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler";
franta-hg@43
    40
franta-hg@45
    41
	public AbstractHandlerAction(ActionContext actionContext) {
franta-hg@45
    42
		super(actionContext);
franta-hg@43
    43
	}
franta-hg@43
    44
franta-hg@45
    45
	public abstract DefaultHandler getDefaultHandler();
franta-hg@43
    46
franta-hg@43
    47
	/**
franta-hg@45
    48
	 * For e.g. comment nodes.
franta-hg@45
    49
	 *
franta-hg@45
    50
	 * @return may return null if this output module does not support lexical events
franta-hg@43
    51
	 */
franta-hg@43
    52
	public LexicalHandler getLexicalHandler() {
franta-hg@43
    53
		return null;
franta-hg@43
    54
	}
franta-hg@43
    55
franta-hg@45
    56
	@Override
franta-hg@45
    57
	public void run(SAXParser parser, InputSource source) throws OutputActionException {
franta-hg@45
    58
		DefaultHandler defaultHandler = getDefaultHandler();
franta-hg@45
    59
		LexicalHandler lexicalHandler = getLexicalHandler();
franta-hg@45
    60
franta-hg@45
    61
		if (lexicalHandler == null) {
franta-hg@45
    62
			log.log(Level.FINE, "No LexicalHandler provided → no comment nodes etc. on output.");
franta-hg@45
    63
		} else {
franta-hg@45
    64
			try {
franta-hg@45
    65
				parser.setProperty(LEXICAL_HANDLER_PROPERTY, lexicalHandler);
franta-hg@45
    66
			} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
franta-hg@45
    67
				log.log(Level.WARNING, "LexicalHandler registration exception:", e);
franta-hg@45
    68
				log.log(Level.WARNING,
franta-hg@45
    69
						"Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
franta-hg@45
    70
						new Object[]{lexicalHandler, parser});
franta-hg@45
    71
			}
franta-hg@45
    72
		}
franta-hg@45
    73
franta-hg@45
    74
		try {
franta-hg@45
    75
			parser.parse(source, defaultHandler);
franta-hg@45
    76
		} catch (IOException | SAXException e) {
franta-hg@45
    77
			throw new OutputActionException("Error while parsing document: " + source, e);
franta-hg@45
    78
		}
franta-hg@45
    79
	}
franta-hg@43
    80
}