java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 07 Sep 2014 01:28:07 +0200
changeset 97 a72d55ff30c9
parent 79 3581fe8cd4fe
child 101 fd83cd102325
permissions -rw-r--r--
out-xpath: optional variable resolver for environment variables – can be used together with those from command line
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@72
    24
import java.util.Arrays;
franta-hg@69
    25
import java.util.HashMap;
franta-hg@69
    26
import java.util.List;
franta-hg@69
    27
import java.util.Map;
franta-hg@63
    28
import javax.xml.transform.dom.DOMResult;
franta-hg@63
    29
import javax.xml.xpath.XPath;
franta-hg@63
    30
import javax.xml.xpath.XPathExpression;
franta-hg@63
    31
import javax.xml.xpath.XPathExpressionException;
franta-hg@63
    32
import javax.xml.xpath.XPathFactory;
franta-hg@97
    33
import javax.xml.xpath.XPathVariableResolver;
franta-hg@43
    34
franta-hg@43
    35
/**
franta-hg@43
    36
 *
franta-hg@43
    37
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@43
    38
 */
franta-hg@63
    39
public class XPathAction extends AbstractDOMAction {
franta-hg@43
    40
franta-hg@69
    41
	public static final String PARAMETER_TYPED_PARAMETERS = "typed-parameters";
franta-hg@79
    42
	public static final String PARAMETER_LINE_BREAK = "line-break";
franta-hg@97
    43
	public static final String PARAMETER_ENVIRONMENT_VARIABLES = "environment-variables";
franta-hg@79
    44
	private final boolean typedParameters;
franta-hg@79
    45
	private final boolean lineBreak;
franta-hg@97
    46
	private final boolean environmentVariables;
franta-hg@66
    47
	private final XPathFactory xpathFactory;
franta-hg@66
    48
	private final XPath xpath;
franta-hg@68
    49
	private final String expressionString;
franta-hg@68
    50
	private final XPathExpression xpathExpression;
franta-hg@66
    51
franta-hg@68
    52
	public XPathAction(ActionContext actionContext) throws OutputActionException {
franta-hg@43
    53
		super(actionContext);
franta-hg@66
    54
		xpathFactory = XPathFactory.newInstance();
franta-hg@66
    55
		xpath = xpathFactory.newXPath();
franta-hg@43
    56
franta-hg@69
    57
		final List<String> actionData = getActionContext().getActionData();
franta-hg@63
    58
franta-hg@69
    59
		if (actionData.size() < 1) {
franta-hg@69
    60
			throw new OutputActionException("Please specify the XPath expression as action data");
franta-hg@69
    61
		} else {
franta-hg@69
    62
franta-hg@79
    63
			typedParameters = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_TYPED_PARAMETERS));
franta-hg@79
    64
			lineBreak = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_LINE_BREAK, Boolean.TRUE.toString()));
franta-hg@97
    65
			environmentVariables = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_ENVIRONMENT_VARIABLES, Boolean.FALSE.toString()));
franta-hg@69
    66
franta-hg@69
    67
			Map<String, Object> xpathParameters = new HashMap<>();
franta-hg@69
    68
franta-hg@69
    69
			for (int i = 1; i < actionData.size(); i++) {
franta-hg@69
    70
				String parameterName = actionData.get(i++);
franta-hg@72
    71
				String parameterType = typedParameters ? actionData.get(i++) : PARAMETER_TYPE.STRING.name();
franta-hg@71
    72
				Object parameterValue = parseParameterValue(parameterType, actionData.get(i));
franta-hg@69
    73
				xpathParameters.put(parameterName, parameterValue);
franta-hg@69
    74
			}
franta-hg@69
    75
franta-hg@97
    76
			XPathVariableResolver cliVariableResolver = new PropertiesVariableResolver(xpathParameters);
franta-hg@97
    77
			XPathVariableResolver variableResolver = environmentVariables ? new CompoundVariableResolver(cliVariableResolver, new EnvironmentVariableResolver()) : cliVariableResolver;
franta-hg@97
    78
franta-hg@97
    79
			xpath.setXPathVariableResolver(variableResolver);
franta-hg@69
    80
