物采系统中,读取*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/validator") public class ExpImpValidator { /** The type. */ @SetProperty(attributeName = "type", pattern = "validators/header/validator") private String type; @BeanPropertySetter( pattern = "validators/header/validator/className" ) private String className; @BeanPropertySetter( pattern = "validators/header/validator/filedName" ) private String filedName; @BeanPropertySetter( pattern = "validators/header/validator/methodName" ) private String methodName; @BeanPropertySetter( pattern = "validators/header/validator/reg" ) private String reg; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getFiledName() { return filedName; } public void setFiledName(String filedName) { this.filedName = filedName; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public String getReg() { return reg; } public void setReg(String reg) { this.reg = reg; } }
注:可以看到在java类里出现了@SetProperty标签和@BeanPropertySetter标签,前者对应的是形如:
<id value="abc" />这样的映射,后者对应的是形如:<id>abc</id>这样的映射。
3.在main函数中启动:
package com.cnpc.pms.exp.inf.dto; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.commons.digester.Digester; import org.apache.commons.digester.annotations.DigesterLoader; import org.apache.commons.digester.annotations.DigesterLoaderBuilder; import org.springframework.core.io.Resource; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import com.cnpc.pms.base.util.ConfigurationUtil; public class Index { public static final String DEFAULT_CONFIG_LOCATION = "/conf/expinfo-import-validate-rules.xml"; public static void main(String[] args) { Resource[] resources = ConfigurationUtil .getAllResources(DEFAULT_CONFIG_LOCATION); if (resources != null) { for (int i = 0; i < resources.length; i++) { Resource resource = resources[i]; try { ExpImpValContext expImpValContext = parseXMLObject(ExpImpValContext.class, resource); List<ExpImpValHeader> list = expImpValContext.getHeaders(); list.iterator(); } catch (IOException e) { } catch (SAXException e) { } } } } public static <T> T parseXMLObject(Class<T> clazz, Resource resource) throws IOException, SAXException { InputStream is = resource.getInputStream(); InputSource inputSource = new InputSource(is); DigesterLoader digesterLoader = new DigesterLoaderBuilder().useDefaultAnnotationRuleProviderFactory() .useDefaultDigesterLoaderHandlerFactory(); Digester digester = digesterLoader.createDigester(clazz); return (T) digester.parse(inputSource); } }