XJC Plugin customizations

给你一囗甜甜゛ 提交于 2019-12-12 19:06:22

问题


I'm developing XJC plugin which uses customizations. The problem is I get

[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

Here is my schema:

<?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>

I'm not using external binding configuration (no -b option) so

  1. "It is attached to a wrong place"
  2. "so it is inconsistent with other bindings" - I've got only one customization and I don't use external binding files

So it seems <myPlugin:testAnnotation>test</myPlugin:testAnnotation> is in the wrong place. But it is inside an annotation tag, and I don't know why it doesn't work.


回答1:


The short answer is that you probably are doing everything correctly. You just have to take the next step in the implementation of your plugin and tell XJC that you saw the customization.

You will get this error any time that none of the plugins "acknowledge" the customization. To deal with customizations, you will generally want to override a couple of the Plugin methods and then when you handle the customization, call 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();
    }
}

The idea behind the wrong place verbiage is that the plugin might only be looking for the customization at the 'property' level but the document has it at the 'class' level (e.g., on a complexType). In this case, the plugin code would miss it (because it is not looking for it on the classInfo) and therefore it does not get acknowledged.



来源:https://stackoverflow.com/questions/38338697/xjc-plugin-customizations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!