0

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221
Kristof Plennings
  • 469
  • 1
  • 6
  • 17

1 Answers1

0

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Sorry for the late response, I've been pretty occupied with other tasks. With the example above the enum classes are generated correctly (albeit without annotations) if I remove the annotations in the XSD file and thanks to the episode file the generated enum classes are reused in the generation of the two other files rather than the enum classes being duplicated. The problem with adding a binding file is that I can't seem to use both a binding file and episodes. https://github.com/highsource/maven-jaxb2-plugin/issues/131 . If you'd like I could mail you a small sample mail project. – Kristof Plennings Feb 06 '18 at 06:52
  • @KristofPlennings You absolutely can use episodes and binding files. See the [ogc-schemas](https://github.com/highsource/ogc-schemas) project, it heavily uses episodes and uses binding files almost everywhere. – lexicore Feb 08 '18 at 15:56
  • odd if I add a binding to a class it no longer ends up in the episode file. – Kristof Plennings Feb 13 '18 at 09:51
  • @KristofPlennings It's not odd, it's due to the fact that episode files generation is implemented rather simple. – lexicore Feb 13 '18 at 11:12
  • Oh I know, but then how would you combine it like you mentioned on the 8th? Since for the annotation I'd need to use a binding files like you mentioned in your original topic, but then it would no longer end up in the episode file of the enum. – Kristof Plennings Feb 14 '18 at 08:00
  • @KristofPlennings If you put additional bindings in `src/main/resources`, they would end up in the resulting JAR. `maven-jaxb2-plugin` allows you to reference bindings from dependency resources - so you can use the episode (automatically) together with other bindings from the JAR (needs explicit configuration) together. – lexicore Feb 14 '18 at 08:48