Serve clients' specific protocol requirements with Brazil, Part 2

How to support XML applications with the Brazil project

1 2 Page 2
Page 2 of 2

Using a Brazil server configured as described in Listing 10, you would receive the results illustrated in Listing 9 for data that is normally displayed on a Webpage. This format is simple to parse and used in Java applications or applets. In fact, the Property class includes a method that loads this data directly. Some developers feel that the Property class does not satisfy their needs. They forget that you can provide your own Property class that can abstract several different concrete implementations. If you have a million properties, you might want to store them in a database, or some type of linked list or tree-data structure, and possibly provide a regular expression interface to find the keys or values you are looking for.

The stock data below originates from a Web server that assumes it is serving an HTML user.


Listing 9. Output of a Tcl script that parses data originally targeted to a Webpage

Parsing a realtime data stream and generating XML is a much simpler approach. And, if the stream is already XML, then you've got it made. However, in many real-world applications, some interesting piece of data always exists on some other Website. (Please note that there are many legal restrictions on taking content and redistributing it.) Emerging satellite multicast technology can multicast audited, authentic financial data in realtime to the entire world in an XML format, driving the cost of realtime financial data to pennies.

The following paragraphs and examples demonstrate how to retrieve data from an existing Website and reformat it. The output of the Tcl script below produces the data in Listing 9.

Use Tcl as the scripting language

Many programmers use the Tcl programming language for scripting applications (see Resources for more information on Tcl). The Brazil project supports Tcl scripting directly in a Webpage. The procedure for using Tcl is very simple:

  • Enable Tcl in your config file
  • Create a Webpage with the <server language="tcl"> tag

Below, you will find a typical configuration file for constructing a Brazil Web server that supports Tcl. This example assumes that other handlers are creating properties.

handler=main log= port=8081
main.class=sunlabs.brazil.template.TemplateHandler main.prefix=/ 

Listing 10. Configure the Brazil technology to support Tcl server-side scripting

The following code searches for a property called symbol. It then takes the values of symbol and iterates over them looking for "", "symbol.ask", and "symbol.price", producing an HTML table.

<server language="tcl"> package require java set props [java::field 
puts "<table border=2>" puts
foreach stock [$props getProperty "symbols"] { puts "<tr>" puts 
    $stock </td>" foreach attr "price bid ask" { puts 
    getProperty $stock.$attr]</td>" } puts "</tr>" } puts

Listing 11. Tcl code to extract and parse the source of the stock data

The above Tcl script obtains a Webpage that has stock data mixed in with formatting data. (In the future, we hope that this will be an XML page or an RMI service.) The Tcl script gives each value a fully qualified unique name and puts this data in the request object. The code below combines BSL and HTML to easily generate HTML for a user to read.

<table border>
<foreach name=th property=rta.sma.trackAttributes >
<th> <property th> </th>
<foreach name=stock property=trackStocks sort>
  <td><property stock>
  <if name=rta.sma.trackAttributes value="">
    <foreach name=x list="name price pe">
       <td><property [stock].[x]>
    <foreach name=x property=rta.sma.trackAttributes>
       <td><property [stock].[x]>

Listing 12. BSL for putting data produced in Listing 11 on a Webpage

Use Python as the scripting language

Python is a new, fashionable language that Brazil also supports. The example below demonstrates how to configure a handler in the Brazil Web server to support Python.

handler=main log=9 port=8085
main.class=sunlabs.brazil.template.TemplateHandler main.prefix=/ 
main.templates=sunlabs.brazil.tcl.PythonServerTemplate sunlabs.brazil.template.PropsTemplate \ sunlabs.brazil.template.BSLTemplate
main.cookie=Pythonserver main.debug=on

Listing 13. Python configuration file

Below is an HTML page that uses this handler to rewrite the page in the Python language:

<title>python server
test</title> <h1>test</h1> <server 
language=python> print
"<br>Hello from port", server.props.getProperty("port") print 
request headers are:<pre>", request.headers print
"</pre><br>done" request.props.put("PYTHON", "true") 
<p> <if PYTHON> The python interpreter succeeded <else> 
Boo Boo
in python interp </if> <h1> The end </h1>

Listing 14. Using Python -- HTML example

Handling data from devices or from applications

A good Web-based service should provide information to users so that they don't have to visit numerous Websites. Such services require Web applications that easily integrate with devices and other Web applications. In order to realize this goal, Web applications need to implement better mechanisms to exchange data. One such technique is XML, parsing existing content heuristically when you have no other choice. This type of content aggregation -- taking content from different sources and synthesizing new content -- is a unique ability of Brazil technology. It completes content aggregation via extensible handlers that allow content to pass through n levels of filtering until it reaches its desired form. This approach could suit many Web applications.

For example, consider the typical Internet search engine. Many search engines do not return their results in a format that is easy to use by other programs. In an ideal world, they would produce XML output directly. To overcome this short-term deficiency, we can parse a Website's data and tag it with some XML -- accepting the possibility that the parsing could break at any moment, either intentionally or due to a new revision -- by using Brazil's handlers to create Property objects. Application developers can then use that data to generate XML-tagged data for use in other applications. If all search engine companies tagged their data this way, one interface program could aggregate the results of a search across multiple engines and create a unified view of the results.

I am often frustrated with the lack of inter-Web application abilities. For example, my brokerage, bank, insurance, and credit card companies do not communicate with each other over the Web. When you add bill-paying services to the mix, it becomes more confusing. Many of us pay companies to simply open our envelopes and scan our bills in so we can read them as PDF files. In a more intelligent reality, companies could provide secure XML representations of my data and allow me to develop my own applications so that I would not have to use a portal that tries to act as a one-stop source. Perhaps personal portals provide the solution; they would combine and aggregate content on your behalf. The same idea applies to devices in your home that do not interact with each other. To have single sources of information and control, we need to improve the ways in which we define data when using applications that can't combine data and content.


In this article, we discussed ways to use XML with the Brazil Web server to exchange content between applications. I see tremendous value in the XML approach and in the Java object approach easily supported by JINI and RMI technologies. "The Java Electronic Commerce Framework and Modern Protocol Designs" in Resources discusses this subject in greater detail.

In Part 3 of this series, I will build on the concepts introduced here -- devices, scripting, XML -- to introduce yet another scripting language and technique for supporting wireless applications. Part 4 will demonstrate how to support JINI infrastructure and RMI technology.

Rinaldo Di Giorgio is a regular columnist for JavaWorld. He would like to thank members of the Brazil project team at Sun for their support in getting some of the examples in this article working.

Learn more about this topic

Recent XML articles in JavaWorld

1 2 Page 2
Page 2 of 2