1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java Thu Sep 04 00:39:11 2014 +0200
1.3 @@ -0,0 +1,65 @@
1.4 +/**
1.5 + * Alt2XML
1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +package cz.frantovo.alt2xml.out;
1.22 +
1.23 +import javax.xml.parsers.SAXParser;
1.24 +import javax.xml.transform.Transformer;
1.25 +import javax.xml.transform.TransformerException;
1.26 +import javax.xml.transform.TransformerFactory;
1.27 +import javax.xml.transform.TransformerFactoryConfigurationError;
1.28 +import javax.xml.transform.dom.DOMResult;
1.29 +import javax.xml.transform.sax.SAXSource;
1.30 +import org.xml.sax.InputSource;
1.31 +import org.xml.sax.SAXException;
1.32 +
1.33 +/**
1.34 + * Recommended base class for Actions based on DOM processing.
1.35 + * This actiond does not support streaming – whole document must be parsed/converted to DOM and
1.36 + * after that the action is executed.
1.37 + *
1.38 + * @author Ing. František Kučera (frantovo.cz)
1.39 + */
1.40 +public abstract class AbstractDOMAction extends AbstractAction {
1.41 +
1.42 + public AbstractDOMAction(ActionContext actionContext) {
1.43 + super(actionContext);
1.44 + }
1.45 +
1.46 + @Override
1.47 + public void run(SAXParser parser, InputSource source) throws OutputActionException {
1.48 +
1.49 + try {
1.50 + TransformerFactory tf = TransformerFactory.newInstance();
1.51 + Transformer t = tf.newTransformer();
1.52 +
1.53 + DOMResult domResult = new DOMResult();
1.54 +
1.55 + t.transform(new SAXSource(parser.getXMLReader(), source), domResult);
1.56 +
1.57 + run(domResult);
1.58 +
1.59 + } catch (SAXException e) {
1.60 + throw new OutputActionException("Unable to get SAX reader during SAX→DOM conversion.", e);
1.61 + } catch (TransformerException | TransformerFactoryConfigurationError e) {
1.62 + throw new OutputActionException("Unable to convert SAX events to DOM using XSLT.", e);
1.63 + }
1.64 + }
1.65 +
1.66 + protected abstract void run(DOMResult domResult) throws OutputActionException;
1.67 +
1.68 +}