java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 04 Sep 2014 18:16:33 +0200
changeset 71 75556166817a
parent 69 f9c73ee49287
child 72 4ba54a632eb8
permissions -rw-r--r--
out-xpath: fix – support more parameters
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.out.xpath;
    19 
    20 import cz.frantovo.alt2xml.out.AbstractDOMAction;
    21 import cz.frantovo.alt2xml.out.ActionContext;
    22 import cz.frantovo.alt2xml.out.OutputActionException;
    23 import java.io.PrintWriter;
    24 import java.util.HashMap;
    25 import java.util.List;
    26 import java.util.Map;
    27 import javax.xml.transform.dom.DOMResult;
    28 import javax.xml.xpath.XPath;
    29 import javax.xml.xpath.XPathExpression;
    30 import javax.xml.xpath.XPathExpressionException;
    31 import javax.xml.xpath.XPathFactory;
    32 
    33 /**
    34  *
    35  * @author Ing. František Kučera (frantovo.cz)
    36  */
    37 public class XPathAction extends AbstractDOMAction {
    38 
    39 	public static final String PARAMETER_TYPED_PARAMETERS = "typed-parameters";
    40 	private final XPathFactory xpathFactory;
    41 	private final XPath xpath;
    42 	private final String expressionString;
    43 	private final XPathExpression xpathExpression;
    44 
    45 	public XPathAction(ActionContext actionContext) throws OutputActionException {
    46 		super(actionContext);
    47 		xpathFactory = XPathFactory.newInstance();
    48 		xpath = xpathFactory.newXPath();
    49 
    50 		final List<String> actionData = getActionContext().getActionData();
    51 
    52 		if (actionData.size() < 1) {
    53 			throw new OutputActionException("Please specify the XPath expression as action data");
    54 		} else {
    55 
    56 			boolean typedParameters = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_TYPED_PARAMETERS));
    57 
    58 			Map<String, Object> xpathParameters = new HashMap<>();
    59 
    60 			for (int i = 1; i < actionData.size(); i++) {
    61 				String parameterName = actionData.get(i++);
    62 				String parameterType = typedParameters ? actionData.get(i++) : "string";
    63 				Object parameterValue = parseParameterValue(parameterType, actionData.get(i));
    64 				xpathParameters.put(parameterName, parameterValue);
    65 			}
    66 
    67 			xpath.setXPathVariableResolver(new PropertiesVariableResolver(xpathParameters));
    68 
    69 			expressionString = actionData.get(0);
    70 			try {
    71 				xpathExpression = xpath.compile(expressionString);
    72 			} catch (XPathExpressionException e) {
    73 				throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
    74 			}
    75 
    76 		}
    77 	}
    78 
    79 	private static Object parseParameterValue(String type, String value) {
    80 		// TODO: support more types
    81 		return value;
    82 	}
    83 
    84 	@Override
    85 	public void run(DOMResult domResult) throws OutputActionException {
    86 		try {
    87 			String result = xpathExpression.evaluate(domResult.getNode());
    88 			try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
    89 				out.println(result);
    90 			}
    91 		} catch (XPathExpressionException e) {
    92 			throw new OutputActionException("Unable to evaluate XPath: " + xpathExpression, e);
    93 		}
    94 	}
    95 }