highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
109 stars 54 forks source link

Compatibility of inheritance plugin with "-episode" #123

Closed Vampire closed 9 months ago

Vampire commented 4 years ago

I tried to do something like shown at https://dzone.com/articles/reusing-generated-jaxb-classes, with the difference, that the imported schema uses inheritance:implements tags to define interfaces for some of the classes. But it seems this does not work properly. The other way around, having inheritance:implements in the importing schema works. When translating the imported schema with the inheritance:implements tags, the episode file is generated. If I then want to translate the importing schema, giving the episode file as binding, I get complaints like these:

[ERROR] compiler was unable to honor this implements customization. It is attached to a wrong place, or its inconsistent with other bindings.
  line 372 of file:/.../imported.xsd

[ERROR] (the above customization is attached to the following location in the schema)
  line 366 of file:/.../imported.xsd

Is this not supported? Can it be supported? Can I do something differently to make it work?

mattrpav commented 3 years ago

Can you share your code? We do episodes+inheritance. We place the inheritance rules in a jaxb-bindings file instead of the schema, and we use catalogs for namespace replacement.

Vampire commented 3 years ago

Find attached a showcase.zip that demoes the problem. Just unpack it and execute gradlew showcase in it.

laurentschoelens commented 1 year ago

@Vampire thanks for the share. In case you want to know, I managed to make your showcase works with the following adjustements :

  1. Remove inheritance from foo.xsd
    <xs:simpleType name="foo">
        <xs:restriction base="xs:string">
            <xs:enumeration value="FOO"/>
            <xs:enumeration value="BAR"/>
        </xs:restriction>
    </xs:simpleType>
  1. Create the foo.xjb binding file with inheritance customization
<jaxb:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
        jaxb:extensionBindingPrefixes="inheritance"
        jaxb:version="2.1">

    <jaxb:bindings schemaLocation="foo.xsd" node="/xsd:schema">
        <jaxb:bindings node="xsd:simpleType[@name='foo']">
            <inheritance:implements>baz.Baz</inheritance:implements>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>
  1. Add the following arg to XJC task for foo build

"binding"("file" to "foo.xjb")

I got the following generated with in bar-dest ObjectFactory refering to bar

image
Vampire commented 1 year ago

Thanks for the work-around. It would still be nice if it worked with the inline definition, because having it in an extra file decreases readability and maintainability imho. But at least if we need to have this constellation, I now have a work-around in the backhand, thanks. Currently, I only had it while playing around and the actual case where he have it, inheritance is not used so far. :-)

laurentschoelens commented 1 year ago

Thanks for the work-around. It would still be nice if it worked with the inline definition, because having it in an extra file decreases readability and maintainability imho. But at least if we need to have this constellation, I now have a work-around in the backhand, thanks. Currently, I only had it while playing around and the actual case where he have it, inheritance is not used so far. :-)

@Vampire you're welcome

I found that in jaxb-annotate of highsource with same problem : https://github.com/highsource/jaxb2-annotate-plugin/issues/49

Seems like @highsource did know what could be done to solve the problem (but not giving hint on it - and sadly no more there to tell 😔) and also provide the same workaround as I do.

I'll have to debug to see if something's jumps out from the code. In the meantime, you still can use the workaround provided

laurentschoelens commented 1 year ago

Hi @Vampire,

Just made a PR in jaxb-ri to fix the above common problem with episode / inlined-plugin-customization in xsd.

laurentschoelens commented 9 months ago

Hi @Vampire

This is part of the release in 4.0.2 of jaxb-tools

Could you give it a try ?

We will backport this in v2 too soon

laurentschoelens commented 9 months ago

Fixed by https://github.com/highsource/jaxb-tools/issues/425