[英]XML & JAXB: pass attribute into value
我有大量通过JAXB( maven-jaxb2-plugin
)生成的对象,并使用jaxb2-annotate-plugin
。 这些类可以定义一个RelationType
,我想用相应的@RelationType
注释@RelationType
注释。 我使用XPath表达式在XSD中找到name属性并注释该类,并将其特定类型传递到注释中。 下面是一个示例:
<jaxb:bindings node="//xsd:complexType[@name='SomeRelationType']">
<annox:annotate target="class">@com.example.RelationType(type = "SomeRelationType")</annox:annotate>
</jaxb:bindings>
对应以下XSD代码段:
<xsd:complexType name="SomeRelationType">
<xsd:complexContent>
<xsd:extension base="RelationType">
<xsd:sequence>
<xsd:element name="someValue" type="SomeValue"/>
<xsd:element name="otherValue" type="OtherValue"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
我找到了在的ComplexType SomeRelationType
的名称,并注释类@RelationType
注解,它具有SomeRelationType
作为其类型参数。 它将生成以下类:
@RelationType(type = "SomeRelationType")
public class SomeRelationType extends RelationType implements Serializable {
private final static long serialVersionUID = 1L;
protected SomeValue someValue;
protected OtherValue otherValue;
}
如果它只是几个域对象,则效果很好。 但是我的工作量很大,手动定义每个注释不仅繁琐,而且在更改和扩展方面也很糟糕。
为了泛化它,我可以将XPath表达式重写为以下内容:
<jaxb:bindings node="//xsd:complexType[substring(@name, string-length(@name) - string-length('RelationType') + 1)]" multiple="true">
<annox:annotate target="class">@com.example.RelationType(type = "SomeRelationType")</annox:annotate>
</jaxb:bindings>
问题:我的注释的type参数仍然定义为"SomeRelationType"
。 如果我可以使用XPath表达式中定义的@name
,那将是很好的。 然后,所有名称以"RelationType"
结尾的类也将自动获取带有正确type
参数的@RelationType
批注。
当然,它不像执行以下操作那样简单,但是它显示了我想要实现的目标:
<jaxb:bindings node="//xsd:complexType[substring(@name, string-length(@name) - string-length('RelationType') + 1)]" multiple="true">
<annox:annotate target="class">@com.example.RelationType(type = @name)</annox:annotate>
</jaxb:bindings>
这样的事情在XML / JAXB中甚至可能吗?
但是我的工作量很大,手动定义每个注释不仅繁琐,而且在更改和扩展方面也很糟糕。
对我来说, @com.example.RelationType(type = "SomeRelationType")
看起来像一个简单的元信息,可以通过反射来导出而无需任何注释。 因此,请检查是否有一种方法可以进行“配置之上的约定”。
jaxb2-annotate-plugin
不也将不支持参数化,它实现起来太狭窄也太复杂。 免责声明我是jaxb2-annotate-plugin的作者 。
我看到两个选择:
是的,只有两个选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.