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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package cz.frantovo.alt2xml.out;
19 import javax.xml.parsers.SAXParser;
20 import javax.xml.transform.Transformer;
21 import javax.xml.transform.TransformerException;
22 import javax.xml.transform.TransformerFactory;
23 import javax.xml.transform.TransformerFactoryConfigurationError;
24 import javax.xml.transform.dom.DOMResult;
25 import javax.xml.transform.sax.SAXSource;
26 import org.xml.sax.InputSource;
27 import org.xml.sax.SAXException;
30 * Recommended base class for Actions based on DOM processing.
31 * This actiond does not support streaming – whole document must be parsed/converted to DOM and
32 * after that the action is executed.
34 * @author Ing. František Kučera (frantovo.cz)
36 public abstract class AbstractDOMAction extends AbstractAction {
38 public AbstractDOMAction(ActionContext actionContext) {
43 public void run(SAXParser parser, InputSource source) throws OutputActionException {
46 TransformerFactory tf = TransformerFactory.newInstance();
47 Transformer t = tf.newTransformer();
49 DOMResult domResult = new DOMResult();
51 t.transform(new SAXSource(parser.getXMLReader(), source), domResult);
55 } catch (SAXException e) {
56 throw new OutputActionException("Unable to get SAX reader during SAX→DOM conversion.", e);
57 } catch (TransformerException | TransformerFactoryConfigurationError e) {
58 throw new OutputActionException("Unable to convert SAX events to DOM using XSLT.", e);
62 protected abstract void run(DOMResult domResult) throws OutputActionException;