Digester中正则表达式的使用

  Digester转换xml到bean是比较方便的,但一个很常见的问题是,有时候同一种元素会出现在多个父元素下面,每次为它重写一遍映射的代码当然可以,但太麻烦了,有没有用正则表达式的方式匹配一下这种元素,达到只写一遍的目的呢,于是经过一番探索和邮件列表里的讨论,可以使用RegexRules、SimpleRegexMatcher来完成,不得不说,Digester的文档写得太差了,从文档里找不到任何使用它的方法。

 

xml:

config.xml

 

   这个xml里有Mutable和Constant元素是可以在main或sub元素下同时出现的,而且对于TextTable元素,也可以换成ImgTable,下面的结构完全一样,这段xml是我从项目中摘出来的,作用是描述一些数据到表的映射,有文本表,图片表。

   先看一下不用正则的方式如何写Constant和Mutable元素的映射:

d.addObjectCreate("Class/TextTable/Main/Constant",ConstantProp.class); d.addSetNext("Class/TextTable/Main/Constant","addConstantProp"); d.addSetProperties("Class/TextTable/Main/Constant"); d.addObjectCreate("Class/TextTable/Main/Mutable",MutableProp.class); d.addSetNext("Class/TextTable/Main/Mutable","addMutableProp"); d.addSetProperties("Class/TextTable/Main/Mutable"); d.addObjectCreate("Class/TextTable/Sub/Constant",ConstantProp.class); d.addSetNext("Class/TextTable/Sub/Constant","addConstantProp"); d.addSetProperties("Class/TextTable/Sub/Constant"); d.addObjectCreate("Class/TextTable/Sub/Mutable",MutableProp.class); d.addSetNext("Class/TextTable/Sub/Mutable","addMutableProp"); d.addSetProperties("Class/TextTable/Sub/Mutable");

 对于TextTable一种,要分别写sub和main下面的,需要12行来完成,如果再加一种表ImgTable,又要12行,再加N个表,就是12N,非常恐怖吧。如果用正则表达式怎么写呢?

d.addObjectCreate("Class/*/Constant",ConstantProp.class); d.addSetNext("Class/*/Constant","addConstantProp"); d.addSetProperties("Class/*/Constant"); d.addObjectCreate("Class/*/Mutable",MutableProp.class); d.addSetNext("Class/*/Mutable","addMutableProp"); d.addSetProperties("Class/*/Mutable");

无论添加什么类型的表元素,就这6行就够了,因为无论哪种表,都是一样的结构,爽吧。

贴出用到的所有类。

 ClassItem.java

package com.siamon.model.config; import java.util.ArrayList; import java.util.List; public class ClassItem { private String method; private String name; private List tableMapList=new ArrayList(); public ClassItem(String name, String method, List tableMapList) { this.name = name; this.method = method; this.tableMapList = tableMapList; } public ClassItem() { } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void addTableMap(TableMap tm){ getTableMapList().add(tm); } public List getTableMapList() { return tableMapList; } }

ConstantProp.java

package com.siamon.model.config; public class ConstantProp { private String column; private String value; public String getColumn() { return column; } public void setColumn(String column) { this.column = column; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "ConstantProp [ " + "column='" + column + '/'' + ", value='" + value + '/'' + " ] "; } }

MutableProp.java

package com.siamon.model.config; public class MutableProp { private String property; private int argIndex; private String column; public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } public int getArgIndex() { return argIndex; } public void setArgIndex(int argIndex) { this.argIndex = argIndex; } public String getColumn() { return column; } public void setColumn(String column) { this.column = column; } public String toString() { return "MutableProp [ " + "property='" + property + '/'' + ", argIndex=" + argIndex + ", column='" + column + '/'' + " ] "; } }

 

 TableMap.java

package com.siamon.model.config; import java.util.ArrayList; import java.util.List; public class TableMap { private String type; private String handler; private List constantPropList=new ArrayList(); private List mutablePropList=new ArrayList(); public String getType() { return type; } public void setType(String type) { this.type = type; } public List getMutablePropList() { return mutablePropList; } public List getConstantPropList() { return constantPropList; } public void addMutableProp(MutableProp prop){ getMutablePropList().add(prop); } public void addConstantProp(ConstantProp prop){ getConstantPropList().add(prop); } public String getHandler() { return handler; } public void setHandler(String handler) { this.handler = handler; } }

解析类ConfigManager.java

package com.siamon.config; import java.io.IOException; import com.siamon.model.config.*; import org.apache.commons.digester.Digester; import org.apache.commons.digester.RegexRules; import org.apache.commons.digester.SimpleRegexMatcher; import org.xml.sax.SAXException; public class ConfigManager { public static void main(String[] args) throws IOException, SAXException { Digester d = new Digester(); d.setRules(new RegexRules(new SimpleRegexMatcher())); d.addObjectCreate("Class", ClassItem.class); d.addSetProperties("Class"); d.addObjectCreate("Class/TextTable",TableMap.class); d.addSetProperties("Class/*/Handler","class","handler"); d.addSetNext("Class/TextTable","addTableMap"); d.addSetProperties("Class/TextTable"); d.addObjectCreate("Class/*/Constant",ConstantProp.class); d.addSetNext("Class/*/Constant","addConstantProp"); d.addSetProperties("Class/*/Constant"); d.addObjectCreate("Class/*/Mutable",MutableProp.class); d.addSetNext("Class/*/Mutable","addMutableProp"); d.addSetProperties("Class/*/Mutable"); d.addCallMethod("Class/TextTable","setType",1); d.addObjectParam("Class/TextTable",0,"txt"); d.parse(ConfigManager.class.getResource("/config.xml")); ClassItem ci=(ClassItem)d.getRoot(); System.out.println("bye bye"); } }

运行时的截图:

 

   Digester的基本使用请参见我的另一篇文章,http://blog.csdn.net/theoffspring/archive/2011/03/11/6241390.aspx, 听说Digester 3也快出来了,现在正在sandbox阶段,正在测试,支持if/else这样条件表达式,希望越来越好吧。

 

你可能感兴趣的:(正则表达式,string,class,xml,sandbox,encoding)