out-xpath: XPath parameters are following „--“ and the expression; can be typed (which is specified by an action property)
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 04 Sep 2014 17:56:43 +0200
changeset 69f9c73ee49287
parent 68 b814f45b6beb
child 70 ead6a0d4e022
out-xpath: XPath parameters are following „--“ and the expression; can be typed (which is specified by an action property)
java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/PropertiesVariableResolver.java
java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java
     1.1 --- a/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/PropertiesVariableResolver.java	Thu Sep 04 16:56:47 2014 +0200
     1.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/PropertiesVariableResolver.java	Thu Sep 04 17:56:43 2014 +0200
     1.3 @@ -17,7 +17,7 @@
     1.4   */
     1.5  package cz.frantovo.alt2xml.out.xpath;
     1.6  
     1.7 -import java.util.Properties;
     1.8 +import java.util.Map;
     1.9  import javax.xml.namespace.QName;
    1.10  import javax.xml.xpath.XPathVariableResolver;
    1.11  
    1.12 @@ -27,15 +27,15 @@
    1.13   */
    1.14  public class PropertiesVariableResolver implements XPathVariableResolver {
    1.15  
    1.16 -	private Properties data;
    1.17 +	private Map<String,Object> data;
    1.18  
    1.19 -	public PropertiesVariableResolver(Properties data) {
    1.20 +	public PropertiesVariableResolver(Map<String,Object> data) {
    1.21  		this.data = data;
    1.22  	}
    1.23  
    1.24  	@Override
    1.25  	public Object resolveVariable(QName variableName) {
    1.26 -		return data.getProperty(variableName.getLocalPart());
    1.27 +		return data.get(variableName.getLocalPart());
    1.28  	}
    1.29  
    1.30  }
     2.1 --- a/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Thu Sep 04 16:56:47 2014 +0200
     2.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Thu Sep 04 17:56:43 2014 +0200
     2.3 @@ -21,6 +21,9 @@
     2.4  import cz.frantovo.alt2xml.out.ActionContext;
     2.5  import cz.frantovo.alt2xml.out.OutputActionException;
     2.6  import java.io.PrintWriter;
     2.7 +import java.util.HashMap;
     2.8 +import java.util.List;
     2.9 +import java.util.Map;
    2.10  import javax.xml.transform.dom.DOMResult;
    2.11  import javax.xml.xpath.XPath;
    2.12  import javax.xml.xpath.XPathExpression;
    2.13 @@ -33,6 +36,7 @@
    2.14   */
    2.15  public class XPathAction extends AbstractDOMAction {
    2.16  
    2.17 +	public static final String PARAMETER_TYPED_PARAMETERS = "typed-parameters";
    2.18  	private final XPathFactory xpathFactory;
    2.19  	private final XPath xpath;
    2.20  	private final String expressionString;
    2.21 @@ -43,22 +47,40 @@
    2.22  		xpathFactory = XPathFactory.newInstance();
    2.23  		xpath = xpathFactory.newXPath();
    2.24  
    2.25 -		if (!actionContext.getActionProperties().isEmpty()) {
    2.26 -			xpath.setXPathVariableResolver(new PropertiesVariableResolver(actionContext.getActionProperties()));
    2.27 -		}
    2.28 +		final List<String> actionData = getActionContext().getActionData();
    2.29  
    2.30 -		if (getActionContext().getActionData().size() == 1) {
    2.31 -			expressionString = getActionContext().getActionData().get(0);
    2.32 +		if (actionData.size() < 1) {
    2.33 +			throw new OutputActionException("Please specify the XPath expression as action data");
    2.34 +		} else {
    2.35 +
    2.36 +			boolean typedParameters = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_TYPED_PARAMETERS));
    2.37 +
    2.38 +			Map<String, Object> xpathParameters = new HashMap<>();
    2.39 +
    2.40 +			for (int i = 1; i < actionData.size(); i++) {
    2.41 +				String parameterName = actionData.get(i++);
    2.42 +				String parameterType = typedParameters ? actionData.get(i++) : "string";
    2.43 +				Object parameterValue = parseParameterValue(parameterType, actionData.get(i++));
    2.44 +				xpathParameters.put(parameterName, parameterValue);
    2.45 +			}
    2.46 +
    2.47 +			xpath.setXPathVariableResolver(new PropertiesVariableResolver(xpathParameters));
    2.48 +
    2.49 +			expressionString = actionData.get(0);
    2.50  			try {
    2.51  				xpathExpression = xpath.compile(expressionString);
    2.52  			} catch (XPathExpressionException e) {
    2.53  				throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
    2.54  			}
    2.55 -		} else {
    2.56 -			throw new OutputActionException("Please specify the XPath expression as action data");
    2.57 +
    2.58  		}
    2.59  	}
    2.60  
    2.61 +	private static Object parseParameterValue(String type, String value) {
    2.62 +		// TODO: support more types
    2.63 +		return value;
    2.64 +	}
    2.65 +
    2.66  	@Override
    2.67  	public void run(DOMResult domResult) throws OutputActionException {
    2.68  		try {