将查询关键字加下颜色

将查询关键字加上颜色
package com.wzg.lore.util;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 文正光
 * @version 创建时间:Apr 12, 2011 1:30:35 PM 类说明 查询时给查询内容的关键字加上指定的颜色
 */
public class DecoratorSingleKeyword {

/**
* 给一个对象中的属性值中包含的一个关键字加上需要的颜色
* @param object
* @param keyword
* @param color
* @return
* @throws Exception
*/
public static Object handlerObject(Object object, String keyword,
String color) {
Class objClass = object.getClass();
Field[] fields = objClass.getDeclaredFields();
Field field = null;
PropertyDescriptor pd = null;
if (keyword != null && keyword.trim().length() != 0) {
for (int i = 0; i < fields.length; i++) {
field = fields[i];
if (!field.getName().equals("serialVersionUID")) {
try {
pd = new PropertyDescriptor(field.getName(), objClass);
String s = pd.getReadMethod().invoke(object,
new Object[] {}).toString();// 得到getXXX的值
// 对属性中包含的关键字加上指定的颜色
s = s.replaceAll(keyword, "<font color=\"" + color
+ "\">" + keyword + "</font>");
ConvertDataType.invorkMethod(field, object, pd
.getWriteMethod(), s);// 调用set方法重新赋值
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
return object;
}

/**
* 给一个容器中的每个对象的属性值中包含的一个关键字加上指定的颜色
* @param collection
* @param keyword
* @param color
* @return
*/
public static List handlerList(List list, String keyword, String color) {
List resultList = new ArrayList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
resultList.add(handlerObject(object, keyword, color));
}
return resultList;
}

}









package com.wzg.lore.util;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 文正光
 * @version 创建时间:Apr 13, 2011 12:12:43 AM 类说明
 */
public class DecoratorMultipleKeywords {

/**
* 给一个对象中的属性值中包含的多个关键字加上需要的颜色
* @param object
* @param color
* @param keywords
* @return
*/
public static Object handlerObject(Object object, String color,
String... keywords) {
Class objClass = object.getClass();
Field[] fields = objClass.getDeclaredFields();
Field field = null;
PropertyDescriptor pd = null;
for (String keyword : keywords) {
if (keyword != null && keyword.trim().length() != 0) {
for (int i = 0; i < fields.length; i++) {
field = fields[i];
if (!field.getName().equals("serialVersionUID")) {
try {
pd = new PropertyDescriptor(field.getName(),
objClass);
String s = pd.getReadMethod().invoke(object,
new Object[] {}).toString();// 得到getXXX的值
// 对属性中包含的关键字加上指定的颜色
s = s.replaceAll(keyword, "<font color=\"" + color
+ "\">" + keyword + "</font>");
ConvertDataType.invorkMethod(field, object, pd
.getWriteMethod(), s);// 调用set方法重新赋值
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
return object;
}

/**
* 给一个容器中的每个对象的属性值中包含的多个关键字加上指定的颜色
* @param list
* @param color
* @param keywords
* @return
*/
public static List handlerList(List list, String color, String... keywords) {
List resultList = new ArrayList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
resultList.add(handlerObject(object, color, keywords));
}
return resultList;
}
}








package com.wzg.lore.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Date;

public class ConvertDataType {

/**
* 对Date类型转换
* @param args
* @return
*/
public static Date toDate(String args) {
Date result = null;
if (null == args || "".equals(args))
result = null;
else
try {
result = DateFormat.getInstance().parse(args);
} catch (Exception e) {
result = null;
}
return result;
}

/**
* 对Timestamp类型转换
* @param args
* @return
*/
public static Timestamp toTimestamp(String args) {
Timestamp result = null;
if (null == args || "".equals(args))
result = null;
else
try {
result = Timestamp.valueOf(args);
} catch (Exception e) {
result = null;
}

return result;
}

/**
* 对Long类型转换
* @param args
* @return
*/
public static Long toLong(String args) {
Long result = null;
if (null == args || "".equals(args))
result = new Long(0);
else
try {
result = Long.parseLong(args);
} catch (Exception e) {
result = new Long(0);
}

return result;
}

/**
* 对Double类型转换
* @param args
* @return
*/
public static Double toDouble(String args) {
Double result = null;
if (null == args || "".equals(args))
result = new Double(0);
else
try {
result = Double.parseDouble(args);
} catch (Exception e) {
result = new Double(0);
}

return result;
}

/**
* 对Float类型转换
* @param args
* @return
*/
public static Float toFloat(String args) {
Float result = null;
if (null == args || "".equals(args))
result = new Float(0);
else
try {
result = Float.parseFloat(args);
} catch (Exception e) {
result = new Float(0);
}

return result;
}

/**
* 对Integer类型转换
* @param args
* @return
*/
public static Integer toInteger(String args) {
Integer result = null;
if (null == args || "".equals(args))
result = 0;
else {
try {
result = Integer.parseInt(args);
} catch (Exception e) {
result = 0;
}
}
return result;
}

/**
* 对Short类型转换
* @param args
* @return
*/
public static Short toShort(String args) {
Short result = null;
if (null == args || "".equals(args))
result = 0;
else {
try {
result = Short.parseShort(args);
} catch (Exception e) {
result = 0;
}
}
return result;
}

/**
* 重写了对String类型的转换
* @param args
* @return
*/
public static String toString(String args) {
String result = null;
if (null == args || "".equals(args))
result = "";
else
result = args.toString();
return result;
}
/**
* 根据字段的数据类型执行相应的set方法
* @param field
* @param object
* @param method
* @param value
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void invorkMethod(Field field, Object object, Method method,
String value) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
if (field.getType() == String.class) { // 处理当为String类型情况
method.invoke(object, new Object[] { toString(value) });
} else if (field.getType() == Integer.class) {// 处理当为Integer类型情况
method.invoke(object, new Object[] { toInteger(value) });
} else if (field.getType() == Float.class) {// 处理当为Float类型情况
method.invoke(object, new Object[] { toFloat(value) });
} else if (field.getType() == Double.class) {// 处理当为Double类型情况
method.invoke(object, new Object[] { toDouble(value) });
} else if (field.getType() == Long.class) {// 处理当为Long类型情况
method.invoke(object, new Object[] { toLong(value) });
} else if (field.getType() == int.class) {// 处理当为int类型情况
method.invoke(object, new Object[] { toInteger(value) });
} else if (field.getType() == float.class) {// 处理当为float类型情况
method.invoke(object, new Object[] { toFloat(value) });
} else if (field.getType() == double.class) {// 处理当为double类型情况
method.invoke(object, new Object[] { toDouble(value) });
} else if (field.getType() == Date.class) {// 处理当为Date类型情况
method.invoke(object, new Object[] { toTimestamp(value) });
} else if (field.getType() == short.class) {// 处理当为Short类型情况
method.invoke(object, new Object[] { toShort(value) });
} else if (field.getType() == Timestamp.class) {// 处理当为Timestamp类型情况
method.invoke(object, new Object[] { toTimestamp(value) });
}
}
}