1.1 --- a/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java Thu Sep 04 16:28:49 2014 +0200
1.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java Thu Sep 04 16:56:47 2014 +0200
1.3 @@ -35,33 +35,39 @@
1.4
1.5 private final XPathFactory xpathFactory;
1.6 private final XPath xpath;
1.7 + private final String expressionString;
1.8 + private final XPathExpression xpathExpression;
1.9
1.10 - public XPathAction(ActionContext actionContext) {
1.11 + public XPathAction(ActionContext actionContext) throws OutputActionException {
1.12 super(actionContext);
1.13 xpathFactory = XPathFactory.newInstance();
1.14 xpath = xpathFactory.newXPath();
1.15 - }
1.16
1.17 - @Override
1.18 - public void run(DOMResult domResult) throws OutputActionException {
1.19 + if (!actionContext.getActionProperties().isEmpty()) {
1.20 + xpath.setXPathVariableResolver(new PropertiesVariableResolver(actionContext.getActionProperties()));
1.21 + }
1.22
1.23 if (getActionContext().getActionData().size() == 1) {
1.24 - String expressionString = getActionContext().getActionData().get(0);
1.25 -
1.26 + expressionString = getActionContext().getActionData().get(0);
1.27 try {
1.28 - XPathExpression xpe = xpath.compile(expressionString);
1.29 -
1.30 - String result = xpe.evaluate(domResult.getNode());
1.31 -
1.32 - try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
1.33 - out.println(result);
1.34 - }
1.35 -
1.36 + xpathExpression = xpath.compile(expressionString);
1.37 } catch (XPathExpressionException e) {
1.38 - throw new OutputActionException("Unable to compile or evaluate XPath: " + expressionString, e);
1.39 + throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
1.40 }
1.41 } else {
1.42 throw new OutputActionException("Please specify the XPath expression as action data");
1.43 }
1.44 }
1.45 +
1.46 + @Override
1.47 + public void run(DOMResult domResult) throws OutputActionException {
1.48 + try {
1.49 + String result = xpathExpression.evaluate(domResult.getNode());
1.50 + try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
1.51 + out.println(result);
1.52 + }
1.53 + } catch (XPathExpressionException e) {
1.54 + throw new OutputActionException("Unable to evaluate XPath: " + xpathExpression, e);
1.55 + }
1.56 + }
1.57 }