//过滤特殊字符
public static String encoding(String src){
if (src==null)
return "";
StringBuilder result=new StringBuilder();
if (src!=null){
src=src.trim();
for (int pos=0;pos<src.length();pos++){
switch(src.charAt(pos)){
case '\"':result.append(""");break;
case '<':result.append("<");break;
case '>':result.append(">");break;
case '\'':result.append("'");break;
case '&':result.append("&");break;
case '%':result.append("&pc;");break;
case '_':result.append("&ul;");break;
case '#':result.append("&shap;");break;
case '?':result.append("&ques;");break;
default:result.append(src.charAt(pos));break;
}
}
}
return result.toString();
}
//反过滤特殊字符
public static String decoding(String src){
if (src==null)
return "";
String result=src;
result=result.replace(""", "\"").replace("'", "\'");
result=result.replace("<", "<").replace(">", ">");
result=result.replace("&", "&");
result=result.replace("&pc;", "%").replace("&ul", "_");
result=result.replace("&shap;", "#").replace("&ques", "?");
return result;
}
//利用反射调用一个继承层次上的函数族,比如安装程序,有安装数据库的,安装文件系统的等,命名均已“install”开始,你就可以将参数part设为“install”,src是其实类实例,root是终止父类
public static <T> void invokeMethods(String part,T src,Class root) throws ExceptionManager{
if (root!=null){
if (!root.isInstance(src))return;
root=(Class)root.getGenericSuperclass();
}
HashMap<String,Method> invokees=new HashMap<String,Method>();
Class target=src.getClass();
do{
Method [] methods=target.getDeclaredMethods();
for (Method method:methods){
String mn=method.getName();
Boolean isPass=mn.startsWith(part);
if (isPass){
Integer nopt=method.getParameterTypes().length;
Boolean isStatic=Modifier.isStatic(method.getModifiers());
if ((nopt==0)&&(!isStatic)){
if (!invokees.containsKey(mn))
invokees.put(mn, method);
}
}
}
target=(Class)target.getGenericSuperclass();
}while(target!=root);
Iterator<String> methods=invokees.keySet().iterator();
while (methods.hasNext()){
Method invokee=invokees.get(methods.next());
Boolean access=invokee.isAccessible();
invokee.setAccessible(true);
try {
invokee.invoke(src);
} catch (InvocationTargetException e) {
throw ExceptionManager.wrap(e.getTargetException());
}catch (Exception e){}
invokee.setAccessible(access);
}
}