java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 02 Oct 2016 11:46:19 +0200
changeset 107 c5a987931ef9
parent 63 89acb486bb52
child 111 e4900596abdb
permissions -rw-r--r--
in-properties: fix typo in comment
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package cz.frantovo.alt2xml.out;
    19 
    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;
    29 
    30 /**
    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.
    34  *
    35  * @author Ing. František Kučera (frantovo.cz)
    36  */
    37 public abstract class AbstractDOMAction extends AbstractAction {
    38 
    39 	public AbstractDOMAction(ActionContext actionContext) {
    40 		super(actionContext);
    41 	}
    42 
    43 	@Override
    44 	public void run(SAXParser parser, InputSource source) throws OutputActionException {
    45 
    46 		try {
    47 			TransformerFactory tf = TransformerFactory.newInstance();
    48 			Transformer t = tf.newTransformer();
    49 
    50 			DOMResult domResult = new DOMResult();
    51 
    52 			t.transform(new SAXSource(parser.getXMLReader(), source), domResult);
    53 
    54 			run(domResult);
    55 
    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);
    60 		}
    61 	}
    62 
    63 	protected abstract void run(DOMResult domResult) throws OutputActionException;
    64 
    65 }