java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 63 89acb486bb52
permissions -rw-r--r--
fix license version: GNU GPLv3
     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, version 3 of the License.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 package cz.frantovo.alt2xml.out;
    18 
    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;
    28 
    29 /**
    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.
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public abstract class AbstractDOMAction extends AbstractAction {
    37 
    38 	public AbstractDOMAction(ActionContext actionContext) {
    39 		super(actionContext);
    40 	}
    41 
    42 	@Override
    43 	public void run(SAXParser parser, InputSource source) throws OutputActionException {
    44 
    45 		try {
    46 			TransformerFactory tf = TransformerFactory.newInstance();
    47 			Transformer t = tf.newTransformer();
    48 
    49 			DOMResult domResult = new DOMResult();
    50 
    51 			t.transform(new SAXSource(parser.getXMLReader(), source), domResult);
    52 
    53 			run(domResult);
    54 
    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);
    59 		}
    60 	}
    61 
    62 	protected abstract void run(DOMResult domResult) throws OutputActionException;
    63 
    64 }