From a0c562b9149d5f85b65fc39fa9c5f48fb49f57ae Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Tue, 30 Aug 2022 16:57:26 +0800
Subject: [PATCH] fix

---
 src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java |   86 ++++++++++++++++++++----------------------
 1 files changed, 41 insertions(+), 45 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 ce47824..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,18 +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;
@@ -50,22 +60,6 @@
         this.element = element.clone();
         this.originalElement = element;
         this.parserData = parserData;
-    }
-
-    public static void main(String[] args) {
-//        String data = "{id=[1,2,3], page=1, limit=5, field=art}";
-//        Articles articles = new AttrNode().parserTag(data, Articles.class);
-
-//        String value = "{id=${col.id}, page=1, limit=5, field=art}";
-//        String pattern = "(?<=\\$\\{)[\\s\\S]*?(?=\\})";
-//        Matcher matcher = Pattern.compile(pattern).matcher(value);
-//        while (matcher.find()) {
-//            String group = matcher.group();
-//            System.out.println(1);
-//        }
-
-
-        System.out.println(1);
     }
 
     private boolean isNeedEmpty() {
@@ -133,6 +127,7 @@
             }
         }
 
+
         runDataInject();
     }
 
@@ -142,15 +137,17 @@
             String key = attribute.getKey().replaceAll("\\$", "");
             String value = attribute.getValue();
 
+            String result = attrValueFormat(value);
             // @{} 为java表达式; ${}为需要注入的数据项
             if (value.startsWith("@{")) {
-                value = value.replaceAll("\\@\\{", "").replaceAll("}", "");
+                value = result.replaceAll("\\@\\{", "").replaceAll("}", "");
                 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();
-                    binding.setProperty(fieldKey, data);
-                    binding.setVariable(fieldKey + ".index", 1);
+                    binding.setProperty(fieldKey, data.get("state"));
+                    int index = (int) data.get("index");
+                    binding.setVariable( "index", index);
                 }
                 GroovyShell shell = new GroovyShell(binding);
                 String evaluate = (String) shell.evaluate(value);
@@ -158,11 +155,14 @@
                 this.element.removeAttr("class");
                 this.element.attr("class", evaluate);
             } else if (value.contains( "${")) {
-
-                String result = attrValueFormat(value);
-                System.out.println(result);
                 if ("text".equals(key)) {
                     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);
                 }
@@ -177,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();
@@ -206,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();
             }
         }
 
@@ -258,4 +250,8 @@
     public void systemData(Map<String, Object> systemData) {
         this.systemData = systemData;
     }
+
+    public Object getSystemDataValue(String key) {
+        return this.systemData.get(key);
+    }
 }

--
Gitblit v1.9.1