Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jaxb override package of a specific generated set of classes

Tags:

java

jaxb

xsd

I have a large third party xsd file that pulls in a whole bunch of other xsd files through imports. The whole thing generates over 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 don't specify a package, then the processor creates java packages using the target attributes in the xsd files, and the whole thing generates without any errors.

The problem is that the package structure is awful because the target names are chosen really poorly. Other developers hate it. They only need a few classes.

So what I've been trying to do is to give the processor a bindings.xml file where I want to specify certain classes to be generated in a given package name. I've gone back and forth between documentation on Oracle's site, forums, samples. I can't quote every single 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 Error to get generated in ABC. Any help would be greatly appreciated.

like image 495
Mike Avatar asked Jan 12 '12 16:01

Mike


2 Answers

Unfortunately it is not possible to do what you want. I want the same thing and I'm in the same boat. But straight from Oracles mouth:

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

Relevant Info:

name - is the name of the derived Java interface. It must be a legal Java interface name and must not contain a package prefix. The package prefix is inherited from the current value of package.

So there is simply no way on the element to specify a different package its inherited per xsd.

like image 78
Justin Dennahower Avatar answered Sep 29 '22 11:09

Justin Dennahower


The way I would do it 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>
like image 36
Petru Gardea Avatar answered Sep 29 '22 12:09

Petru Gardea