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