Feature
Exploring XML Schema Styles Using JAXB in Enterprise Applications
Choose the right XML Schema for your enterprise applications
Aug. 2, 2005 10:15 PM
Code-generation tools are capable of significantly impacting productivity and are now an essential part of a developer's tool set. There are two categories of transformation tools: those whose output is used by software components, and those whose output is used by developers. With tools that generate output for developers, it becomes important to properly design the input to these tools so that the output is comprehensible and usable, and the input is maintainable by developers. Java Architecture for XML Binding (JAXB) is one such tool that takes an XML Schema file as input, and transforms this input into a Java class model that is usable directly by a developer. Since an essential part of any application is its information model, it becomes important to properly design the XML Schema so that the generated class model is comprehensible and usable, and the schema itself is maintainable and reusable.
This article explores various styles for designing XML Schemas and assesses each style in terms of certain quality attributes. Figure 1 shows how an information model is transformed to a class model using JAXB, and shows which qualities of the artifacts should be assessed based on a set of quality attributes.
There are four XML Schema styles based on various combinations of the global and local scope of elements and complex types. The styles are assessed by evaluating how the JAXB output satisfies comprehensibility and usability concerns, and how well the XML Schema satisfies modifiability and reusability concerns. The JAXB component's quality is not assessed since its implementation is based on a standard and there is little variability in its behavior; once a developer learns to use it, its model behavior is predictable.
The Design Context
When designing an XML-centric application, two concerns need to be addressed:
- Functional Concerns - How to represent an information model, i.e., a data model or a UML class model, in XML using XML Schema so that any subset of the information model can be produced and consumed in a flexible and extensible manner, and the information model can be correctly represented in XML.
- Quality Concerns - How to design an XML Schema that is maintainable and reusable, and allows processing by a tool (JAXB) so that the generated output from the tool is comprehensible and usable by a developer.
The Quality concerns are the primary focus of this article, since the design of the XML Schema will be a significant contributing factor to how well quality concerns are satisfied.
Figure 2 shows a design concern tree with the two main design concern paths and further subconcerns for each path. The functional concerns are primarily satisfied by the XML Schema specification, while the Quality concerns will be satisfied to varying degrees based on the XML-to-Java binding framework, and the design of the XML Schema used as input to the framework.
XML Schema Styles
XML Schema styles are based on various combinations of global and local scopes for XML Schema Element and Complex Type declarations. Table 1 shows the four styles.
Why is one style chosen over another? Normally, a developer may choose to use Style 1 <L, L> over Style 3 <L, G> simply due to level of knowledge and experience, lack of time, or simply a preference. However, each style will have different degrees of influence on the qualities of the XML Schema, and the resulting generated Java code. For example, maintainability of an XML Schema file will be significantly impacted based on what style is used, and comprehensibility of the resulting Java code will be sensitive to each style as well.
Quality Attributes for Evaluating XML Schema Styles
Figure 3 shows a tree view of quality attributes that will be used to assess the quality of the XML Schema and the generated class model for each XML schema style. This list is not comprehensive, but is only meant to be a starting point for introducing quality assessment of XML Schema styles in the context of code generation tools. Each quality attribute will be assessed simply as High or Low, so as not to get overly complex. Table 2 shows what High or Low means for each quality attribute.
When creating an XML Schema, element specifications and type specifications are normally defined separately since type specifications represent pure business domain entities, and element specifications represent roles of their underlying type.
Example
First, a business object model (BOM) is described that shows the key business entities and relationships. Then the BOM is specified in XML Schema using each of the four styles discussed earlier. Each XML Schema is then input into JAXB to generate a class model, whose UML representation is presented as well. Each style's XML Schema and generated class model are assessed using the quality attributes.
Business Object Model
Figure 4 shows an example business object model. This model shows simple aggregate associations, e.g., Customer to Order, and multiple associations between two entities with different roles, e.g., Customer to Address. In order to understand the dynamics among the XML Schema, JAXB transformation, and generated class model, the model was kept simple, hence it leaves out self-relationships and inheritance.
Listings 1 to 4 and Figures 5 to 8 show a representation of the object model in XML Schema using the four styles, and their corresponding generated JAXB class models, respectively.
Assessing the Styles
Based on the example analyzed above, quality assessment of each style is summarized in Tables 3 and 4. For the XML Schema quality, styles 2 and 4, the ones with global complex types, offer the highest modifiability and reusability, and style 3 and 4, the ones with global complex types, have the highest flexibility. For the generated class model, style 2 has the highest simplicity, while any style with at least a global element or complex types has a high structural affinity.
About Pushkar VarmaPushkar Varma is an IT architect with IBM Global Services whose interests include application architectures, architecture assessment methods, Web application development using Web services and J2EE, and rich clients using Eclipse.