得到CDA properties 的配置文件

来源:互联网 发布:阿尔弗雷德大学 知乎 编辑:程序博客网 时间:2024/06/05 06:33

ProperConfigUtil.java

package com.*.util.config;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Hashtable;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Properties;import java.util.Set;import org.apache.log4j.Logger;import com.*.util.string.StringMatch;/** * 得到CDA properties 的配置文件 * @author Administrator * */public class ProperConfigUtil {private static Logger log = Logger.getLogger(ProperConfigUtil.class);private static Map<String, String> map = new Hashtable<String, String>();static Properties properties = new Properties();static {InputStream in = ProperConfigUtil.class.getClassLoader().getResourceAsStream("CommXConfig.properties");try {properties.load(in);} catch (IOException e) {log.error("初始化Config失败", e);}Set<Entry<Object, Object>> set = properties.entrySet();for (Entry<Object, Object> o : set) {String tempKey = o.getKey().toString();String value = o.getValue().toString();map.put(tempKey,value);    }}/** * 得到key的配置文件 * @param key * @param map 匹配$[XXX] * @return */public static String getConfigByKey(String key, Map map) {String value = properties.getProperty(key);if (map != null&&!map.isEmpty()) {value = StringMatch.translateParam(value, map);}//log.debug(key+"取得值--->"+value);return value;}/** * 得到key的配置文件 * @param key * @param list 匹配$[0] $[1]$[2] 从0开始 * @return */public static String getConfigByKey(String key,List<Object> list) {String value = properties.getProperty(key);if (list != null) {Map<String,String> map = new HashMap<String, String>();int list_length = list.size();for(int i=0;i<list_length;i++){map.put(String.valueOf(i), list.get(i).toString());}value = getConfigByKey(key,map);}log.debug(key+"取得值--->"+value);return value;}/** * 得到key的配置文件 * @param key * @param objs 匹配 $[1] $[2] * @return */public static String getConfigByKey(String key,Object... objs) {String value = properties.getProperty(key);if (objs != null) {Map<String,String> map = new HashMap<String, String>();int i = 0;for(Object obj:objs){map.put(String.valueOf(i), obj.toString());}value = getConfigByKey(key,map);}log.debug(key+"取得值--->"+value);return value;}public static String getConfigByKey(String key)   {return getConfigByKey(key, new HashMap());}public static void setConfig(String key, String value){properties.setProperty(key, value);}/** * 返回以map形式的数据 * @return */public static Map<String, String> getGlobalMap() {return map;}}

StringMatch.java

package com.*.util.string;import java.util.Map;import org.apache.commons.lang.StringUtils;public class StringMatch { public static String translateParamtwo(String expression, char[] openChars,Map map) {int maxLoopCount = 1;String result = expression;for (char open : openChars) {int loopCount = 1;int pos = 0;// this creates an implicit StringBuffer and shouldn't be used in// the inner loopfinal String lookupChars = open + "{";while (true) {int start = expression.indexOf(lookupChars, pos);if (start == -1) {pos = 0;loopCount++;start = expression.indexOf(lookupChars);}if (loopCount > maxLoopCount) {// translateVariables prevent infinite loop / expression// recursive evaluationbreak;}int length = expression.length();int x = start + 2;int end;char c;int count = 1;while (start != -1 && x < length && count != 0) {c = expression.charAt(x++);if (c == '{') {count++;} else if (c == '}') {count--;}}end = x - 1;if ((start != -1) && (end != -1) && (count == 0)) {String var = expression.substring(start + 2, end);Object obj = map.get(var);String o ="";if(obj instanceof String[]){o=((String[])obj)[0];}if(obj instanceof String){o=(String) obj;}String left = expression.substring(0, start);String right = expression.substring(end + 1);String middle = null;if (o != null) {middle = o.toString();if (StringUtils.isEmpty(left)) {result = o;} else {result = left.concat(middle);}if (StringUtils.isNotEmpty(right)) {result = result.toString().concat(right);}expression = left.concat(middle).concat(right);} else {// the variable doesn't exist, so don't display anythingexpression = left.concat(right);result = expression;}pos = (left != null && left.length() > 0 ? left.length() - 1: 0)+ (middle != null && middle.length() > 0 ? middle.length() - 1 : 0) + 1;pos = Math.max(pos, 1);} else {break;}}}return result;}/** * 匹配$[XXX] * @param expression * @param openChars * @param map * @return */public static String translateParam(String expression, char[] openChars,Map map) {int maxLoopCount = 1;String result = expression;for (char open : openChars) {int loopCount = 1;int pos = 0;// this creates an implicit StringBuffer and shouldn't be used in// the inner loopfinal String lookupChars = open + "[";while (true) {int start = expression.indexOf(lookupChars, pos);if (start == -1) {pos = 0;loopCount++;start = expression.indexOf(lookupChars);}if (loopCount > maxLoopCount) {// translateVariables prevent infinite loop / expression// recursive evaluationbreak;}int length = expression.length();int x = start + 2;int end;char c;int count = 1;while (start != -1 && x < length && count != 0) {c = expression.charAt(x++);if (c == '[') {count++;} else if (c == ']') {count--;}}end = x - 1;if ((start != -1) && (end != -1) && (count == 0)) {String var = expression.substring(start + 2, end);Object obj = map.get(var);String o ="";if(obj instanceof String[]){o=((String[])obj)[0];}if(obj instanceof String){o=(String) obj;}String left = expression.substring(0, start);String right = expression.substring(end + 1);String middle = null;if (o != null) {middle = o.toString();if (StringUtils.isEmpty(left)) {result = o;} else {result = left.concat(middle);}if (StringUtils.isNotEmpty(right)) {result = result.toString().concat(right);}expression = left.concat(middle).concat(right);} else {// the variable doesn't exist, so don't display anythingexpression = left.concat(right);result = expression;}pos = (left != null && left.length() > 0 ? left.length() - 1: 0)+ (middle != null && middle.length() > 0 ? middle.length() - 1 : 0) + 1;pos = Math.max(pos, 1);} else {break;}}}return result;}/** * 匹配${XXX} * @param expression * @param openChars * @param map * @return */public static String translateParam(String expression,Map map) {if(map==null){return expression;}return translateParamtwo(expression, new char[] { '$' },map);}/** * 全局匹配 匹配$[XXX] * @param expression * @param map * @return */public static String translateParamGlobal(String expression,Map map) {if(map==null){return expression;}return translateParam(expression, new char[] { '$' },map);}/** * 截取字符串前面的正整数,如"22天"得"天","18个人"得到"个人". * @return */public static String getQuantity(String regular){int index = 0;for (int i = 0; i < regular.length(); i++) {char c = regular.charAt(i);if (Character.isDigit(c)) {if (i == regular.length() - 1) {index = i + 1;} else {index = i;}continue;} else {index = i;break;}}return regular.substring(index, regular.length());}/** * 去除字符串中间的.和空格 * @param regular * @return */public static String rmStringPiont(String regular){regular = regular.replace(".", "");regular = regular.replace(" ", "");return regular;}//测试public static void main(String[] args) {String str = "22天ssdf";String str2 = getQuantity(str);str2 = rmStringPiont(str2);System.out.println(str2);}}


0 0