aderesh
aderesh

Reputation: 947

XJC Plugin customizations

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.

Upvotes: 2

Views: 838

Answers (1)

Rob
Rob

Reputation: 6497

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.

Upvotes: 1

Related Questions