package dom.traversal; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.traversal.TreeWalker; import org.apache.xerces.dom.TreeWalkerImpl; import org.w3c.dom.traversal.NodeFilter; import org.apache.xerces.parsers.DOMParser; import org.xml.sax.InputSource; import java.io.StringReader; import java.util.Vector; public class TestWalker { // this many children of root node private static final int NUM_BRANCH = 10; // this many children of each branch node private static final int NUM_TWIG = 10; // this many children for each twig node private static final int NUM_LEAF = 11; // this many children for each twig node private static final String SEARCH_VALUE = "Leaf1.1.1"; public TestWalker() { try { runTest(parseXML(createXML())); } catch (StackOverflowError e) { System.out.println(e); } } // end constructor() // Creates a TreeWalker with a simple name filter and loops // through each node returned until the document is finished. private void runTest(Document doc) { // Create non-filtering TreeWalker. { System.out.println("Testing firstChild()..."); TreeWalker simpleWalker = new TreeWalkerImpl(doc, NodeFilter.SHOW_ALL, null, true); Node node = simpleWalker.firstChild(); // root node = simpleWalker.firstChild(); // rootheader node = simpleWalker.firstChild(); // null if (node != null) { System.out.println("FAILED: got " + node.getNodeName() + " but expected null"); } else { System.out.println("PASSED"); } } System.out.println("Testing nextNode()..."); // Create TreeWalker with inline filter. // The filter simply looks for a node with a specific value TreeWalker walker = new TreeWalkerImpl(doc, NodeFilter.SHOW_ALL, new NodeFilter() { public short acceptNode (Node n) { String testValue = n.getNodeValue(); if (testValue == null) { return FILTER_SKIP; } if (testValue.equals(SEARCH_VALUE)) { return FILTER_ACCEPT; } return FILTER_SKIP; } // end acceptNode(Node) }, true); // Loop until done. long startTime; long endTime; Vector matchingNodes = new Vector(); startTime = System.currentTimeMillis(); Node node = walker.nextNode(); while (node != null) { matchingNodes.addElement(node); node = walker.nextNode(); } endTime = System.currentTimeMillis(); check(matchingNodes); double runTime = (double)(endTime - startTime); System.out.println("approximately " + (runTime/1000.0) + " seconds"); // Try going backwards System.out.println("Testing previousNode()..."); // Start at the last node in the tree Node lastNode = doc; while (lastNode.getLastChild() != null) { lastNode = lastNode.getLastChild(); } walker.setCurrentNode(lastNode); matchingNodes = new Vector(); startTime = System.currentTimeMillis(); node = walker.previousNode(); while (node != null) { matchingNodes.addElement(node); node = walker.previousNode(); } endTime = System.currentTimeMillis(); check(matchingNodes); runTime = (double)(endTime - startTime); System.out.println("approximately " + (runTime/1000.0) + " seconds"); } // end runTest() // Determines whether matching nodes contains exactly 1 Node and // whether that 1 Node has a value of SEARCH_VALUE private void check(Vector matchingNodes) { if (matchingNodes.size() != 1) { System.out.println("FAILED: found " + matchingNodes.size() + " matching nodes " + " but expected 1"); } else { String value = ((Node)matchingNodes.elementAt(0)).getNodeValue(); if (! value.equals(SEARCH_VALUE)) { System.out.println("FAILED: found " + value + " but expected " + SEARCH_VALUE); } else { System.out.println("PASSED"); } } } // Parses given String and returns a Document private Document parseXML(String xmlSrc) { // Double check accuracy of created xml: // System.out.println("Source:\n" + xmlSrc); DOMParser parser = new DOMParser(); InputSource in = new InputSource(new StringReader(xmlSrc)); try { parser.parse(in); } catch (Exception e) {e.printStackTrace();System.exit(1);} return parser.getDocument(); } // end parseXML() // Creates a simple Document based on global parameters. // The root has NUM_BRANCH children, each has NUM_TWIG children, // and each of those has NUM_LEAF children. private String createXML() { StringBuffer xmlSrc = new StringBuffer(); xmlSrc.append("\n"); xmlSrc.append("\n"); for (int x=0; x < NUM_BRANCH; x++) { xmlSrc.append ("\tBranch"+x+"\n"); xmlSrc.append ("\t\tResource"+x+"\n"); for (int y=0; yTwig"+x+"."+y+ "\n"); for (int z=0; zLeaf"+x+"."+y+"."+z+"\n"); xmlSrc.append("\t\t\n"); } xmlSrc.append("\t\n"); } xmlSrc.append("The End\n"); xmlSrc.append(""); return xmlSrc.toString(); } // end createXML() // Usage: java TestWalker public static void main(String[] args) { TestWalker bob = new TestWalker(); } // end main } // end class TestWalker