Java实现${}进行匹配占位符并且替换数据工具类

PropertyUtils: 工具类使用了 commons-beanutils-1.8.2.jar 工具包进行获取数据源中数据

public class PlaceholderUtils {

    /** PLACEHOLDER_PATTERN */
    public static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{(.*?)\\}");

    /**
     * 替换string中占位符
     *
     * @param model 具有占位符的字符串,可以是json格式
     * @param data  数据源
     * @return the string
     * @throws InvocationTargetException invocation target exception
     * @throws IllegalAccessException    illegal access exception
     * @throws NoSuchMethodException     no such method exception
     * @since 1.4.0
     */
    public static String process(String model, String data) throws InvocationTargetException,
                                                                   IllegalAccessException,
                                                                   NoSuchMethodException {
        // 匹配器
        Matcher matcher = PLACEHOLDER_PATTERN.matcher(model);
        //判断如果 匹配到了占位符并且 数据源为空时抛出异常
        boolean findReplace = matcher.find();
        if (!findReplace) {
            return model;
        }
        //判断数据是否为空(自己封装的工具类,可以修改为自己业务代码中的异常判断)
        Assertions.notBlank(data, String.format("匹配到占位符,但数据源为空,不能进行数据转换:%s", model));
        // 处理匹配到的值
        return doProcess(matcher, JsonUtils.parse(data, Map.class));
    }

    /**
     * 替换string中占位符
     *
     * @param model 具有占位符的字符串,可以是json格式
     * @param data  数据源
     * @return the string
     * @throws InvocationTargetException invocation target exception
     * @throws IllegalAccessException    illegal access exception
     * @throws NoSuchMethodException     no such method exception
     * @since 1.4.0
     */
    public static String process(String model, Map<String, Object> data) throws InvocationTargetException,
                                                                                IllegalAccessException,
                                                                                NoSuchMethodException {
        // 匹配器
        Matcher matcher = PLACEHOLDER_PATTERN.matcher(model);
        boolean findReplace = matcher.find();
        if (!findReplace) {
            return model;
        }
       //判断数据是否为空(自己封装的工具类,可以修改为自己业务代码中的异常判断)
        Assertions.notBlank(data, String.format("匹配到占位符,但数据源为空,不能进行数据转换:%s", model));
        // 处理匹配到的值
        return doProcess(matcher, data);
    }

    /**
     * Do process
     *
     * @param matcher matcher
     * @param data    data
     * @return the string
     * @throws IllegalAccessException    illegal access exception
     * @throws InvocationTargetException invocation target exception
     * @throws NoSuchMethodException     no such method exception
     * @since 1.4.0
     */
    @NotNull
    private static String doProcess(Matcher matcher, Object data) throws IllegalAccessException,
                                                                         InvocationTargetException,
                                                                         NoSuchMethodException {
        StringBuffer values = new StringBuffer();
        matcher.reset();
        while (matcher.find()) {
            String key = matcher.group(1);
            Object value = PropertyUtils.getProperty(data, key);
            Assertions.notNull(value, String.format("占位符未匹配到数据:%s", key));
            matcher.appendReplacement(values, JsonUtils.toJson(value));
        }
        matcher.appendTail(values);
        return values.toString();
    }
  }

/**
     * 处理json转换时多余的双引号
     *
     * @param str str
     * @return string string
     * @since 1.4.0
     */
    public static String doProcessQuotes(String str) {
    //使用正则表达式匹配对应的数据
        Matcher matcher = PROCESS_QUOTES.matcher(str);
        StringBuffer values = new StringBuffer();
        while (matcher.find()) {
            String s = matcher.group().replace("\\", "");
            //截取出的格式为 :"{"color":"red1","temperature":217111,"guanji":2}",需要将前后的双引号去掉
            StringBuilder strobe = new StringBuilder();
            for (int i = 0; i < s.length(); i++) {
                if (i == 1 || i == s.length() - 1) {
                    continue;
                }
                strobe.append(s.charAt(i));
            }
            matcher.appendReplacement(values, doProcessQuotes(strobe.toString()));
        }
        //将后续的数据追加进去
        matcher.appendTail(values);
        return values.toString();
    }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值