4.1: ClassGraph cd = new ClassGraph(); PrintWriter printWriter = new PrintWriter(); Visitor htmlPrintVisitor = new HTMLPrintVisitor(printWriter); StrategyGraph sg = new StrategyGraph("from Document to *"); TraversalGraph tg = TraversalGraph.compute(cd,sg); tg.traverse(document, htmlPrintVisitor); The default traversal order of DJ happens to be recursive preorder. The traversal traverses the Document and prints it in HTML format. 4.2: Since DJ is just Java the new exception class can be subclassed from java.lang.Exception. The top function that does the traversal inside DJ should implement a catching block, destroy the exception and return the result. From dougo@ccs.neu.edu Mon Nov 15 16:45:33 1999 To: Karl Lieberherr Answers for 4.1 and 4.2 by Doug Orleans: only a partial answer was expected to get full credit. Karl Lieberherr writes: > PrintWriter printWriter = new PrintWriter(); > Visitor htmlPrintVisitor = new HTMLPrintVisitor(printWriter); > TreeTraversal treeTraversal = > new RecursivePreorderTreeTraversal(htmlPrintVisitor); > treeTraversal.traverse(document); > printWriter.close(); > What is your interpretation of the behavior of this code? > Give a brief explanation in English. It traverses the tree object representing the XML document, in depth-first preorder, printing all its elements as HTML (by calling methods on a HTMLPrintVisitor object as it traverses). > Write a DJ program that has a similar behavior. > Assume that you are given HTMLPrintVisitor. PrintWriter printWriter = new PrintWriter(); Visitor htmlPrintVisitor = new HTMLPrintVisitor(printWriter); TraversalGraph treeTraversal = TraversalGraph.compute(new ClassGraph(), new Strategy("from * to *"); treeTraversal.traverse(document, htmlPrintVisitor); printWriter.close(); > Consider the following documentation of EndTraversalException: > > public class EndTraversalException > extends TreeTraversalException > > ---- > XML4J tree traversal exception which signals from a > Visitor to the tree traversal algorithm that all further > traversal should be ended because the visit > on the document object hierarchy has been successfully completed. > For example, this exception could be used by > search Visitors that are looking for the first TXElement Node > which contains certain state data. > ---- > > Question 4.2: > 15 points > How could EndTraversalException be added to DJ? > Give a brief discussion of the interesting issues. If it extends RuntimeException, you'd simply need to add a try { ... } catch (EndTraversalException e) { } block around the body of TraversalGraph.traverse() (as well as fetch() and gather() and the container adapters). Also, the code that invokes visitor methods and catches InvocationTargetException would have to see if the contained exception were an EndTraversalException and re-throw it (rather than throw a plain RuntimeException which is what it does now). If it is a checked exception, the user's Visitor methods that throw an EndTraversalException would have to declare it in the method signature; fortunately this doesn't affect method invocation through the Java Reflection API, but Demeter/Java would have to declare this exception in all of its generated traversal methods. --Doug Notes: unchecked exceptions are exceptions of classes RuntimeException and Error, or subclasses of these exceptions. checked exceptions must be declared. public Object invoke(Object onThis, Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException InvocationTargetException is thrown by invoke if the invoked method itself throws an exception.