占位符

package com.datacloudsec.analysis.rule.utils;

import com.alibaba.fastjson.JSON;
import com.datacloudsec.analysis.rule.commons.event.model.EventLinkObject;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import sun.rmi.server.InactiveGroupException;

import java.lang.reflect.Field;

/**
 * @Author xlj
 * @Date 2018/7/30 14:44
 */
public class PlaceHolderUtils {
    private static final String placeholderPrefix = "${";
    private static final CharSequence placeholderSuffix = "}";
    private static final String valueSeparator = ":";


    private static <T> String resolvePlaceholder(String placeholder, T replaceValue, boolean toUnderLine) {
        try {
            if (toUnderLine) {
                placeholder = CamelCaseUtils.toUnderlineName(placeholder);
            }
            return BeanUtils.getProperty(replaceValue, placeholder);
        } catch (Exception e) {
            return null;
        }
    }

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

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

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

        return -1;
    }

    private 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;
        }
    }

    /**
     * @param src          带有占位符${}的类
     * @param replaceValue 需要替换的属性值
     * @param toUnderLine
     * @param <T>
     * @return
     */
    public static <T> void parseStringValue(T src, T replaceValue, boolean toUnderLine) {
        if (replaceValue == null) {
            replaceValue = src;
        }
        Field[] declaredFields = src.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            try {
                Class<?> type = field.getType();
                String value = String.valueOf(field.get(src));
                if (StringUtils.isBlank(value)) {
                    continue;
                }
                StringBuilder result = new StringBuilder(value);
                int startIndex = value.indexOf(placeholderPrefix);

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

                        if (propVal != null) {
                            result.replace(startIndex, endIndex + placeholderSuffix.length(), propVal);
                            startIndex = result.indexOf(placeholderPrefix, startIndex + propVal.length());
                        } else {
                            startIndex = result.indexOf(placeholderPrefix, endIndex + placeholderSuffix.length());
                        }
                    } else {
                        startIndex = -1;
                    }
                }
                if (result.toString().equals(value)) {
                    continue;
                }
                if (int.class == type) {
                    field.set(src, Integer.parseInt(result.toString()));
                } else {
                    field.set(src, result.toString());
                }
            } catch (IllegalAccessException e) {
                continue;
            }
        }
    }

    public static void main(String[] args) {
        final EventLinkObject event = new EventLinkObject();
        event.setName("名称:${event_content:默认名称},目的ip:${dst_address}");
        event.setContent("内容测试:${event_content}");
        event.setDst_address("127.0.0.1");
        event.setEvent_content("替换成功了");

        parseStringValue(event, event, false);
        System.out.println(JSON.toJSON(event));
    }
}
package com.datacloudsec.analysis.rule.utils;

public class CamelCaseUtils {
 
    private static final char SEPARATOR = '_';
 
    public static String toUnderlineName(String s) {
        if (s == null) {
            return null;
        }
 
        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
 
            boolean nextUpperCase = true;
 
            if (i < (s.length() - 1)) {
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }
 
            if ((i >= 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
                    if (i > 0) sb.append(SEPARATOR);
                }
                upperCase = true;
            } else {
                upperCase = false;
            }
 
            sb.append(Character.toLowerCase(c));
        }
 
        return sb.toString();
    }
 
    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }
 
        s = s.toLowerCase();
 
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
 
            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
 
        return sb.toString();
    }
 
    public static String toCapitalizeCamelCase(String s) {
        if (s == null) {
            return null;
        }
        s = toCamelCase(s);
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值