The XML Generation Component
XML generator is a component that consists of a set of classes responsible for generating the appropriate XML page for the response. For example, the OptionsXML.java class exposes a getOptionsXML()
static method, which generates the Options.jsp
file in the specified language and contains the content required for the requesting device.
The method arguments are pageType
("Web", "Wap", or "iMode"), language, country, and an HttpResponse object that writes the response to the client, for example:
OptionsXML.getOptionsXML(pageType, language, country, response);
The XML generator class first creates a locale object and then loads the required Resource Bundle file.
// creating Locale and ResourceBundle object
Locale locale = new Locale(language,country);
ResourceBundle rb = ResourceBundle.getBundle("ArticlesGUI",locale);
In the preceding code, "ArticlesGUI" is the resource file name. The locale instance defines the suffix for this resource file. For example, a request for the Japanese language would use the ResourceBundle file ArticlesGUI_ja_JP.properties. Similarly, for an English-language request, the ResourceBundle file would be ArticlesGUI_en_US.properties.
For more information on internationalization in Java applications, visit http://java.sun.com/products/jdk/1.1/docs/guide/intl/
After loading the appropriate ResourceBundle file, the JSP page instantiates a GenerateXML object. In truly internationalized applications, all the texteven column labels, button text, and messagesare fetched from the resource file, as the following code demonstrates. The code fragment adds a page title, a form element and a hyperlink to the page. The last line of code retrieves the generated XML page as a string.
Role of XSL / XSLT
Element form = xml.addForm(1, "optionform",true);
rb.getString(pageType+"_Options_Page_Option_1"), " ",
sb = xml.getGeneratedXMLString();
XSL plays a major role in this application and is responsible for transforming the generated XML to a specific user agent format. The application uses server-side XML transforms rather than client-side transforms because:
- Not all clients have an XML parser installed and available. Parsing the XML on the server removes the dependency of the XML parsing from the client making it much easier to extend to support any user agent.
- XSL technology is still in its infancy stage, and the standards have not yet solidified; therefore different clients might transform the XML in different ways. Using server-side transformations avoids the problem altogether.
Because this application is small and has a very simple GUIeach user agent needs only one XSL stylesheet:
- articles-html-form.xsl. for generating HTML (Web)
- articles-wml-form.xsl.for generating WML (WAP)
- articles-imode-form.xsl.for generating CHTML (iMode)
Still, the architecture
is extensible. As the GUI complexity increases, you can write separate XSL stylesheets for each page that correspond to each desired user agent format. Listing 1
shows an excerpt from the WML-specific stylesheet articles-wml-form.xsl
Sun's API's have been used extensively in the example application for transformation of the content. Visit Sun's Java XML download page
to download the API and documentation. You should look closely at the sample code to help you understand how the transformation occurs.
The details of XSL/XSLT transformation techniques are beyond the scope of this article, but the resource links for this article contain more information on XSL techniques.