使用xsl来动态生成java代码
2009-03-27 22:34
726 查看
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;
}
}
这里首先给一个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;
}
}
相关文章推荐
- 动态生成JAVA代码
- java wsdl反向生成源码,并使用CXF实现客户端调用代码
- Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解
- 动态生成JAVA代码(三)
- Java使用Flying Saucer实现HTML代码生成PDF文档
- 在Java中使用Highcharts+Ajax+Json生成动态饼图
- 使用idea和wsdl生成java代码
- 使用cmd根据WSDL网址生成java客户端代码
- 简化 Ajax 和 Java 开发,第 1 部分: 用 JSP 标记文件动态生成 JavaScript 代码
- java开发环境eclipse的使用(模块代码折叠、可视化开发插件、生成Jar插件)
- java用axis2生成webservice客户端代码及使用方法
- 使用java代码生成图片验证码
- 使用java语言中的注解生成器生成代码
- 想完全使用代码动态生成GridView真难
- 使用java的wsimport.exe工具生成wsdl的客户端代码
- 四种动态生成Java代码的方法(一)
- Java语言使用注解处理器生成代码——第二部分:注解处理器
- 使用java代码启动generator生成相关的文件
- C#分析数据库结构,使用XSL模板自动生成代码
- 使用自动生成java文件和自动编译的动态代理模式-马士兵设计模式教程