kooyee ‘s blog
開源軟件, 眾人努力的結晶, 全人類的共同財富
posts - 103, comments - 55, trackbacks - 0, articles - 66
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
[JAVA] 使用xsl來動態生成java代碼
Posted on 2007-12-08 19:54
kooyee
閱讀(557)
評論(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比較方便
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
『Java』 用 Javac 編譯時的問題
Java程序員必看--擴展鼠標右鍵菜單功能
【Java】Java程序員必須了解的開源協議 轉
【Java】java.lang.NoSuchFieldError: counts
『Java』java.lang.UnsupportedOperationException at java.util.AbstractLis
[Data Structure] Vector 和 ArrayList的不同
[JAVA] 使用xsl來動態生成java代碼
[Java]代碼動態生成利器ASM 轉
[WEB] MetaData Programme
【Java】properties的使用
Powered by:
BlogJava
Copyright © kooyee
日歷
<
2010年8月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
公告
一起暢游計算機的世界
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
留言簿
(7)
給我留言
查看公開留言
查看私人留言
隨筆分類
AI 人工智能(1)
Ajax學習手記(2)
C/C++(1)
Database數據庫技術(4)
Groovy on Grails(1)
GUI骨衣 (6)
J2EE(1)
Jasper Report (2)
Java (15)
Lniux/Unix (14)
Regular Expression正則表達式
Software(1)
Software Engineering 軟件工程(2)
Swing/Applet(19)
Web Design網頁設計 (4)
Web Framework 網絡框架(1)
Windows (2)
Wireless Ad-hoc and sensor network(4)
開源 OpenSource(1)
隨筆檔案
2009年1月 (1)
2008年12月 (3)
2008年11月 (3)
2008年10月 (2)
2008年7月 (2)
2008年6月 (22)
2008年5月 (3)
2008年4月 (2)
2008年3月 (10)
2008年2月 (14)
2008年1月 (5)
2007年12月 (6)
2007年11月 (5)
2007年10月 (5)
2007年9月 (2)
2007年8月 (17)
搜索
積分與排名
積分 - 163948
排名 - 363
最新評論
1.?re: SUM, COUNT 等在 jasper report 中使用方法
<javascript language="java"> alert("aaaa")</javascript>
--sd
2.?re: 【Bug】當調用nam時錯誤
怎么重裝ns-2.33
--雨中蝶
3.?re: [SWT] SWT table中select item以及添加其他control(checkbox, button)
如果要加的CheckBox很多的話,會不會速度很慢呢?
--問路
4.?re: [JAVA] 使用xsl來動態生成java代碼
評論內容較長,點擊標題查看
--cosplay
5.?re: 『Java』java.lang.UnsupportedOperationException at java.util.AbstractLis
api 的設計多此一舉還搞個內部類
--冬天雞雞好冷
閱讀排行榜
1.? VB使用WebBrowser讀取網頁內容(12254)
2.?【Simulator】Cygwin下NS2安裝和配置(3667)
3.?什么是*.ps文件(3608)
4.?『Java』java.lang.UnsupportedOperationException at java.util.AbstractLis(3558)
5.?【linux腳本】bad interpreter: No such file or directory(3387)
主站蜘蛛池模板:
成年人免费网站在线观看
|
黄色网址免费大全
|
亚洲国产精品国自产拍电影
|
久久久久国产免费
|
久久亚洲国产成人影院
|
gogo全球高清大胆亚洲
|
久久久久久AV无码免费网站下载
|
久久久久免费看成人影片
|
亚洲国产福利精品一区二区
|
免费吃奶摸下激烈视频
|
免费的全黄一级录像带
|
jiz zz在亚洲
|
亚洲国产精华液网站w
|
成人午夜18免费看
|
日本不卡免费新一区二区三区
|
亚洲人成色77777在线观看
|
亚洲日韩激情无码一区
|
毛片免费观看网站
|
a级毛片毛片免费观看永久
|
亚洲狠狠成人综合网
|
亚洲第一AAAAA片
|
国产免费卡一卡三卡乱码
|
欧洲精品99毛片免费高清观看
|
老妇激情毛片免费
|
亚洲毛片免费观看
|
自拍偷自拍亚洲精品第1页
|
性感美女视频免费网站午夜
|
免费的全黄一级录像带
|
日本视频免费观看
|
亚洲日韩精品国产3区
|
久久亚洲AV成人无码国产
|
亚洲日本韩国在线
|
免费爱爱的视频太爽了
|
最近在线2018视频免费观看
|
一本到卡二卡三卡免费高
|
亚洲AV无码一区二区三区久久精品
|
无码日韩精品一区二区三区免费
|
131美女爱做免费毛片
|
三级毛片在线免费观看
|
免费精品国自产拍在线播放
|
国产成人精品日本亚洲直接
|