Helius
2022-07-03 f32a53f5534aa9f9a8591e8b197bb1f8acd9e6c0
src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java
File was renamed from src/main/java/com/xcong/farmer/cms/cms/node/AttrNode.java
@@ -1,17 +1,18 @@
package com.xcong.farmer.cms.cms.node;
package com.xcong.farmer.cms.core.node;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcong.farmer.cms.cms.handler.DataParserHandler;
import com.xcong.farmer.cms.cms.tag.TagsEnum;
import com.xcong.farmer.cms.cms.tag.model.Articles;
import com.xcong.farmer.cms.cms.template.Configuration;
import com.xcong.farmer.cms.core.handler.DataParserHandler;
import com.xcong.farmer.cms.core.tag.TagsEnum;
import com.xcong.farmer.cms.core.template.Configuration;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import org.apache.commons.text.StringSubstitutor;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.HashMap;
import java.util.Map;
@@ -28,11 +29,14 @@
    private Object data;
    // 用于给这个标签attr注入的数据
    private Map<String, Object> parserData;
    private Map<String, Object> systemData;
    // Tag参数 {id=[1,2,3], page=1, limit=5, field=art}
    private Object param;
    private Element element;
    private Element originalElement;
    private boolean processContinue = true;
    public AttrNode() {
    }
@@ -64,7 +68,30 @@
        System.out.println(1);
    }
    private boolean isNeedEmpty() {
        Elements children = this.element.children();
        if (CollUtil.isNotEmpty(children)) {
            return true;
        }
        Attributes attributes = this.element.attributes();
        if (CollUtil.isEmpty(attributes)) {
            return false;
        }
        for (Attribute attribute : attributes) {
            if (attribute.getKey().contains("@") || attribute.getKey().contains("$")) {
                return true;
            }
        }
        return false;
    }
    public void parser() {
        // 判断是否为最小节点,如果是且没有特殊标签,则跳过清空
        if (!isNeedEmpty()) {
            return;
        }
        this.element.empty();
        Attributes attributes = this.element.attributes();
        if (attributes.isEmpty()) {
@@ -101,6 +128,9 @@
            }
            this.tag = tagsEnum.getName();
            if (this.data == null) {
                this.processContinue = false;
            }
        }
        runDataInject();
@@ -127,9 +157,10 @@
                this.element.removeAttr("class");
                this.element.attr("class", evaluate);
            } else if (value.startsWith("${")) {
            } else if (value.contains( "${")) {
                String result = attrValueFormat(value);
                System.out.println(result);
                if ("text".equals(key)) {
                    this.element.text(result);
                } else {
@@ -146,22 +177,41 @@
        Map<String, String> targetData = new HashMap<>();
        while (matcher.find()) {
            String group = matcher.group();
            String splitValue = group.replaceAll("\\$\\{", "").replaceAll("}", "");
            String[] split = splitValue.split("\\.");
            if (split.length == 0) {
                continue;
            }
//            String splitValue = group.replaceAll("\\$\\{", "").replaceAll("}", "");
//            String[] split = splitValue.split("\\.");
//            if (split.length == 0) {
//                continue;
//            }
//
//            for (Map.Entry<String, Object> entry : this.parserData.entrySet()) {
//                String fieldKey = entry.getKey();
//                Map<String, Object> data = (Map<String, Object>) entry.getValue();
//                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(data.get("state")));
//
//                for (Map.Entry<String, Object> map : jsonObject.entrySet()) {
//                    if (map.getValue() instanceof String) {
//                        targetData.put(fieldKey + "." + map.getKey(), (String) map.getValue());
//                    }
//                }
//            }
            Binding binding = new Binding();
            for (Map.Entry<String, Object> entry : this.parserData.entrySet()) {
                String fieldKey = entry.getKey();
                Map<String, Object> data = (Map<String, Object>) entry.getValue();
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(data.get("state")));
                binding.setProperty(fieldKey, data.get("state"));
            }
                for (Map.Entry<String, Object> map : jsonObject.entrySet()) {
                    if (map.getValue() instanceof String) {
                        targetData.put(fieldKey + "." + map.getKey(), (String) map.getValue());
                    }
                }
            if (systemData != null) {
                binding.setProperty("system", systemData);
            }
            GroovyShell shell = new GroovyShell(binding);
            Object evaluate = shell.evaluate(group);
            if (evaluate == null) {
                targetData.put(group, "");
            } else {
                targetData.put(group, evaluate.toString());
            }
        }
@@ -200,4 +250,12 @@
    public String getTag() {
        return tag;
    }
    public boolean processContinue() {
        return processContinue;
    }
    public void systemData(Map<String, Object> systemData) {
        this.systemData = systemData;
    }
}