日期:2014-05-16 浏览次数:20809 次
物采系统中,读取*query*.xml文件,使用了Apache的Digester来解析,我做的专家信息导入模块,也需要读取定义在XMl中的导入验证规则。原来使用的是ShiChao提供的SAX解析XML文件方法。现在改为用Digester来解析。
使用的是org.apache.commons.digester3.annotations下的包,annotations顾名思义就是注释模式,可参照:
http://commons.apache.org/digester/guide/annotations.html?上的样例。
我的做法具体如下:
1.我的导入规则验证的XML文件的内容:
?
<?xml version="1.0" encoding="UTF-8"?> <validators> <header name="出生日期"> <validator type="reg"> <reg>^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$</reg> </validator> </header> <header name="身份证*"> <validator type="reg"> <reg>^(\d{17})(\d|[xX])$</reg> </validator> <validator type="uniq"> <className>com.cnpc.pms.exp.inf.entity.ExpertInfo</className> <filedName>idCardNum</filedName> </validator> </header> <header name="推荐单位*"> <validator type="refer"> <className>com.cnpc.pms.bizbase.rbac.orgview.entity.PurStruOrg</className> <filedName>code</filedName> </validator> </header> <header name="评审专业*"> <validator type="custom"> <className>expImportExcelManager</className> <methodName>verifyCategory</methodName> </validator> </header> .................. </validators>
?2.由于使用的是Digester的annotations模式,所以需要建立几个与xml文件内容相互映射的java bean:
? (1).ExpImpValContext类:
package com.cnpc.pms.exp.inf.dto; import java.util.ArrayList; import java.util.List; import org.apache.commons.digester.annotations.rules.ObjectCreate; import org.apache.commons.digester.annotations.rules.SetNext; import com.cnpc.pms.base.query.model.PMSQuery; /** * @author Song Shitao * @since 2011/7/11 */ @ObjectCreate(pattern = "validators") public class ExpImpValContext { /** The queries. */ private List<ExpImpValHeader> headers = new ArrayList<ExpImpValHeader>(); /** * Adds the header. * * @param header * the header */ @SetNext public void addHeader(ExpImpValHeader header) { this.headers.add(header); } /** * Gets the headers. * * @return the headers */ public List<ExpImpValHeader> getHeaders() { return headers; } }
?(2).ExpImpValHeader类:
package com.cnpc.pms.exp.inf.dto; import java.util.ArrayList; import java.util.List; import org.apache.commons.digester.annotations.rules.ObjectCreate; import org.apache.commons.digester.annotations.rules.SetNext; import org.apache.commons.digester.annotations.rules.SetProperty; /** * @author Song Shitao * @since 2011/7/11 */ @ObjectCreate(pattern = "validators/header") public class ExpImpValHeader { /** The name. */ @SetProperty(attributeName = "name", pattern = "validators/header") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } /** The validators. */ private List<ExpImpValidator> validators = new ArrayList<ExpImpValidator>(); /** * Adds the validator. * * @param validator * the validator */ @SetNext public void addValidator(ExpImpValidator validator) { this.validators.add(validator); } /** * Gets the validators. * * @return the validators */ public List<ExpImpValidator> getValidators() { return validators; } }
?(3).ExpImpValidator类:
package com.cnpc.pms.exp.inf.dto; import org.apache.commons.digester.annotations.rules.BeanPropertySetter; import org.apache.commons.digester.annotations.rules.ObjectCreate; import org.apache.commons.digester.annotations.rules.SetProperty; /** * @author Song Shitao * @since 2011/7/11 */ @ObjectCreate(pattern = "validators/header/validato