Java XJC插件定制

Java XJC插件定制,java,plugins,jaxb,xjc,Java,Plugins,Jaxb,Xjc,我正在开发使用定制的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/sampleInlineAnno

我正在开发使用定制的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>

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

  • “它连接到错误的位置”
  • “因此它与其他绑定不一致”-我只有一个自定义项,不使用外部绑定文件

  • 看来
    test
    放错地方了。但是它在一个注释标记中,我不知道为什么它不起作用。

    简单的回答是,您可能做的每件事都是正确的。您只需在插件的实现过程中迈出下一步,并告诉XJC您看到了定制

    当没有插件“确认”定制时,您将随时收到此错误。要处理定制,通常需要覆盖几个插件方法,然后在处理定制时,调用
    markasackowledged

    @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();
        }
    }
    
    @覆盖
    公共列表getCustomizationURI(){
    List uris=new ArrayList();
    uris.add(…);
    返回URI;
    }
    @凌驾
    公共布尔isCustomizationTagName(字符串nsUri,字符串localName){
    返回。。。;
    }
    对于(CPluginCustomization自定义:propertyInfo.getCustomizations()){
    如果(isCustomizationTagName)(customization.element.getNamespaceURI(),
    customization.element.getLocalName()){
    //应用定制。
    ...
    //告诉XJC定制已被使用。
    customization.markAsAcknowledged();
    }
    }
    

    错误位置背后的想法是,插件可能只在“属性”级别查找自定义项,但文档在“类”级别(例如,在complexType上)查找自定义项。在这种情况下,插件代码将丢失它(因为它没有在classInfo上查找它),因此它不会得到确认。

    您开发了什么样的插件?@lexicore它是一个内部使用的插件,有多种用途。我知道xjc有一些插件,其中一些是由您编写的=)