XML binding with JAXB 3/3 : plugins

Once you’ll have been working with JAXB basis, you’ll want to enhance what your doing. For example, in my case, I want to bind my datas with a GUI. For that, I’ll need to use beans binding (JSR 295) with netbeans. You can begin to design your GUI, and test it. It will almost work.

But you’ll see that your data changes are not propaged on your GUI components. The reason is that JAXB didn’t generate the firePropertyChanged calls in the set methods of your beans. And you don’t want to change manually the generated code.

The solution is in the plugins. If your search on internet, your find to user the property listener (AKA bound properties) injector plugin. Trying it, I discovered that this plugin generates fireVetoableChange calls, not exactly what I expected.

Fortunately, a hack of this plugin, Enhanced Property Listener Injection plugin, is available and make what I want.

You know just have to plug it in your build script.

ant

You’ll have to add the plugin jar in the library path of your xjc task :

    <target name=&quot;xjc-typedef-target&quot; depends=&quot;-init-project&quot;>
        <typedef classname=&quot;com.sun.tools.xjc.XJCTask&quot; name=&quot;xjc&quot; xmlns:s=&quot;http://xml.netbeans.org/schema/JAXBWizConfig&quot;>
            <classpath>
<pathelement path=&quot;${jaxbwiz.xjcdef.classpath}&quot; />
<pathelement location=&quot;lib/proplistinjector-0.1.1.jar&quot; />

            </classpath>
        </typedef>
    </target>

and add the argument option to xjc target :

        <xjc package=&quot;package.name&quot; destdir=&quot;build/generated/jaxbCache/SchemaName&quot; catalog=&quot;catalog.xml&quot;>
            <classpath>
<pathelement location=&quot;${src.dir}&quot;/>
<pathelement path=&quot;${jaxbwiz.xjcrun.classpath}&quot;/>
            </classpath>
            <arg value=&quot;-xmlschema&quot;/>
            <arg value=&quot;-extension&quot;/>
            <arg value=&quot;<span style=&quot;font-family: Consolas; font-size: x-small;&quot;>-Xinject-prop-listeners</span>&quot; />
            <schema file=&quot;xml-resources/jaxb/SchemaName/schemaname.xsd&quot;/>
<produces dir=&quot;build/generated/jaxbCache/schemaname&quot;/>
        </xjc>

maven

First, using Maven you’ll have to place your schema files in src/main/resources/.

You’ll find here (maven-jaxb2-plugin) how to configure your pom.xml file for JAXB (information you could find elsewhere), and more important how to enable JAXB 2 plugin.

Unfortunately, I haven’t been able to configure JAXB plugin with maven. So I’ll go back using ant.

If you want to go further using JAXB, here are two goods links for improving our knowledge :

  1. A JAXB Tutorial on glassfish website

  2. JAXB commons where you’ll find useful plugins

comments powered by Disqus