|
| 1 | +/* Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | + * you may not use this file except in compliance with the License. |
| 3 | + * You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | +package org.flowable.common.engine.impl.el; |
| 14 | + |
| 15 | +import java.math.BigDecimal; |
| 16 | +import java.math.BigInteger; |
| 17 | + |
| 18 | +import org.flowable.common.engine.impl.javax.el.ELContext; |
| 19 | +import org.flowable.common.engine.impl.javax.el.ELResolver; |
| 20 | + |
| 21 | +/** |
| 22 | + * An {@link ELResolver} that supports invoking the Jackson (2 and 3) value node accessor methods |
| 23 | + * (e.g. {@code asText()}, {@code asString()}, {@code textValue()}, {@code asInt()}, {@code isTextual()}) |
| 24 | + * on plain {@link String}, {@link Number} and {@link Boolean} values. |
| 25 | + * <p> |
| 26 | + * Plain values are used in places where JSON value nodes used to be exposed |
| 27 | + * (e.g. the element variable of a multi instance or repetition collection that is a JSON array). |
| 28 | + * This resolver keeps expressions that call such methods working. |
| 29 | + * The methods behave like they do on the corresponding Jackson 2 value node, |
| 30 | + * which is the lenient superset of the Jackson 3 behavior. |
| 31 | + * Methods that are not supported by this resolver are left to the other resolvers. |
| 32 | + * |
| 33 | + * @author Filip Hrisafov |
| 34 | + */ |
| 35 | +public class JsonValueNodeMethodsELResolver extends ELResolver { |
| 36 | + |
| 37 | + protected static final Object NOT_RESOLVED = new Object(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public Object getValue(ELContext context, Object base, Object property) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Class<?> getType(ELContext context, Object base, Object property) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void setValue(ELContext context, Object base, Object property, Object value) { |
| 51 | + // Nothing to do, this resolver only supports invoking methods |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean isReadOnly(ELContext context, Object base, Object property) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Class<?> getCommonPropertyType(ELContext context, Object base) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Object invoke(ELContext context, Object base, Object method, Class<?>[] paramTypes, Object[] params) { |
| 66 | + if (method == null || !isResolvable(base)) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + Object result; |
| 71 | + if (params == null || params.length == 0) { |
| 72 | + result = invokeWithoutParameters(base, method.toString()); |
| 73 | + } else if (params.length == 1) { |
| 74 | + result = invokeWithDefaultValue(base, method.toString(), params[0]); |
| 75 | + } else { |
| 76 | + result = NOT_RESOLVED; |
| 77 | + } |
| 78 | + |
| 79 | + if (result == NOT_RESOLVED) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + context.setPropertyResolved(true); |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + protected Object invokeWithoutParameters(Object base, String methodName) { |
| 88 | + return switch (methodName) { |
| 89 | + case "asText", "asString" -> base.toString(); |
| 90 | + case "textValue", "stringValue" -> base instanceof String ? base : null; |
| 91 | + case "asInt" -> asInt(base, 0); |
| 92 | + case "asLong" -> asLong(base, 0L); |
| 93 | + case "asDouble" -> asDouble(base, 0.0); |
| 94 | + case "asBoolean" -> asBoolean(base, false); |
| 95 | + case "intValue" -> base instanceof Number number ? number.intValue() : 0; |
| 96 | + case "longValue" -> base instanceof Number number ? number.longValue() : 0L; |
| 97 | + case "doubleValue" -> base instanceof Number number ? number.doubleValue() : 0.0; |
| 98 | + case "floatValue" -> base instanceof Number number ? number.floatValue() : 0.0f; |
| 99 | + case "decimalValue" -> base instanceof Number number ? toBigDecimal(number) : BigDecimal.ZERO; |
| 100 | + case "bigIntegerValue" -> base instanceof Number number ? toBigDecimal(number).toBigInteger() : BigInteger.ZERO; |
| 101 | + case "numberValue" -> base instanceof Number ? base : null; |
| 102 | + case "booleanValue" -> base instanceof Boolean ? base : Boolean.FALSE; |
| 103 | + case "isTextual", "isString" -> base instanceof String; |
| 104 | + case "isNumber" -> base instanceof Number; |
| 105 | + case "isInt" -> base instanceof Integer; |
| 106 | + case "isLong" -> base instanceof Long; |
| 107 | + case "isDouble" -> base instanceof Double; |
| 108 | + case "isBigDecimal" -> base instanceof BigDecimal; |
| 109 | + case "isBigInteger" -> base instanceof BigInteger; |
| 110 | + case "isBoolean" -> base instanceof Boolean; |
| 111 | + case "isValueNode" -> true; |
| 112 | + case "isNull", "isMissingNode", "isContainerNode", "isContainer", "isObject", "isArray" -> false; |
| 113 | + default -> NOT_RESOLVED; |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + protected Object invokeWithDefaultValue(Object base, String methodName, Object defaultValue) { |
| 118 | + return switch (methodName) { |
| 119 | + // Jackson returns the default value only for null or missing nodes |
| 120 | + case "asText", "asString" -> base.toString(); |
| 121 | + case "asInt" -> defaultValue instanceof Number number ? asInt(base, number.intValue()) : NOT_RESOLVED; |
| 122 | + case "asLong" -> defaultValue instanceof Number number ? asLong(base, number.longValue()) : NOT_RESOLVED; |
| 123 | + case "asDouble" -> defaultValue instanceof Number number ? asDouble(base, number.doubleValue()) : NOT_RESOLVED; |
| 124 | + case "asBoolean" -> defaultValue instanceof Boolean booleanValue ? asBoolean(base, booleanValue) : NOT_RESOLVED; |
| 125 | + default -> NOT_RESOLVED; |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + protected int asInt(Object base, int defaultValue) { |
| 130 | + if (base instanceof Number number) { |
| 131 | + return number.intValue(); |
| 132 | + } else if (base instanceof Boolean booleanValue) { |
| 133 | + return booleanValue ? 1 : 0; |
| 134 | + } |
| 135 | + |
| 136 | + String value = ((String) base).trim(); |
| 137 | + if (value.isEmpty()) { |
| 138 | + return defaultValue; |
| 139 | + } |
| 140 | + |
| 141 | + if (isIntegral(value)) { |
| 142 | + try { |
| 143 | + return Integer.parseInt(value); |
| 144 | + } catch (NumberFormatException e) { |
| 145 | + return defaultValue; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + try { |
| 150 | + return (int) Double.parseDouble(value); |
| 151 | + } catch (NumberFormatException e) { |
| 152 | + return defaultValue; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + protected long asLong(Object base, long defaultValue) { |
| 157 | + if (base instanceof Number number) { |
| 158 | + return number.longValue(); |
| 159 | + } else if (base instanceof Boolean booleanValue) { |
| 160 | + return booleanValue ? 1L : 0L; |
| 161 | + } |
| 162 | + |
| 163 | + String value = ((String) base).trim(); |
| 164 | + if (value.isEmpty()) { |
| 165 | + return defaultValue; |
| 166 | + } |
| 167 | + |
| 168 | + if (isIntegral(value)) { |
| 169 | + try { |
| 170 | + return Long.parseLong(value); |
| 171 | + } catch (NumberFormatException e) { |
| 172 | + return defaultValue; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + try { |
| 177 | + return (long) Double.parseDouble(value); |
| 178 | + } catch (NumberFormatException e) { |
| 179 | + return defaultValue; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + protected double asDouble(Object base, double defaultValue) { |
| 184 | + if (base instanceof Number number) { |
| 185 | + return number.doubleValue(); |
| 186 | + } else if (base instanceof Boolean booleanValue) { |
| 187 | + return booleanValue ? 1.0 : 0.0; |
| 188 | + } |
| 189 | + |
| 190 | + String value = ((String) base).trim(); |
| 191 | + if (value.isEmpty()) { |
| 192 | + return defaultValue; |
| 193 | + } |
| 194 | + |
| 195 | + try { |
| 196 | + return Double.parseDouble(value); |
| 197 | + } catch (NumberFormatException e) { |
| 198 | + return defaultValue; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + protected boolean asBoolean(Object base, boolean defaultValue) { |
| 203 | + if (base instanceof Boolean booleanValue) { |
| 204 | + return booleanValue; |
| 205 | + } else if (base instanceof String string) { |
| 206 | + String value = string.trim(); |
| 207 | + if ("true".equals(value)) { |
| 208 | + return true; |
| 209 | + } else if ("false".equals(value)) { |
| 210 | + return false; |
| 211 | + } |
| 212 | + return defaultValue; |
| 213 | + } else if (base instanceof BigInteger bigInteger) { |
| 214 | + return bigInteger.signum() != 0; |
| 215 | + } else if (base instanceof Integer || base instanceof Long || base instanceof Short || base instanceof Byte) { |
| 216 | + return ((Number) base).longValue() != 0; |
| 217 | + } |
| 218 | + |
| 219 | + // Jackson returns the default value for floating point numbers |
| 220 | + return defaultValue; |
| 221 | + } |
| 222 | + |
| 223 | + protected boolean isIntegral(String value) { |
| 224 | + int start = value.charAt(0) == '-' || value.charAt(0) == '+' ? 1 : 0; |
| 225 | + if (start == value.length()) { |
| 226 | + return false; |
| 227 | + } |
| 228 | + for (int i = start; i < value.length(); i++) { |
| 229 | + char c = value.charAt(i); |
| 230 | + if (c < '0' || c > '9') { |
| 231 | + return false; |
| 232 | + } |
| 233 | + } |
| 234 | + return true; |
| 235 | + } |
| 236 | + |
| 237 | + protected BigDecimal toBigDecimal(Number number) { |
| 238 | + if (number instanceof BigDecimal bigDecimal) { |
| 239 | + return bigDecimal; |
| 240 | + } else if (number instanceof BigInteger bigInteger) { |
| 241 | + return new BigDecimal(bigInteger); |
| 242 | + } else if (number instanceof Double || number instanceof Float) { |
| 243 | + return BigDecimal.valueOf(number.doubleValue()); |
| 244 | + } |
| 245 | + return BigDecimal.valueOf(number.longValue()); |
| 246 | + } |
| 247 | + |
| 248 | + protected boolean isResolvable(Object base) { |
| 249 | + return base instanceof String || base instanceof Number || base instanceof Boolean; |
| 250 | + } |
| 251 | +} |
0 commit comments