Browse DevX
Sign up for e-mail newsletters from DevX


Supercharge Your Java Web Applications with Translets : Page 3

Translets, a brand new addition to Java, can improve your Web application performance multiple fold. This article walks through the creation of translets, discusses translet functionality, and demonstrates how to use them from within a Java application.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 stylesheet StreamSource stylesheet = new StreamSource(xsl); // The TransformerFactory will compile the stylesheet // put the translet classes inside the Templates //object TransformerFactory 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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date