variusエンティティタイプを記述した一連のXMLファイルがあります。ミックスインに基づく実装を使用して、これらのXMLファイルをJavaクラス(ソースコードなので、コンパイルされていることを確認できます)に変換したいと思います。
例:
村人を説明するXMLファイル:
<?xml version="1.0" encoding="utf-8" ?>
<entity>
<id>1</id>
<gathers>
<rate>12</rate>
</gathers>
<moves>
<speed>4</speed>
</moves>
</entity>
次のクラスになる必要があります。
public class Villager implements Gathers, Moves {
private final Gathers gathers;
private final Moves moves;
int getId() {
return 1;
}
@Override
public int getSpeed() {
return this.moves.getSpeed();
}
@Override
public int getRate() {
return this.gathers.getRate();
}
public Villager() {
super();
this.moves = new MovesMixin(4);
this.gathers = new GathersMixin(12);
}
}
新しいプロパティをカバーするように簡単に拡張できる方法でこれを実行したいと思います。
このようなことを行うことができる既存のパッケージ/ツールはありますか?
テンプレートを使用した更新された例:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="entity">
/*
* DO NOT MODIFY
* This is an automatically generated class.
*/
package xslt.entities;
import xslt.*;
public strictfp final class <xsl:value-of select="typeName"/> implements <xsl:apply-templates select="moves" mode="implement"/><xsl:apply-templates select="gathers" mode="implement"/><xsl:apply-templates select="shoots" mode="implement"/>Entity {
<xsl:apply-templates select="moves" mode="mixin"/>
<xsl:apply-templates select="gathers" mode="mixin"/>
<xsl:apply-templates select="shoots" mode="mixin"/>
public <xsl:value-of select="typeName"/>() {
super();
}
}
</xsl:template>
<xsl:template match="moves" mode="implement">Moves, </xsl:template>
<xsl:template match="gathers" mode="implement">Gathers, </xsl:template>
<xsl:template match="shoots" mode="implement">Shoots, </xsl:template>
<xsl:template match="moves" mode="mixin">
private final Moves moves = new MovesMixin(<xsl:value-of select="speed"/>);
@Override
public int getSpeed() {
return this.moves.getSpeed();
}
</xsl:template>
<xsl:template match="gathers" mode="mixin">
private final Gathers gathers = new GathersMixin(<xsl:value-of select="rate"/>);
@Override
public int getRate() {
return this.gathers.getRate();
}
</xsl:template>
<xsl:template match="shoots" mode="mixin">
private final Shoots shoots = new ShootsMixin(<xsl:value-of select="range"/>);
@Override
public int getRange() {
return this.shoots.getRange();
}
</xsl:template>
</xsl:transform>