CS642 Fall 2010 Project: Sample Code

Implementation notes

Some sample code in Java is made available which implements a simple XML client/server application. You are welcome to copy this code, and study it or use it as a starting point if you want to. You are not required to use this code, it is simply provided as a convenience.

Sample code download: tar.gz zip

The sample application implements a 4 function calculator as a client/server protocol. The client reads expressions from the command line, parses the arithmetic expression into an XML message, and sends the message to the server. The server parses the XML message and validates the XML against a DTD, computes the value of the expression, and sends the answer in another XML message back to the client.

The example is intended to demonstrate the basics of sending and receiving validated XML over sockets in a client/server protocol. You should be able to figure out the following things by studying this code:

Build Instructions

First, read the README file in the package top-level directory.

The sample code includes build files. The Java version of the code uses the "ant" build system from the apache project. The build file is called "build.xml" . To build the Java classes, simple type "ant", which is installed on the CSL machines in /unsup/ant/bin/ . You may need to set JAVA_HOME - see the README for details.

Sample Interactive Session

Here is an simple interaction with the calculator program on a CSL Linux system:

Start the server in window 1:

 $ java -cp src/server:src/common:lib/xercesImpl.jar Server 

Start the client in window 2:

 $ java -cp src/client:src/common:lib/xercesImpl.jar Client 

Enter an arithmetic expression in the client window:

 > (4+5)*(8-6) 

Watch the request the client sends to the server:

sending to server
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE calc PUBLIC "http://localhost/calc.dtd"
                      "calc.dtd">
<calc>
   <mult>
   <plus>
<number val="4.0"/>
 <number val="5.0"/>
</plus>
   <minus>
         <number val="8.0"/>
         <number val="6.0"/>
      </minus>
   </mult>
</calc> 

Watch the response the server sends to the client:

received from server
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE answer PUBLIC "http://localhost/answer.dtd"
                        "answer.dtd">
<answer>18.0</answer> 

Observe how the XML document representing the calculation request is structured hierarchially, like a tree. There is some boilerplate at the top which identifies it as an XML document, and specifies that it will conform to a DTD called "calc.dtd". Then, there is a single outermost element called <calc>...</calc> that is the root of the document. Inside the <calc>...</calc> the calculation is represented as a series of embedded <plus>, <minus>, <mult>, and <div> elements. Each element must have exactly 2 children inside of it. These children can either be another nested operation, or the empty <number> element, which takes a numeric attribute called "val". This attribute is required. The <number> element is called empty because it has no other elements nested inside of it.

These rules about what a calculation request may contain is codified in the DTD, presented below.

DTD for calculator request

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT calc (plus|minus|mult|div)>
<!ELEMENT plus ((plus|minus|mult|div|num), (plus|minus|mult|div|num))>
<!ELEMENT minus ((plus|minus|mult|div|num), (plus|minus|mult|div|num))>
<!ELEMENT mult ((plus|minus|mult|div|num), (plus|minus|mult|div|num))>
<!ELEMENT div ((plus|minus|mult|div|num), (plus|minus|mult|div|num))>
<!ELEMENT num EMPTY>
<!ATTLIST num
       val #PCDATA #REQUIRED
> 

DTD for calculator response

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT answer (#PCDATA)> 

Other Documentation

Xerces-J XML Library
The code uses the Xerces-J library for XML manipulation. Detailed documentation about the Xerces-J XML library can be found at the website: http://xml.apache.org/xerces2-j/index.html

XML
Some XML resources you may find helpful:
A Technical Introduction to XML
XML DTD Tutorial

Copyright © 2005-2007 Louis Kruger, Mihai Christodorescu, Jake Rosin. All rights reserved.
Modified by Drew Davidson and Jake Rosin from a version maintained by Mihai Christodorescu (http://www.cs.wisc.edu/~mihai).
Created: Tue Nov 2 14:13:16 2010
Last Modified: Tue Nov 2 14:13:16 2010