CS639 Programming Assignment 2

POX Servlets, Reading XML via XPath and SAX

Due: Tuesday, Mar. 21 by midnight, in your UNIX cs639/pa2 directory.

In PA1, we turned Java source into XML appropriate to support the Package Explorer in Eclipse.  Now we will write a POX (plain old XML) servlet to supply the XML for one Java class at a time, and the XML Schema file as well..  We will also write two clients to accept and display the XML.You are welcome to start from the posted solution to pa1b. Please report any problems with that code.

The XML looks the same as in PA1, and now the basic XML schema is available in $cs639/pa1bsoln. Just add a namespace http://schemas.cs.umb.edu/jsource as a default namespace to the generated XML and to the XML Schema, and make it the targetNamespace as well.

  1. Write a servlet JSourceServlet that accepts the source class name (for a public class) as part of the request URI, and sends back the XML for that class. With the webapp named pa2, the request URI of the URL would be something like  /pa2/examples.sort.SortEx.xml. Deploy it on UNIX as well as on your own home PC.  You can test this with a browser and wget.  Modify the build.xml from the posted example $cs639/servlet1.  "ant test1a" should work with wget as it does in the servlet1 example, and obtain Grid.java's XML on the client. Be sure to use “${TOMCAT_URL}” to specify the URL for wget, for the grading script. Similarly "ant test1b" should do the same for SortEx.java, writing file examples.sort.SortEx.xml. Note that there is no need to write an .xml file on the server side: in the servlet, just output the results directly to the output stream (PrintWriter) provided for the servlet response.
  2. Support validation by having JSourceServlet additionally serve out javaSource.xsd, that is, if the client's request URI is /pa2/javaSource.xsd, send back the text of the schema.  This is a little trickier than it sounds because you need to access this file in the filesystem (in the WEB-INF/schema directory, hidden from direct browser view) from your servlet. See the servlet2 example. Once you have this working on Linux (sf08.cs.umb.edu), the xsd file will have globally accessible URLs, at least while your sf08 tomcat site is up. Prove that this works by adding target test2[ab] to run Counter on the client side, on Grid/SortEx, where both Grid.xml and the schema (via Counter's -schema flag) are specified by URLs into your Linux tomcat website. "ant test2a" (and 2b) should work from home as well as on Linux. 
  3. Write a client JSourceClient1 that connects to the servlet, retrieves the XML, and then analyzes the XML using code (modified) from TestXPathIgnoreNS.java to find all the method nodes and for each, what its full name is (like outerclass.innerclass.foomethod) and print them out. Usage:  JSourceClient1 <URL of servlet> <classname to display>.  "ant test3a"  should use this client to report on Grid and put the output in Grid_methods1.txt.  Similarly for 3b. Again, be sure to use “${TOMCAT_URL}” to specify the URL in build.xml.
  4. Rewrite JSourceClient2 to use SAX to parse the incoming XML for the same information.  "ant test4[ab]" should use this client to report on Grid/SortEx and put the output in Grid_methods2.txt, etc.  Note: if you want to try StAX, you can use that instead.
  5. In memo.txt, provide a guide to your files, like a README (one sentence per file.)  Explain any shortcomings of your implementations.  Explain any help you received: code from Web, other examples, etc., not given in class or the text. Also provide an HTML file welcome.html in WebContent/welcome.html with links for Grid.xml, SortEx.xml, javaSource.dtd, and javaSource.xsd. This will be accessible at $TOMCAT_URL/pa2/welcome.html via tomcat's web server functionality. "ant test5" should use wget to retrieve this welcome.html.


Implementation Notes

Getting Started. You need to install tomcat at home.  See the linked docs on these topics under Resouces/tomcat and Resouces/servlets on the class web page.  Post any problems to the Google group, preferably before Mar. 7.  Bring up servlet1 and servlet2 as a first servlet projects, following the instructions in TomcatSetupForPC.html. Then merge your pa1b or the posted pa1b solution and servlet2. Note that pa1b is a Java project but servlet2 is a Dynamic Web project, as is pa2. You can merge outside eclipse and then bring up a new Dynamic Web project, or try to merge inside eclipse, although this can be tricky. One trick I was slow to notice: you can shift-click to select a group of files in Project Explorer, then control-C to copy, then select a target package for them and control-V to paste the files into the other package (possibly in another project.)

Tracking the Element Hierarchy when using SAX, needed for pa2

 

When working with SAX, you have to track the element hierarchy yourself, you can't ask SAX what the parent of a given element is (unlike DOM). 

In pa2 getting the full name of the method requires tracking the class hierarchy. Given an xml document, we can find the path of any element using the following approach with a SAX parser:

 

       startDocument: create a stack of type String

       startElement: push the local name onto the stack

       endElement:  pop the top String on the stack

       endDocument: the stack should be empty

 

Example 6.7 uses this approach (ignore the JTree GUI).

 

Also, for pa2, note that TestXPath has ancestry-related code (climbs the tree), which is easier than in the SAX case because a DOM tree has been created here.

Packages.   Use package cs639.presentation for the top-level client programs, cs639.servlet for the top-level servlet code, cs639.xpath for the XPath support (use a new filename since "TestXPath" sounds like a JUnit test) and cs639.sax for the SAX code. Continue to use cs639.jsource for the java source/reflection related code.

Files for grading:  Hopefully you haven't needed to change the jsource code, except as needed for namespaces.  Note in memo.txt if you did change it.
In pa2, optionally, README with notes to grader/instructor
In pa2: build.xml, with targets  test1[ab], test2[ab], test3[ab], test4[ab], and test5.
In pa2: memo.txt (with one-sentence descriptions)
In pa2/WebContent: welcome.html:  HTML with links for Grid.xml, SortEx.xml, and javaSource.xsd.
In pa2/WebContent/WEB-INF: web.xml, classes dir
In pa2/WebContent/WEB-INF/schema: javaSource.xsd with targetNamespace added
In pa2/src/cs639/jsource: pa1a code, yours or posted solution
In pa2/src/cs639/validation: pa1a code, yours or posted solution
In pa2/src/cs639/presentation:  JSourceClient1.java, JSourceClient2.java
In pa2/src/cs639/servlet: JSourceServlet.java, plus helper sources.
In pa2/src/cs639/xpath: *.java (nothing starting with "Test" unless you are using JUnit)
In pa2/src/cs639/sax: *.java  (nothing starting with "Test" unless...)