XJC 插件定制

XJC Plugin customizations

我正在开发使用定制的 XJC 插件。问题是我得到

[ERROR] compiler was unable to honor this myPlugin:testAnnotation customization. It is attached to a wrong place, or its inconsistent with other bindings.
  line 16 of file:/C:/JaxbPlugin_jar/withInternalBinding/sampleInlineAnnotation.xsd

[ERROR] (the above customization is attached to the following location in the schema)
  line 13 of file:/C:/JaxbPlugin_jar/withInternalBinding/sampleInlineAnnotation.xsd

这是我的架构:

<?xml version='1.0' ?>
<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:myPlugin="http://www.example.org"  
targetNamespace="http://www.books.org"  
xmlns="http://www.books.org" 

xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc myPlugin" 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
jaxb:version="2.1">

    <xs:element name="a" type="xs:string">
        <xs:annotation>
            <xs:appinfo>
                <myPlugin:testAnnotation>test</myPlugin:testAnnotation>
            </xs:appinfo>
        </xs:annotation>    
    </xs:element>
</xs:schema>

我没有使用外部绑定配置(没有 -b 选项)所以

  1. "It is attached to a wrong place"
  2. "所以它与其他绑定不一致" - 我只有一个自定义并且我不使用外部绑定文件

看来 <myPlugin:testAnnotation>test</myPlugin:testAnnotation> 放错地方了。但是在注释标签里面,不知道为什么不行

简短的回答是你可能做的每件事都是正确的。您只需执行插件实施的下一步并告诉 XJC 您看到了定制。

只要 none 的插件 "acknowledge" 自定义,您就会收到此错误。要处理自定义,您通常需要重写几个插件方法,然后在处理自定义时调用 markAsAcknowledged.

@Override
public List<String> getCustomizationURIs() {
    List<String> uris = new ArrayList<>();
    uris.add(...);
    return uris;
}

@Override
public boolean isCustomizationTagName(String nsUri, String localName) {
    return ...;
}


for (CPluginCustomization customization : propertyInfo.getCustomizations()) {
    if (isCustomizationTagName(customization.element.getNamespaceURI(),
            customization.element.getLocalName())) {
        // Apply the customization.
        ...

        // Tell XJC that the customization was consumed.
        customization.markAsAcknowledged();
    }
}

wrong place 措辞背后的想法是插件可能只在 'property' 级别寻找自定义,但文档在 'class' 级别(例如,在复杂类型上)。在这种情况下,插件代码会错过它(因为它没有在 classInfo 上寻找它),因此它不会得到确认。