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@66: private final XPathFactory xpathFactory; franta-hg@66: private final XPath xpath; franta-hg@68: private final String expressionString; franta-hg@68: private final XPathExpression xpathExpression; franta-hg@66: franta-hg@68: public XPathAction(ActionContext actionContext) throws OutputActionException { franta-hg@43: super(actionContext); franta-hg@66: xpathFactory = XPathFactory.newInstance(); franta-hg@66: xpath = xpathFactory.newXPath(); franta-hg@43: franta-hg@68: if (!actionContext.getActionProperties().isEmpty()) { franta-hg@68: xpath.setXPathVariableResolver(new PropertiesVariableResolver(actionContext.getActionProperties())); franta-hg@68: } franta-hg@63: franta-hg@65: if (getActionContext().getActionData().size() == 1) { franta-hg@68: expressionString = getActionContext().getActionData().get(0); franta-hg@65: try { franta-hg@68: xpathExpression = xpath.compile(expressionString); franta-hg@65: } catch (XPathExpressionException e) { franta-hg@68: throw new OutputActionException("Unable to compile XPath: " + expressionString, e); franta-hg@63: } franta-hg@65: } else { franta-hg@65: throw new OutputActionException("Please specify the XPath expression as action data"); franta-hg@63: } franta-hg@43: } franta-hg@68: franta-hg@68: @Override franta-hg@68: public void run(DOMResult domResult) throws OutputActionException { franta-hg@68: try { franta-hg@68: String result = xpathExpression.evaluate(domResult.getNode()); franta-hg@68: try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) { franta-hg@68: out.println(result); franta-hg@68: } franta-hg@68: } catch (XPathExpressionException e) { franta-hg@68: throw new OutputActionException("Unable to evaluate XPath: " + xpathExpression, e); franta-hg@68: } franta-hg@68: } franta-hg@43: }