Jaxb:为固定值属性生成常量值 [英] Jaxb: Generate constant value for fixed-value attribute
问题描述
我目前正在使用xsd,它使用以下结构:
I'm currently working on a xsd which uses the following contruct:
<xs:attribute name="listVersionID" type="xs:normalizedString" use="required" fixed="1.0">
虽然本身没有问题,但使用起来相当烦人,因为这是固定值xsd规范版本之间的定义增加,我们需要修改单独的常量类中的值以使它们保持有效,尽管xsd中任何感兴趣的内容都没有改变。 xsd在其他地方维护,所以只是改变它是没有选择的。
While not problematic per se, it is rather annoying to work with, since the fixed-value of this definition increases between releases of the xsd spec, and we need to modify the values in a seperate constants-class to keep them valid, although little if anything of interest in the xsd has changed. The xsd is maintained elsewhere, so just changing it is no option.
因此我问自己是否有一个jaxb-plugin或类似的将固定值属性变为常量ala
Thus I was asking myself wether there is a jaxb-plugin or similar to turn fixed-value attributes into constants ala
@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected final String listVersionID = "1.0";
而不仅仅是
@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected String listVersionID;
必须手动填充。
有没有人知道这样的?
推荐答案
如果你不想修改架构,另一个选择是使用外部绑定文件:
If you don't want to modify your schema, another option is to use an external binding file:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jaxb:bindings schemaLocation="yourschema.xsd" node="/xs:schema">
<jaxb:globalBindings fixedAttributeAsConstantProperty="true" />
</jaxb:bindings>
</jaxb:bindings>
这相当于@jmattheis在答案中的建议。
It is equivalent to what proposes @jmattheis in his answer.
这篇关于Jaxb:为固定值属性生成常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!