Prevent generation of duplicate classes from XSDs without namespaces

走远了吗. 提交于 2020-01-15 17:36:02

问题


I am currently implementing functionally for our Netbeans Platform application using classes generated by JAXB using the Mojo Maven plugin 'jaxb2-maven-plugin'. Unfortunately the creation of the XSD files is not in my hands and they are confidential. I am trying to provide a minimal running example to further demonstrate this but for now I hope that somebody can push me in the right direction solely from my description.

We have many XSD files and got a few additions in the last weeks. Two of these XSDs (lets call them A.xsd and B.xsd) include another XSD (lets call it common.xsd) which contains common types used by both of the other XSDs. This common.xsd has no namespace and this should stay this way.

This creates the following problem: For types defined in this common.xsd there are three duplicates generated by xjc. One residing in a package named 'generated' (exactly the classes I want to use) and two others residing in the packages of A.xsd and B.xsd which are mostly the same as the classes in 'generated' except a few namespaces which are not needed by me.

I understand from reading a few other questions on Stackoverflow that Episodes would resolve this but I am not getting this to work with a XSD without a namespace.

My plugin configuration in my POM is pretty simple:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <configuration>
        <npa>true</npa>
    </configuration>
    <executions>
       <execution>
            <goals>
                <goal>xjc</goal>
            </goals>
       </execution>
    </executions>
</plugin>

Is there a resolution for this problem using episodes with a special configuration or maybe some kind of binding that I can use to resolve this?


回答1:


This is an explanation of why episodes fail, unfortunately, not an answer to your question.

You can use jaxb:class/@ref binding to map your schema type onto some existing class. See this post by Blaise.

In short, you can do something like:

<jaxb:binding node="...point to your type here">
    <jaxb:class ref="ExistingClass"/>
<jaxb:binding>

In this case ExistingClass will be reused for your type.

You are trying to make it work with episodes. Episode, in essence is exactly a set of such mappings. BUT episodes use SCD (schema component designator) to point to types, NOT node with XPath-Expressions. And SCD is namespace-driven. If you have no namespace - or, better to say - you have chameleon namespace design, then you don't have a correct namespace. Therefore episodes fail.

The problem is that your "common" classes should use the namespace of the "host" schema (this is what chameleon design is about). Namespace and local names are actually provided in annotations. If you have just one set of classes and one set of annotations - then you have just one namespace. So at the moment I don't see an easy way to have just one set of "common" classes.

If you can to use A and B separately, that is, not in the same context at the same time then it would be possible to trick the annotation reader to use the specified namespace for common.

But if you want to use A and B at the same time, I really don't know how to do it. Maybe you could invent a namespace for common and then apply some preprocessing to replace the namespaces for common elements in A and B by this namespace...

So this is not an answer, more an elaboration on the backgrounds.



来源:https://stackoverflow.com/questions/26274407/prevent-generation-of-duplicate-classes-from-xsds-without-namespaces

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