Reputation: 2122
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:
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.
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>
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>
The xjc file :
<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)
Upvotes: 4
Views: 4437
Reputation: 43699
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.
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.
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.
Upvotes: 2
Reputation: 43699
Try
<bindings node="//xs:simpleType[@name='CodingSystemType']">
<typesafeEnumClass name="CountryCodingSystemType" />
</bindings>
instead.
I think XJC makes a difference between customizations enums and normal classes. See the related question:
Upvotes: 4