java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 17 Sep 2014 21:31:18 +0200
changeset 101 fd83cd102325
parent 97 a72d55ff30c9
child 105 e62a3e498212
permissions -rw-r--r--
out-xpath: support namespace prefixes defined in the input document
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@101
    34
import org.w3c.dom.Node;
franta-hg@43
    35
franta-hg@43
    36
/**
franta-hg@43
    37
 *
franta-hg@43
    38
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@43
    39
 */
franta-hg@63
    40
public class XPathAction extends AbstractDOMAction {
franta-hg@43
    41
franta-hg@69
    42
	public static final String PARAMETER_TYPED_PARAMETERS = "typed-parameters";
franta-hg@79
    43
	public static final String PARAMETER_LINE_BREAK = "line-break";
franta-hg@97
    44
	public static final String PARAMETER_ENVIRONMENT_VARIABLES = "environment-variables";
franta-hg@79
    45
	private final boolean typedParameters;
franta-hg@79
    46
	private final boolean lineBreak;
franta-hg@97
    47
	private final boolean environmentVariables;
franta-hg@66
    48
	private final XPathFactory xpathFactory;
franta-hg@66
    49
	private final XPath xpath;
franta-hg@68
    50
	private final String expressionString;
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@63
    82
		}
franta-hg@43
    83
	}
franta-hg@68
    84
franta-hg@72
    85
	private enum PARAMETER_TYPE {
franta-hg@72
    86
franta-hg@72
    87
		// TODO: wait for Java 8 widespread and rewrite with lambdas :-)
franta-hg@72
    88
		STRING {
franta-hg@72
    89
					@Override
franta-hg@72
    90
					public Object parse(String value) {
franta-hg@72
    91
						return value;
franta-hg@72
    92
					}
franta-hg@72
    93
				},
franta-hg@72
    94
		BOOLEAN {
franta-hg@72
    95
					@Override
franta-hg@72
    96
					public Object parse(String value) {
franta-hg@72
    97
						return Boolean.valueOf(value);
franta-hg@72
    98
					}
franta-hg@72
    99
				},
franta-hg@72
   100
		INTEGER {
franta-hg@72
   101
					@Override
franta-hg@72
   102
					public Object parse(String value) {
franta-hg@72
   103
						return Integer.parseInt(value);
franta-hg@72
   104
					}
franta-hg@72
   105
				},
franta-hg@72
   106
		LONG {
franta-hg@72
   107
					@Override
franta-hg@72
   108
					public Object parse(String value) {
franta-hg@72
   109
						return Long.parseLong(value);
franta-hg@72
   110
					}
franta-hg@72
   111
				},
franta-hg@72
   112
		DOUBLE {
franta-hg@72
   113
					@Override
franta-hg@72
   114
					public Object parse(String value) {
franta-hg@72
   115
						return Double.parseDouble(value);
franta-hg@72
   116
					}
franta-hg@72
   117
				};
franta-hg@72
   118
franta-hg@72
   119
		public abstract Object parse(String value);
franta-hg@72
   120
franta-hg@72
   121
	}
franta-hg@72
   122
franta-hg@72
   123
	private static Object parseParameterValue(String type, String value) throws OutputActionException {
franta-hg@72
   124
franta-hg@72
   125
		try {
franta-hg@72
   126
			PARAMETER_TYPE parameterType = PARAMETER_TYPE.valueOf(type.toUpperCase());
franta-hg@72
   127
franta-hg@72
   128
			try {
franta-hg@72
   129
				return parameterType.parse(value);
franta-hg@72
   130
			} catch (IllegalArgumentException e) {
franta-hg@72
   131
				throw new OutputActionException("Unable to parse value: „" + value + "“ as " + parameterType.name());
franta-hg@72
   132
			}
franta-hg@72
   133
		} catch (IllegalArgumentException e) {
franta-hg@72
   134
			throw new OutputActionException(
franta-hg@72
   135
					"Invalid XPath parameter type: „" + type
franta-hg@72
   136
					+ "“. Possible values are: " + Arrays.toString(PARAMETER_TYPE.values())
franta-hg@72
   137
					+ " and are case insensitive.",
franta-hg@72
   138
					e);
franta-hg@72
   139
		}
franta-hg@69
   140
	}
franta-hg@69
   141
franta-hg@68
   142
	@Override
franta-hg@68
   143
	public void run(DOMResult domResult) throws OutputActionException {
franta-hg@101
   144
		XPathExpression xpathExpression = null;
franta-hg@68
   145
		try {
franta-hg@101
   146
			Node document = domResult.getNode();
franta-hg@101
   147
franta-hg@101
   148
			xpath.setNamespaceContext(new DocumentNamespaceContext(document));
franta-hg@101
   149
franta-hg@101
   150
			try {
franta-hg@101
   151
				xpathExpression = xpath.compile(expressionString);
franta-hg@101
   152
			} catch (XPathExpressionException e) {
franta-hg@101
   153
				throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
franta-hg@101
   154
			}
franta-hg@101
   155
franta-hg@101
   156
			String result = xpathExpression.evaluate(document);
franta-hg@68
   157
			try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
franta-hg@79
   158
				out.print(result);
franta-hg@79
   159
				if (lineBreak) {
franta-hg@79
   160
					out.println();
franta-hg@79
   161
				}
franta-hg@68
   162
			}
franta-hg@68
   163
		} catch (XPathExpressionException e) {
franta-hg@68
   164
			throw new OutputActionException("Unable to evaluate XPath: " + xpathExpression, e);
franta-hg@68
   165
		}
franta-hg@68
   166
	}
franta-hg@43
   167
}