Objective
In this lab you will practice recursion on XMLTrees by implementing the tagCount and outputTextNodes static methods.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying ProjectTemplate. Name the new project XMLTreeRecursion.
- Open the src folder of this project and then open (default package). As a starting point you can use any of the Java files. Rename it XMLTreeRecursion and delete the other files from the project.
- Follow the link to XMLTreeRecursion.java, select all the code on that page and copy it to the clipboard; then open the XMLTreeRecursion.java file in Eclipse and paste the code to replace the file contents. Save the file.
Method
- Complete the body of the tagCount static method
specified as follows:
/** * Returns the number of occurrences of the given tag in the given * {@code XMLTree}. * * @param xml * the {@code XMLTree} * @param tag * the tag name * @return the number of occurrences of the given tag in the given * {@code XMLTree} * @ensures <pre> * tagCount = * [the number of occurrences of the given tag in the given {@code XMLTree}] * </pre> */ private static int tagCount(XMLTree xml, String tag) {...}
- Run the program and test your implementation of tagCount. For testing purposes you can use any XML URL or file. You may want to open it in the XMLTreeViewer application you used in an earlier lab to check the correctness of the output. (If you need some URL samples, click on the XML Input drop-down menu in the XMLTreeViewer application.)
- Complete the body of the outputTextNodes static
method specified as follows:
/** * Outputs the text nodes in the given {@code XMLTree} on separate lines. * * @param xml * the {@code XMLTree} * @param out * the output stream * @updates out.content * @requires out.is_open * @ensures <pre> * out.content = #out.content * [the text nodes of xml on separate lines] * </pre> */ private static void outputTextNodes(XMLTree xml, SimpleWriter out) {...}
- Run the program and test your implementation of outputTextNodes.
Additional Activities
- Complete the body of the outputXML static method
specified as follows:
Basically this method is intended to produce the same output as the String returned by XMLTree's method toString, but of course, you should use recursion and cannot use toString. To complete this task you may want to implement a couple of "helper" methods, outputSpaces and outputAttributes, (themselves not involving recursion) specified in XMLTreeRecursion.java./** * Output the XML textual representation of the given {@code XMLTree}. * * @param xml * the {@code XMLTree} * @param out * the output stream * @param indentationLevel * the level of indentation * @updates out.content * @requires out.is_open and indentationLevel >= 0 * @ensures <pre> * out.content = #out.content * [the XML textual representation of xml] * </pre> */ private static void outputXML(XMLTree xml, SimpleWriter out, int indentationLevel) {...}
- Run the program and test your implementation of outputXML.