AbstractAction → AbstractHandlerAction; akce dostanou rovnou SAXParser a InputSource
3 * Copyright © 2014 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package cz.frantovo.alt2xml.out;
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;
32 * Recommended base class for Actions based on handlers.
34 * @author Ing. František Kučera (frantovo.cz)
36 public abstract class AbstractHandlerAction extends AbstractAction {
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";
41 public AbstractHandlerAction(ActionContext actionContext) {
45 public abstract DefaultHandler getDefaultHandler();
48 * For e.g. comment nodes.
50 * @return may return null if this output module does not support lexical events
52 public LexicalHandler getLexicalHandler() {
57 public void run(SAXParser parser, InputSource source) throws OutputActionException {
58 DefaultHandler defaultHandler = getDefaultHandler();
59 LexicalHandler lexicalHandler = getLexicalHandler();
61 if (lexicalHandler == null) {
62 log.log(Level.FINE, "No LexicalHandler provided → no comment nodes etc. on output.");
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});
75 parser.parse(source, defaultHandler);
76 } catch (IOException | SAXException e) {
77 throw new OutputActionException("Error while parsing document: " + source, e);