diff -r 968de9cc4c5b -r 89acb486bb52 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java Thu Sep 04 00:39:11 2014 +0200 @@ -0,0 +1,65 @@ +/** + * Alt2XML + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.alt2xml.out; + +import javax.xml.parsers.SAXParser; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXSource; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Recommended base class for Actions based on DOM processing. + * This actiond does not support streaming – whole document must be parsed/converted to DOM and + * after that the action is executed. + * + * @author Ing. František Kučera (frantovo.cz) + */ +public abstract class AbstractDOMAction extends AbstractAction { + + public AbstractDOMAction(ActionContext actionContext) { + super(actionContext); + } + + @Override + public void run(SAXParser parser, InputSource source) throws OutputActionException { + + try { + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer t = tf.newTransformer(); + + DOMResult domResult = new DOMResult(); + + t.transform(new SAXSource(parser.getXMLReader(), source), domResult); + + run(domResult); + + } catch (SAXException e) { + throw new OutputActionException("Unable to get SAX reader during SAX→DOM conversion.", e); + } catch (TransformerException | TransformerFactoryConfigurationError e) { + throw new OutputActionException("Unable to convert SAX events to DOM using XSLT.", e); + } + } + + protected abstract void run(DOMResult domResult) throws OutputActionException; + +}