java 代码:
ChartRegistry:
public
class
ChartRegistry
{
List<ChartConfig> registry = new ArrayList<ChartConfig>();
public void addChart(ChartConfig obj)
{
registry.add(obj);
}
public java.util.List<ChartConfig> getRegistry()
{
return registry;
}
public String toString() {
return ReflectionToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
{
List<ChartConfig> registry = new ArrayList<ChartConfig>();
public void addChart(ChartConfig obj)
{
registry.add(obj);
}
public java.util.List<ChartConfig> getRegistry()
{
return registry;
}
public String toString() {
return ReflectionToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
图表设定器ChartConfig:
public
class
ChartConfig
implements
Comparable
{
private String id;
private boolean legendVisible;
private String type;
private int width;
private String description;
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public boolean isLegendVisible()
{
return legendVisible;
}
public void setLegendVisible(boolean legendVisible)
{
this.legendVisible = legendVisible;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public int getWidth()
{
return width;
}
public void setWidth(int width)
{
this.width = width;
}
public String toString() {
return ReflectionToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
}
public int compareTo(Object o)
{
if(o instanceof ChartConfig){
ChartConfig o1 = (ChartConfig)o;
return this.id.compareTo(o1.id);
}
return -1;
}
}
{
private String id;
private boolean legendVisible;
private String type;
private int width;
private String description;
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public boolean isLegendVisible()
{
return legendVisible;
}
public void setLegendVisible(boolean legendVisible)
{
this.legendVisible = legendVisible;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public int getWidth()
{
return width;
}
public void setWidth(int width)
{
this.width = width;
}
public String toString() {
return ReflectionToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
}
public int compareTo(Object o)
{
if(o instanceof ChartConfig){
ChartConfig o1 = (ChartConfig)o;
return this.id.compareTo(o1.id);
}
return -1;
}
}
图表数据设定(简化版)charts.xml:
<?
xml version="1.0" encoding="GBK"
?>
< charts >
< chart id ="BarChart1" >
< legendVisible > false </ legendVisible >
< type > 中国 </ type >
< width > 500 </ width >
< description > 11 </ description >
</ chart >
< chart id ="BarChart2" >
< legendVisible > true </ legendVisible >
< type > 美国 </ type >
< width > 500 </ width >
< description > 22 </ description >
</ chart >
</ charts >
< charts >
< chart id ="BarChart1" >
< legendVisible > false </ legendVisible >
< type > 中国 </ type >
< width > 500 </ width >
< description > 11 </ description >
</ chart >
< chart id ="BarChart2" >
< legendVisible > true </ legendVisible >
< type > 美国 </ type >
< width > 500 </ width >
< description > 22 </ description >
</ chart >
</ charts >
配置规则设定rule.xml:
<?
xml version='1.0'
?>
<! DOCTYPE digester-rules
PUBLIC "-//Jakarta Apache //DTD digester-rules XML V1.0//EN"
"file:///../digester/dtds/digester-rules.dtd" >
< digester-rules >
< pattern value ="charts" > <!-- 遇上charts节点时 -->
< object-create-rule classname ="study.jakarta.commons.digester.ChartRegistry" /> <!-- 创建对象 -->
< set-properties-rule /> <!-- 设置属性 -->
< pattern value ="chart" > <!-- 遇上charts节点时 -->
< object-create-rule classname ="study.jakarta.commons.digester.ChartConfig" /> <!-- 创建对象 -->
< set-next-rule methodname ="addChart" paramtype ="study.jakarta.commons.digester.ChartConfig" /> <!-- 调用定对象方法 -->
< set-properties-rule /> <!-- 设置属性 -->
< bean-property-setter-rule pattern ="legendVisible" /> <!-- 调用对象的setter方法 -->
< bean-property-setter-rule pattern ="type" />
< bean-property-setter-rule pattern ="width" />
< bean-property-setter-rule pattern ="description" />
</ pattern >
</ pattern >
</ digester-rules >
<! DOCTYPE digester-rules
PUBLIC "-//Jakarta Apache //DTD digester-rules XML V1.0//EN"
"file:///../digester/dtds/digester-rules.dtd" >
< digester-rules >
< pattern value ="charts" > <!-- 遇上charts节点时 -->
< object-create-rule classname ="study.jakarta.commons.digester.ChartRegistry" /> <!-- 创建对象 -->
< set-properties-rule /> <!-- 设置属性 -->
< pattern value ="chart" > <!-- 遇上charts节点时 -->
< object-create-rule classname ="study.jakarta.commons.digester.ChartConfig" /> <!-- 创建对象 -->
< set-next-rule methodname ="addChart" paramtype ="study.jakarta.commons.digester.ChartConfig" /> <!-- 调用定对象方法 -->
< set-properties-rule /> <!-- 设置属性 -->
< bean-property-setter-rule pattern ="legendVisible" /> <!-- 调用对象的setter方法 -->
< bean-property-setter-rule pattern ="type" />
< bean-property-setter-rule pattern ="width" />
< bean-property-setter-rule pattern ="description" />
</ pattern >
</ pattern >
</ digester-rules >
跑一下试试看:
public
class
DigesterTest
{
public static void main(String[] args)
{
try
{
String charts = "F:\\计算机\\jakarta-commons\\jakarta-commons-cookbook-code\\examples\\study\\jakarta\\commons\\digester\\charts.xml";
String rule="F:\\计算机\\jakarta-commons\\jakarta-commons-cookbook-code\\examples\\study\\jakarta\\commons\\digester\\rules.xml";
DigesterTest test= new DigesterTest();
System.out.println("======================= getChartRegistryWithCodeRule ==========================");
ChartRegistry c= test.getChartRegistryWithCodeRule(new File(charts));
System.out.println(c);
System.out.println("======================= getChartRegistryWithCodeRule ==========================");
System.out.println(test.getChartRegistryWithFileRule(new File(charts),new File(rule)));
}
catch(Exception e)
{
e.printStackTrace();
}
}
//代码中设定规则
private ChartRegistry getChartRegistryWithCodeRule(File charts) throws IOException, SAXException{
InputStream is = new FileInputStream(charts);
Digester digester = new Digester();
digester.setValidating(false);//不进行XML与相应的DTD的合法性验证
digester.push(new ChartRegistry()); //创建一个ChartRegistry对象, 你这样用这种形式建立顶对象(选用这种方式,注释下一行)
//digester.addObjectCreate("charts", ChartRegistry.class); //创建一个ChartRegistry对象, 你也这样用这种形式建立顶对象(选用这种方式,注释上一行)
digester.addSetProperties("charts");
digester.addObjectCreate("charts/chart", ChartConfig.class); //当遇到<chart>时创建一个ChartConfig对象
digester.addSetProperties("charts/chart"); //根据<chart>元素的属性(attribute),对刚创建的ChartConfig对象的属性(property)进行设置
digester.addBeanPropertySetter("charts/chart/legendVisible");//当遇到<chart>的子元素<legendVisible>时将子元素值设置为ChartConfig对象的legendVisible属性的setter方法
digester.addBeanPropertySetter("charts/chart/type"); //参考上面注释
digester.addBeanPropertySetter("charts/chart/width"); //参考上面注释
digester.addBeanPropertySetter("charts/chart/description"); //参考上面注释
digester.addSetNext("charts/chart", "addChart"); //当再次遇到<charts>的子元素<chart>时调用顶对象的addChart方法。
ChartRegistry c = (ChartRegistry)digester.parse(is); //分析结束后,返回根元素
Collections.sort(c.getRegistry());
return c;
}
//配置中设定规则
private ChartRegistry getChartRegistryWithFileRule(File charts,File rule) throws IOException, SAXException{
Digester digester = DigesterLoader.createDigester(rule.toURL());
ChartRegistry c = (ChartRegistry)digester.parse(charts);
Collections.sort(c.getRegistry());
return c;
}
}
{
public static void main(String[] args)
{
try
{
String charts = "F:\\计算机\\jakarta-commons\\jakarta-commons-cookbook-code\\examples\\study\\jakarta\\commons\\digester\\charts.xml";
String rule="F:\\计算机\\jakarta-commons\\jakarta-commons-cookbook-code\\examples\\study\\jakarta\\commons\\digester\\rules.xml";
DigesterTest test= new DigesterTest();
System.out.println("======================= getChartRegistryWithCodeRule ==========================");
ChartRegistry c= test.getChartRegistryWithCodeRule(new File(charts));
System.out.println(c);
System.out.println("======================= getChartRegistryWithCodeRule ==========================");
System.out.println(test.getChartRegistryWithFileRule(new File(charts),new File(rule)));
}
catch(Exception e)
{
e.printStackTrace();
}
}
//代码中设定规则
private ChartRegistry getChartRegistryWithCodeRule(File charts) throws IOException, SAXException{
InputStream is = new FileInputStream(charts);
Digester digester = new Digester();
digester.setValidating(false);//不进行XML与相应的DTD的合法性验证
digester.push(new ChartRegistry()); //创建一个ChartRegistry对象, 你这样用这种形式建立顶对象(选用这种方式,注释下一行)
//digester.addObjectCreate("charts", ChartRegistry.class); //创建一个ChartRegistry对象, 你也这样用这种形式建立顶对象(选用这种方式,注释上一行)
digester.addSetProperties("charts");
digester.addObjectCreate("charts/chart", ChartConfig.class); //当遇到<chart>时创建一个ChartConfig对象
digester.addSetProperties("charts/chart"); //根据<chart>元素的属性(attribute),对刚创建的ChartConfig对象的属性(property)进行设置
digester.addBeanPropertySetter("charts/chart/legendVisible");//当遇到<chart>的子元素<legendVisible>时将子元素值设置为ChartConfig对象的legendVisible属性的setter方法
digester.addBeanPropertySetter("charts/chart/type"); //参考上面注释
digester.addBeanPropertySetter("charts/chart/width"); //参考上面注释
digester.addBeanPropertySetter("charts/chart/description"); //参考上面注释
digester.addSetNext("charts/chart", "addChart"); //当再次遇到<charts>的子元素<chart>时调用顶对象的addChart方法。
ChartRegistry c = (ChartRegistry)digester.parse(is); //分析结束后,返回根元素
Collections.sort(c.getRegistry());
return c;
}
//配置中设定规则
private ChartRegistry getChartRegistryWithFileRule(File charts,File rule) throws IOException, SAXException{
Digester digester = DigesterLoader.createDigester(rule.toURL());
ChartRegistry c = (ChartRegistry)digester.parse(charts);
Collections.sort(c.getRegistry());
return c;
}
}
结果输出:
=======================
getChartRegistryWithCodeRule
==========================
study.jakarta.commons.digester.ChartRegistry@16df84b[
registry = [study.jakarta.commons.digester.ChartConfig@1aa57fb[
id = BarChart1
legendVisible = false
type = 中国
width = 500
description = 11
], study.jakarta.commons.digester.ChartConfig@763f5d[
id = BarChart2
legendVisible = true
type = 美国
width = 500
description = 22
]]
]
======================= getChartRegistryWithCodeRule ==========================
study.jakarta.commons.digester.ChartRegistry@18f6235[
registry = [study.jakarta.commons.digester.ChartConfig@78a212[
id = BarChart1
legendVisible = false
type = 中国
width = 500
description = 11
], study.jakarta.commons.digester.ChartConfig@1d85f79[
id = BarChart2
legendVisible = true
type = 美国
width = 500
description = 22
]]
]
study.jakarta.commons.digester.ChartRegistry@16df84b[
registry = [study.jakarta.commons.digester.ChartConfig@1aa57fb[
id = BarChart1
legendVisible = false
type = 中国
width = 500
description = 11
], study.jakarta.commons.digester.ChartConfig@763f5d[
id = BarChart2
legendVisible = true
type = 美国
width = 500
description = 22
]]
]
======================= getChartRegistryWithCodeRule ==========================
study.jakarta.commons.digester.ChartRegistry@18f6235[
registry = [study.jakarta.commons.digester.ChartConfig@78a212[
id = BarChart1
legendVisible = false
type = 中国
width = 500
description = 11
], study.jakarta.commons.digester.ChartConfig@1d85f79[
id = BarChart2
legendVisible = true
type = 美国
width = 500
description = 22
]]
]