Geertjan's Blog

  • April 19, 2007

Schema2Beans Generator

Geertjan Wielenga
Product Manager
I followed Toni's 5 part Schema2Beans tutorial in his blog today (part1, part 2, part 3, part 4, part 5). Toni wrote that tutorial all the way back in October. At the time I looked at it, but had no idea what "Schema2Beans" was, nor why I would want to deal with it. In short, Schema2Beans is a NetBeans utility for generating JavaBeans for traversing XML elements and attributes that are governed by a DTD or Schema file. And why would you want to do that? You would want to do that when using the XML MultiView API, which you would use when creating a visual editor, comparable to the IDE's editor for web.xml files. There's a lot of functionality provided by this Schema2Beans utility, as described on the Schema2Beans site. There are also a few problems with it, though, and I hear that alternatives are being considered. In the meantime, however, there's a lot that the utility does do for you, though you might find yourself tweaking the generated beans in some situations, though I haven't found the need to do so yet.

How the utility works is, you use Ant scripts or Java code to specify the DTD or Schema file and then the utility generates all the JavaBeans, one for each element in the DTD or Schema. Typically you would use an Ant script, which is considerably simpler, and since you're going to be using the utility only once per DTD or Schema, since you'll only need to generate the beans once, the Ant script approach makes more sense. And normally you would run the Ant script from the command line. This is where Toni comes in. In the first part of the 5 part blog series, he writes: "For a dedicated NetBeans user, using a command line tool is like admitting defeat, so I had to include some basic support for Schema2Beans." So... Toni, in his blog, provides ALL the code for a wizard where one selects a DTD or Schema, one sets the Schema2Beans parameters, one then clicks Finish, and then everything else is automated by the wizard. No work for you at all. At the end of the wizard, you have a package containing all the generated beans, which you can then include in your module.

So I worked through Toni's tutorial, e-mailed a bit about things that weren't clear, and turned the result into a sample. It is a module that installs a sample in the New Project wizard and not simply a module that installs the "Generate Schema2Beans" menu item because (a) it could form the basis of a more extensive wizard (which would generate stubs for all classes required by the XML MultiView API) and so making the code available could be a good start to that end, (b) there might be bugs in the code and so if you have the code available as a sample, you might simply fix the code yourself, and (c) the people using Schema2Beans are NetBeans module writers and might therefore be interested in the code of this NetBeans module anyway. So, take the following steps and soon you'll be generating JavaBeans from DTDs and Schemas too.

  1. Download org-netbeans-modules-schema2beansgeneratortemplate.nbm and install it in NetBeans IDE 5.5.

  2. Go to the New Project wizard. You should see a new template:

  3. Select the above and complete the wizard. You now see this:

    The first package is the code of the module. The second package, called "examples" contains a very long list of NetBeans DTDs that I found in the NetBeans sources. They're just there to try out the functionality, once you've installed it. Otherwise, ignore these DTDs (there's also one Schema, but it doesn't seem to work in that case, for some reason).

  4. Now install the module. After installing, right-click the node of a DTD or Schema (or right-click inside a DTD or Schema in the editor). For example, one of the DTDs included in the "examples" package is the DTD of the sun-web.xml file. Right-click it and choose the new menu item:

  5. When you choose the menu item, you should see this one-step wizard:

    I don't know what all those parameters, visualized by checkboxes, mean. They're simply the parameters provided by Schema2Beans. Read the documentation for details on them. And the recommended ones are only recommended because the documentation recommends them. The "-javabeans" flag cannot be set (there is no checkbox provided for this reason), because that would generate classes that do not extend BaseBean, which is what we need them to extend. So, some choices have been made. However, you have the same dilemma now as you would have if you weren't using this GUI—which parameters to set? The GUI doesn't help you, it just provides you with a visual view on top of the Schema2Beans JARs.

  6. After choosing the menu item and specifying a package, all the JavaBeans are generated there, one per element in the DTD or Schema, exactly as would be the case if you were using Schema2Beans via an Ant script or self-coded Java. So, for the sun-web.xml DTD, there's a VERY long list of classes that are generated (I couldn't get them all in one screenshot):

    In some cases, I've found that the tool doesn't work. I haven't investigated why this is the case yet. Bear in mind that this is a very early release of this module. Consider it "pre-Alpha". One thing I have found is that attribute lists should be like this:

    <!ATTLIST bindings
    name CDATA #REQUIRED >

    But, not empty like this:

    <!ATTLIST bindings >

    Maybe one of the many parameters should be set to avoid this problem.

That's it, thanks to Toni's great blog tutorial. You're done and, for now, you're on your own. Take the classes and put them in the module where you're going to be creating your multiview editor. First thing to do after that, as explained yesterday, is to change the data object's extension class from whatever it is to XmlMultiViewDataObject. And then read Vadiraj's blog entries on the BookMultiView Sample (links to these blog entries are in my blog entry from yesterday). Vadiraj, Toni, and I are planning to extend this functionality further, so that in the short term the wizard will generate stubs for all the basic classes needed to implement the XML MultiView API. In the longer term, we're thinking about using the Visual Library API to let the user (i.e., a NetBeans module writer) design the layout of the nodes for their multiview editor. But don't hold your breath—it might take some time... By that time though, there could be a different/better/easier API for this. How about "Schliemann for MultiView Editors". Now that would be cool and makes complete sense, since the only difference between one multiview editor and the next is the content...

Join the discussion

Comments ( 2 )
  • Aaron Anderson Saturday, April 21, 2007
    With the advent of JAXB 2.0 couldn't Schema2Beans be replaced with JAXB 2.0 and custom extensions?
  • Geertjan4 Saturday, April 21, 2007
    Sure it could. I'd like to ask Vadiraj to blog about the differences between the two, because I know he's used both. I think he's using JAXB now.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.