<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kooyee ‘s blog

    開源軟件, 眾人努力的結晶, 全人類的共同財富
    posts - 103, comments - 55, trackbacks - 0, articles - 66
       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    [JAVA] 使用xsl來動態生成java代碼

    Posted on 2007-12-08 19:54 kooyee 閱讀(548) 評論(1)  編輯  收藏 所屬分類: Java
    xsl本身就是一個構型良好的xml,它能夠把一個xml文檔轉換成另外一個xml文檔,或者轉換成文本文件、html文件等等。這里就是利用xsl來動態的生成我們想要的java文件(從某種角度看,java代碼其實也就是一個文本文件),希望能夠通過這篇文章,看到xml以及相關的技術所具有的強大能力!

    這里首先給一個xml例子,我們將通過一個xsl從該xml文件中抽取有用的信息來生成java代碼(實際上是一個javabean):
    [code]
    <?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> 
    [/code]

    下面我就直接給出轉換的xsl,如果大家對xsl不是很了解的話,可以先看一些資料,了解之后就會明白了。我在里面稍微做了些注釋:
    [code]
    <?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>
    [/code]

    好了,完成以上工作之后,只要在cmd中,輸入如下的命令行,就可以獲得我們想要的結果了:

    java org.apache.xalan.xslt.Process -in xmlSource  -xsl stylesheet -out outputfile

    這里列出結果:
    [code]
    /**
    * 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;
        }


    }

    [
    /code]

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

    評論

    # re: [JAVA] 使用xsl來動態生成java代碼  回復  更多評論   

    2010-08-23 15:10 by cosplay
    在熟悉了xsl的基本使用之后,理解以上的內容并不是困難;
    2. 這樣做是比較適合預先知道了某些邏輯功能,但由于某種原因,需要動態生成,或者是為了節省不必要的重復工作,可以通過它自動生成代碼;
    3. 修改這個xsl比較方便
    主站蜘蛛池模板: WWW亚洲色大成网络.COM| 亚洲成综合人影院在院播放| 99亚洲乱人伦aⅴ精品| 无码中文在线二区免费| 亚洲成年人电影网站| 无码中文字幕av免费放dvd| 国产AV无码专区亚洲AV男同| 怡红院免费的全部视频| 亚洲精品乱码久久久久久蜜桃不卡| 一级毛片在播放免费| 亚洲自偷自偷在线制服| 久久精品免费视频观看| 午夜亚洲国产理论秋霞| 在线观看免费视频资源| 亚洲永久中文字幕在线| 99视频在线精品免费观看6| 亚洲国产AV一区二区三区四区| 国产精品冒白浆免费视频| 欧洲美女大片免费播放器视频| 国产91精品一区二区麻豆亚洲| 久久成人永久免费播放| 久久精品国产精品亚洲毛片| 成人无码区免费视频观看| 日本黄页网址在线看免费不卡| 国产亚洲av人片在线观看| 日本卡1卡2卡三卡免费| 亚洲国产中文在线视频| 暖暖日本免费在线视频| 国产裸体美女永久免费无遮挡| 亚洲精品私拍国产福利在线| 免费国产黄线在线观看| 青草青草视频2免费观看| 国产亚洲成av人片在线观看| 成人免费毛片内射美女-百度| 美女黄色免费网站| 亚洲av日韩av高潮潮喷无码| 成人毛片免费在线观看| 免费无码黄网站在线看| 亚洲综合欧美色五月俺也去| 国产成人麻豆亚洲综合无码精品| 国产免费久久精品99re丫y|