/*** Copyright (C) 2009 numenzq studio. All Rights Reserved.*/
packagecom.linkstec.mot.job.util;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.lang.reflect.InvocationTargetException;importjava.math.BigDecimal;importjava.util.ArrayList;importjava.util.List;importorg.apache.commons.beanutils.PropertyUtils;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;importcom.linuxense.javadbf.DBFField;importcom.linuxense.javadbf.DBFReader;importcom.linuxense.javadbf.DBFWriter;/*** DBF 文件解析
**/
public classDbfUtil {private final static String CHARSET = "GB2312";/*** 读DBF文件
*@paramfile
*@paramclazz
*@paramtemplate
*@return*@throwsDocumentException
*@throwsInstantiationException
*@throwsIllegalAccessException
*@throwsInvocationTargetException
*@throwsNoSuchMethodException
*@throwsIOException*/
public static List readDbf(File file, Classclazz, InputStream template)throwsDocumentException,
InstantiationException, IllegalAccessException, InvocationTargetException,
NoSuchMethodException, IOException {
FileInputStream fis= newFileInputStream(file);
DBFReader reader= newDBFReader(fis);
reader.setCharactersetName(CHARSET);
List propertys =readTemplate(template);for(Element element : propertys){int fieldsCount =reader.getFieldCount();for (int i = 0; i < fieldsCount; i++) {
DBFField field=reader.getField(i);if(field.getName().equals(element.attributeValue("column"))){
element.addAttribute("index", i+"");break;
}
}
}
List records = new ArrayList();for (int i = 0; i < reader.getRecordCount(); i++) {//System.out.println(i+1 + "/" + reader.getRecordCount());
records.add(readLine(clazz, propertys, reader.nextRecord()));
}
fis.close();returnrecords;
}/*** 写DBF文件
*@parambeans
*@paramtemplate
*@return*@throwsDocumentException
*@throwsIllegalAccessException
*@throwsInvocationTargetException
*@throwsNoSuchMethodException
*@throwsIOException*/
public static byte[] writeDbf(List extends Object> beans, InputStream template) throwsDocumentException,
IllegalAccessException, InvocationTargetException, NoSuchMethodException,
IOException {
List propertys =readTemplate(template);
DBFWriter writer= newDBFWriter();
writer.setCharactersetName(CHARSET);
writer.setFields(writeFields(propertys));for (int i = 0; i < beans.size(); i++) {
writer.addRecord(writeLine(beans.get(i), propertys));
}
ByteArrayOutputStream baos= newByteArrayOutputStream();
writer.write(baos);returnbaos.toByteArray();
}/*** 写DBF文件(方式2)
*@parambeans
*@paramtemplate
*@paramfile
*@throwsDocumentException
*@throwsIllegalAccessException
*@throwsInvocationTargetException
*@throwsNoSuchMethodException
*@throwsIOException*/
public static void writeDbf(List extends Object> beans, InputStream template,File file) throwsDocumentException,
IllegalAccessException, InvocationTargetException, NoSuchMethodException,
IOException {
List propertys =readTemplate(template);
DBFWriter writer= newDBFWriter(file);
writer.setCharactersetName(CHARSET);
writer.setFields(writeFields(propertys));for (int i = 0; i < beans.size(); i++) {
writer.addRecord(writeLine(beans.get(i), propertys));
}
writer.write();
}/*** SAX解析表结构模板
*@paramin
*@return*@throwsDocumentException*/@SuppressWarnings("unchecked")private static List readTemplate(InputStream in) throwsDocumentException {
SAXReader reader= newSAXReader();
Document document=reader.read(in);return (List)document.getRootElement().elements();
}/*** 读取一行数据
*@paramclazz
*@parampropertys
*@paramvalues
*@return*@throwsInstantiationException
*@throwsIllegalAccessException
*@throwsInvocationTargetException
*@throwsNoSuchMethodException*/
private static T readLine(Class clazz, Listpropertys, Object[] values)throwsInstantiationException, IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
T bean=clazz.newInstance();for (int i = 0; i < propertys.size(); i++) {
Element property=propertys.get(i);if(property.attributeValue("index") != null && property.attributeValue("index").length() > 0){int index = Integer.parseInt(property.attributeValue("index"));
Object value=values[index];if (property.attributeValue("type").equals("C")) {
value=((String) value).trim();
}else if (property.attributeValue("type").equals("N")) {//if (property.attributeValue("scale")!=null && !"".equals(property.attributeValue("scale"))) {
value = newBigDecimal(((Double) value).doubleValue());//}else{//value = ((Double) value).longValue();//}
}//BeanUtil.copyProperty(bean, property.attributeValue("name"), value);
PropertyUtils.setProperty(bean, property.attributeValue("name"), value);
}
}returnbean;
}/*** 写一行数据
*@parambean
*@parampropertys
*@return*@throwsIllegalAccessException
*@throwsInvocationTargetException
*@throwsNoSuchMethodException*/
private static Object[] writeLine(Object bean, Listpropertys)throwsIllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object[] row= newObject[propertys.size()];for (int i = 0; i < propertys.size(); i++) {
Element element=propertys.get(i);
row[i]= PropertyUtils.getProperty(bean, element.attributeValue("name"));
}returnrow;
}/*** 设置表结构
*@parampropertys
*@return
*/
private static DBFField[] writeFields(Listpropertys) {
DBFField[] fields= newDBFField[propertys.size()];for (int i = 0; i < propertys.size(); i++) {
Element property=propertys.get(i);
fields[i]= newDBFField();
fields[i].setName(property.attributeValue("column"));
fields[i].setDataType((byte) property.attributeValue("type").charAt(0));if (property.attributeValue("length") != null) {
fields[i].setFieldLength(Integer.parseInt(property.attributeValue("length")));
}if (property.attributeValue("scale") != null) {
fields[i].setDecimalCount(Integer.parseInt(property.attributeValue("scale")));
}
}returnfields;
}
}