我有一系列描述各种实体类型的 XML 文件。我想使用基于 mixins 的实现将这些 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>