天下第二博

Tian Xia The Second BO
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用xsl来动态生成java代码

Posted on 2009-03-27 22:34  Nuke'Blog  阅读(465)  评论(0编辑  收藏  举报

xsl本身就是一个构型良好的xml,它能够把一个xml文档转换成另外一个xml文档,或者转换成文本文件、html文件等等。这里就是利用xsl来动态的生成我们想要的java文件(从某种角度看,java代码其实也就是一个文本文件),希望能够通过这篇文章,看到xml以及相关的技术所具有的强大能力!

这里首先给一个xml例子,我们将通过一个xsl从该xml文件中抽取有用的信息来生成java代码(实际上是一个javabean):

 

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<bean>
    
<name>Product</name> 
    
<comments>This bean represents a product that the company
offers to its customers
</comments> 
    
<property>
        
<name>code</name> 
        
<type>int</type> 
        
<comments>the product inventory code</comments> 
    
</property>
    
<property>
        
<name>name</name> 
        
<type>String</type> 
        
<comments>the product name</comments> 
    
</property>
    
<property>
        
<name>testedOnAnimals</name> 
        
<type>boolean</type> 
        
<comments>the flag that indicates if the product was
tested on animals
</comments> 
    
</property>
    
<property>
        
<name>availableSince</name> 
        
<type>java.util.Date</type> 
        
<comments>the date when the company started offering this
product to its customers
</comments> 
    
</property>
</bean> 

 

下面我就直接给出转换的xsl,如果大家对xsl不是很了解的话,可以先看一些资料,了解之后就会明白了。我在里面稍微做了些注释:

 

<?xml version="1.0"?>
<xsl:stylesheet 
    
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version
="1.0"   
    xmlns:java
="http://xml.apache.org/xslt/java"
    exclude-result-prefixes
="java">

<!--这里就是指定通过这个xsl所转换的结果的类型,是text格式-->
<xsl:output method = "text"/>

<!--xslt使用模版来处理xml中的节点-->
<xsl:template match="bean">

/**
<xsl:value-of select="comments"/>//这里是获取xml文档中的节点值
* This class has been generated by the XSLT processor from the metadata 
*/
public class 
<xsl:value-of select="name"/> {

    /**
     * Creates a new instance of the 
<xsl:value-of select="name"/> bean
     */
    public 
<xsl:value-of select="name"/>() {}
         
    
<xsl:apply-templates select="property"/>
}
</xsl:template>

<xsl:template match="property">
    private 
<xsl:value-of select="type"/>
    
<xsl:text> </xsl:text>//输出文本,这里是输出一个空格
    
<xsl:value-of select="name"/>;
    
<xsl:variable name="name" select="name"/>//定义xsl中要使用的变量
    
<xsl:variable name="cname" select="java:Capitalizer.capitalize($name)"/>//这里使用了xslt extensions,它可以允许在xslt中直接引用java中方法,非常方便。
    /**
     * Sets 
<xsl:value-of select="comments"/>
     * @param 
<xsl:value-of select="name"/> is <xsl:value-of
select="comments"/>
     */
    public void set
<xsl:value-of select="$cname"/>(<xsl:value-of select="type"/> <xsl:text> </xsl:text><xsl:value-of select="name"/>) {
      this.
<xsl:value-of select="name"/> = <xsl:value-of
select="name"/>;          
    }

    /**
     * Returns 
<xsl:value-of select="comments"/>
     * @return 
<xsl:value-of select="comments"/>
     */
    public 
<xsl:value-of select="type"/><xsl:text></xsl:text>
    
<xsl:apply-templates select="type"/><xsl:value-of
    
select="$cname"/>() {
      return 
<xsl:value-of select="name"/>;
    }
</xsl:template>

<xsl:template match="type">
    
<xsl:variable name="type" select="."/>
    
<xsl:choose>
    
<xsl:when test="$type='boolean'">is</xsl:when>
    
<xsl:otherwise>get</xsl:otherwise>
    
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

 

好了,完成以上工作之后,只要在cmd中,输入如下的命令行,就可以获得我们想要的结果了:
java org.apache.xalan.xslt.Process -in xmlSource  -xsl stylesheet -out outputfile

这里列出结果:

 

 

/**
* This bean represents a product that the company offers to its
customers
* This class has been generated by the XSLT processor from the
metadata
*/
public class Product {

    
/**
     * Creates a new instance of the Product bean
     
*/
    
public Product() {}
         
         
    
private int code;
         
    
/**
     * Sets the product inventory code
     * 
@param code is the product inventory code
     
*/
    
public void setCode(int code) {
        
this.code = code;          
    }

    
/**
     * Returns the product inventory code
     * 
@return the product inventory code
     
*/
    
public int getCode() {
        
return code;
    }

    
private String name;
         
    
/**
     * Sets the product name
     * 
@param name is the product name
     
*/
    
public void setName(String name) {
        
this.name = name;          
    }

    
/**
     * Returns the product name
     * 
@return the product name
     
*/
    
public String getName() {
        
return name;
    }

    
private boolean testedOnAnimals;
         
    
/**
    * Sets the flag that indicates if the product was tested on animals
    * 
@param testedOnAnimals is the flag that indicates if the product
was tested on animals
    
*/
    
public void setTestedOnAnimals(boolean testedOnAnimals) {
        
this.testedOnAnimals = testedOnAnimals;          
    }

    
/**
     * Returns the flag that indicates if the product was tested on
animals
     * 
@return the flag that indicates if the product was tested on
animals
     
*/
    
public boolean isTestedOnAnimals() {
        
return testedOnAnimals;
    }

    
private java.util.Date availableSince;
         
    
/**
     * Sets the date when the company started offering this product to
its customers
     * 
@param availableSince is the date when the company started
offering this product to its customers
     
*/
    
public void setAvailableSince(java.util.Date availableSince) {
        
this.availableSince = availableSince;          
    }

    
/**
     * Returns the date when the company started offering this product
to its customers
     * 
@return the date when the company started offering this product
to its customers
     
*/
    
public java.util.Date getAvailableSince() {
        
return availableSince;
    }

}