franta-hg@69
    81
			expressionString = actionData.get(0);
franta-hg@65
    82
			try {
franta-hg@68
    83
				xpathExpression = xpath.compile(expressionString);
franta-hg@65
    84
			} catch (XPathExpressionException e) {
franta-hg@68
    85
				throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
franta-hg@63
    86
			}
franta-hg@69
    87
franta-hg@63
    88
		}
franta-hg@43
    89
	}
franta-hg@68
    90
franta-hg@72
    91
	private enum PARAMETER_TYPE {
franta-hg@72
    92
franta-hg@72
    93
		// TODO: wait for Java 8 widespread and rewrite with lambdas :-)
franta-hg@72
    94
		STRING {
franta-hg@72
    95
					@Override
franta-hg@72
    96
					public Object parse(String value) {
franta-hg@72
    97
						return value;
franta-hg@72
    98
					}
franta-hg@72
    99
				},
franta-hg@72
   100
		BOOLEAN {
franta-hg@72
   101
					@Override
franta-hg@72
   102
					public Object parse(String value) {
franta-hg@72
   103
						return Boolean.valueOf(value);
franta-hg@72
   104
					}
franta-hg@72
   105
				},
franta-hg@72
   106
		INTEGER {
franta-hg@72
   107
					@Override
franta-hg@72
   108
					public Object parse(String value) {
franta-hg@72
   109
						return Integer.parseInt(value);
franta-hg@72
   110
					}
franta-hg@72
   111
				},
franta-hg@72
   112
		LONG {
franta-hg@72
   113
					@Override
franta-hg@72
   114
					public Object parse(String value) {
franta-hg@72
   115
						return Long.parseLong(value);
franta-hg@72
   116
					}
franta-hg@72
   117
				},
franta-hg@72
   118
		DOUBLE {
franta-hg@72
   119
					@Override
franta-hg@72
   120
					public Object parse(String value) {
franta-hg@72
   121
						return Double.parseDouble(value);
franta-hg@72
   122
					}
franta-hg@72
   123
				};
franta-hg@72
   124
franta-hg@72
   125
		public abstract Object parse(String value);
franta-hg@72
   126
franta-hg@72
   127
	}
franta-hg@72
   128
franta-hg@72
   129
	private static Object parseParameterValue(String type, String value) throws OutputActionException {
franta-hg@72
   130
franta-hg@72
   131
		try {
franta-hg@72
   132
			PARAMETER_TYPE parameterType = PARAMETER_TYPE.valueOf(type.toUpperCase());
franta-hg@72
   133
franta-hg@72
   134
			try {
franta-hg@72
   135
				return parameterType.parse(value);
franta-hg@72
   136
			} catch (IllegalArgumentException e) {
franta-hg@72
   137
				throw new OutputActionException("Unable to parse value: „" + value + "“ as " + parameterType.name());
franta-hg@72
   138
			}
franta-hg@72
   139
		} catch (IllegalArgumentException e) {
franta-hg@72
   140
			throw new OutputActionException(
franta-hg@72
   141
					"Invalid XPath parameter type: „" + type
franta-hg@72
   142
					+ "“. Possible values are: " + Arrays.toString(PARAMETER_TYPE.values())
franta-hg@72
   143
					+ " and are case insensitive.",
franta-hg@72
   144
					e);
franta-hg@72
   145
		}
franta-hg@69
   146
	}
franta-hg@69
   147
franta-hg@68
   148
	@Override
franta-hg@68
   149
	public void run(DOMResult domResult) throws OutputActionException {
franta-hg@68
   150
		try {
franta-hg@68
   151
			String result = xpathExpression.evaluate(domResult.getNode());
franta-hg@68
   152
			try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
franta-hg@79
   153
				out.print(result);
franta-hg@79
   154
				if (lineBreak) {
franta-hg@79
   155
					out.println();
franta-hg@79
   156
				}
franta-hg@68
   157
			}
franta-hg@68
   158
		} catch (XPathExpressionException e) {
franta-hg@68
   159
			throw new OutputActionException("Unable to evaluate XPath: " + xpathExpression, e);
franta-hg@68
   160
		}
franta-hg@68
   161
	}
franta-hg@43
   162
}