From 447f8f9a305ee8f304d48b0e63bdabc4acbbd826 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Sat, 09 Jul 2022 13:43:16 +0800
Subject: [PATCH] fix groovy bug
---
src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java | 37 ++++++++++++-------------------------
src/main/resources/application.yml | 4 ++--
2 files changed, 14 insertions(+), 27 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 e43cfca..783dfe1 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
@@ -11,6 +11,7 @@
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;
@@ -196,26 +197,9 @@
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 = groovyShell.getContext();
+ 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();
@@ -226,14 +210,17 @@
binding.setProperty("system", systemData);
}
-// GroovyShell shell = new GroovyShell(binding);
- Object evaluate = groovyShell.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 == null) {
+ targetData.put(group, "");
+ } else {
+ targetData.put(group, evaluate.toString());
+ }
+ groovyShell.getClassLoader().clearCache();
}
- groovyShell.getClassLoader().clearCache();
}
StringSubstitutor str = new StringSubstitutor(targetData);
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 85174c9..673d005 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -99,7 +99,7 @@
cms:
base-url: http://localhost
static-url: http://120.27.238.55:8000/cms/static/
- template-path: /Users/helius/Desktop/template
+ template-path: /Users/helius/Desktop/template-online
static-path: /Users/helius/Desktop/static
- output-path: /Users/helius/Desktop/web/output
+ output-path: /Users/helius/Desktop/template-online/output
--
Gitblit v1.9.1