JET2简单例子

1.JET生成java代码:

 

下列实例是一个模板,用于创建拥有任意数量属性的类。每个属性将有 getter 和 setter 与之关联,还有一些初始值。此外,所调用的函数的名称将输出到命令行,通过这种方式,模板即可为各函数添加简单的日志。

 

class <c:get select="/app/@class" /> {
<c:iterate select="/app/property" var="p" >
	private <c:get select="$p/@type" /> <c:get select="$p/@name" />;
</c:iterate>

	public <c:get select="/app/@class" />() {
	<c:iterate select="/app/property" var="p" >
		this.<c:get select="$p/@name" /> = <c:choose select="$p/@type" >
		<c:when test="'String'">"<c:get select="$p/@initial" />"</c:when>
		<c:otherwise><c:get select="$p/@initial" /></c:otherwise>
		</c:choose>
;
	</c:iterate>
	}

<c:iterate select="/app/property" var="p" >
	public void set<c:get select=\
	"camelCase($p/@name)" />(<c:get select="$p/@type" />
	<c:get select="$p/@name" />) {
		System.out.println\
		("In set<c:get select=\
		"camelCase($p/@name)" />()");
		this.<c:get select="$p/@name" /> = <c:get select="$p/@name" />;
	}
	
	public <c:get select=\
	"$p/@type" /> get<c:get select="camelCase($p/@name)" />() {
		System.out.println("In get<c:get select="camelCase($p/@name)" />()");
		return <c:get select="$p/@name" />;
	}
	
</c:iterate>
}

 

这里是该模板的输入模型实例:

 

<app class="Car">
	<property name="model" type="String" initial="Honda Accord" />
	<property name="horsepower" type="int" initial="140" />
	<property name="spareTires" type="boolean" initial="true" />
</app>

 

这些输入参数生成如下类:

 

class Car {
	private String model;
	private int horsepower;
	private boolean spareTires;

	public Car() {
		this.model = "Honda Accord";
		this.horsepower = 140;
		this.spareTires = true;
	}

	public void setModel(String model) {
		System.out.println("In setModel()");
		this.model = model;
	}
	
	public String getModel() {
		System.out.println("In getModel()");
		return model;
	}
	
	public void setHorsepower(int horsepower) {
		System.out.println("In setHorsepower()");
		this.horsepower = horsepower;
	}
	
	public int getHorsepower() {
		System.out.println("In getHorsepower()");
		return horsepower;
	}
	
	public void setSparetires(boolean spareTires) {
		System.out.println("In setSparetires()");
		this.spareTires = spareTires;
	}
	
	public boolean getSparetires() {
		System.out.println("In getSparetires()");
		return spareTires;
	}
	
}

 

 

2.JET生成其他代码:

 

为强调 JET 不仅仅可用来生成代码,我们给出了下面这个实例,这是一个模板,生成具有不同语气的电子邮件消息。所生成的各电子邮件的目的是是向某人索要求各种东西。下面提供控制文件(main.jet)及其调用的模板(email.jet)。

 

main.jet

<c:iterate select="/app/email" var="currEmail" >
	<ws:file template="templates/email.jet"
	path="{$org.eclipse.jet.resource.project.name}/{$currEmail/@to}.txt" />
</c:iterate>

 

email.jet

<c:setVariable var="numItems" select="0" />
<c:iterate select="$currEmail/request" var="r">
	<c:setVariable var="numItems" select="$numItems+1" />
</c:iterate>
<c:set select="$currEmail" name="numItems"><c:get select="$numItems" /></c:set>
	<c:choose select="$currEmail/@mood" >
	<c:when test="'happy'">My dear</c:when>
	<c:when test="'neutral'">Dear</c:when>
	<c:when test="'angry'">My enemy</c:when>
</c:choose> <c:get select="$currEmail/@to" />,

I am writing you <c:choose select="$currEmail/@mood" >
<c:when test="'happy'">in joy </c:when>
<c:when test="'neutral'"></c:when>
<c:when test="'angry'">in burning anger </c:when>
</c:choose>to ask for <c:choose select="$currEmail/@numItems" >
<c:when test="1">
a <c:get select="$currEmail/request/@item" />. 
</c:when>
<c:otherwise>
the following:

<c:setVariable var="i" select="0" />
<c:iterate select="$currEmail/request" var="r">
	<c:setVariable var="i" select="$i+1" />
	<c:get select="$i" />. <c:get select="$r/@item" />
</c:iterate>

</c:otherwise>
</c:choose>
<c:choose select="$currEmail/@mood">
	<c:when test="'happy'">Please</c:when>
	<c:when test="'neutral'">Please</c:when>
	<c:when test="'angry'">Either suffer my wrath, or</c:when>
</c:choose> send me <c:choose select="$currEmail/@numItems">
<c:when test="1">
this item</c:when>
<c:otherwise>
these items</c:otherwise>
</c:choose> <c:choose select="$currEmail/@mood" >
<c:when test="'happy'">at your earliest convenience.</c:when>
<c:when test="'neutral'">promptly.</c:when>
<c:when test="'angry'">immediately!</c:when>
</c:choose>

<c:choose select="$currEmail/@mood" >
<c:when test="'happy'">Your friend,</c:when>
<c:when test="'neutral'">Sincerely,</c:when>
<c:when test="'angry'">In rage,</c:when>
</c:choose>

<c:get select="/app/@from" />

 

该模板的输入模型实例如下:

sample.xml

<app from="Nathan" >
	<email to="Chris" mood="angry" >
		<request item="well-written article" />
	</email>
	<email to="Nick" mood="happy" >
		<request item="Piano" />
		<request item="Lollipop" />
		<request item="Blank DVDs" />
	</email>
</app>

 

将 mood 电子邮件蓝图应用于这些参数,生成下列两个文件。

 

你可能感兴趣的:(单例)