${}替换符

package com.datacloudsec.alarm.process;

import com.datacloudsec.alarm.bean.AlarmBean;
import com.datacloudsec.alarm.repo.entity.EventAlarmLog;
import com.datacloudsec.alarm.util.CamelCaseUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Author xlj
 * @Date 2018/7/30 14:44
 */
@Component
public class PlaceHolderProcess {
    private static Logger logger = LoggerFactory.getLogger(PlaceHolderProcess.class);
    private String placeholderPrefix = "${";
    private CharSequence placeholderSuffix = "}";
    private String valueSeparator = ":";

    public String parseStringValue(EventAlarmLog log, AlarmBean alarmBean) {
        String value = log.getAlarmName();
        StringBuilder result = new StringBuilder(value);
        int startIndex = value.indexOf(this.placeholderPrefix);

        while (startIndex != -1) {
            int endIndex = this.findPlaceholderEndIndex(result, startIndex);
            if (endIndex != -1) {
                String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
                String propVal = resolvePlaceholder(placeholder, alarmBean);
                if (propVal == null && this.valueSeparator != null) {
                    int separatorIndex = placeholder.indexOf(this.valueSeparator);
                    if (separatorIndex != -1) {
                        String actualPlaceholder = placeholder.substring(0, separatorIndex);
                        String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                        propVal = resolvePlaceholder(actualPlaceholder, alarmBean);
                        if (propVal == null) {
                            propVal = defaultValue;
                        }
                    }
                }

                if (propVal != null) {
                    result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                    startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
                } else {
                    startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
                }
            } else {
                startIndex = -1;
            }
        }

        return result.toString();
    }

    private String resolvePlaceholder(String placeholder, AlarmBean alarmBean) {
        try {
            String underlineName = CamelCaseUtils.toUnderlineName(placeholder);
            return BeanUtils.getProperty(alarmBean, underlineName);
        } catch (Exception e) {
            return null;
        }
    }

    private int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
        int index = startIndex + this.placeholderPrefix.length();
        int withinNestedPlaceholder = 0;

        while (index < buf.length()) {
            if (substringMatch(buf, index, this.placeholderSuffix)) {
                if (withinNestedPlaceholder <= 0) {
                    return index;
                }

                --withinNestedPlaceholder;
                index += this.placeholderSuffix.length();
            } else {
                ++index;
            }
        }

        return -1;
    }

    public static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
        if (index + substring.length() > str.length()) {
            return false;
        } else {
            for (int i = 0; i < substring.length(); ++i) {
                if (str.charAt(index + i) != substring.charAt(i)) {
                    return false;
                }
            }

            return true;
        }
    }
}

 

@Test
public void placeHolderTest(){
    PlaceHolderProcess placeHolderProcess = new PlaceHolderProcess();
    EventAlarmLog log = new EventAlarmLog();
    log.setAlarmName("告警名称:${eventName:异常告警},告警时间:${occurTime},告警内容:${eventContent:默认告警内容}");

    AlarmBean bean = new AlarmBean();
    bean.setEvent_name("SQL注入");
    bean.setOccur_time(String.valueOf(new Date().getTime()));
    String value = placeHolderProcess.parseStringValue(log, bean);
    System.out.println(value);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值