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@69
|
24 |
import java.util.HashMap;
|
franta-hg@69
|
25 |
import java.util.List;
|
franta-hg@69
|
26 |
import java.util.Map;
|
franta-hg@63
|
27 |
import javax.xml.transform.dom.DOMResult;
|
franta-hg@63
|
28 |
import javax.xml.xpath.XPath;
|
franta-hg@63
|
29 |
import javax.xml.xpath.XPathExpression;
|
franta-hg@63
|
30 |
import javax.xml.xpath.XPathExpressionException;
|
franta-hg@63
|
31 |
import javax.xml.xpath.XPathFactory;
|
franta-hg@43
|
32 |
|
franta-hg@43
|
33 |
/**
|
franta-hg@43
|
34 |
*
|
franta-hg@43
|
35 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@43
|
36 |
*/
|
franta-hg@63
|
37 |
public class XPathAction extends AbstractDOMAction {
|
franta-hg@43
|
38 |
|
franta-hg@69
|
39 |
public static final String PARAMETER_TYPED_PARAMETERS = "typed-parameters";
|
franta-hg@66
|
40 |
private final XPathFactory xpathFactory;
|
franta-hg@66
|
41 |
private final XPath xpath;
|
franta-hg@68
|
42 |
private final String expressionString;
|
franta-hg@68
|
43 |
private final XPathExpression xpathExpression;
|
franta-hg@66
|
44 |
|
franta-hg@68
|
45 |
public XPathAction(ActionContext actionContext) throws OutputActionException {
|
franta-hg@43
|
46 |
super(actionContext);
|
franta-hg@66
|
47 |
xpathFactory = XPathFactory.newInstance();
|
franta-hg@66
|
48 |
xpath = xpathFactory.newXPath();
|
franta-hg@43
|
49 |
|
franta-hg@69
|
50 |
final List<String> actionData = getActionContext().getActionData();
|
franta-hg@63
|
51 |
|
franta-hg@69
|
52 |
if (actionData.size() < 1) {
|
franta-hg@69
|
53 |
throw new OutputActionException("Please specify the XPath expression as action data");
|
franta-hg@69
|
54 |
} else {
|
franta-hg@69
|
55 |
|
franta-hg@69
|
56 |
boolean typedParameters = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_TYPED_PARAMETERS));
|
franta-hg@69
|
57 |
|
franta-hg@69
|
58 |
Map<String, Object> xpathParameters = new HashMap<>();
|
franta-hg@69
|
59 |
|
franta-hg@69
|
60 |
for (int i = 1; i < actionData.size(); i++) {
|
franta-hg@69
|
61 |
String parameterName = actionData.get(i++);
|
franta-hg@69
|
62 |
String parameterType = typedParameters ? actionData.get(i++) : "string";
|
franta-hg@71
|
63 |
Object parameterValue = parseParameterValue(parameterType, actionData.get(i));
|
franta-hg@69
|
64 |
xpathParameters.put(parameterName, parameterValue);
|
franta-hg@69
|
65 |
}
|
franta-hg@69
|
66 |
|
franta-hg@69
|
67 |
xpath.setXPathVariableResolver(new PropertiesVariableResolver(xpathParameters));
|
franta-hg@69
|
68 |
|
franta-hg@69
|
69 |
expressionString = actionData.get(0);
|
franta-hg@65
|
70 |
try {
|
franta-hg@68
|
71 |
xpathExpression = xpath.compile(expressionString);
|
franta-hg@65
|
72 |
} catch (XPathExpressionException e) {
|
franta-hg@68
|
73 |
throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
|
franta-hg@63
|
74 |
}
|
franta-hg@69
|
75 |
|
franta-hg@63
|
76 |
}
|
franta-hg@43
|
77 |
}
|
franta-hg@68
|
78 |
|
franta-hg@69
|
79 |
private static Object parseParameterValue(String type, String value) {
|
franta-hg@69
|
80 |
// TODO: support more types
|
franta-hg@69
|
81 |
return value;
|
franta-hg@69
|
82 |
}
|
franta-hg@69
|
83 |
|
franta-hg@68
|
84 |
@Override
|
franta-hg@68
|
85 |
public void run(DOMResult domResult) throws OutputActionException {
|
franta-hg@68
|
86 |
try {
|
franta-hg@68
|
87 |
String result = xpathExpression.evaluate(domResult.getNode());
|
franta-hg@68
|
88 |
try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
|
franta-hg@68
|
89 |
out.println(result);
|
franta-hg@68
|
90 |
}
|
franta-hg@68
|
91 |
} catch (XPathExpressionException e) {
|
franta-hg@68
|
92 |
throw new OutputActionException("Unable to evaluate XPath: " + xpathExpression, e);
|
franta-hg@68
|
93 |
}
|
franta-hg@68
|
94 |
}
|
franta-hg@43
|
95 |
}
|