/**
 * 
 */
package com.platomix.userprofile.utils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wookvn
 *
 *	辅助工具类
 */
public class TagKit {

	final static Pattern FORMAT_PATTERN = Pattern.compile("\\$\\w*");
	final static Pattern SQL_FORMAT_PATTERN = Pattern.compile("\\$\\{\\w*\\}");
	
	/**
	 * 文本替换
	 * @param tplContent
	 * @param params
	 * @return
	 */
	public static String textMatchReplace(String tplContent, Map<String, ?> params) {
		if(params == null || params.isEmpty()) {
			return tplContent;
		}
		Matcher matcher = FORMAT_PATTERN.matcher(tplContent);
		while (matcher.find()) {
			String group = matcher.group();
			String key = group.substring(1, group.length());
			if (params.containsKey(key)) {
				tplContent = tplContent.replace(group, params.get(key).toString());
			}
		}
		return tplContent;
	}
	
	/**
	 * 
	 * @param tplContent
	 * @param params
	 * @return
	 */
	public static String sqlMatchReplace(String tplContent, Map<String, String> params) {
		Matcher matcher = SQL_FORMAT_PATTERN.matcher(tplContent);
		while (matcher.find()) {
			String group = matcher.group();
			String key = group.substring(2, group.length() - 1);
			if (params.containsKey(key)) {
				tplContent = tplContent.replace(group, params.get(key));
			}
		}
		return tplContent;
	}
}
