XML和XSLT实现代码生成器(IV)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

XML和XSLT实现代码生成器(IV)

结果处理

本文第一部分描述了如何使用静态XML文档和XSLT以及一个简单的Java转换程序实现基本的代码生成器,然而通过分析结果,我的实现至少还有两点是十分原始的,首先代码输出结果的格式非常不理想(参看图2.1),输出代码之间完全没有的空行、缩进,导致代码非常难以阅读,这就需要通过一个程序将原始结果过滤为符合阅读需求的Java源代码,可以将这一步称为代码美容。一种可选的方法是使用现存的产品自动清理Java代码,例如JIndent (http://www.jindent.com);另一种方法是自己动手美容代码,列表2.2显示了我的简单实现

Pic 2.1 Result of Generated Code(Using IE Browser)

package com.xs.xgen.util;

import java.io.*;

import java.util.*;

/**

* <p>Title: Code Generator based on XML and XSLT</p>

* <p>Description: Beta Version For Code Generator</p>

* <p>Copyright: xchu@Copyright (c) 2004</p>

* <p>University: Melbourne University</p>

* @author Xingchen Chu

* @version 0.1

*/

public class IndentUtil {

/**

* <description> add indent to the content of input file output </description>

* @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 them to 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 indent)throws IOException{

if(begin>content.length()-1){ //now the position is the end of the content

writer.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<indent;j++){

writer.write(" ");

}

writer.write(currentChar);

//check whether is the end of the class file

if((begin+1)<content.length()&&content.charAt(begin+1)!=')'){

writer.write("\r\n");

for (int j = 0; j < indent; j++) {

writer.write(" ");

}

}

}else{

writer.write(currentChar);

if(currentChar=='{'){

writer.write("\r\n");

indent+=5;

for(int j=0;j<indent;j++){

writer.write(" ");

}

}else if(currentChar==';'){

//check whether is the end of the method

if((begin+1)<content.length()&&content.charAt(begin+1)!='}'){

writer.write("\r\n");

for (int j = 0; j < indent; 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)

动态生成XML

除了之前提到的代码美容外,我的初试方案中的另外一个缺陷是XML文档是静态写入的,考虑到图形化代码生成器的需要,静态方式将无法满足图形化的要求。因此,这里必须实现XML文档的动态生成功能。可以采取SAX或着DOM标准实现XML动态生成,在此我利用JDOM API,因为它是DOM的面向对象版本,比直接使用DOM更加容易。然后利用JDOM的方法将XML JDOM节点转换为标准的DOM节点,通过JAXP的Transformer对象实现从DOM节点并利用XSLT到Java代码的转换。

Pic 2.3 UML for My Data Model and JDOM Util

数据模型

这里我将采用如下策略生成数据模型

1.首先根据之前定义的DTD定制数据结构并采取如下方式,DTD中的任何ELEMENT如果其内容是#PCDATA且没有任何属性,则使用String,否则定义对应的Java类。

2.根据DTD的ELEMENT定义中的通配符,如果是*或+则在对应元素的Java类中使用Collection表示其子元素。

3.对于任何属性列表,在Java类定义中使用Map对象表示。

例如对于Property元素,它的定义为<!ELEMENT property (name,exception*)>,

同时还为其定义了属性列

<!ATTLIST property

type CDATA #REQUIRED

access (public | protected | private | package) #REQUIRED

set (yes | no) #REQUIRED

get (yes | no) #REQUIRED

>

根据我们的规则,其Java类定义如下

package com.xs.xgen.javabean;

import java.util.*;

public class PropertyData {

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=attributes;

}

public String getName(){

return name;

}

public Map getAttributes(){

return Collections.unmodifiableMap(attributes);

}

public Collection getExceptions(){

return Collections.unmodifiableCollection(exceptions);

}

}

list 2.4 PropertyData.java

同理,JavaBean元素和Package元素的定义就十分直观了

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(implement);

}

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

读者可能注意到这些类定义完全基于DTD文件,而且之前的XSLT也是基于DTD结构的,所以当处理的XML文档结构复杂时一定要定义DTD或XML Schema,即便不包含数据验证机制这样的需求,它们也有助于软件的开发。

References

[1] Eric M. Burke. Java and XSLT O’Reilly&Associates,Inc. 2001

Copyright: Xingchen Chu@Copyright Reserved(c) 2004 Melbourne University

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航