home
NEWS       BLOGS       FORUMS       NEWSLETTERS       RESEARCH       EVENTS       DIGITAL LIBRARY       CAREERS  
Network Computing Network Computing Powered by InformationWeek Business Technology Network

IMMERSE YOURSELF:

SOA

  |

Data Center

  |

802.11n

  |

Data Privacy

  |
APO  |

Virtualization

  |

NAC

  |

Security

  |

Network Mgmt

  |

Enterprise Apps

  |

Storage & Servers



Netdesign Manual

Part 3

Java XML Programmers Reference

Chapter 11: XML Tools for Information Appliances


October 8, 2001


Brought to you by:





Check It Out!

Usage and Examples

So now let's look at some code, which transforms XML and notifies us of the new (transformed) document via SAX.

First, recall the signature of Translet.transform():

public void transform(DOM Document,

transletOutputHandler handler)

throws TransletException


And here's our code:

//load and create the translet

Class cls = Class.forName("MyClass");

Translet xlet = (Translet)cls.newInstance();

DOMImpl dom = new DOMImpl(); //will contain the parsed

//source XML

//build DOM tree from source XML into the dom object (not

// shown)

Handler saxHandler; //implements sax.DocumentHandler

TextOutput textOutput; //implements TransletOutputHandler

try {

saxHandler= new Handler();

textOutput = new TextOutput(saxHandler);

//pass the translet the source XML and a handler

xlet.transform(dom, textOutput);

}

catch (Exception e) {

e.printStackTrace();

}

This code performs the following steps:

  • Loads the translet with Java reflection
  • Creates a DOM tree from source XML (this part has been removed, but we will demonstrate how to do this in the section Example: Compiling and Using A Translet)
  • Creates an object which implements interface org.xml.sax.DocumentHandler (saxHandler)
  • Creates a TextOutput object and passes it the saxHandler
  • Starts the transformation by calling transform()

We can clearly see in the bolded line how TextOutput maps its implementation of TransletOutputHandler to DocumentHandler.

Class TransletException
org.apache.xalan.xsltc

public class TransletException

extends java.lang.Exception

This is the exception class thrown by Translet.transform() and all of the methods in interface TransletOutputHandler. Since you probably will use the SAX interface via the TextOutput wrapper (never directly implementing TransletOutputHandler), you won't need to catch TransletExceptions except when calling Translet.transform().

There aren't any special methods in this class. You should handle TransletException objects in the same manner that you treat other Throwable classes extending java.lang.Exception.

Now let's take a look at a creating a translet and an application which uses it.

Example: Compiling and Using a Translet

In this example, we have a trouble ticket system to which our client connects. The client can add, update, and view trouble tickets. For this example, however, we'll concern ourselves only with viewing trouble tickets already in the system. Here are the steps our client application will take:

  • Read an XML document, representing a single existing trouble ticket. To simplify matters, we'll read the document from persistent storage instead of from a network
  • Invoke a translet to convert the TroubleTicket document into Wireless Markup Language (WML). You don't need to know WML to understand this example, but if you do, we'll translate the single trouble ticket into a single card in one WML deck. A more advanced system might be able to query and collate multiple trouble tickets into multiple cards within the same deck to save network trips
  • If we passed the WML to a browser at this point, or wrote our own browser within the application, we could view the WML. However, for simplicity, we'll just write the WML to stdout

Before we write the application, we will need to compile a translet from a "TroubleTicket to WML" XSL stylesheet. So, here's how we'll present this example:

  • Examine a document instance of a trouble ticket document class

  • Present an XSL stylesheet that transforms <TroubleTicket/> documents into WML

  • Compile a translet from the XSL stylesheet

  • Write the client application that uses the translet and a trouble ticket document instance to produce an instance of a WML document
Sample TroubleTicket.xml

So let's begin by taking a look at TroubleTicket.xml, a TroubleTicket document instance:



This is pretty straightforward so we won't go into it much. This, and documents of this class, will be the source XML to our translet. For brevity's sake, the DTD for this document class has been omitted.

Sample TroubleTicket.xsl

Now let's take a look at the guts of the application: the XSL stylesheet that converts TroubleTicket document instances into WML. We'll call this TroubleTicket.xsl:



An XSLT processor, using the stylesheet above, will produce the following WML from the TroubleTicket document instance in The TroubleTicket Document:



Compiling a Translet

Now let's compile the XSL stylesheet from the previous section into a translet (Java class file). The compiler is class org.apache.xalan.xsltc.compiler.XSLTC, and you will need to set your classpath to include the following JAR files:

/jaxp-1.1/jaxp.jar

/xalan-j_2_1_0/bin/xsltc.jar

/xalan-j_2_1_0/bin/runtime.jar

/xalan-j_2_1_0/bin/BCEL.jar

/xerces-1_4_0/xerces.jar

> java org.apache.xalan.xsltc.compiler.XSLTC TroubleTicket.xsl


Make sure that TroubleTicket.xsl is in the current directory, or provide its full path on the command-line.

You should now have a class file called TroubleTicket.class. It resides in the directory from which you ran XSLTC, unless the d <directory> argument is used. Note that a build script to build this with Apache's ant tool is available from http://www.wrox.com/. See Appendix A.


PAGE: 1 | 2 | 3 | 4 | 5 | 6 | NEXT PAGE
 





Ready to take that job and shove it?

Function:

Keyword(s):

State:
SPONSOR
RECENT JOB POSTINGS
CAREER NEWS
Go beyond Google and get vertical. These specialized search sites will help you find the business information you need -- fast.

Ari Balogh was named to the post of chief technology officer as the companys for a "realignment" of employees.










InformationWeek U.S. IT Salary Survey 2008
Salaries for business technology professionals are falling. Here's what you need to know in order to make good hiring decisions and personal career choices. Download Today
 
ROLLING RIGHT ALONG
Follow key Network Computing Reviews from conception to completion. This Week: Holistic APM.



Network Computing Reports Emerging Enterprise Podcast Series: Secrets to Success








TechSearch


Microsite of the Week


Powerful Information at Your Fingertips



InformationWeek Business Technology Network
InformationWeekInformationWeek 500InformationWeek 500 ConferenceInformationWeek AnalyticsInformationWeek CIO
InformationWeek EventsInformationWeek ReportsInformationWeek MagazinebMightyByte and SwitchDark Reading
Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingNo Jitter
space
Techweb Events Network
InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0 ConferenceMobile Business ExpoSoftware ConferenceCSI - Computer Security Institute
Black HatGTECEnergy CampMashup CampStartup Camp
space
Light Reading Communications Network
Light ReadingLight Reading EuropeUnstrungLight Reading's Cable Digital NewsConstantinopleInternet Evolution
Heavy ReadingLight Reading Live!Light Reading InsiderEthernet ExpoOptical ExpoTeleco TVTower Technology Summit
space
Financial Technology Network
Advanced TradingBank Systems & TechnologyInsurance & TechnologyWall Street & TechnologyAccelerating Wall StreetBank Systems & Technology Executive SummitBuyside Trading SummitInsurance & Technology Executive Summit
space
Microsoft Technology Network
MSDN MagazineTechNetThe Architecture Journal
space
App Infrastructure   |   Messaging & Collaboration   |   Network & Systems Mgmt   |   Network Infrastructure   |   Security  |   Storage & Servers   |   Wireless   |   Enterprise Apps
About Us  |  Contact Us  |  Site Map  |  Technology Marketing Solutions  |  Advertising Contacts  |   Briefing Centers
Copyright © 2008  United Business Media LLC  |  Privacy Statement  |  Terms of Service  |  Your California Privacy Rights