diff -r 96ce967c0182 -r b814f45b6beb 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 Thu Sep 04 16:28:49 2014 +0200 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java Thu Sep 04 16:56:47 2014 +0200 @@ -35,33 +35,39 @@ private final XPathFactory xpathFactory; private final XPath xpath; + private final String expressionString; + private final XPathExpression xpathExpression; - public XPathAction(ActionContext actionContext) { + public XPathAction(ActionContext actionContext) throws OutputActionException { super(actionContext); xpathFactory = XPathFactory.newInstance(); xpath = xpathFactory.newXPath(); - } - @Override - public void run(DOMResult domResult) throws OutputActionException { + if (!actionContext.getActionProperties().isEmpty()) { + xpath.setXPathVariableResolver(new PropertiesVariableResolver(actionContext.getActionProperties())); + } if (getActionContext().getActionData().size() == 1) { - String expressionString = getActionContext().getActionData().get(0); - + expressionString = getActionContext().getActionData().get(0); try { - XPathExpression xpe = xpath.compile(expressionString); - - String result = xpe.evaluate(domResult.getNode()); - - try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) { - out.println(result); - } - + xpathExpression = xpath.compile(expressionString); } catch (XPathExpressionException e) { - throw new OutputActionException("Unable to compile or evaluate XPath: " + expressionString, e); + throw new OutputActionException("Unable to compile XPath: " + expressionString, e); } } else { throw new OutputActionException("Please specify the XPath expression as action data"); } } + + @Override + public void run(DOMResult domResult) throws OutputActionException { + try { + String result = xpathExpression.evaluate(domResult.getNode()); + try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) { + out.println(result); + } + } catch (XPathExpressionException e) { + throw new OutputActionException("Unable to evaluate XPath: " + xpathExpression, e); + } + } }