commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yunfeng Hou <houy...@yahoo.com>
Subject Fwd: enhancement to digester
Date Thu, 30 Jan 2003 10:22:25 GMT
Commons digester is very powerful, and it is rule
centered. It makes digester very useful, but at the
same time, not a easy job to write the rules. To deal
with a complex XML file, we will have to write many
rules for each class and it's properties, methods. I
feel it not very flexible and hard to mantain. What I
need is the following:
1. Using a xml config file to define the relationship
between Java Class and XML element. (XML rule is a
step forward, but not enough)
2. As simple as castor xml mapping, but still have the
power of digester.

With the tool I write myself, marshal an XML document
to a java object could be much easier. Here's the
sample.
I have an mapping file digester.xml.
=============================================
<mapping>
	<include>digester_inc.xml</include>

	<class name="net.yunfeng.digester.config.MappingInfo"
xmlName="mapping">
		<object xmlName="class"
type="net.yunfeng.digester.config.ClassInfo"
method="addClass"/>
		<method name="addInclude" xmlName="include"/>
	</class>

	<class name="net.yunfeng.digester.config.ClassInfo"
xmlName="class">
		<object
type="net.yunfeng.digester.config.PropertyInfo"
method="addProperty"/>
		<object type =
"net.yunfeng.digester.config.MethodInfo"
method="addMethod"/>
		<object
type="net.yunfeng.digester.config.ObjectInfo"
method="addObject"/>
	</class>
	
	
	<class name="net.yunfeng.digester.config.Methodnfo"
xmlName="method">
		<object type="net.yunfeng.digester.config.ParamInfo"
method="addParam"/>
	</class>
	
	
</mapping>
===============================================

and digester_inc.xml
===============================================
<mapping>
	<class
name="net.yunfeng.digester.config.PropertyInfo"
xmlName="property"/>
	<class name="net.yunfeng.digester.config.ObjectInfo"
xmlName="object"/>
	<class name="net.yunfeng.digester.config.ParamInfo"
xmlName="param"/>
</mapping>
=================================================
I can easily read these two files into my config
classes( I only list two of them)

MappingInfo.java
=================================================
public class MappingInfo {
	
  private ArrayList classes = new ArrayList();
  private ArrayList includes = new ArrayList();  
  // and the accessors of these two

  public void addClass(ClassInfo classInfo) {
    classes.add(classInfo);
  }

  public void addInclude(String include) {
    includes.add(include);
  }
}	
================================================
MethodInfo.java
================================================
public class MethodInfo {
	
  private String name;
  private String xmlName;
  private ArrayList params = new ArrayList();
  // and their accessors

  public void addParam(ParamInfo param) {
    params.add(param);
  }
}
================================================

The code to digest it is:
================================================
URL file =
DigesterHelper.class.getClassLoader().getResource("net/yunfeng/digester/digester.xml");
DigesterHelper helper = new DigesterHelper(file);
MappingInfo mappingInfo = (MappingInfo)
helper.load(file, MappingInfo.class);

================================================
The first statement is get a mapping file;
second statement is create a helper object and have it
load the mapping file to later use;
the last statement is to digester an xml file into an
specified java class. In this case, it happens to be
the same of the mapping file. :-)

Please note that you do not need to specify all the
properties of a class within the mapping file. The
tool will automatically add rules for all properties
of a class. So if the property has the same name as
the element name of XML, you can just leave the tool
handle it.

If you think it worth putting it into the code base,
will be more than happy to help.

Yunfeng Hou	


_________________________________________________________
Do You Yahoo!? 
"雅虎节日大转盘惊喜不断 快乐节日好礼连连"
http://cn.promo.yahoo.com/holiday/
Mime
View raw message