May 25, 2013

Generating Collada loader with JAXB

Collada is a XML 3D Model data format. I was looking for a library that can automatically load in structure. I found JAXB that does exactly what I was looking for. However, there was a little trouble generating Java source code.

After struggling for a few hour, I was able to generate JAXB source codes from Collada Schema v1.5.
Here is how I did and it is for anybody who may have the problem I was having.

I got the Collada schema file from here.
At my first try, I got errors:
> xjc.exe -p collada collada_schema_1_5.txt
[ERROR] Property "Source" is already defined. Use <jaxb:property> to resolve this conflict.
[ERROR] The following location is relevant to the above error
This error is due to name conflict. The name "source" is defined for an attribute and inner element. It can be like this:
<skin_type source="somekindofURL"> <source></source> </skin_type>
 The generated Java source code from JAXB will have a member method "getSource()" for both the element and the attribute so does the conflict happens.

This issue can be resolved by providing "binding" information that simply rename one of them from "source" to something else. The binding information looks like this:
<bindings br="" xmlns="">          xmlns:xsi=""
    <bindings schemalocation="collada_schema_1_5.xsd" version="1.0">

        <!-- rename the value element -->
        <bindings node="//xs:complexType[@name='skin_type']">
            <bindings node=".//xs:attribute[@name='source']">
                <property name="source_attr">
        <bindings node="//xs:complexType[@name='morph_type']">
            <bindings node=".//xs:attribute[@name='source']">
                <property name="source_attr">

But this solves only half of the problem.
> xjc.exe -b collada_binding.xjc -p collada collada_schema_1_5.xsd
[ERROR] Property "MiOrMoOrMn" is already defined. Use <jaxb:property> to resolve this conflict.
 This error was easier to resolve by internet searching.

This issue can be solved by having an extension file:
<xs:schema br="" targetnamespace="no-such-thing">    xmlns:xs=""
    xmlns:jaxb="" jaxb:version="2.0"
    xmlns:xjc= "" jaxb:extensionBindingPrefixes="xjc">
 Now JXB should be able to generate the Java source files from Collada schema.
> xjc.exe -extension simpleMode.xjc -b collada_binding.xjc -p collada collada_schema_1_5.xsd
 I am not sure what kind of magic this extension file is playing but it works for me for now...

No comments: