Jaxb overrides the package of a specific generated set of classes

I have a large third-party xsd file that captures a whole bunch of other xsd files through import. All this creates more than 1000 classes. When I tell the xjc (jaxb) processor to generate everything in a specific package, I get all kinds of naming conflicts. If I do not specify a package, then the processor creates java packages using the target attributes in the xsd files, and all this is generated without any errors.

The problem is that the package structure is terrible because the target names are chosen very poorly. Other developers hate this. They only need a few classes.

So, what I was trying to do was provide the processor with a bindings.xml file where I want to specify specific classes that will be generated in the given package name. I went back and forth between the documentation on the Oracle website, forums, and samples. I cannot quote every iteration of my bindings file. My current incarnation is this:

<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.1" schemaLocation="heavy.xsd" node="//xsd:element[@name='Error']" > <jaxb:bindings node="//xsd:element[@name='Error']"> <jaxb:package name="ABC"/> </jaxb:bindings> <jaxb:globalBindings underscoreBinding="asCharInWord" localScoping="toplevel" typesafeEnumMaxMembers="10000" generateElementClass="true" > </jaxb:globalBindings> <!-- <jaxb:package name="ABC"/> --> <!-- <jaxb:schemaBindings> <jaxb:package name="ABC"/> </jaxb:schemaBindings> --> <!-- <jaxb:schemaBindings> <jaxb:package name="ABC"/> <jaxb:nameXmlTransform> <jaxb:elementName prefix="Error"/> </jaxb:nameXmlTransform> </jaxb:schemaBindings> --> </jaxb:bindings> 

No matter what I try, I have not yet seen it generate any classes in the ABC package. In this case, I want the Error to occur in ABC. Any help would be greatly appreciated.

+4
source share
3 answers

The way I do this is more like this:

 <?xml version="1.0" encoding="utf-8"?> <jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.1" jxb:extensionBindingPrefixes="xjc"> <jxb:bindings schemaLocation="XSD1.xsd" node="/xs:schema"> <jxb:globalBindings> <!-- --> </jxb:globalBindings> <jxb:schemaBindings xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"> <jxb:package name="com.something"> </jxb:package> </jxb:schemaBindings> <jxb:bindings node="//xs:element[@name='Error']"> <jxb:class xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" name="MyError"/> <jxb:property xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" name="Whatever"/> </jxb:bindings> </jxb:bindings> <jxb:bindings schemaLocation="XSD2.xsd" node="/xs:schema"> <jxb:schemaBindings xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"> <jxb:package name="com.somethingelse"> </jxb:package> </jxb:schemaBindings> </jxb:bindings> </jxb:bindings> 
+1
source

Unfortunately, it is impossible to do what you want. I want the same thing, and I'm in the same boat. But straight from the oracle of ur:

http://docs.oracle.com/cd/E19316-01/819-3669/bnbbt/index.html

Relevant Information:

name is the name of the Java derived interface. This must be the legal name of the Java interface and must not contain a package prefix. The package prefix is โ€‹โ€‹inherited from the current value of the package.

Thus, there is simply no way for elements to specify a different package that he inherited from xsd.

+1
source

Here is an example bindings.xjb file where the java source is created in the primer.myPo package.

 <jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <jxb:bindings schemaLocation="po.xsd" node="/xs:schema"> <jxb:globalBindings fixedAttributeAsConstantProperty="false" collectionType="java.util.Vector" typesafeEnumBase="xs:NCName" choiceContentProperty="false" typesafeEnumMemberName="generateError" enableFailFastCheck="false" generateIsSetMethod="false" underscoreBinding="asCharInWord"/> <jxb:schemaBindings> <jxb:package name="primer.myPo"> <jxb:javadoc><![CDATA[<body>Package level documentation for generated package primer.myPo.</body>]]> </jxb:javadoc> </jxb:package> <jxb:nameXmlTransform> <jxb:elementName suffix="Element"/> </jxb:nameXmlTransform> </jxb:schemaBindings> <jxb:bindings node="//xs:complexType[@name='PurchaseOrderType']"> <jxb:class name="POType"> <jxb:javadoc>A &lt;b>Purchase Order&lt;/b> consists of addresses and items.</jxb:javadoc> </jxb:class> </jxb:bindings> <jxb:bindings node="//xs:complexType[@name='USAddress']"> <jxb:class> <jxb:javadoc><![CDATA[First line of documentation for a <b>USAddress</b>.]]></jxb:javadoc> </jxb:class> <jxb:bindings node=".//xs:element[@name='name']"> <jxb:property name="toName"/> </jxb:bindings> <jxb:bindings node=".//xs:element[@name='zip']"> <jxb:property name="zipCode"/> </jxb:bindings> </jxb:bindings> <!-- node="//xs:complexType[@name='USAddress']" --> <jxb:bindings node="//xs:complexType[@name='Items']"> <jxb:bindings node=".//xs:element[@name='item']//xs:element[@name='quantity']"> <jxb:property generateIsSetMethod="true"/> <jxb:bindings node="./xs:simpleType"> <jxb:javaType name="short" parseMethod="javax.xml.bind.DatatypeConverter.parseShort" printMethod="javax.xml.bind.DatatypeConverter.printShort" /> </jxb:bindings> </jxb:bindings> <!-- node="//xs:complexType[@name='Items']" --> <jxb:bindings node=".//xs:element[@name='USPrice']"> <jxb:property name="Price"/> </jxb:bindings> <jxb:bindings node=".//xs:attribute[@name='partNum']"> <jxb:property name="partNumber"/> </jxb:bindings> </jxb:bindings> <jxb:bindings node="//xs:simpleType[@name='USState']"> <jxb:typesafeEnumClass/> </jxb:bindings> <jxb:bindings node="//xs:simpleType[@name='ZipCodeType']"> <jxb:javaType name="int" parseMethod="javax.xml.bind.DatatypeConverter.parseInt" printMethod="javax.xml.bind.DatatypeConverter.printInt"/> </jxb:bindings> </jxb:bindings> <!-- schemaLocation="po.xsd" node="/xs:schema" --> </jxb:bindings> 
0
source

Source: https://habr.com/ru/post/1390674/


All Articles