java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 15 Nov 2020 20:20:39 +0100
changeset 116 94081a55bf41
parent 111 e4900596abdb
permissions -rw-r--r--
Added tag v0.2 for changeset 96e1125c8500
franta-hg@63
     1
/**
franta-hg@63
     2
 * Alt2XML
franta-hg@63
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@63
     4
 *
franta-hg@63
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@63
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@111
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@63
     8
 *
franta-hg@63
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@63
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@63
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@63
    12
 * GNU General Public License for more details.
franta-hg@63
    13
 *
franta-hg@63
    14
 * You should have received a copy of the GNU General Public License
franta-hg@63
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@63
    16
 */
franta-hg@63
    17
package cz.frantovo.alt2xml.out;
franta-hg@63
    18
franta-hg@63
    19
import javax.xml.parsers.SAXParser;
franta-hg@63
    20
import javax.xml.transform.Transformer;
franta-hg@63
    21
import javax.xml.transform.TransformerException;
franta-hg@63
    22
import javax.xml.transform.TransformerFactory;
franta-hg@63
    23
import javax.xml.transform.TransformerFactoryConfigurationError;
franta-hg@63
    24
import javax.xml.transform.dom.DOMResult;
franta-hg@63
    25
import javax.xml.transform.sax.SAXSource;
franta-hg@63
    26
import org.xml.sax.InputSource;
franta-hg@63
    27
import org.xml.sax.SAXException;
franta-hg@63
    28
franta-hg@63
    29
/**
franta-hg@63
    30
 * Recommended base class for Actions based on DOM processing.
franta-hg@63
    31
 * This actiond does not support streaming – whole document must be parsed/converted to DOM and
franta-hg@63
    32
 * after that the action is executed.
franta-hg@63
    33
 *
franta-hg@63
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@63
    35
 */
franta-hg@63
    36
public abstract class AbstractDOMAction extends AbstractAction {
franta-hg@63
    37
franta-hg@63
    38
	public AbstractDOMAction(ActionContext actionContext) {
franta-hg@63
    39
		super(actionContext);
franta-hg@63
    40
	}
franta-hg@63
    41
franta-hg@63
    42
	@Override
franta-hg@63
    43
	public void run(SAXParser parser, InputSource source) throws OutputActionException {
franta-hg@63
    44
franta-hg@63
    45
		try {
franta-hg@63
    46
			TransformerFactory tf = TransformerFactory.newInstance();
franta-hg@63
    47
			Transformer t = tf.newTransformer();
franta-hg@63
    48
franta-hg@63
    49
			DOMResult domResult = new DOMResult();
franta-hg@63
    50
franta-hg@63
    51
			t.transform(new SAXSource(parser.getXMLReader(), source), domResult);
franta-hg@63
    52
franta-hg@63
    53
			run(domResult);
franta-hg@63
    54
franta-hg@63
    55
		} catch (SAXException e) {
franta-hg@63
    56
			throw new OutputActionException("Unable to get SAX reader during SAX→DOM conversion.", e);
franta-hg@63
    57
		} catch (TransformerException | TransformerFactoryConfigurationError e) {
franta-hg@63
    58
			throw new OutputActionException("Unable to convert SAX events to DOM using XSLT.", e);
franta-hg@63
    59
		}
franta-hg@63
    60
	}
franta-hg@63
    61
franta-hg@63
    62
	protected abstract void run(DOMResult domResult) throws OutputActionException;
franta-hg@63
    63
franta-hg@63
    64
}