Jaxb2-annotate-plugin does not work with episodes

327 views Asked by At

I am trying to enforce enum validation for my generated JAXB classes, but I am having some issues with getting them bound.

The basic XSD setup is:

-enumsXSD
-2 other XSDs that import this XSD

To provent duplication of my classes I am using episodes, but it looks like this does not play nice when adding annotions in the enumsXSD

com.sun.istack.SAXParseException2: compiler was unable to honor this annox:annotateEnumValueMethod customization. It is attached to a wrong place, or its inconsistent with other bindings.

[ERROR] Error while generating code.Location [ file:somewhere/generic.episode{64,99}].
com.sun.istack.SAXParseException2: (the above customization is attached to the following location in the schema) 

The code:

<plugins>
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.13.3</version>
        <configuration>
          <extension>true</extension>
          <args>
            <arg>-Xannotate</arg>
          </args>
          <plugins>
            <plugin>
              <groupId>org.jvnet.jaxb2_commons</groupId>
              <artifactId>jaxb2-basics-annotate</artifactId>
              <version>1.0.4</version>
            </plugin>
          </plugins>
        </configuration>
        <executions>
          <!--GENERIC ENUMS -->
          <execution>
            <id>ENUMS</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <extension>true</extension>
            <generatePackage>com.foo.generic.enums</generatePackage>
              <generateDirectory>${project.build.directory}/generated-sources/xjc1/generic</generateDirectory>
              <!-- Define the directory where we should find the XSD files -->
              <schemaDirectory>
                src/main/resources/dtd/generic
              </schemaDirectory>
              <schemaIncludes>
                <source>enums.xsd</source>
              </schemaIncludes>
              <episodeFile>
                ${project.build.directory}/generated-sources/xjc1/generic/META-INF/generic.episode
              </episodeFile>
            </configuration>
          </execution>
          <execution>
            <id>A_XSD</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <extension>true</extension>
              <bindingDirectory>${project.build.directory}/generated-sources/xjc1/generic/META-INF</bindingDirectory>
              <bindingIncludes>
                <include>generic.episode</include>
              </bindingIncludes>

              <!-- Set the package of the generated code -->
              <generatePackage>com.foo.something</generatePackage>
              <generateDirectory>${project.build.directory}/generated-sources/xjc1/a_something</generateDirectory>

              <!-- Define the directory where we should find the XSD files -->
              <schemaDirectory>
                src/main/resources/dtd/someplace/a/
              </schemaDirectory>
              <schemaIncludes>
                <source>*.xsd</source>
              </schemaIncludes>
            </configuration>
          </execution>
     </executions>
   <plugin>
</plugins>

And in the enums XSD

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" id="commonEnums"
           targetNamespace="http://foo.com/xsd/commons/enum"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           jaxb:version="2.1"
           xmlns:annox="http://annox.dev.java.net"
           jaxb:extensionBindingPrefixes="annox">
<xs:simpleType name="bulletinCategory">
    <xs:annotation>
      <xs:appinfo>
        <annox:annotateEnumValueMethod>@java.lang.Deprecated</annox:annotateEnumValueMethod>
      </xs:appinfo>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:enumeration value="valueA" />
      <xs:enumeration value="valueB" />
      <xs:enumeration value="valueC" />
    </xs:restriction>
  </xs:simpleType>
</xs>

Ideally speaking the @deprecated should be @XmlJavaTypeAdapter(FooAdapter.class) but I thought let's start small.

1

There are 1 answers

6
lexicore On

The problem is probably that you've included customizations directly into the enums.xsd. So when you compile other schemas, these customizations are considered as well.

But since you also seem to use the enums.xsd as episode, this effectively prevents generation of enum classes. With no enum classes, customizations cannot be applied so they are not marked as "recognized" and this produces the error you get.

To solve this, when compiling enums.xsd, simply move customizations into a separate bindings file and use it during compilation.

Then, when compiling other schemas, use enums.xsd as episode but do not use that bindings file with customizations for enums.xsd.