franta-hg@43: /** franta-hg@43: * Alt2XML franta-hg@43: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@43: * franta-hg@43: * This program is free software: you can redistribute it and/or modify franta-hg@43: * it under the terms of the GNU General Public License as published by franta-hg@43: * the Free Software Foundation, either version 3 of the License, or franta-hg@43: * (at your option) any later version. franta-hg@43: * franta-hg@43: * This program is distributed in the hope that it will be useful, franta-hg@43: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@43: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@43: * GNU General Public License for more details. franta-hg@43: * franta-hg@43: * You should have received a copy of the GNU General Public License franta-hg@43: * along with this program. If not, see . franta-hg@43: */ franta-hg@49: package cz.frantovo.alt2xml.out.xpath; franta-hg@43: franta-hg@63: import cz.frantovo.alt2xml.out.AbstractDOMAction; franta-hg@43: import cz.frantovo.alt2xml.out.ActionContext; franta-hg@49: import cz.frantovo.alt2xml.out.OutputActionException; franta-hg@63: import java.io.PrintWriter; franta-hg@63: import javax.xml.transform.dom.DOMResult; franta-hg@63: import javax.xml.xpath.XPath; franta-hg@63: import javax.xml.xpath.XPathExpression; franta-hg@63: import javax.xml.xpath.XPathExpressionException; franta-hg@63: import javax.xml.xpath.XPathFactory; franta-hg@43: franta-hg@43: /** franta-hg@43: * franta-hg@43: * @author Ing. František Kučera (frantovo.cz) franta-hg@43: */ franta-hg@63: public class XPathAction extends AbstractDOMAction { franta-hg@43: franta-hg@49: public XPathAction(ActionContext actionContext) { franta-hg@43: super(actionContext); franta-hg@43: } franta-hg@43: franta-hg@43: @Override franta-hg@63: public void run(DOMResult domResult) throws OutputActionException { franta-hg@63: franta-hg@63: // TODO: reuse franta-hg@63: XPathFactory xpf = XPathFactory.newInstance(); franta-hg@63: XPath xp = xpf.newXPath(); franta-hg@63: franta-hg@63: // String expressionString = getActionContext(). franta-hg@63: String expressionString = "1+1"; franta-hg@63: expressionString = "//property[@name='ccc']"; franta-hg@63: franta-hg@63: try { franta-hg@63: XPathExpression xpe = xp.compile(expressionString); franta-hg@63: franta-hg@63: String result = xpe.evaluate(domResult.getNode()); franta-hg@63: franta-hg@63: try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) { franta-hg@63: out.println(result); franta-hg@63: } franta-hg@63: franta-hg@63: } catch (XPathExpressionException e) { franta-hg@63: throw new OutputActionException("Unable to compile or evaluate XPath: " + expressionString, e); franta-hg@63: } franta-hg@43: } franta-hg@43: }