author | František Kučera <franta-hg@frantovo.cz> |
Thu, 04 Sep 2014 16:14:15 +0200 | |
changeset 65 | 5c4ae2ed5d97 |
parent 64 | e2c691eedf4d |
child 66 | 9837e58e0754 |
permissions | -rw-r--r-- |
franta-hg@43 | 1 |
/** |
franta-hg@43 | 2 |
* Alt2XML |
franta-hg@43 | 3 |
* Copyright © 2014 František Kučera (frantovo.cz) |
franta-hg@43 | 4 |
* |
franta-hg@43 | 5 |
* This program is free software: you can redistribute it and/or modify |
franta-hg@43 | 6 |
* it under the terms of the GNU General Public License as published by |
franta-hg@43 | 7 |
* the Free Software Foundation, either version 3 of the License, or |
franta-hg@43 | 8 |
* (at your option) any later version. |
franta-hg@43 | 9 |
* |
franta-hg@43 | 10 |
* This program is distributed in the hope that it will be useful, |
franta-hg@43 | 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
franta-hg@43 | 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
franta-hg@43 | 13 |
* GNU General Public License for more details. |
franta-hg@43 | 14 |
* |
franta-hg@43 | 15 |
* You should have received a copy of the GNU General Public License |
franta-hg@43 | 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
franta-hg@43 | 17 |
*/ |
franta-hg@49 | 18 |
package cz.frantovo.alt2xml.out.xpath; |
franta-hg@43 | 19 |
|
franta-hg@63 | 20 |
import cz.frantovo.alt2xml.out.AbstractDOMAction; |
franta-hg@43 | 21 |
import cz.frantovo.alt2xml.out.ActionContext; |
franta-hg@49 | 22 |
import cz.frantovo.alt2xml.out.OutputActionException; |
franta-hg@63 | 23 |
import java.io.PrintWriter; |
franta-hg@63 | 24 |
import javax.xml.transform.dom.DOMResult; |
franta-hg@63 | 25 |
import javax.xml.xpath.XPath; |
franta-hg@63 | 26 |
import javax.xml.xpath.XPathExpression; |
franta-hg@63 | 27 |
import javax.xml.xpath.XPathExpressionException; |
franta-hg@63 | 28 |
import javax.xml.xpath.XPathFactory; |
franta-hg@43 | 29 |
|
franta-hg@43 | 30 |
/** |
franta-hg@43 | 31 |
* |
franta-hg@43 | 32 |
* @author Ing. František Kučera (frantovo.cz) |
franta-hg@43 | 33 |
*/ |
franta-hg@63 | 34 |
public class XPathAction extends AbstractDOMAction { |
franta-hg@43 | 35 |
|
franta-hg@49 | 36 |
public XPathAction(ActionContext actionContext) { |
franta-hg@43 | 37 |
super(actionContext); |
franta-hg@43 | 38 |
} |
franta-hg@43 | 39 |
|
franta-hg@43 | 40 |
@Override |
franta-hg@63 | 41 |
public void run(DOMResult domResult) throws OutputActionException { |
franta-hg@63 | 42 |
|
franta-hg@63 | 43 |
// TODO: reuse |
franta-hg@63 | 44 |
XPathFactory xpf = XPathFactory.newInstance(); |
franta-hg@63 | 45 |
XPath xp = xpf.newXPath(); |
franta-hg@63 | 46 |
|
franta-hg@65 | 47 |
if (getActionContext().getActionData().size() == 1) { |
franta-hg@65 | 48 |
String expressionString = getActionContext().getActionData().get(0); |
franta-hg@63 | 49 |
|
franta-hg@65 | 50 |
try { |
franta-hg@65 | 51 |
XPathExpression xpe = xp.compile(expressionString); |
franta-hg@63 | 52 |
|
franta-hg@65 | 53 |
String result = xpe.evaluate(domResult.getNode()); |
franta-hg@63 | 54 |
|
franta-hg@65 | 55 |
try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) { |
franta-hg@65 | 56 |
out.println(result); |
franta-hg@65 | 57 |
} |
franta-hg@65 | 58 |
|
franta-hg@65 | 59 |
} catch (XPathExpressionException e) { |
franta-hg@65 | 60 |
throw new OutputActionException("Unable to compile or evaluate XPath: " + expressionString, e); |
franta-hg@63 | 61 |
} |
franta-hg@65 | 62 |
} else { |
franta-hg@65 | 63 |
throw new OutputActionException("Please specify the XPath expression as action data"); |
franta-hg@63 | 64 |
} |
franta-hg@43 | 65 |
} |
franta-hg@43 | 66 |
} |