Download NetBeans!

20070625 Monday June 25, 2007

JAXB and Visual Editors in 6.0

Those who like setting up tents in rain storms or eating dry cornflakes in the eye of a tornado will like the XML Multiview API. I've blogged about it before—the end result is extremely cool, but the long road there is a twisted one. But in 6.0 things get much better! Why? Because of the cool JAXB Wizard. It can generate the entire model for you, from an XML document, like a schema or a DTD, for example. The people behind this wizard are from the Web Service Core team, so the wizard is primarily meant for web service related XML documents like WSDL files. However, I grabbed one of the NetBeans DTDs, the one that describes the Editor Palette Item XML file, and some time later I had the basis of a visual editor for this DTD, as can be seen here:

Above, you also see all the classes I needed to create the above multiview editor. Not that many, considering the coolness of the result.

In the past, I always used the Schema2Beans module, which is a NetBeans-specific approach to working with XML from Java. But JAXB does the job just as well and, on top of that, is the official standard when it comes to marshalling/unmarshalling Java objects to/from XML. Hence, that's what NetBeans IDE 6.0 will support, a complete wizard for generating JAXB artifacts. And you can regenerate, after changing the options you had initially set. Plus, there will be templates that will generate stubs for the marshalling/unmarshalling code.

This is the magic snippet that connects the XML Multiview API infrastructure to the JAXB architecture. If there is one crucial piece to the whole puzzle, then it is this one:

private ObjectFactory factory = new ObjectFactory();
EditorPaletteItem item = null;

public EditorPaletteItem getPaletteItem() throws IOException {
    try {
        item = factory.createEditorPaletteItem();
        JAXBContext jaxbCtx = JAXBContext.newInstance(item.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        item = (EditorPaletteItem) unmarshaller.unmarshal(FileUtil.toFile(this.getPrimaryFile()));
        return item;
    } catch (JAXBException ex) {
        Exceptions.printStackTrace(ex);
    }
    return null;
}

Thanks Girish, for helping with the above snippet. (And for working on the excellent JAXB Wizard, of course). I'm looking forward to working with the templates soon, for marshalling and unmarshalling, and will blog about those when I start working with them. Plus, the above scenario will end up in a tutorial. Soon, though, we should have a wizard that generates the entire XML Multiview API infrastructure, which is a project I'm working on with Vadiraj in Bangalore and Toni in Munich. Since implementations of the API, at least the central part of it, is probably about 95% the same, a wizard would be perfect in this area, especially since implementations are so hard to put together. Hmmm... Toni is in Munich, Vadiraj in Bangalore, while I am in Prague. So maybe we should have a team meeting soon, somewhere half way between our three locations. I wonder where that would be. Maybe I need to create a NetBeans module to work that out. I could probably use the NASA World Wind globe somehow in that module, even if just for decoration.

Jun 25 2007, 05:13:50 PM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/jaxb_and_visual_editors_in
Comments:

Nice work! is there any place where I can take a look at the code please?

Posted by Enrico Scantamburlo on June 26, 2007 at 01:00 AM PDT #

Hi! First of all: Great blog! But concerning JAXB & NB-modules I got a problem. (Couldn't find a solution anywhere yet :( ) I created a small XML Schema and xjc-ed the .java for it. Everything works on a stand-alone project. But when I try to do the same code snippet within a NetBeans module I get a NoClassDefFoundError for JAXBContext. Means: JAXBContext.newInstance(MyStuff.class); leads to java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException I got no idea how to solve this. JAXBContext is a JDK-class (I'm using 1.6) but I get the error on NetBeans 5.5.1/6.0M10. Any ideas about how to solve this are _VERY_ welcome! Thanks & bye David

Posted by david on July 21, 2007 at 01:10 PM PDT #

Got it! The solution is to add an implementation dep. to JAX-WS21 - as you wrote in Part1 of "Visual Editor".
So as I already said: Thanks again for this cool blog! :)

Posted by david on July 21, 2007 at 01:53 PM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed