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
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.out.xpath;
    19 
    20 import cz.frantovo.alt2xml.out.AbstractDOMAction;
    21 import cz.frantovo.alt2xml.out.ActionContext;
    22 import cz.frantovo.alt2xml.out.OutputActionException;
    23 import java.io.PrintWriter;
    24 import java.util.Arrays;
    25 import java.util.HashMap;
    26 import java.util.List;
    27 import java.util.Map;
    28 import javax.xml.transform.dom.DOMResult;
    29 import javax.xml.xpath.XPath;
    30 import javax.xml.xpath.XPathExpression;
    31 import javax.xml.xpath.XPathExpressionException;
    32 import javax.xml.xpath.XPathFactory;
    33 import javax.xml.xpath.XPathVariableResolver;
    34 
    35 /**
    36  *
    37  * @author Ing. František Kučera (frantovo.cz)
    38  */
    39 public class XPathAction extends AbstractDOMAction {
    40 
    41 	public static final String PARAMETER_TYPED_PARAMETERS = "typed-parameters";
    42 	public static final String PARAMETER_LINE_BREAK = "line-break";
    43 	public static final String PARAMETER_ENVIRONMENT_VARIABLES = "environment-variables";
    44 	private final boolean typedParameters;
    45 	private final boolean lineBreak;
    46 	private final boolean environmentVariables;
    47 	private final XPathFactory xpathFactory;
    48 	private final XPath xpath;
    49 	private final String expressionString;
    50 	private final XPathExpression xpathExpression;
    51 
    52 	public XPathAction(ActionContext actionContext) throws OutputActionException {
    53 		super(actionContext);
    54 		xpathFactory = XPathFactory.newInstance();
    55 		xpath = xpathFactory.newXPath();
    56 
    57 		final List<String> actionData = getActionContext().getActionData();
    58 
    59 		if (actionData.size() < 1) {
    60 			throw new OutputActionException("Please specify the XPath expression as action data");
    61 		} else {
    62 
    63 			typedParameters = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_TYPED_PARAMETERS));
    64 			lineBreak = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_LINE_BREAK, Boolean.TRUE.toString()));
    65 			environmentVariables = Boolean.parseBoolean(actionContext.getActionProperties().getProperty(PARAMETER_ENVIRONMENT_VARIABLES, Boolean.FALSE.toString()));
    66 
    67 			Map<String, Object> xpathParameters = new HashMap<>();
    68 
    69 			for (int i = 1; i < actionData.size(); i++) {
    70 				String parameterName = actionData.get(i++);
    71 				String parameterType = typedParameters ? actionData.get(i++) : PARAMETER_TYPE.STRING.name();
    72 				Object parameterValue = parseParameterValue(parameterType, actionData.get(i));
    73 				xpathParameters.put(parameterName, parameterValue);
    74 			}
    75 
    76 			XPathVariableResolver cliVariableResolver = new PropertiesVariableResolver(xpathParameters);
    77 			XPathVariableResolver variableResolver = environmentVariables ? new CompoundVariableResolver(cliVariableResolver, new EnvironmentVariableResolver()) : cliVariableResolver;
    78 
    79 			xpath.setXPathVariableResolver(variableResolver);
    80 
    81 			expressionString = actionData.get(0);
    82 			try {
    83 				xpathExpression = xpath.compile(expressionString);
    84 			} catch (XPathExpressionException e) {
    85 				throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
    86 			}
    87 
    88 		}
    89 	}
    90 
    91 	private enum PARAMETER_TYPE {
    92 
    93 		// TODO: wait for Java 8 widespread and rewrite with lambdas :-)
    94 		STRING {
    95 					@Override
    96 					public Object parse(String value) {
    97 						return value;
    98 					}
    99 				},
   100 		BOOLEAN {
   101 					@Override
   102 					public Object parse(String value) {
   103 						return Boolean.valueOf(value);
   104 					}
   105 				},
   106 		INTEGER {
   107 					@Override
   108 					public Object parse(String value) {
   109 						return Integer.parseInt(value);
   110 					}
   111 				},
   112 		LONG {
   113 					@Override
   114 					public Object parse(String value) {
   115 						return Long.parseLong(value);
   116 					}
   117 				},
   118 		DOUBLE {
   119 					@Override
   120 					public Object parse(String value) {
   121 						return Double.parseDouble(value);
   122 					}
   123 				};
   124 
   125 		public abstract Object parse(String value);
   126 
   127 	}
   128 
   129 	private static Object parseParameterValue(String type, String value) throws OutputActionException {
   130 
   131 		try {
   132 			PARAMETER_TYPE parameterType = PARAMETER_TYPE.valueOf(type.toUpperCase());
   133 
   134 			try {
   135 				return parameterType.parse(value);
   136 			} catch (IllegalArgumentException e) {
   137 				throw new OutputActionException("Unable to parse value: „" + value + "“ as " + parameterType.name());
   138 			}
   139 		} catch (IllegalArgumentException e) {
   140 			throw new OutputActionException(
   141 					"Invalid XPath parameter type: „" + type
   142 					+ "“. Possible values are: " + Arrays.toString(PARAMETER_TYPE.values())
   143 					+ " and are case insensitive.",
   144 					e);
   145 		}
   146 	}
   147 
   148 	@Override
   149 	public void run(DOMResult domResult) throws OutputActionException {
   150 		try {
   151 			String result = xpathExpression.evaluate(domResult.getNode());
   152 			try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
   153 				out.print(result);
   154 				if (lineBreak) {
   155 					out.println();
   156 				}
   157 			}
   158 		} catch (XPathExpressionException e) {
   159 			throw new OutputActionException("Unable to evaluate XPath: " + xpathExpression, e);
   160 		}
   161 	}
   162 }