# HG changeset patch
# User František Kučera <franta-hg@frantovo.cz>
# Date 1409783951 -7200
# Node ID 89acb486bb5241afa1b23e9c0b8c8efd8d2187af
# Parent  968de9cc4c5b713c8a97c49716ac8376c7268bd9
out-xpath: basics + AbstractDOMAction

diff -r 968de9cc4c5b -r 89acb486bb52 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java
--- a/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java	Wed Sep 03 19:53:14 2014 +0200
+++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractAction.java	Thu Sep 04 00:39:11 2014 +0200
@@ -20,6 +20,9 @@
 /**
  * Recommended base class for Actions.
  *
+ * @see AbstractHandlerAction
+ * @see AbstractDOMAction
+ *
  * @author Ing. František Kučera (frantovo.cz)
  */
 public abstract class AbstractAction implements Action {
diff -r 968de9cc4c5b -r 89acb486bb52 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/AbstractDOMAction.java	Thu Sep 04 00:39:11 2014 +0200
@@ -0,0 +1,65 @@
+/**
+ * Alt2XML
+ * Copyright © 2014 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package cz.frantovo.alt2xml.out;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.sax.SAXSource;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Recommended base class for Actions based on DOM processing.
+ * This actiond does not support streaming – whole document must be parsed/converted to DOM and
+ * after that the action is executed.
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public abstract class AbstractDOMAction extends AbstractAction {
+
+	public AbstractDOMAction(ActionContext actionContext) {
+		super(actionContext);
+	}
+
+	@Override
+	public void run(SAXParser parser, InputSource source) throws OutputActionException {
+
+		try {
+			TransformerFactory tf = TransformerFactory.newInstance();
+			Transformer t = tf.newTransformer();
+
+			DOMResult domResult = new DOMResult();
+
+			t.transform(new SAXSource(parser.getXMLReader(), source), domResult);
+
+			run(domResult);
+
+		} catch (SAXException e) {
+			throw new OutputActionException("Unable to get SAX reader during SAX→DOM conversion.", e);
+		} catch (TransformerException | TransformerFactoryConfigurationError e) {
+			throw new OutputActionException("Unable to convert SAX events to DOM using XSLT.", e);
+		}
+	}
+
+	protected abstract void run(DOMResult domResult) throws OutputActionException;
+
+}
diff -r 968de9cc4c5b -r 89acb486bb52 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java
--- a/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java	Wed Sep 03 19:53:14 2014 +0200
+++ b/java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/Action.java	Thu Sep 04 00:39:11 2014 +0200
@@ -23,6 +23,8 @@
 /**
  * Executive class of an output module.
  *
+ * @see AbstractAction
+ *
  * @author Ing. František Kučera (frantovo.cz)
  */
 public interface Action {
diff -r 968de9cc4c5b -r 89acb486bb52 java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java
--- a/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Wed Sep 03 19:53:14 2014 +0200
+++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Thu Sep 04 00:39:11 2014 +0200
@@ -17,25 +17,49 @@
  */
 package cz.frantovo.alt2xml.out.xpath;
 
-import cz.frantovo.alt2xml.out.AbstractAction;
+import cz.frantovo.alt2xml.out.AbstractDOMAction;
 import cz.frantovo.alt2xml.out.ActionContext;
 import cz.frantovo.alt2xml.out.OutputActionException;
-import javax.xml.parsers.SAXParser;
-import org.xml.sax.InputSource;
+import java.io.PrintWriter;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import org.w3c.dom.Node;
 
 /**
  *
  * @author Ing. František Kučera (frantovo.cz)
  */
-public class XPathAction extends AbstractAction {
+public class XPathAction extends AbstractDOMAction {
 
 	public XPathAction(ActionContext actionContext) {
 		super(actionContext);
 	}
 
 	@Override
-	public void run(SAXParser parser, InputSource source) throws OutputActionException {
-		// TODO: XPath
-		throw new UnsupportedOperationException("Not supported yet.");
+	public void run(DOMResult domResult) throws OutputActionException {
+
+		// TODO: reuse
+		XPathFactory xpf = XPathFactory.newInstance();
+		XPath xp = xpf.newXPath();
+
+		// String expressionString = getActionContext().
+		String expressionString = "1+1";
+		expressionString = "//property[@name='ccc']";
+
+		try {
+			XPathExpression xpe = xp.compile(expressionString);
+
+			String result = xpe.evaluate(domResult.getNode());
+
+			try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
+				out.println(result);
+			}
+
+		} catch (XPathExpressionException e) {
+			throw new OutputActionException("Unable to compile or evaluate XPath: " + expressionString, e);
+		}
 	}
 }