Download NetBeans!

20050803 Wednesday August 03, 2005

Friends, Romans, Countrymen...

There's a cool web service (click here) that returns Shakespearean quotations to clients that send it appropriate search strings. For example, I type this in my client (click to enlarge):

And I get this in return (click to enlarge):

Thanks to NetBeans IDE, creating the client (from the running web service, described by the freely available WSDL file) is as simple as clicking through a wizard (and then manually coding the JSP page). However, there are two tricky bits afterwards. This is because the web service sends the quotation back as an XML stream, which you can see here -- the screenshot shows you the IDE's built-in client that lets you test web services without you having to do any coding whatsoever (click to enlarge):

So, your only contribution to the client (apart from the aforementioned manually coded JSP page) is a little bit of code in the servlet's processRequest method. You use this method to write the XML stream to System.out as well-formed XML. Once the XML stream is well-formed XML, you use an XSL Stylesheet (also created by means of a NetBeans template in the IDE) to give it the structure that you see in the second illustration above. So, the two tricky bits are (1) writing out the XML stream and (2) giving it a pleasing structure, via an XSL Stylesheet.

Everything I know about processing XML with Java I learnt yesterday in Elliotte Rusty Harold's wonderful Processing XML with Java. Here's what my servlet's processRequest method (without error catching) looks like. It does two different things -- it outputs the XML stream to System.out and it writes the XML stream to an actual, physical XML file. The latter isn't really necessary, but useful for testing the XSL Stylesheet prior to deploying it live (further discussed below).

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
        
    //Receive the search string from the JSP and return the matching quotation from the web service
    String quotation = request.getParameter("quotation");
    String s = getShakespeareSoap().getSpeech(quotation);
        
    //Output the returned quotation as XML with XSL Stylesheet reference
    response.setContentType("text/xml; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<?xml version=\"1.0\"?>");
    out.println("<?xml-stylesheet type=\"text/xsl\" href=\"quotation.xsl\"?>");
    out.println(s);
    out.close();
        
    //Create an XML file and write the returned quotation to it
    OutputStream fout= new FileOutputStream("quotation.xml");
    OutputStream bout= new BufferedOutputStream(fout);
    OutputStreamWriter out1 = new OutputStreamWriter(bout, "UTF-8");
    out1.write("<?xml version=\"1.0\" ");
    out1.write("encoding=\"UTF-8\"?>\r\n");
    out1.write(s);
    out1.flush();
    out1.close();
}

That's it. But then you need to create the XSL Stylesheet -- and put it in the web root directory, in the same place where the index.jsp is found, because that's where the <?xml-stylesheet> reference (in the code above) says it is. So, I use the IDE's New File wizard to create the XSL Stylsheet and then fill it out like this:

<xsl:template match="PLAY">
  <p><b>Title of play:</b><br></br>
  <u><xsl:apply-templates/></u></p>
</xsl:template>

<xsl:template match="SPEAKER">
  <p><b>Speaker:</b><br></br> 
  <i><xsl:apply-templates/></i></p>
  <b>Quotation: </b>  
</xsl:template>

Then, because NetBeans IDE is so cool, it includes this XSL Transformation dialog box (click to enlarge):

I used this to test the effect of the XSL Stylesheet on the XML file (which is why it makes sense to create an actual XML file from the stream, during the processRequest method). Via this dialog box, the IDE creates an HTML file showing the XML stream transformed by the XSL Stylesheet. It's quite a good idea to test the XSL Stylesheet quickly every now and again while creating it -- instead of having to deploy and redeploy the whole application to see the impact of the XSL Stylesheet -- which is why the XSL Transformation dialog box is really useful. (This means that, while creating the XSL Stylesheet, I commented out the code in the processRequest method that writes the XML stream to System.out. I wrote the XML stream to an XML file and then used the XSL Transfomation dialog box to test the effect of the stylesheet. Once I was happy with the stylesheet, I used the parts of the method that write the XML stream to System.out, because only at that point is the completed stylesheet used.)

So, I think this is all pretty cool. I'd like to thank Elliotte Rusty Harold for writing Processing XML with Java. It's incredibly clear, well laid out, and as practical as one could hope to find.

Aug 03 2005, 12:52:47 AM PDT Permalink