XML and XSLT implementation code generator (V)
Generate a JDOM documentation
When we define the data model, the next step is to convert the data model to the JDOM document structure. There can be different design methods: You can generate a JDOM's ELEMENT for each Java class definition; you can also add a method to conversions in each Java class; you can also create a customization of a JDM Subclasses, in subclasses, constructor can incorporate data objects as parameters. No matter which method is used, consistency is the most important target [1]. Implement is very simple, as long as the object structure defined according to DTD is easy to generate the ELEMENT object of JDOM.
Package com.xs.xgen.javabean;
Import java.util. *;
Import Org.jdom. *;
Public class javabeanjdom {
Private javabeanjdom () {}
Public Static Element Product (Javabeandata Javabean) {
Element Java Benelement = New Element ("Javabean");
JavaBeaneElement.addContent (New Element ("name"). setText (javabean.getname ()))
JavaBeaneElement.addContent
Packagejdom.ProduceElement (javabean.getpackagedata));
Iterator importitit = javabean.getimplement (). Iterator ();
While (Implementit.hasNext ()) {
JavaBeaneElement.addContent ("IMPLEMENT"). setxt (String) Implementit.next ()))));
}
Iterator property = javabean.getpropertydata (). Iterator ();
While (property.hasnext ()) {
JavaBeaneElement.addContent
Propertyjdom.ProduceElement ((PropertyData) Propertyit.next ()))
}
Return javabeanelement;
}
List 2.7 javabeanjdom.java
Public class propertyjdom {
Private propertyjdom () {}
Public Static Element Product (PropertyData PropertyData) {
ELEMENT PropertyElement = New Element ("Property");
AddPropertyAttributes (PropertyElement, PropertyData);
PropertyElement.addContent
New element ("name"). setText (propertyData.getname ())));
Iterator Exceptionit = propertyData.Getexceptions (). Iterator ();
While (Exceptionit.hasNext ()) {
PropertyElement.addContent
New element ("exception"). setText (String) Exceptionit.next ())));
}
Return PropertyElement;
Private Static Void AddpropertyAttributes (Element Property, PropertyData DATA) {
Property.Addattribute ("Type", (String) Data.GetaTRibutes (). get ("type"));
Property.Addattribute ("Access", (String) Data.GetaTRibutes (). Get ("Access"));
Property.Addattribute ("SET", (String) Data.GetaTRibutes (). get ("set"));
Property.Addattribute ("get", (string) Data.gettributes (). Get ("get"));
}
}
List 2.8 Propertyjdom.java
Public class packagejdom {
PRIVATE PACKAGEJDOM () {}
Public Static Element Product (packagedata packagedata) {
Element packageElement = New Element ("package");
PackageElement.addContent (New Element). setText (packagedata.getname ()));
IF (! ("". Equals (packagedata.getdescription ()))))))
PackageElement.addContent
New Element ("Description"). settext (packageData.getdescription ());
}
Return packageElement;
}
}
List 2.9 packagejdom.java
Convert JDOM to output
When dynamically generating an XML document is a JDOM structure, the only thing to do is to input it as a data source to the JAXP-enabled Transformer class implementing the actual code conversion. Because the current JAXP version does not directly support the JDOM ELEMENT, you need to first convert Jdom to standard DOM, and complete this task using JDOM's DomoutPutter class.
Element root = new element ("xgen");
Root.addContent (JavaBeanjdom.ProduceElement (Javabeandata));
Org.jdom.document doc =
New org.jdom.document (root);
Org.jdom.Output.domOutputter Domout = new org.jdom.output.domource ();
Org.w3c.dom.node domnode = domout.output (doc);
Next, you can use JAXP's Transformer class to complete the conversion.
TRANSFORMER TRANSFORMER =
TRANSFORMERFAACTORY.NEWINSTANCE (). New StreamSource (Xslfile);
Transformer.Transform (New DomSource (Domnode), New StreamResult (JavaFile);
Graphic code generator
Since all data interface design has been completed, you only need to use some graphic design tools such as NetBean or JBuilder to complete the graphical requirements. This step relatively simple, the method of graphics interface only needs to generate the corresponding data object, and uses JDOM generation The tool class generates a dynamic XML file and then calls the JAXP's API to complete the code generated. I don't have introduced here, interested readers can design their user interfaces themselves. But pay attention to the reading of the XSLT file because it is static and stored as a file form, so providing an absolute path to the XSLT file. You can use JAR to package class files and XSLT files to a JAR file and use the classloader.getsystemResource (String RelativePath method to get the absolute path of the XSLT file. to sum up
The first part of this article describes how to use XML and XSLT implementation of simple code generation functions, and the second part discusses how to optimize the results and provide dynamic XML data generation functions for graphical requirements. The purpose of this article is not for JavaBean code generation (because a system that is just automatically generating JavaBean code is not very attractive, there are many systems to provide this feature), but use the combination of XML & XSLT to provide a light for code generation problems. Quantitative solution. The reader can consider that the use of XML Schema automatically generates Java objects in JAXB, using this document can achieve exact same features. Similar questions such as automatically generate Java RMI objects, automatically generate EJB classes, automatically generate web or ejb formulation files, automatically generate unit test code for the development of class, and generate XML Schema and SOAP messages for Web Service data, this article They can play a role, and it is easy to develop. As long as the metadata is supplied, the metadata can be converted into a variety of desired formats.
References
[1] Eric M. Burke. Java and Xslt O'Reilly & Associates, Inc. 2001
Copyright: Xingchen Chu @ Copyright Reserved (C) 2004 Melbourne University