JAXB 将绑定应用于多个 .xsd 模式

JAXB apply bindings to more than one .xsd schema

我有几个我无法控制的 XML 模式,我想在通过 XJC 生成 Java 类 时应用某些自定义。

一些自定义设置应适用于所有 .xsd 文件,但我不确定如何实现。

目前我有这个 .xjb 文件:

<jxb:bindings version="2.1" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
  jxb:extensionBindingPrefixes="inheritance">

    <jxb:bindings schemaLocation="../schemas/A.xsd">
         <jxb:schemaBindings>
            <jxb:package name="my.foo.a" />
        </jxb:schemaBindings> 

        <jxb:bindings node="//xs:complexType[@name='A']">
            <jxb:class name="A" />
            <inheritance:implements>my.foo.interfaces.IA</inheritance:implements>           
         </jxb:bindings>

         <jxb:bindings node="//xs:complexType[@name='Device']">
            <jxb:class name="Device" />
            <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
         </jxb:bindings>             
    </jxb:bindings>

    <jxb:bindings schemaLocation="../schemas/B.xsd">        
        <jxb:schemaBindings>
            <jxb:package name="my.foo.b" />
        </jxb:schemaBindings>

        <jxb:bindings node="//xs:complexType[@name='B']">
            <jxb:class name="B" />
            <inheritance:implements>my.foo.interfaces.IB</inheritance:implements>       
         </jxb:bindings>

        <jxb:bindings node="//xs:complexType[@name='Device']">
            <jxb:class name="Device" />
            <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
         </jxb:bindings>             
    </jxb:bindings>      
</jxb:bindings>

还有这个 Ant 任务:

<taskdef name="xjc" classname="org.jvnet.jaxb2_commons.xjc.XJC2Task">
    <classpath>
        <fileset dir="../../dependencies/lib" >
            <include name="activation-*.jar"/>
            <include name="jaxb-api-*.jar"/>
            <include name="jaxb-impl-*.jar"/>
            <include name="jsr173_api-*.jar"/>
            <include name="stax-api-*.jar"/>

            <include name="jaxb2-basics-jaxb-xjc-*.jar"/>
            <include name="jaxb-xjc-*.jar"/>
            <include name="jaxb2-basics-ant-*.jar"/>
        </fileset>
    </classpath>
</taskdef>

<xjc destdir="../src" extension="true" header="false" binding="bindings.xjb">
    <arg line="-Xinheritance"/>

    <schema dir="../schemas">
        <include name="*.xsd"/>
        <exclude name="Foo.xsd"/>
    </schema>

    <!-- Plugins -->
    <classpath>
        <fileset dir="../../dependencies/lib">
            <!-- JAXB2 Basics library -->
            <include name="jaxb2-basics-*.jar"/>
            <!-- JAXB2 Basics library dependencies -->
            <include name="jaxb2-basics-runtime-*.jar"/>
            <include name="jaxb2-basics-tools-*.jar"/>
            <include name="javaparser-*.jar"/>
            <include name="commons-beanutils-*.jar"/>
            <include name="commons-lang-*.jar"/>
            <include name="commons-logging-*.jar"/>
        </fileset>
    </classpath>
</xjc>

最后我想做的是以某种方式指定公共块(其中我有多个),即:

<jxb:bindings node="//xs:complexType[@name='Device']">
    <jxb:class name="Device" />
    <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
</jxb:bindings> 

对于我所有的 .xsd 个文件。

假设是这样的:

<jxb:bindings version="2.1" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
  jxb:extensionBindingPrefixes="inheritance">

    <jxb:bindings schemaLocation="../schemas/*.xsd">
        <jxb:bindings node="//xs:complexType[@name='Device']">
            <jxb:class name="Device" />
            <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
        </jxb:bindings>  
    </jxb:bindings>

    <jxb:bindings schemaLocation="../schemas/A.xsd">
        <jxb:schemaBindings>
            <jxb:package name="my.foo.a" />
        </jxb:schemaBindings> 

        <jxb:bindings node="//xs:complexType[@name='A']">
            <jxb:class name="A" />
            <inheritance:implements>my.foo.interfaces.IA</inheritance:implements>           
        </jxb:bindings>           
    </jxb:bindings>

    <jxb:bindings schemaLocation="../schemas/B.xsd">        
        <jxb:schemaBindings>
            <jxb:package name="my.foo.b" />
        </jxb:schemaBindings>

        <jxb:bindings node="//xs:complexType[@name='B']">
            <jxb:class name="B" />
            <inheritance:implements>my.foo.interfaces.IB</inheritance:implements>       
        </jxb:bindings>       
    </jxb:bindings>      
</jxb:bindings>

我怎样才能做到这一点?

所以,(大部分)在这里使用这个答案:

我已经设法使架构正常工作,作为参考,您需要做的就是设置 schemaLocation="*"(当然是为了读取所有架构)并用 [=12= 标记绑定元素] 和 required="false" 因为有些模式可能包含这些元素(一次或多次),有些可能根本不包含它们。

<jxb:bindings version="2.1" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
  jxb:extensionBindingPrefixes="inheritance">

    <jxb:bindings schemaLocation="*">
        <jxb:bindings node="//xs:complexType[@name='Device']" multiple="true" required="false">
            <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
        </jxb:bindings>  
    </jxb:bindings>
    <!-- rest of the bindings (per schema ones) go here -->
</jxb:bindings>

请注意,出于某些(我不知道的)原因,我不得不删除 class 绑定(例如 <jxb:class name="Device" />)以使文件被 XJC 接受。

它大部分都有效,我无法开始工作的是 inheritance:extends 自定义,它不想 运行 在与 implements 相同的条件下一个...