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

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

and add the argument option to xjc target :

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

About this entry