From 902c0685803925e0f525f91fc50f1d5f3be37152 Mon Sep 17 00:00:00 2001 From: kosmosr Date: Mon, 28 Feb 2022 22:53:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84ContentHolderUtil=EF=BC=8C?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E6=89=93=E5=8D=B0=E9=94=99=E8=AF=AF=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/utils/ContentHolderUtil.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/austin-support/src/main/java/com/java3y/austin/support/utils/ContentHolderUtil.java b/austin-support/src/main/java/com/java3y/austin/support/utils/ContentHolderUtil.java index 386dd70..c843f0f 100644 --- a/austin-support/src/main/java/com/java3y/austin/support/utils/ContentHolderUtil.java +++ b/austin-support/src/main/java/com/java3y/austin/support/utils/ContentHolderUtil.java @@ -5,6 +5,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.util.PropertyPlaceholderHelper; import java.text.MessageFormat; +import java.util.HashMap; import java.util.Map; /** @@ -37,15 +38,17 @@ public class ContentHolderUtil { public static String replacePlaceHolder(final String template, final Map paramMap) { String replacedPushContent = PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(template, - new CustomPlaceholderResolver(paramMap)); + new CustomPlaceholderResolver(template, paramMap)); return replacedPushContent; } private static class CustomPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver { + private final String template; private final Map paramMap; - public CustomPlaceholderResolver(Map paramMap) { + public CustomPlaceholderResolver(String template, Map paramMap) { super(); + this.template = template; this.paramMap = paramMap; } @@ -53,12 +56,20 @@ public class ContentHolderUtil { public String resolvePlaceholder(String placeholderName) { String value = paramMap.get(placeholderName); if (null == value) { - String errorStr = MessageFormat.format("template:{} require param:{},but not exist! paramMap:{}", - placeholderName, paramMap.toString()); + String errorStr = MessageFormat.format("template:{0} require param:{1},but not exist! paramMap:{2}", + template, placeholderName, paramMap.toString()); throw new IllegalArgumentException(errorStr); } return value; } } + public static void main(String[] args) { + Map params = new HashMap<>(); + params.put("content", "test"); +// params.put("url", "123"); + String content = ContentHolderUtil.replacePlaceHolder("{\"content\":\"{$content}\",\"url\":\"{$url}\",\"title\":\"\"}", params); + System.out.println(content); + } + }