XJC:两个声明引起ObjectFactory类碰撞 [英] xjc: Two declarations cause a collision in the ObjectFactory class

查看:1003
本文介绍了XJC:两个声明引起ObjectFactory类碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行下面的 XJC 命令引发错误:

  $ XJCftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd
解析模式...
编译架构...
[错误]两个声明引起ObjectFactory类的碰撞。
  ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd线340[错误](与上面的错误),这是其他声明。
  ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd线475

虽然我明白JAXB绑定,什么是在XJC冲突,我不明白的地方是在当前模式的冲突。

我应该怎么解决这个问题?

谢谢,

皮埃尔

更新:这里是错误的背景:

  $卷曲-sftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd| SED的/ ^ [\\ t] * //'|猫-n | egrep的-w -A 10 -B 10'(340 | 475)
   330 LT; XS:元素的maxOccurs =1NAME =说明
   331 TYPE =XS:字符串的minOccurs =0>
   332 LT; XS:注释>
   333< XS:文件>
   334可选当选择eOther特别提供的说明
   335 LT; / XS:文件>
   336< / XS:注释>
   337 LT; / XS:组件>
   338< XS:元素的名称=BioSampleSet的minOccurs =0的maxOccurs =1>< XS:注释>< XS:文件>号称&LT当生物样品的标识; / XS:文件>
   339 LT; / XS:注释>
   340 LT; XS:复杂类型>< XS:序列>< XS:元素的名称=ID的maxOccurs =无界类型=XS:令牌>< / XS:组件>
   341 LT; / XS:序列>
   342 LT; / XS:复杂类型>
   343 LT; / XS:组件>
   344 LT; / XS:序列>
   345°; XS:属性名称=sample_scope使用=需要>
   346 LT; XS:注释>
   347< XS:文件>
   348,生物样品的范围和纯度用于研究
   349< / XS:文件>
   350℃; / XS:注释>
-
   465< XS:文件>请填写说明元素选择eOther&LT时; / XS:文件>
   466< / XS:注释>
   467 LT; / XS:枚举>
   468 LT; / XS:限制>
   469 LT; / XS:简单类型>
   470 LT; / XS:属性>
   471 LT; / XS:复杂类型>
   472 LT; / XS:组件>
   473 LT; XS:元素的名称=TargetBioSampleSet>
   474 LT; XS:注释>< XS:文件>设置的目标提及生物样品< / XS:文件>< / XS:注释>
   475 LT; XS:复杂类型>
   476< XS:序列>
   477< XS:元素的名称=ID类型=XS:令牌的minOccurs =1的maxOccurs =无界>< / XS:组件>
   478< / XS:序列>
   479 LT; / XS:复杂类型>
   480 LT; / XS:组件>
   481< / XS:选择>
   482下; XS:元素名称=方法的minOccurs =1>
   483< XS:注释>
   484< XS:文件>
   485用于获取提交到存档数据库中的数据的核心实验方法


解决方案

我会从<报价href=\"http://jaxb.java.net/guide/Dealing_with_errors.html#Two_declarations_cause_a_collision_in_the_ObjectFactory_class\">the在JAXB大多数官方非官方指南在网络上。


  

在架构包含容貌相似元素/类型名称,他们可以
  造成两个声明引起的ObjectFactory碰撞
  类错误。为了更precise,对于每一个所有类型的许多
  元素(到底是什么因素让一个工厂,什么不算位
  棘手的解释),XJC产生的ObjectFactory类的一种方法
  在同一个包。 ObjectFactory类为每个创建
  包XJC产生一些文件之中。该方法的名称是
  从XML元素/类型名称来源,并报告错误如果两个
  元素/类型尝试生成相同的方法名称


这是说,你有两个选择。

