|
| 1 | +package org.fanjr.simplify.el.invoker.node; |
| 2 | + |
| 3 | +import com.alibaba.fastjson2.JSON; |
| 4 | +import com.alibaba.fastjson2.JSONObject; |
| 5 | +import com.alibaba.fastjson2.JSONReader; |
| 6 | +import com.alibaba.fastjson2.reader.ObjectReader; |
| 7 | +import org.fanjr.simplify.el.ELVisitor; |
| 8 | +import org.fanjr.simplify.el.ElException; |
| 9 | +import org.fanjr.simplify.utils.ElUtils; |
| 10 | + |
| 11 | +import java.lang.reflect.Field; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +public class IndexMapNodeInvoker extends NodeInvoker { |
| 17 | + |
| 18 | + private final String indexNodeName; |
| 19 | + |
| 20 | + private IndexMapNodeInvoker(String nodeName, NodeInvoker superNode, String indexNodeName) { |
| 21 | + super(nodeName); |
| 22 | + parentNodeInvoker = superNode; |
| 23 | + this.indexNodeName = indexNodeName; |
| 24 | + } |
| 25 | + |
| 26 | + public static IndexMapNodeInvoker newInstance(String nodeName, NodeInvoker superNode, String indexNodeName) { |
| 27 | + return new IndexMapNodeInvoker(nodeName, superNode, indexNodeName); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + Object getValueByParent(Object ctx, NodeHolder parentNode) { |
| 32 | + String nodeName = this.indexNodeName; |
| 33 | + if (null == parentNode) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + Object parentValue = parentNode.getValue(); |
| 37 | + if (null == parentValue) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + if (parentValue instanceof Map) { |
| 41 | + return ((Map<?, ?>) parentValue).get(nodeName); |
| 42 | + } |
| 43 | + Class<?> parentClass = parentValue.getClass(); |
| 44 | + if (parentClass == Class.class) { |
| 45 | + if (((Class<?>) parentValue).isEnum()) { |
| 46 | + return Enum.valueOf((Class<? extends Enum>) parentValue, nodeName); |
| 47 | + } |
| 48 | + } else if (parentClass.isEnum()) { |
| 49 | + try { |
| 50 | + Field field = parentClass.getDeclaredField(nodeName); |
| 51 | + field.setAccessible(true); |
| 52 | + return field.get(parentValue); |
| 53 | + } catch (Exception e) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + } else if (parentClass == String.class) { |
| 57 | + String json = (String) parentValue; |
| 58 | + if (ElUtils.isBlank(json) || "null".equals(json)) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + char first = json.trim().charAt(0); |
| 63 | + if (first == '{') { |
| 64 | + try { |
| 65 | + JSONObject jsonObject = JSON.parseObject(json); |
| 66 | + parentNode.setChange(true); |
| 67 | + return jsonObject.get(nodeName); |
| 68 | + } catch (Exception e) { |
| 69 | + // 解析JSON字符串失败,不按照JSON进行解析 |
| 70 | + // TODO 按临时解决方案,后续考虑更具性能的方案 |
| 71 | + return null; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return ElUtils.getFieldByPojo(parentValue, nodeName); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + void removeValueByParent(NodeHolder parentNode, int index) { |
| 81 | + String nodeName = this.indexNodeName; |
| 82 | + if (null == parentNode) { |
| 83 | + return; |
| 84 | + } |
| 85 | + Object parentValue = parentNode.getValue(); |
| 86 | + if (null == parentValue) { |
| 87 | + return; |
| 88 | + } |
| 89 | + if (parentValue instanceof Map) { |
| 90 | + ((Map<?, ?>) parentValue).remove(nodeName); |
| 91 | + } |
| 92 | + |
| 93 | + Class<?> parentClass = parentValue.getClass(); |
| 94 | + if (parentClass == String.class) { |
| 95 | + //Parent类型为字符串,操作完后确保推送回Parent为字符串 |
| 96 | + String json = (String) parentValue; |
| 97 | + if (ElUtils.isBlank(json) || "null".equals(json)) { |
| 98 | + return; |
| 99 | + } else { |
| 100 | + char first = json.trim().charAt(0); |
| 101 | + if (first == '{') { |
| 102 | + try (JSONReader reader = JSONReader.of(json)) { |
| 103 | + ObjectReader<JSONObject> objectReader = reader.getObjectReader(JSONObject.class); |
| 104 | + JSONObject jsonObject = objectReader.readObject(reader, 0); |
| 105 | + jsonObject.remove(nodeName); |
| 106 | + parentNode.setValue(jsonObject.toString()); |
| 107 | + return; |
| 108 | + } |
| 109 | + } else { |
| 110 | + return; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (parentValue instanceof List || parentClass.isArray()) { |
| 116 | + // 不做处理 |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + ElUtils.putFieldByPojo(parentValue, nodeName, null); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + void setValueByParent(NodeHolder parentNode, Object value, int index) { |
| 125 | + String nodeName = this.indexNodeName; |
| 126 | + if (null == parentNode) { |
| 127 | + throw new ElException("不可对【" + this + "】进行赋值!"); |
| 128 | + } |
| 129 | + Object parentValue = parentNode.getValue(); |
| 130 | + if (null == parentValue) { |
| 131 | + if (parentNode.isRoot()) { |
| 132 | + throw new ElException("ROOT节点为空!不可对【" + this + "】进行赋值!"); |
| 133 | + } |
| 134 | + parentNode.setValue(JSONObject.of(nodeName, value)); |
| 135 | + } else { |
| 136 | + if (parentValue instanceof Map) { |
| 137 | + ((Map<String, Object>) parentValue).put(nodeName, value); |
| 138 | + if (parentNode.isChange()) { |
| 139 | + parentNode.setValue(parentValue); |
| 140 | + } |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + Class<?> parentClass = parentValue.getClass(); |
| 145 | + if (parentClass == String.class) { |
| 146 | + //Parent类型为字符串,操作完后确保推送回Parent为字符串 |
| 147 | + String json = (String) parentValue; |
| 148 | + if (json.isEmpty() || "null".equals(json)) { |
| 149 | + parentNode.setValue(JSONObject.of(nodeName, value).toString()); |
| 150 | + return; |
| 151 | + } else { |
| 152 | + char first = json.trim().charAt(0); |
| 153 | + if (first == '{') { |
| 154 | + try (JSONReader reader = JSONReader.of(json)) { |
| 155 | + ObjectReader<JSONObject> objectReader = reader.getObjectReader(JSONObject.class); |
| 156 | + JSONObject jsonObject = objectReader.readObject(reader, 0); |
| 157 | + jsonObject.put(nodeName, value); |
| 158 | + parentNode.setValue(jsonObject.toString()); |
| 159 | + return; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if (parentClass.isArray() || parentValue instanceof Collection) { |
| 166 | + //打破原有结构 |
| 167 | + JSONObject jsonObject = new JSONObject(); |
| 168 | + jsonObject.put(nodeName, value); |
| 169 | + parentNode.setValue(jsonObject); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + if (ElUtils.putFieldByPojo(parentValue, nodeName, value)) { |
| 174 | + if (parentNode.isChange()) { |
| 175 | + parentNode.setValue(parentValue); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public String toString() { |
| 183 | + return parentNodeInvoker.toString() + "['" + indexNodeName + "']"; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public boolean isVariable() { |
| 188 | + if (null == parentNodeInvoker || parentNodeInvoker instanceof RootNodeInvoker) { |
| 189 | + // 没有上层节点,或者上层节点为ROOT节点,说明当前节点为变量 |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + // 上级节点为变量则当前节点也为变量 |
| 194 | + return parentNodeInvoker.isVariable(); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + protected void acceptChild(ELVisitor visitor) { |
| 199 | + // skip |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments