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 javax.xml.parsers.SAXParser;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.TransformerException;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.TransformerFactoryConfigurationError;
25 import javax.xml.transform.dom.DOMResult;
26 import javax.xml.transform.sax.SAXSource;
27 import org.xml.sax.InputSource;
28 import org.xml.sax.SAXException;
31 * Recommended base class for Actions based on DOM processing.
32 * This actiond does not support streaming – whole document must be parsed/converted to DOM and
33 * after that the action is executed.
35 * @author Ing. František Kučera (frantovo.cz)
37 public abstract class AbstractDOMAction extends AbstractAction {
39 public AbstractDOMAction(ActionContext actionContext) {
44 public void run(SAXParser parser, InputSource source) throws OutputActionException {
47 TransformerFactory tf = TransformerFactory.newInstance();
48 Transformer t = tf.newTransformer();
50 DOMResult domResult = new DOMResult();
52 t.transform(new SAXSource(parser.getXMLReader(), source), domResult);
56 } catch (SAXException e) {
57 throw new OutputActionException("Unable to get SAX reader during SAX→DOM conversion.", e);
58 } catch (TransformerException | TransformerFactoryConfigurationError e) {
59 throw new OutputActionException("Unable to convert SAX events to DOM using XSLT.", e);
63 protected abstract void run(DOMResult domResult) throws OutputActionException;