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