devxlogo

Supercharge Your Java Web Applications with Translets

Supercharge Your Java Web Applications with Translets

SL (eXtensible Stylesheet Language) enables you to transform an XML document into HTML, another XML document, or any plain text. Using XML with XSL separates the business logic data from the presentation data and allows application developers and Web designers to work independently with ease. It also simplifies application maintenance, as you can make changes to the presentation data without touching the business logic part of the application.

As simple as it sounds, doing transformation with XSL does have a drawback when done within the application: it can take a considerable amount of time and reduce performance. The time needed to parse XML and XSL documents is directly proportional to the size of the documents. Each transformation requires the XML and XSL documents to be loaded, syntax checked, and parsed. Developers can optimize all other bottlenecks in the application, but they cannot do anything to optimize XSL transformation. In the worst case, if the XSL file is loaded from a URL, it further degrades the performance of the application.

Translets, the latest addition to the Java application development framework, offer the perfect solution. Translets are precompiled XSL documents that are optimized and converted into simple Java classes. When you compile your application Java files, you compile your XSL files into Java class files. During runtime, you can load translets like any regular Java class and perform XSL transformations over and over again. The syntax checking and parsing of XSL documents are done when the XSL files are compiled. The transformation therefore takes only as long as the compiled code takes to execute, which improves performance multiple folds.

Since translets are simple Java classes, you can very easily jar and deploy them along with the application. Depending on the design of your Web applications, you can deploy and use the translets jar on either the client side or the server side.

This article walks you through the creation of translets, discusses various options that translet functionality provides, and finally demonstrates how to use them from within a Java application. If you already use XSL in your Web applications, you can readily apply the same concept to them.

Translets in Action
Suppose you have an XML representation of all the employees of a company and your application needs to construct a HTML page for presentation. A simple Java application in this case demonstrates how you can use translets. However, the same concept can be incorporated into the servlets or JSPs of a Web application very easily. The source code download provided with this article contains all the files you need to install and run this sample application, which requires JDK 1.3 or higher. (It also contains a readme.txt file that details the steps for running the demo application.)

The following is the company employee XML:

Mr. JavaprogrammerIT40KMr. ServletsprogrammerIT45K

The following XSL transforms the above XML into a simple HTML page, displaying the content in a HTML table (These contents are from example.xsl in the source code download):

    	
Company Employees
Employee Name Designation Department Salary

Generate a Translet
As a first step, generate a translet for the XSL file in the previous section. (XSLCompiler.java contains the code to generate the translet.) The code to compile an XSL file is very simple:

//Set XSLTC's TransformerFactory implementation as  //default            System.setProperty("javax.xml.transform.TransformerFactory",                                     "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");// Get an input stream for the XSL stylesheetStreamSource stylesheet = new StreamSource(xsl);// The TransformerFactory will compile the stylesheet // put the translet classes inside the Templates //objectTransformerFactory factory = TransformerFactory.newInstance();factory.setAttribute("generate-translet", Boolean.TRUE);Templates templates = factory.newTemplates(stylesheet);

When an XSL file is passed as an argument to XSLCompiler, it generates a class file using the XSL file name as its class name. The TransformerFactory abstract class needs the system property “javax.xml.transform.TransformerFactory” be set to the appropriate factory implementation. The following properties (among others) can be set for the TransformerFactory:

  • translet-name
  • destination-directory
  • package-name
  • jar-name

When the translet-name property is not set, it assigns the XSL file name as the translet name. In this case, since example is the name of the XSL file, it generates example.class translet.

Perform Transformation with Translets
Use the compiled XSL file (example.class) in your Java application to do the actual transformation of XML into HTML. Again, the code for this is very straightforward:

TransformerFactory tf = TransformerFactory.newInstance();            tf.setAttribute(org.apache.xalan.xsltc.trax.TransformerFactoryImpl.USE_CLASSPATH , "true");	    	Transformer transformer = tf.newTransformer(new StreamSource(transletURI));  	StreamSource document = new StreamSource(xml);	    	StreamSource convertedDoc = new StreamSource();			StringWriter conDoc = new StringWriter();	        //output the result to a StringWriter			StreamResult result = new StreamResult(conDoc);        transformer.transform(document, result);

After creating a Transformer instance from TransformerFactory, you can use it to process XML from a variety of sources and write the transformation output to a variety of sinks. A Transformer instance may be used multiple times but it is not thread safe. Parameters and output properties are preserved across transformations.

Run the Application
Now it’s time to run the sample application and generate a HTML page. Compiling the XSL files is simple and straightforward. However, you start your Java Virtual Machine in a slightly different way when you run the application. You need to specify the virtual machine at boot time to use the latest implementation of xercesImpl.jar, xalan.jar, and xml-apis.jar. You can accomplish this by using the following command:

>java -Xbootclasspath/p:/xalan.jar;xercesImp.jar;
xml-apis.jar XSLTranformer example.xml example

This will ensure that the Java Virtual Machine uses these jars instead of the ones that Sun supplies with Java runtime environment.

If you want to use the translets in your servlets or JSP pages, make sure to use the ‘-Xbootclasspath’ option in your Web server startup script.

Supercharge Your Web Apps
The sample employee application demonstrates how simple using translets to do XSL transformation is. Along with the benefits of XSL, translets improve your application’s performance and provide greater flexibility for configuring and deploying your XSL files. Once you master this new technology, you can apply translets and supercharge your Java Web applications.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist