CS 210 Intermediate Programming with Data Structures
Programming Assignment 5
Spring 2006

Due Monday, 24, noon

 

Purpose

This assignment aims to help you:

·         Learn about XML

·         Work with the DOM (Document Object Model) to access XML documents from a program

·         Work with trees

·         Get more practice with recursion

Reading

Read Weiss, Chapter18, Trees, and the XML introduction(s) linked from the class web page.  Note that the XML classes we will be using are available along with the rest of the JDK we are using, and their API documentation is in the usual place too.

Description

XML is used to express data that needs to be understood by more than one application.  Although it is not intended for direct human reading, it is in text and is not difficult to understand, for example, to check for correctness.  To help human readers read XML (XML fragments), we will write an XML browser much like the one in Internet Explorer, so try that out first by browsing to an XML file such as book.xml linked from the class web page.  You can click on the +’s and –‘s and expand or collapse whole subtrees.  Note how indentation is used to represent levels in the XML tree.  You need only support elements, attributes, and text nodes.

However, we want a line-oriented approach for simple user interface, so we will use the following setup. Number the lines in the fully-expanded version, and use indentation, one space per level:

    1 -<book>

   2  -<section>

   3   -<section title="Tree Frogs">

 4     All right-thinking people          (this is a text node)

   5     <bold>love</bold>

 6     tree frogs.

 7    </section>

 8   </section>

 9  </book>

Note that the element start tag <bold> does not have a – in front of it, because it is a leaf node, i.e., it has no children to expand/collapse.  All the non-leaf elements do have – signs in the original display. If the user types 2, do the collapse indicated by the - at line 2, and print out the new representation:

     1 -<book>

     2  +<section>

     9  </book>

Note that only the start tag shows in a collapsed subtree, with a + in front of it.  Here we see the start tag <section> with a + in front of it, but not its end tag, </section>.  If the user now types 2, the expansion indicated by the + will be done, and the original form will be printed out again.  If the user types 3 from the original state, 5 lines will be collapsed into one.  Then if the user types 3, the second form above is printed out.  If the user then types 3 again, the previous partly-collapsed tree will be printed out.  We say the +/- marks are “sticky”, that is, they don’t change unless the user specifically directs it.

1.      Write XMLDisplay, a program that simply displays the original indented format with line numbers but without the – signs, and exits.  Usage: java XMLDisplay book.xml   for example

2.      Write XMLBrowser, the browser described above.  Usage: java XMLBrowser book.xml   for example.

Design

For 1, start from the provided program DomEcho01.java, and set up a recursive tree traversal printing out elements, attributes, and text nodes.  See printPreOrder, pg. 612 for a recursive tree traversal of a binary tree as a related example.  Use level to control indentation, as we did in pa04.  When you process an element, you need to print out the Attributes, then see if this element has only one child that is a single textnode.  In that case print the text node’s value on the same line as the element start tag, then the elements end tag.  Otherwise, recursively call to print out each child node, and then print out this element’s end tag on a new line.You do not need to build any additional data structures here, just keep track of the level in a local variable and print out information as you go.  A single-class implementation is fine here, in XMLDisplay.java.  Usage: java XMLDisplay file.xml.

For 2, it's time to refactor.  Rework XMLDisplay.java into XMLDocument.java, with public methods display() and toggleExpansion(int lineNumber).  To start, only implement display() as in 1.  Put the top-level code in XMLBrowser.java.  Usage: java XMLBrowser file.xml.

For 2, you need to keep track of the +/- status, i.e. the expansion status, plus the line-numbers (of start and end tags), of elements.  Another way to say it is that we need to decorate the tree (not a joke, a real technical concept!)  or at least decorate the element nodes of the tree.  More exactly, for each element node, we need to store its expansion status and line number.

Here’s my plan (propose something else if you want, but avoid setting up a huge parallel data structure.)
Set up a simple Decoration class as a nested class of XMLDocument, with the expansion status and the line numbers and their getters and setters.  Set up another nested class DecorationMap with put(Node n, Decoration n1), Decoration get(Node n), implemented using a TreeMap<Integer, Decoration>.  The integer key for the TreeMap is for a Node n is  ((Object)n).hashCode(), guaranteed to be unique for each object.  (This is avoiding using Node.equals(), which we don’t know anything about.) With this set up, you can map from a Node ref (or any subclass of Node) to the decoration for it, needed for the new display method,  the recursive display of 1 modified to suppress printout of collapsed subtrees and print +/-.

You also need to be able to toggle the expansion state of a node given its line number.  You can find the right-numbered node by binary search down the tree: at each level, since the line-numbers of the children are in ascending order. At each level, find the best-match child and search its children.  Alternatively, you can build another access method to the data inside XMLDocument, a map from lineNumber to Node.

memo.txt

In the file memo.txt, answer the following questions, in one to three pages (60-180 lines) of text.  Use complete sentences, as you would for an English class

·        What problems did you encounter and how did you solve them? 

·        Any trouble with the XML library classes?

·        Quote the statement from the JDK Javadoc that guarantees that ((Object)n).hashCode() is unique for each object

 ·      Give the big-Oh performance of toggleExpansion(lineNumber) and display() for N nodes in the XMLTree.

·        Did you use the DecorationMap idea, or something else to keep track of needed data on expansion status?  Discuss.

·        Did you use the binary search idea or a separate data structure to find the node whose expansion status needs toggling?  Discuss.

.    Are you now comfortable with using nested classes?  Do you think it is a good idea here?

Turn In

Use the turnin system to submit your files.  Before the due date, submit the following files to the pa6 folder of your turnin system account: