admin 管理员组文章数量: 1184232
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 loop
final 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 evaluation
break;
}
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 anything
expression = 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 loop
final 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 evaluation
break;
}
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 anything
expression = 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);
}
}
版权声明:本文标题:深入探讨CDA配置文件中的消息CDA调试技巧 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1770767703a3537357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论