franta-hg@43: /** franta-hg@43: * Alt2XML franta-hg@43: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@43: * franta-hg@43: * This program is free software: you can redistribute it and/or modify franta-hg@43: * it under the terms of the GNU General Public License as published by franta-hg@111: * the Free Software Foundation, version 3 of the License. franta-hg@43: * franta-hg@43: * This program is distributed in the hope that it will be useful, franta-hg@43: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@43: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@43: * GNU General Public License for more details. franta-hg@43: * franta-hg@43: * You should have received a copy of the GNU General Public License franta-hg@43: * along with this program. If not, see . franta-hg@43: */ franta-hg@43: package cz.frantovo.alt2xml.out; franta-hg@43: franta-hg@45: import java.io.IOException; franta-hg@45: import java.util.logging.Level; franta-hg@45: import java.util.logging.Logger; franta-hg@45: import javax.xml.parsers.SAXParser; franta-hg@45: import org.xml.sax.InputSource; franta-hg@45: import org.xml.sax.SAXException; franta-hg@45: import org.xml.sax.SAXNotRecognizedException; franta-hg@45: import org.xml.sax.SAXNotSupportedException; franta-hg@43: import org.xml.sax.ext.LexicalHandler; franta-hg@45: import org.xml.sax.helpers.DefaultHandler; franta-hg@43: franta-hg@43: /** franta-hg@45: * Recommended base class for Actions based on handlers. franta-hg@43: * franta-hg@43: * @author Ing. František Kučera (frantovo.cz) franta-hg@43: */ franta-hg@45: public abstract class AbstractHandlerAction extends AbstractAction { franta-hg@43: franta-hg@45: private static final Logger log = Logger.getLogger(AbstractHandlerAction.class.getName()); franta-hg@45: public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler"; franta-hg@43: franta-hg@45: public AbstractHandlerAction(ActionContext actionContext) { franta-hg@45: super(actionContext); franta-hg@43: } franta-hg@43: franta-hg@45: public abstract DefaultHandler getDefaultHandler(); franta-hg@43: franta-hg@43: /** franta-hg@45: * For e.g. comment nodes. franta-hg@45: * franta-hg@45: * @return may return null if this output module does not support lexical events franta-hg@43: */ franta-hg@43: public LexicalHandler getLexicalHandler() { franta-hg@43: return null; franta-hg@43: } franta-hg@43: franta-hg@45: @Override franta-hg@45: public void run(SAXParser parser, InputSource source) throws OutputActionException { franta-hg@45: DefaultHandler defaultHandler = getDefaultHandler(); franta-hg@45: LexicalHandler lexicalHandler = getLexicalHandler(); franta-hg@45: franta-hg@45: if (lexicalHandler == null) { franta-hg@45: log.log(Level.FINE, "No LexicalHandler provided → no comment nodes etc. on output."); franta-hg@45: } else { franta-hg@45: try { franta-hg@45: parser.setProperty(LEXICAL_HANDLER_PROPERTY, lexicalHandler); franta-hg@45: } catch (SAXNotRecognizedException | SAXNotSupportedException e) { franta-hg@45: log.log(Level.WARNING, "LexicalHandler registration exception:", e); franta-hg@45: log.log(Level.WARNING, franta-hg@45: "Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}", franta-hg@45: new Object[]{lexicalHandler, parser}); franta-hg@45: } franta-hg@45: } franta-hg@45: franta-hg@45: try { franta-hg@45: parser.parse(source, defaultHandler); franta-hg@45: } catch (IOException | SAXException e) { franta-hg@45: throw new OutputActionException("Error while parsing document: " + source, e); franta-hg@45: } franta-hg@45: } franta-hg@43: }