franta-hg@63: /** franta-hg@63: * Alt2XML franta-hg@63: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@63: * franta-hg@63: * This program is free software: you can redistribute it and/or modify franta-hg@63: * 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@63: * franta-hg@63: * This program is distributed in the hope that it will be useful, franta-hg@63: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@63: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@63: * GNU General Public License for more details. franta-hg@63: * franta-hg@63: * You should have received a copy of the GNU General Public License franta-hg@63: * along with this program. If not, see . franta-hg@63: */ franta-hg@63: package cz.frantovo.alt2xml.out; franta-hg@63: franta-hg@63: import javax.xml.parsers.SAXParser; franta-hg@63: import javax.xml.transform.Transformer; franta-hg@63: import javax.xml.transform.TransformerException; franta-hg@63: import javax.xml.transform.TransformerFactory; franta-hg@63: import javax.xml.transform.TransformerFactoryConfigurationError; franta-hg@63: import javax.xml.transform.dom.DOMResult; franta-hg@63: import javax.xml.transform.sax.SAXSource; franta-hg@63: import org.xml.sax.InputSource; franta-hg@63: import org.xml.sax.SAXException; franta-hg@63: franta-hg@63: /** franta-hg@63: * Recommended base class for Actions based on DOM processing. franta-hg@63: * This actiond does not support streaming – whole document must be parsed/converted to DOM and franta-hg@63: * after that the action is executed. franta-hg@63: * franta-hg@63: * @author Ing. František Kučera (frantovo.cz) franta-hg@63: */ franta-hg@63: public abstract class AbstractDOMAction extends AbstractAction { franta-hg@63: franta-hg@63: public AbstractDOMAction(ActionContext actionContext) { franta-hg@63: super(actionContext); franta-hg@63: } franta-hg@63: franta-hg@63: @Override franta-hg@63: public void run(SAXParser parser, InputSource source) throws OutputActionException { franta-hg@63: franta-hg@63: try { franta-hg@63: TransformerFactory tf = TransformerFactory.newInstance(); franta-hg@63: Transformer t = tf.newTransformer(); franta-hg@63: franta-hg@63: DOMResult domResult = new DOMResult(); franta-hg@63: franta-hg@63: t.transform(new SAXSource(parser.getXMLReader(), source), domResult); franta-hg@63: franta-hg@63: run(domResult); franta-hg@63: franta-hg@63: } catch (SAXException e) { franta-hg@63: throw new OutputActionException("Unable to get SAX reader during SAX→DOM conversion.", e); franta-hg@63: } catch (TransformerException | TransformerFactoryConfigurationError e) { franta-hg@63: throw new OutputActionException("Unable to convert SAX events to DOM using XSLT.", e); franta-hg@63: } franta-hg@63: } franta-hg@63: franta-hg@63: protected abstract void run(DOMResult domResult) throws OutputActionException; franta-hg@63: franta-hg@63: }