001    import java.io.IOException;
002    
003    import org.w3c.dom.Document;
004    import org.w3c.dom.Node;
005    
006    import fi.iki.hsivonen.xml.DOMUtils;
007    import fi.iki.hsivonen.xml.PrudentHttpEntityResolver;
008    
009    public class DomTester {
010        static {
011            PrudentHttpEntityResolver.setParams(5000, 5000, 100);
012            PrudentHttpEntityResolver.setUserAgent(System.getProperty("fi.iki.hsivonen.verifierservlet.version", "VerifierServlet-RELAX-NG-Validator/2.x (http://hsivonen.iki.fi/validator/)"));
013        }
014    
015        private static final void traverse(Node node) {
016            Node current = node;
017            Node next;
018            for (;;) {
019                switch (current.getNodeType()) {
020                    case Node.TEXT_NODE:
021                    case Node.CDATA_SECTION_NODE:
022                        System.out.println(current.getNodeValue());
023                        break;
024                    case Node.ELEMENT_NODE:
025                    case Node.DOCUMENT_FRAGMENT_NODE:
026                    case Node.DOCUMENT_NODE:
027                        if ((next = current.getFirstChild()) != null) {
028                            current = next;
029                            continue;
030                        }
031                        break;
032                }
033                for (;;) {
034                    if ((next = current.getNextSibling()) != null) {
035                        current = next;
036                        break;
037                    }
038                    current = current.getParentNode();
039                    if (current == node) {
040                        return;
041                    }
042                }
043            }
044        }
045        
046        /**
047         * @param args
048         * @throws IOException 
049         * @throws Exception 
050         */
051        public static void main(String[] args) throws Exception, IOException {
052            Document doc = DOMUtils.loadFromUrl("http://whatwg.org/specs/web-apps/current-work/");
053            long start = System.currentTimeMillis();
054            for (int i = 0; i < 1000; i++) {
055                traverse(doc);
056            }
057            System.out.println((System.currentTimeMillis() - start));
058        }
059    
060    }