简体   繁体   English

如何使用XJC影响组声明的类生成

[英]How to influence class generation of group declarations with XJC

We have the following problem. 我们有以下问题。 We try to generate Java code from XSD files that contain group declarations and multiple group references to these group declarations. 我们尝试从XSD文件生成Java代码,这些文件包含组声明和对这些组声明的多个组引用。 Here a simplified version: 这里是一个简化的版本:

<xs:group name="Information">
  <xs:sequence>
    <xs:element name="Name">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute name="updated" type="xs:boolean"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
 </xs:sequence>
</xs:group>

<xs:element name="Address">
  <xs:complexType>
    <xs:sequence>
      <xs:group ref="Information" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="Customer">
  <xs:complexType>
    <xs:sequence>
       <xs:group ref="Information"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

The main points are: 要点如下:

  • Each of these elements is defined in its own file 这些元素中的每一个都在自己的文件中定义
  • The same group Information is references twice 同一组Information被引用两次
  • There is no separate type for the group 该组没有单独的类型
  • There is also no separate type for the Name element inside the group 组内的Name元素也没有单独的类型
  • We cannot change the XSD file 我们无法更改XSD文件

The problem we face now is that xjc generates non-deterministically depending on the machine where we build, sometimes the type Address.Name and sometimes the type Customer.Name , because the Name element is a complex type and requires a type. 现在我们面临的问题是xjc会不确定地生成,具体取决于我们构建的机器,有时是Address.Name类型,有时是Customer.Name类型,因为Name元素是复杂类型,需要类型。

Is there any way to tell xjc to always generate the same type? 有什么办法告诉xjc总是生成相同的类型?

  • Customize the anonymous complex type with the jaxb:class binding and specify the class name. 使用jaxb:class绑定定制匿名复杂类型,并指定类名称。
  • Use <jaxb:globalBindings localScoping="toplevel"/> to generate inner classes on the top level instead. 使用<jaxb:globalBindings localScoping="toplevel"/>可以在<jaxb:globalBindings localScoping="toplevel"/>生成内部类。

A combination of these two will give you a predictable class. 两者的结合将为您提供可预测的课程。

You could try the -episode command line option of XJC . 您可以尝试XJC-episode 命令行选项 It is primarily meant for compilation in multiple steps, but I think it basically "dumps decisions" taken by XJC during a compilation into a (binding) configuration file. 它主要用于多步编译,但是我认为它基本上是将XJC在编译过程中做出的决定“转储”到(绑定)配置文件中。 If you are lucky, you find suitable settings related to the Name element/type in the episode file which you can copy into your binding configuration file to make XJC behave deterministically. 如果幸运的话,您可以在情节文件中找到与Name元素/类型相关的合适设置,您可以将这些设置复制到绑定配置文件中,以使XJC具有确定性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM