1.1 --- a/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java Thu Sep 04 18:16:33 2014 +0200
1.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java Thu Sep 04 18:33:34 2014 +0200
1.3 @@ -21,6 +21,7 @@
1.4 import cz.frantovo.alt2xml.out.ActionContext;
1.5 import cz.frantovo.alt2xml.out.OutputActionException;
1.6 import java.io.PrintWriter;
1.7 +import java.util.Arrays;
1.8 import java.util.HashMap;
1.9 import java.util.List;
1.10 import java.util.Map;
1.11 @@ -59,7 +60,7 @@
1.12
1.13 for (int i = 1; i < actionData.size(); i++) {
1.14 String parameterName = actionData.get(i++);
1.15 - String parameterType = typedParameters ? actionData.get(i++) : "string";
1.16 + String parameterType = typedParameters ? actionData.get(i++) : PARAMETER_TYPE.STRING.name();
1.17 Object parameterValue = parseParameterValue(parameterType, actionData.get(i));
1.18 xpathParameters.put(parameterName, parameterValue);
1.19 }
1.20 @@ -76,9 +77,61 @@
1.21 }
1.22 }
1.23
1.24 - private static Object parseParameterValue(String type, String value) {
1.25 - // TODO: support more types
1.26 - return value;
1.27 + private enum PARAMETER_TYPE {
1.28 +
1.29 + // TODO: wait for Java 8 widespread and rewrite with lambdas :-)
1.30 + STRING {
1.31 + @Override
1.32 + public Object parse(String value) {
1.33 + return value;
1.34 + }
1.35 + },
1.36 + BOOLEAN {
1.37 + @Override
1.38 + public Object parse(String value) {
1.39 + return Boolean.valueOf(value);
1.40 + }
1.41 + },
1.42 + INTEGER {
1.43 + @Override
1.44 + public Object parse(String value) {
1.45 + return Integer.parseInt(value);
1.46 + }
1.47 + },
1.48 + LONG {
1.49 + @Override
1.50 + public Object parse(String value) {
1.51 + return Long.parseLong(value);
1.52 + }
1.53 + },
1.54 + DOUBLE {
1.55 + @Override
1.56 + public Object parse(String value) {
1.57 + return Double.parseDouble(value);
1.58 + }
1.59 + };
1.60 +
1.61 + public abstract Object parse(String value);
1.62 +
1.63 + }
1.64 +
1.65 + private static Object parseParameterValue(String type, String value) throws OutputActionException {
1.66 +
1.67 + try {
1.68 + PARAMETER_TYPE parameterType = PARAMETER_TYPE.valueOf(type.toUpperCase());
1.69 +
1.70 + try {
1.71 + return parameterType.parse(value);
1.72 + } catch (IllegalArgumentException e) {
1.73 + throw new OutputActionException("Unable to parse value: „" + value + "“ as " + parameterType.name());
1.74 + }
1.75 + } catch (IllegalArgumentException e) {
1.76 + throw new OutputActionException(
1.77 + "Invalid XPath parameter type: „" + type
1.78 + + "“. Possible values are: " + Arrays.toString(PARAMETER_TYPE.values())
1.79 + + " and are case insensitive.",
1.80 + e);
1.81 + }
1.82 }
1.83
1.84 @Override