CIT 597 Assignment 7: DOM Output
Fall 2003, David Matuszek

Purposes of this assignment:

Idea of the assignment:

Write a basic API for outputting a DOM tree.

In more detail:

The DOM does not provide any output facilities. Your main task is to supply some, in the form of an API. In addition, write a simple program to use and test your API.

To make your class maximally useful, you should not just print on System.out (which is an object of class PrintStream). Instead, extend OutputStream with a new class (call it DomOutputStream) and add methods to it. This way the user gets the benefit of all the OutputStream methods as well as your new methods.

Your DomOutputStream class should have a constructor that takes a OutputStream as an argument. (Note that System.out is a PrintStream, which is a FilterOutputStream, which is an OutputStream; thus, if System.out is the actual parameter to the DomOutputStream constructor, the user will have all the usual System.out methods, such as print and println, along with your new methods.)

In your DomOutputStream class you should have several new write methods. (We won't call them print and println because these new methods will typically print on multiple lines, depending on their arguments.) You should be able to print out Element, Attr, and Text nodes (with their values). Since this is intended to be a fairly light assignment, you don't have to deal with all the other types of Node, but you should have a catch-all method (with a Node parameter) for dealing with them.

Output should be well-structured XML.

If you have trouble getting started with this assignment, see slide 18 in my first DOM lecture.

Indentation:

Output should be properly indented, that is, nested elements should be indented relative to the elements that contain them. There are three factors to consider:

It seems to me that the best way to resolve this is to give the user complete control: Maintain the current level of indentation (and use it by default), but let the user access it and change it. Hence, we would have:

public void setIndentationLevel(int level)
Sets the default (starting) indentation level to level.


public int getIndentationLevel()
Returns the default indentation level. This value should initially be zero.


public void setIndentationAmount(int amount)
Sets the number of spaces to indent for each new level of indentation. That is, the number of spaces to put at the beginning of a line is level * amount.


public int getIndentationAmount()
Returns the number of spaces that will be indented for each succeeding level.

Each of the write methods, in addition to the one-parameter version, should be overloaded with a version that takes an indentation level as a second parameter. This second parameter will also serve as the new default indentation level.

Testing:

To test your program, write (or find) a reasonably small, well-structured XML file. The file should contain examples of all the things that your DOM routines can print out, including at least one feature that will test your catch-all write method (for dealing with a Node that isn't an Element, Attr, or Text node). Include this XML file when you turn in your program.

Write (in a separate class) a main method that tests out all aspects of your program, including indentation levels. Print the entire XML file, and various parts of the XML file. Be sure your program is self-explanatory, so that we don't have to do a lot of work to figure out if it is doing what it is supposed to do.

Due date:

Monday, November 10, by midnight.