XML and XSLT Implementation Code Builder (IV)
Resulting
The first part of this article describes how to use a static XML document and XSLT and a simple Java conversion program to implement basic code builder, but through analysis results, my implementation is at least two points are very original, first of all the code output results. It is very unimaginable (see Figure 2.1), there is a complete space between the output code, indent, causing the code very difficult to read, which requires the original result to filter the original result into the Java source code that meets the needs of reading. This can be One step is called code beauty. An optional method is to automatically clean up Java code using existing products, such as jindent (http://www.jindent.com); another method is to do itself, list 2.2 shows my simple implementation
Pic 2.1 Result of Generated Code (Using IE Browser)
Package com.xs.xgen.util;
Import java.io. *;
Import java.util. *;
/ **
*
Title: Code Generator based on XML and XSLT P>
*
Description: Beta Version for Code Generator p>
*
Copyright: XCHU @ CopyRight (C) 2004 P>
*
University:
Melbourne
University
p>
* @Author xingchen chu
* @version 0.1
* /
Public class indeututil {
/ **
*
* @Param java.io.file input
* @Param java.io.file Output
* /
Public Static Void IndentJavasource (file input, file output) {
Try {
// read all the content at one time and write the the String Buffer
BufferedReader IN = New BufferedReader (New FileReader (INPUT);
StringBuffer SB = new stringbuffer ();
String line = NULL;
While ((line = IN.Readline ())! = null) {
Sb.append (line);
}
String content = sb.toString ();
FileWriter Writer = New FileWriter (OUTPUT);
IndentContent (Writer, Content, 0, 0); // Indent from The Beginning of The Content
} catch (exception e) {
Throw new runtimeException (e);
} finally {
Writer.close ();
}
}
Private static void IndentContent (Writer Writer, String Content,
INT begin, int indept {
IF (Begin> Content.Length () - 1) {// Now the position is the end of the contentWriter.flush ();
Return;
}
Char currentchar = content.charat (begin); // Get the current char of the content
IF (currentchar == '}') {
Writer.write ("/ r / n");
Indent- = 5;
For (int J = 0; j Writer.write ("" "); } Writer.write (Currentchar); // Check WHETHER is The end of the class file IF ((Begin 1) Writer.write ("/ r / n"); For (int J = 0; j Writer.write ("" "); } } } else { Writer.write (Currentchar); IF (currentchar == '{') { Writer.write ("/ r / n"); Indent = 5; For (int J = 0; j Writer.write ("" "); } } else if (currentchar == ';') { // Check WHETHER is the end of the method IF ((Begin 1) Writer.write ("/ r / n"); For (int J = 0; j Writer.write ("" "); } } } else {/// Nothing to do} } IndentContent (Writer, Content, Begin 1, Indent); // Recursively Evaluate Next Char } } List 2.2 indentutil.java (Recursive Version of Indent Method) Dynamically generate XML In addition to the previously mentioned code beauty, another defect in my initial test scheme is that the XML document is static, taking into account the need for graphical code generator, the static mode will not meet graphical requirements. Therefore, this dynamic generation function of XML documents must be implemented here. You can take SAX or DOM standard to implement XML dynamic generation, here I use the JDOM API because it is an object-oriented version of the DOM, which is easier than direct use of DOM. Then use the JDOM to convert the XML JDOM node to a standard DOM node, and achieve the conversion from the DOM node and utilize XSLT to Java code via JAXP's Transformer object. Pic 2.3 UML for My Data Model and JDOM Util Data model Here I will generate data models as follows 1. First, according to the previously defined DTD custom data structure and take the following, any ELEMENT in the DTD is used if its content is #pcdata and there is no attribute, otherwise the corresponding Java class is defined. 2. According to the wildcard in the ELEMENT definition of the DTD, if * or is used in the Java class of the corresponding element to represent its child elements. 3. For any list of properties, use the Map object in the Java class definition. For example, for the Property element, it is defined as At the same time, it also defines the attribute column. "Attlist property Type cdata #Required Access (public | protected | private | package) #Required Set (Yes | No) #Required Get (Yes | NO) #Required > According to our rules, its Java class is defined as follows. Package com.xs.xgen.javabean; Import java.util. *; Public class protydata { Private map attributes = new hashmap (); PRIVATE STRING NAME; PRIVATE COLLECTION Exceptions; Public PropertyData (String Name, Collection Exceptions, Map Attributes) { THIS.NAME = Name; THIS.EXCEPTIONS = EXCEPTIONS; THIS.ATTRIBUTES = attribute; } Public string getname () { Return Name; } Public map getAttributes () { Return Collections.unmodifiableMap (Attributes); } Public Collection getExceptions () { Return Collections.unmodiFiableCollection (Exceptions); } } List 2.4 PropertyData.java Similarly, the definition of JavaBean elements and package elements is very intuitive. Package com.xs.xgen.javabean; Import java.util. *; Public class javabeandata { PRIVATE STRING NAME; Private packagedata packagedata; PRIVATE COLLECTION IMPLEMENT; Private collection propertydata; Public Javabeandata (String Name, PackageData PackageData, Collection Implement, Collection PropertyData) { THIS.NAME = Name; this.PackageData = packagedata; THIS.IMPLEMENT = IMPLEMENT; this.propertydata = propertydata; } Public string getname () {return name;} Public packagedata getpackagedata () {return packagedata; Public collection getimplement () { Return Collections.unmodifiableCollection;} Public collection getpropertydata () { Return Collections.UnmodiFiableCollection (PropertyData); } } List 2.5 javabeandata.java Package com.xs.xgen.javabean; Public class packagedata { PRIVATE STRING NAME; Private string description; Public packagedata (string name) { THIS (Name, "); } Public packageData (String name, string description) { THIS.NAME = Name; THIS.DESCRIPTION = DESCRIPTION; } Public string getname () { Return Name; } Public string getdescription () { Return description; } } List 2.6 packagedata.java Readers may notice that these class definitions are completely DTD files, and the previous XSLT is also based on DTD structures, so they must define DTD or XML Schema when processing the XML document structure, even if the data verification mechanism is not included, It also helps the development of software. References [1] Eric M. Burke. Java and Xslt O'Reilly & Associates, Inc. 2001 Copyright: Xingchen Chu @ Copyright Reserved (C) 2004 Melbourne University