首先是定义一个外部绑定这样的XML

 &LT; JAXB:绑定的xmlns:JAXB =htt​​p://java.sun.com/xml/ns/jaxb
  的xmlns:XS =htt​​p://www.w3.org/2001/XMLSchema
  版本=1.0&GT;
  &LT; JAXB:绑定的schemaLocation =Core.xsd&GT;
    &LT; JAXB:绑定节点=// XS:元素[@名称='BioSampleSet'] / XS:复杂类型&GT;
      &LT; JAXB:factoryMethod NAME =TypeBioSampleSet/&GT;
    &LT; / JAXB:绑定&GT;
    &LT; JAXB:绑定节点=// XS:元素[@名称='TargetBioSampleSet'] / XS:复杂类型&GT;
      &LT; JAXB:factoryMethod NAME =TypeTargetBioSampleSet/&GT;
    &LT; / JAXB:绑定&GT;
  &LT; / JAXB:绑定&GT;
&LT; / JAXB:绑定&GT;

在生成的的ObjectFactory 类,这将创建两个方法叫做 createTypeBioSampleSet createTypeTargetBioSampleSet (JAXB将追加指定字的名称创建),可用于生产 BioSampleSet TargetBioSampleSet 的对象。

(这是没有必要定义绑定的两个的类型。)

我不知道是什么原因JAXB拒绝生成从给定的架构类,但是当我只指定了一个( BioSampleSet )后绑定然后其他类型的工厂方法被命名为喜欢<$c$c>createTypeProjectProjectTypeSubmissionWhateverThisAndThatTargetTargetSampleBioCatDogWoofTypeIDoNotKnowWhatElse所以我觉得JAXB哽咽这漫长的方法标识符,因为它总算创建的同一个两种类型。我认为这是JAXB一些实现细节。

另一个解决方案是一个 BioSampleSet 创建一个基本类型,并使用在这样

这两个位置

 &LT; XS:元素的名称=ProjectTypeSubmission&GT;...  &LT; XS:元素的名称=目标&GT;    ...    &LT; XS:元素的名称=BioSampleSetTYPE =typeBioSampleSet的minOccurs =0的maxOccurs =1/&GT;    ...  &LT; / XS:组件&gt;  ...  &LT; XS:元素的名称=TargetBioSampleSetTYPE =typeBioSampleSet/&GT;  ...&LT; XS:元素/&GT;...&LT; XS:复杂类型名称=typeBioSampleSet&GT;
  &LT; XS:序列&GT;
    &LT; XS:元素的名称=ID的maxOccurs =无界类型=XS:令牌&GT;&LT; / XS:组件&gt;
  &LT; / XS:序列&GT;
&LT; / XS:复杂类型&GT;

最好的解决办法是每一个匿名类型声明,从你的schema下降。如果你能做到这一点,做到这一点,因为该模式(至少对我来说)看起来像一个烂摊子。

Running the following xjc command raises an error :

$ xjc "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd"
parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 340 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

[ERROR] (Related to above error) This is the other declaration.   
  line 475 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

Although I understand the JAXB bindings and what are is conflict in XJC, I don't understand where is the conflict in the current schema.

how should I fix this ?

Thanks,

Pierre

update: here is the context of the errors:

$ curl -s "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd" | sed 's/^[ \t]*//' | cat -n | egrep -w -A 10 -B 10 '(340|475)' 
   330  <xs:element maxOccurs="1" name="Description"
   331  type="xs:string" minOccurs="0">
   332  <xs:annotation>
   333  <xs:documentation>
   334  Optionally provide description especially when "eOther" is selected
   335  </xs:documentation>
   336  </xs:annotation>
   337  </xs:element>
   338  <xs:element name="BioSampleSet" minOccurs="0" maxOccurs="1"><xs:annotation><xs:documentation>Identifier of the BioSample when known</xs:documentation>
   339  </xs:annotation>
   340  <xs:complexType><xs:sequence><xs:element name="ID" maxOccurs="unbounded" type="xs:token"></xs:element>
   341  </xs:sequence>
   342  </xs:complexType>
   343  </xs:element>
   344  </xs:sequence>
   345  <xs:attribute name="sample_scope" use="required">
   346  <xs:annotation>
   347  <xs:documentation>
   348  The scope and purity of the biological sample used for the study
   349  </xs:documentation>
   350  </xs:annotation>
