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();
    }

你可能感兴趣的:(Java,java)