XJC - 编译器无法遵守此类自定义 [英] XJC - compiler was unable to honor this class customization

查看:99
本文介绍了XJC - 编译器无法遵守此类自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Java项目调用ISAN Restful API,所以我尝试使用maven-jaxb2-plugin从xsd文件生成java bean。以下是xsds:

I'd like to call ISAN Restful API from my Java project, so I'm trying to generate java beans from xsd files using maven-jaxb2-plugin. Here are the xsds:

  • http://www.isan.org/schema/v1.11/common/common.xsd
  • http://www.isan.org/schema/v1.21/common/serial.xsd
  • http://www.isan.org/schema/v1.11/common/version.xsd
  • http://www.isan.org/ISAN/isan.xsd
  • http://www.isan.org/schema/v1.11/common/title.xsd
  • http://www.isan.org/schema/v1.11/common/externalid.xsd
  • http://www.isan.org/schema/v1.11/common/participant.xsd
  • http://www.isan.org/schema/v1.11/common/language.xsd
  • http://www.isan.org/schema/v1.11/common/country.xsd

我下载了这些文件并将它们复制到我的src / main / resources文件夹中并定义了一个目录。
当我构建项目时,我收到一个错误,因为两个类型具有相同的名称:

I downloaded theses files and copied them into my src/main/resources folder and defined a catalog. When I build the project, I get an error because two types have the same name:

org.xml.sax.SAXParseExceptionpublicId: http://www.isan.org/schema/v1.11/common/language; systemId: http://www.isan.org/schema/v1.11/common/language.xsd; lineNumber: 39; columnNumber: 48; A class/interface with the same name "org.isan.CodingSystemType" is already in use. Use a class customization to resolve this conflict.
org.xml.sax.SAXParseExceptionpublicId: http://www.isan.org/schema/v1.11/common/country; systemId: http://www.isan.org/schema/v1.11/common/country.xsd; lineNumber: 39; columnNumber: 48; (Relevant to above error) another "CodingSystemType" is generated from here.

这是正确的:language.xsd和country.xsd都定义了一个名为CodingSystemType的类型:

That is correct : language.xsd and country.xsd both define a type named CodingSystemType:

    <xs:simpleType name="CodingSystemType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="ISO639_2"/>
            <xs:enumeration value="RFC3066"/>
        </xs:restriction>
    </xs:simpleType> 

    <xs:simpleType name="CodingSystemType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="ISO3166_1"/>
        </xs:restriction>
    </xs:simpleType> 

根据建议,我尝试使用country.xsd类型的类自定义。我在pom.xml中添加了这个绑定:

As suggested, I tried to use a class customization for the country.xsd type. I added this binding in pom.xml:

<bindings>
    <binding>
        <url>http://www.isan.org/schema/v1.11/common/country.xjb</url>
    </binding>
</bindings>

xjc文件:

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:annox="http://annox.dev.java.net"
    xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
    <bindings schemaLocation="http://www.isan.org/schema/v1.11/common/country.xsd">

        <bindings node="//xs:simpleType[@name='CodingSystemType']">
        <class name="CountryCodingSystemType" />
        </bindings>

    </bindings>
</bindings>

现在,我收到另一个我无法处理的错误:

Now, I get another error I can't deal with :

[ERROR] Error while parsing schema(s).Location [ http://www.isan.org/schema/v1.11/common/country.xjb{7,58}].
com.sun.istack.SAXParseException2; systemId: http://www.isan.org/schema/v1.11/common/country.xjb; lineNumber: 7; columnNumber: 58; compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings.
[ERROR] Error while parsing schema(s).Location [ http://www.isan.org/schema/v1.11/common/country.xsd{39,48}].
com.sun.istack.SAXParseException2; systemId: http://www.isan.org/schema/v1.11/common/country.xsd; lineNumber: 39; columnNumber: 48; (the above customization is attached to the following location in the schema)


推荐答案

我将添加另一个答案,因为它涉及有关您的架构编译的不同主题。

I'll add another answer since it takes on a different topic concerning your schema compilation.

我还注意到这两个简单类型实际上属于不同的命名空间。所以它们实际上不应该首先在同一个包中生成。

What I also noticed is that these two simple types actually belong to different namespaces. So they actually should not be generated in the same package in the first place.

我想你只需要使用 generatePackage 指定 org.isan 作为目标包。因此,所有命名空间最终都在一个包中,这非常糟糕。如果每个命名空间有一个包,则JAXB最有效。如果你不这样做会很奇怪。

I guess you just use generatePackage to specify org.isan as your target package. So all your namespaces end up in one package which is pretty bad. JAXB works best if you have one package per namespace. It will get weird if you don't.

所以我一般不鼓励使用 generatePackage ,使用 jaxb:package 而不是:

So I generally discourage usage of generatePackage, use jaxb:package instead:

  <bindings schemaLocation="http://www.isan.org/schema/v1.11/common/country.xsd">
    <schemaBindings>
      <package name="org.isan.schema.v1_11.common.country"/>
    </schemaBindings>
  </bindings>

我还建议使用主要/次要架构版本的软件包名称。您可能需要稍后并行支持多个模式版本。

I'd also recommend using major/minor schema version the the package name. You may need to support several schema versions in parallel later on.

这篇关于XJC - 编译器无法遵守此类自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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