--
   465  <xs:documentation>Please,  fill Description element when choose "eOther"</xs:documentation>
   466  </xs:annotation>
   467  </xs:enumeration>
   468  </xs:restriction>
   469  </xs:simpleType>
   470  </xs:attribute>
   471  </xs:complexType>
   472  </xs:element>
   473  <xs:element name="TargetBioSampleSet">
   474  <xs:annotation><xs:documentation>Set of Targets references to BioSamples</xs:documentation></xs:annotation>
   475  <xs:complexType>
   476  <xs:sequence>
   477  <xs:element name="ID" type="xs:token" minOccurs="1" maxOccurs="unbounded"></xs:element>                                                 
   478  </xs:sequence>
   479  </xs:complexType>
   480  </xs:element>                        
   481  </xs:choice>
   482  <xs:element name="Method" minOccurs="1">
   483  <xs:annotation>
   484  <xs:documentation>
   485  The core experimental approach used to obtain the data that is submitted to archival databases

解决方案

I'll quote from the most official unofficial guide on JAXB on the net.

When schemas contain similar looking element/type names, they can result in "Two declarations cause a collision in the ObjectFactory class" errors. To be more precise, for each of all types and many elements (exactly what elements get a factory and what doesn't is bit tricky to explain), XJC produces one method on the ObjectFactory class in the same package. The ObjectFactory class is created for each package that XJC generates some files into. The name of the method is derived from XML element/type names, and the error is reported if two elements/types try to generate the same method name.

That said, you have two options.

The first is to define an external binding XML like this

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="1.0">
  <jaxb:bindings schemaLocation="Core.xsd">
    <jaxb:bindings node="//xs:element[@name='BioSampleSet']/xs:complexType">
      <jaxb:factoryMethod name="TypeBioSampleSet"/>
    </jaxb:bindings>
    <jaxb:bindings node="//xs:element[@name='TargetBioSampleSet']/xs:complexType">
      <jaxb:factoryMethod name="TypeTargetBioSampleSet"/>
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>

In the generated ObjectFactory class this will create two methods called createTypeBioSampleSet and createTypeTargetBioSampleSet (JAXB will append the name you specify to the word create) that can be used to produce BioSampleSet and TargetBioSampleSet objects.

(It's not necessary to define a binding for both types.)

I'm not exactly sure why JAXB refuses to generate classes from the given schema, but when I specified only one binding (for BioSampleSet for example) then the other type's factory method was named like createTypeProjectProjectTypeSubmissionWhateverThisAndThatTargetTargetSampleBioCatDogWoofTypeIDoNotKnowWhatElse so I think JAXB choked on this long method identifier, because it somehow managed to create the same one for both types. I think this is some implementation detail in JAXB.

The other solution is to create a base type for a BioSampleSet and use that at both locations like this

<xs:element name="ProjectTypeSubmission">

...

  <xs:element name="Target">

    ...

    <xs:element name="BioSampleSet" type="typeBioSampleSet" minOccurs="0" maxOccurs="1"/>

    ...

  </xs:element>

  ...

  <xs:element name="TargetBioSampleSet" type="typeBioSampleSet"/>

  ...

<xs:element/>

...

<xs:complexType name="typeBioSampleSet">
  <xs:sequence>
    <xs:element name="ID" maxOccurs="unbounded" type="xs:token"></xs:element>
  </xs:sequence>
</xs:complexType>

The best solution would be to drop every anonymous type declarations from your schema. If you can do that, do it, because this schema looks like a mess (to me at least).

这篇关于XJC:两个声明引起ObjectFactory类碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