使用xsl來動態產生java代碼

來源:互聯網
上載者:User
動態 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;
    }

}


總結:
1. 在熟悉了xsl的基本使用之後,理解以上的內容並不是困難;
2. 這樣做是比較適合預Crowdsourced Security Testing道了某些邏輯功能,但由於某種原因,需要動態產生,或者是為了節省不必要的重複工作,可以通過它自動產生代碼;
3. 修改這個xsl比較方便。

sjoy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.