java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java
changeset 69 f9c73ee49287
parent 68 b814f45b6beb
child 71 75556166817a
     1.1 --- a/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Thu Sep 04 16:56:47 2014 +0200
     1.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Thu Sep 04 17:56:43 2014 +0200
     1.3 @@ -21,6 +21,9 @@
     1.4  import cz.frantovo.alt2xml.out.ActionContext;
     1.5  import cz.frantovo.alt2xml.out.OutputActionException;
     1.6  import java.io.PrintWriter;
     1.7 +import java.util.HashMap;
     1.8 +import java.util.List;
     1.9 +import java.util.Map;
    1.10  import javax.xml.transform.dom.DOMResult;
    1.11  import javax.xml.xpath.XPath;
    1.12  import javax.xml.xpath.XPathExpression;
    1.13 @@ -33,6 +36,7 @@
    1.14   */
    1.15  public class XPathAction extends AbstractDOMAction {
    1.16  
    1.17 +	public static final String PARAMETER_TYPED_PARAMETERS = "typed-parameters";
    1.18  	private final XPathFactory xpathFactory;
    1.19  	private final XPath xpath;
    1.20  	private final String expressionString;
    1.21 @@ -43,22 +47,40 @@
    1.22  		xpathFactory = XPathFactory.newInstance();
    1.23  		xpath = xpathFactory.newXPath();
    1.24  
    1.25 -		if (!actionContext.getActionProperties().isEmpty()) {
    1.26 -			xpath.setXPathVariableResolver(new PropertiesVariableResolver(actionContext.getActionProperties()));
    1.27 -		}
    1.28 +		final List<String> actionData = getActionContext().getActionData();
    1.29  
    1.30 -		if (getActionContext().getActionData().size() == 1) {
    1.31 -			expressionString = getActionContext().getActionData().get(0);
    1.32 +		if (actionData.size() < 1) {
    1.33 +			throw new OutputActionException("Please specify the XPath expression as action data");
    1.34 +		} else {
    1.35 +
    1.36 +			boolean typedParameters = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_TYPED_PARAMETERS));
    1.37 +
    1.38 +			Map<String, Object> xpathParameters = new HashMap<>();
    1.39 +
    1.40 +			for (int i = 1; i < actionData.size(); i++) {
    1.41 +				String parameterName = actionData.get(i++);
    1.42 +				String parameterType = typedParameters ? actionData.get(i++) : "string";
    1.43 +				Object parameterValue = parseParameterValue(parameterType, actionData.get(i++));
    1.44 +				xpathParameters.put(parameterName, parameterValue);
    1.45 +			}
    1.46 +
    1.47 +			xpath.setXPathVariableResolver(new PropertiesVariableResolver(xpathParameters));
    1.48 +
    1.49 +			expressionString = actionData.get(0);
    1.50  			try {
    1.51  				xpathExpression = xpath.compile(expressionString);
    1.52  			} catch (XPathExpressionException e) {
    1.53  				throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
    1.54  			}
    1.55 -		} else {
    1.56 -			throw new OutputActionException("Please specify the XPath expression as action data");
    1.57 +
    1.58  		}
    1.59  	}
    1.60  
    1.61 +	private static Object parseParameterValue(String type, String value) {
    1.62 +		// TODO: support more types
    1.63 +		return value;
    1.64 +	}
    1.65 +
    1.66  	@Override
    1.67  	public void run(DOMResult domResult) throws OutputActionException {
    1.68  		try {