out-xpath: basics + AbstractDOMAction
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 04 Sep 2014 00:39:11 +0200
changeset 6389acb486bb52
parent 62 968de9cc4c5b
child 64 e2c691eedf4d
out-xpath: basics + AbstractDOMAction
java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java
java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java
java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java
java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java
     1.1 --- a/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java	Wed Sep 03 19:53:14 2014 +0200
     1.2 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java	Thu Sep 04 00:39:11 2014 +0200
     1.3 @@ -20,6 +20,9 @@
     1.4  /**
     1.5   * Recommended base class for Actions.
     1.6   *
     1.7 + * @see AbstractHandlerAction
     1.8 + * @see AbstractDOMAction
     1.9 + *
    1.10   * @author Ing. František Kučera (frantovo.cz)
    1.11   */
    1.12  public abstract class AbstractAction implements Action {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java	Thu Sep 04 00:39:11 2014 +0200
     2.3 @@ -0,0 +1,65 @@
     2.4 +/**
     2.5 + * Alt2XML
     2.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package cz.frantovo.alt2xml.out;
    2.22 +
    2.23 +import javax.xml.parsers.SAXParser;
    2.24 +import javax.xml.transform.Transformer;
    2.25 +import javax.xml.transform.TransformerException;
    2.26 +import javax.xml.transform.TransformerFactory;
    2.27 +import javax.xml.transform.TransformerFactoryConfigurationError;
    2.28 +import javax.xml.transform.dom.DOMResult;
    2.29 +import javax.xml.transform.sax.SAXSource;
    2.30 +import org.xml.sax.InputSource;
    2.31 +import org.xml.sax.SAXException;
    2.32 +
    2.33 +/**
    2.34 + * Recommended base class for Actions based on DOM processing.
    2.35 + * This actiond does not support streaming – whole document must be parsed/converted to DOM and
    2.36 + * after that the action is executed.
    2.37 + *
    2.38 + * @author Ing. František Kučera (frantovo.cz)
    2.39 + */
    2.40 +public abstract class AbstractDOMAction extends AbstractAction {
    2.41 +
    2.42 +	public AbstractDOMAction(ActionContext actionContext) {
    2.43 +		super(actionContext);
    2.44 +	}
    2.45 +
    2.46 +	@Override
    2.47 +	public void run(SAXParser parser, InputSource source) throws OutputActionException {
    2.48 +
    2.49 +		try {
    2.50 +			TransformerFactory tf = TransformerFactory.newInstance();
    2.51 +			Transformer t = tf.newTransformer();
    2.52 +
    2.53 +			DOMResult domResult = new DOMResult();
    2.54 +
    2.55 +			t.transform(new SAXSource(parser.getXMLReader(), source), domResult);
    2.56 +
    2.57 +			run(domResult);
    2.58 +
    2.59 +		} catch (SAXException e) {
    2.60 +			throw new OutputActionException("Unable to get SAX reader during SAX→DOM conversion.", e);
    2.61 +		} catch (TransformerException | TransformerFactoryConfigurationError e) {
    2.62 +			throw new OutputActionException("Unable to convert SAX events to DOM using XSLT.", e);
    2.63 +		}
    2.64 +	}
    2.65 +
    2.66 +	protected abstract void run(DOMResult domResult) throws OutputActionException;
    2.67 +
    2.68 +}
     3.1 --- a/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java	Wed Sep 03 19:53:14 2014 +0200
     3.2 +++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java	Thu Sep 04 00:39:11 2014 +0200
     3.3 @@ -23,6 +23,8 @@
     3.4  /**
     3.5   * Executive class of an output module.
     3.6   *
     3.7 + * @see AbstractAction
     3.8 + *
     3.9   * @author Ing. František Kučera (frantovo.cz)
    3.10   */
    3.11  public interface Action {
     4.1 --- a/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Wed Sep 03 19:53:14 2014 +0200
     4.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Thu Sep 04 00:39:11 2014 +0200
     4.3 @@ -17,25 +17,49 @@
     4.4   */
     4.5  package cz.frantovo.alt2xml.out.xpath;
     4.6  
     4.7 -import cz.frantovo.alt2xml.out.AbstractAction;
     4.8 +import cz.frantovo.alt2xml.out.AbstractDOMAction;
     4.9  import cz.frantovo.alt2xml.out.ActionContext;
    4.10  import cz.frantovo.alt2xml.out.OutputActionException;
    4.11 -import javax.xml.parsers.SAXParser;
    4.12 -import org.xml.sax.InputSource;
    4.13 +import java.io.PrintWriter;
    4.14 +import javax.xml.transform.dom.DOMResult;
    4.15 +import javax.xml.xpath.XPath;
    4.16 +import javax.xml.xpath.XPathExpression;
    4.17 +import javax.xml.xpath.XPathExpressionException;
    4.18 +import javax.xml.xpath.XPathFactory;
    4.19 +import org.w3c.dom.Node;
    4.20  
    4.21  /**
    4.22   *
    4.23   * @author Ing. František Kučera (frantovo.cz)
    4.24   */
    4.25 -public class XPathAction extends AbstractAction {
    4.26 +public class XPathAction extends AbstractDOMAction {
    4.27  
    4.28  	public XPathAction(ActionContext actionContext) {
    4.29  		super(actionContext);
    4.30  	}
    4.31  
    4.32  	@Override
    4.33 -	public void run(SAXParser parser, InputSource source) throws OutputActionException {
    4.34 -		// TODO: XPath
    4.35 -		throw new UnsupportedOperationException("Not supported yet.");
    4.36 +	public void run(DOMResult domResult) throws OutputActionException {
    4.37 +
    4.38 +		// TODO: reuse
    4.39 +		XPathFactory xpf = XPathFactory.newInstance();
    4.40 +		XPath xp = xpf.newXPath();
    4.41 +
    4.42 +		// String expressionString = getActionContext().
    4.43 +		String expressionString = "1+1";
    4.44 +		expressionString = "//property[@name='ccc']";
    4.45 +
    4.46 +		try {
    4.47 +			XPathExpression xpe = xp.compile(expressionString);
    4.48 +
    4.49 +			String result = xpe.evaluate(domResult.getNode());
    4.50 +
    4.51 +			try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
    4.52 +				out.println(result);
    4.53 +			}
    4.54 +
    4.55 +		} catch (XPathExpressionException e) {
    4.56 +			throw new OutputActionException("Unable to compile or evaluate XPath: " + expressionString, e);
    4.57 +		}
    4.58  	}
    4.59  }