users@jaxb.java.net

transparently switch between JDK internal JAXB and 2.1.7 on classpath

From: meinc <meinc_at_web.de>
Date: Tue, 22 Jul 2008 05:14:39 -0700 (PDT)

Is it feasible to fallback to the internal JAXB delivery shipped with JDK/JRE
6?

I need to add some behaviour to the generated JAXB classes and therfore have
to work with the generated ObjectFactory, which has to be explicity passed
to the created unmarshaller, e.g.

                        // create a JAXBContext capable of handling classes
generated into
                        // the package of this utility class
                        final JAXBContext jc = JAXBContext.newInstance(Util.class
                                        .getPackage().getName(), Util.class.getClassLoader());

                        // create an Unmarshaller
                        Unmarshaller unmarshaller = jc.createUnmarshaller();
                        unmarshaller = jc.createUnmarshaller();

                       
unmarshaller.setProperty("com.sun.xml.bind.ObjectFactory", new
ObjectFactory());


However, this code with hard coded proeprty name only works if an "external"
JAXB implementation is found in the classpathm e.g. JAXB 2.0.5 or JAXB
2.1.7. If not is throws a javax.xml.bind.PropertyException.
Now I modified my code to catch this exception and fall back to the internal
property name, i.e.

                        try {
                                unmarshaller.setProperty("com.sun.xml.bind.ObjectFactory",
                                                new ObjectFactory());
                        }
                        catch (final javax.xml.bind.PropertyException ex) {
                                unmarshaller.setProperty(
                                                "com.sun.xml.internal.bind.ObjectFactory",
                                                new ObjectFactory());
                        }

Although everything works fine in either cases, I'm not sure whether this is
feasible. At least it would be a bit cleaner to request the name of the
property to be used from whatever context via method invocation. Does such a
context/method exist?
-- 
View this message in context: http://www.nabble.com/transparently-switch-between-JDK-internal-JAXB-and-2.1.7-on-classpath-tp18587227p18587227.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.