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