From 90f9311b8f92d84533860e374c2a38c16f8a6e7d Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Tue, 30 Aug 2022 18:01:27 +0800
Subject: [PATCH] fix

---
 src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java |   81 ++++++++++++++--------------------------
 1 files changed, 28 insertions(+), 53 deletions(-)

diff --git a/src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java b/src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java
index 7d9cd18..8748eef 100644
--- a/src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java
+++ b/src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java
@@ -2,20 +2,28 @@
 
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DatePattern;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.xcong.farmer.cms.core.handler.DataParserHandler;
 import com.xcong.farmer.cms.core.tag.TagsEnum;
+import com.xcong.farmer.cms.core.tag.model.TimeTag;
 import com.xcong.farmer.cms.core.template.Configuration;
 import com.xcong.farmer.cms.core.template.TemplateConfiguration;
+import com.xcong.farmer.cms.utils.GroovySingleton;
 import groovy.lang.Binding;
 import groovy.lang.GroovyShell;
+import groovy.lang.Script;
 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.time.format.DateTimeFormatter;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.regex.Matcher;
@@ -72,32 +80,7 @@
         return false;
     }
 
-    public void staticPath() {
-        // 设置img的链接访问
-        if ("img".equals(this.element.tagName())) {
-            String src = this.element.attr("src");
-            if (StrUtil.isNotBlank(STATIC_URL)) {
-                this.element.attr("src", STATIC_URL + src);
-            }
-        }
-
-        if ("link".equals(this.element.tagName())) {
-            String src = this.element.attr("href");
-            if (StrUtil.isNotBlank(STATIC_URL)) {
-                this.element.attr("href", STATIC_URL + src);
-            }
-        }
-
-        if ("script".equals(this.element.tagName())) {
-            String src = this.element.attr("src");
-            if (StrUtil.isNotBlank(STATIC_URL)) {
-                this.element.attr("src", STATIC_URL + src);
-            }
-        }
-    }
-
     public void parser() {
-        staticPath();
         // 判断是否为最小节点,如果是且没有特殊标签,则跳过清空
         if (!isNeedEmpty()) {
             return;
@@ -123,7 +106,6 @@
             i++;
 
             try {
-                Template.TAGS.add(tagsEnum.getName());
                 // {id=${col.id}, page=1, limit=5, field=art} ${col.id} 形式需先设置值
                 String tagValue = attributes.get(tagsEnum.getName());
                 tagValue = attrValueFormat(tagValue);
@@ -145,16 +127,13 @@
             }
         }
 
+
         runDataInject();
     }
 
     public void runDataInject() {
         Attributes attributes = this.element.attributes();
         for (Attribute attribute : attributes) {
-            if (attribute.getKey().startsWith("\\$")) {
-                Template.TAGS.add(attribute.getKey());
-            }
-
             String key = attribute.getKey().replaceAll("\\$", "");
             String value = attribute.getValue();
 
@@ -180,6 +159,10 @@
                     this.element.text(result);
                 } else if ("html".equals(key)) {
                     this.element.html(result);
+                } else if ("time".equals(key)) {
+                    TimeTag time = parserTag(result, TimeTag.class);
+                    String timeStr = DateUtil.format(DateUtil.parse(time.getDate(), DatePattern.NORM_DATETIME_PATTERN), time.getFormat());
+                    this.element.text(timeStr);
                 } else {
                     this.element.attr(key, result);
                 }
@@ -194,24 +177,8 @@
         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;
-//            }
-//
-//            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());
-//                    }
-//                }
-//            }
 
+            GroovyShell groovyShell = GroovySingleton.getSingleton();
             Binding binding = new Binding();
             for (Map.Entry<String, Object> entry : this.parserData.entrySet()) {
                 String fieldKey = entry.getKey();
@@ -223,12 +190,20 @@
                 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());
+            synchronized (this) {
+                Script parse = groovyShell.parse(group);
+                parse.setBinding(binding);
+                Object evaluate = parse.run();
+                if (evaluate instanceof Date) {
+                    evaluate = DateUtil.format((Date) evaluate, DatePattern.NORM_DATETIME_PATTERN);
+                }
+
+                if (evaluate == null) {
+                    targetData.put(group, "");
+                } else {
+                    targetData.put(group, evaluate.toString());
+                }
+                groovyShell.getClassLoader().clearCache();
             }
         }
 

--
Gitblit v1.9.1