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="http://java.sun.com/xml/ns/jaxb">          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          version="2.1">
    <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">
            </property></bindings>
        </bindings>
       
        <bindings node="//xs:complexType[@name='morph_type']">
            <bindings node=".//xs:attribute[@name='source']">
                <property name="source_attr">
            </property></bindings>
        </bindings>

    </bindings></bindings>
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.
https://jaxb.java.net/guide/Dealing_with_errors.html
https://weblogs.java.net/blog/kohsuke/archive/2006/03/simple_and_bett.html

This issue can be solved by having an extension file:
<xs:schema br="" targetnamespace="no-such-thing">    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
    xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
  <xs:annotation>
      <xs:appinfo>
        <jaxb:globalbindings>
            <xjc:simple>
        </xjc:simple></jaxb:globalbindings>
      </xs:appinfo>
  </xs:annotation></xs:schema>
 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: