From 9b51a62597487073b54aa27cdd059828ebfcb2ac Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Fri, 08 Jul 2022 19:19:47 +0800 Subject: [PATCH] 加synchronized --- src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java | 75 +++++++++++++++++++++++++------------ 1 files changed, 50 insertions(+), 25 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..e43cfca 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,10 +2,13 @@ import cn.hutool.core.collection.CollUtil; +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.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 org.apache.commons.text.StringSubstitutor; @@ -52,22 +55,6 @@ 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() { Elements children = this.element.children(); if (CollUtil.isNotEmpty(children)) { @@ -86,7 +73,32 @@ 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; @@ -112,6 +124,7 @@ 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); @@ -133,24 +146,31 @@ } } + 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(); + 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 +178,10 @@ 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 { this.element.attr(key, result); } @@ -195,7 +214,8 @@ // } // } - Binding binding = new Binding(); + GroovyShell groovyShell = GroovySingleton.getSingleton(); + Binding binding = groovyShell.getContext(); for (Map.Entry<String, Object> entry : this.parserData.entrySet()) { String fieldKey = entry.getKey(); Map<String, Object> data = (Map<String, Object>) entry.getValue(); @@ -206,13 +226,14 @@ binding.setProperty("system", systemData); } - GroovyShell shell = new GroovyShell(binding); - Object evaluate = shell.evaluate(group); +// GroovyShell shell = new GroovyShell(binding); + Object evaluate = groovyShell.evaluate(group); if (evaluate == null) { targetData.put(group, ""); } else { targetData.put(group, evaluate.toString()); } + groovyShell.getClassLoader().clearCache(); } StringSubstitutor str = new StringSubstitutor(targetData); @@ -258,4 +279,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