Top Linux Links You Must Click On


Using Apache Tuscany SDO and JSF To Build Dynamic Web Forms
Assembling a very generic system

This was the challenge: Build a generic system that lets users compare data suppliers in different categories. The data to be compared is defined by XML Schemas, where new schemas will be frequently uploaded and existing schemas may be changed. Moreover, the schemas aren't specifically designed for this system, so system specific metadata can't be added as attributes.

Based on the schemas the data suppliers must be presented with standard HTML forms to enter in their own specific data. Alternatively the suppliers must be able to use a Web Service interface.

Overall the sum of the requirements demanded extreme flexibility and robustness of the system.

So basically we wanted something that could go from XSD/XML to HTML forms and back. We stumbled upon a few COTS products, but these weren't suitable. I know many of you will probably think XForms. XForms was what turned up most when searching for known ways to do such a thing. Not knowing XForms beforehand I just superficially skimmed the technology and concluded that it wasn't right for us for several different reasons - one of them being the flexibility the requirements demanded. This may be because we didn't examine it thoroughly enough.

Anyway, looking further we came upon EMF (Eclipse Modelling Framework) and started building a small prototype. EMF seemed rather complex and during a discussion in a newsgroup somebody recommended SDO and the Apache Tuscany SDO implementation, which is based on EMF.

It immediately caught our attention since it seemed to supply just what we needed through a much simpler API. Any issues we had were quickly resolved by the extremely friendly people behind Apache Tuscany via the user mailing list. And our proposals for new functionality were quickly implemented.

Now, let's get into some technical details. This article doesn't introduce SDO as such - for that I recommend the articles by Kelvin Goodson and Geoffrey Winn in earlier issues of JDJ (Volume 11, Issue 12).

SDO
With Apache Tuscany SDO you operate within certain helper contexts. You do your SDO work in a context and you can choose to use this one context for all your work and let it live throughout your application lifetime, or you can use as many contexts as you like. This can be useful when XML Schemas aren't static - even in the same namespace. Once an XML Schema has been defined the types are cached for easy access in your context. If an XML Schema changes (and still keeps the same namespace) you can just scratch the original context and create a new one. Of course it's never recommended to work with XML Schemas that way since it opens up millions of issues regarding schema instances (XML) and the version of the schema. It's recommended that you do your versioning using the namespace. Still it shows some of the flexibility of the Apache Tuscany SDO implementation.

Let's look at some examples of how to define an XML Schema as SDOs and how to load and save XML from them (see Listing 1). For simplicity's sake the examples are without enumerations or many-valued properties. All use Apache Tuscany SDO 1.0 Incubating (beta 1, but the final has just been released).

Okay, so following listing 1, we have got a hold of the SDO data object representing the type of the schema we're interested in. Before we continue let's just see how you can get to this point if you already have existing XML that you want loaded into the data object.

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    + "<test:TestElementX xmlns:test=\"http://mytestnamespace\">"
    + " <test:SubElement1>1.0</test:SubElement1>"
    + " <test:SubElement2>Hello</test:SubElement2>"
    + "</test:TestElementX>";
XMLDocument xmlDoc = context.getXMLHelper().load(xml);
rootDataObject = xmlDoc.getRootObject();

At this point you can choose to extract XML from the data object. This will be more relevant once we've altered the data object, but this is how it can be done:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
xmlDoc.setEncoding("UTF-8");
try {
    context.getXMLHelper().save(xmlDoc, baos, null);
} catch (IOException e) {
    e.printStackTrace();
    System.exit(-1);
}
System.out.println(baos.toString());

If you haven't already got hold of the XMLDocument, it can be created like this:

XMLDocument xmlDoc = context.getXMLHelper().createDocument(dataObject, namespace, "TestElementX");

Before we continue to work with the root data object, let's take a brief look at how we can make any of this useful in a browser.


About Christian Landbo Frederiksen
Christian Landbo Frederiksen is a senior consultant in Ementor Denmark and has worked with Java and J2EE since 2000. He holds an M.Sc. in IT from the IT University of Copenhagen and a B.Sc. in computer science from the University of Copenhagen. He is a SCJP, SCJD and SCWCD.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

is the source code for this article available?


  Subscribe to our RSS feeds now and receive the next article instantly!
In It? Reprint It! Contact advertising(at)sys-con.com to order your reprints!
ADS BY GOOGLE
Subscribe to the World's Most Powerful Newsletters