1 23 24 package org.enhydra.xml.dom; 25 26 import org.enhydra.xml.lazydom.LazyDOMSimpleTraversal; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.DocumentType ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.NamedNodeMap ; 31 import org.w3c.dom.Node ; 32 33 35 38 public class SimpleDOMTraversal { 39 42 public interface Handler { 43 46 public void handleNode(Node node); 47 } 48 49 50 protected Handler fHandler; 51 52 57 public SimpleDOMTraversal(SimpleDOMTraversal.Handler handler) { 58 fHandler = handler; 59 } 60 61 67 public void traverse(Node root) { 68 processNode(root); 69 } 70 71 74 protected void processChildren(Node node) { 75 for (Node child = node.getFirstChild(); child != null; 77 child = child.getNextSibling()) { 78 if (!(child instanceof DocumentType )) { 79 processNode(child); 80 } 81 } 82 } 83 84 87 protected void processAttributes(Node node) { 88 NamedNodeMap attrMap = node.getAttributes(); 89 int len = attrMap.getLength(); 90 for (int i = 0; i < len; i++) { 91 processNode(attrMap.item(i)); 92 } 93 } 94 95 98 public void processDocumentType(Document document) { 99 DocumentType docType = document.getDoctype(); 100 if (docType != null) { 101 processNode(docType); 102 } 103 } 104 105 108 protected void processDocumentTypeContents(DocumentType documentType) { 109 processNamedNodeMap(documentType.getEntities()); 110 processNamedNodeMap(documentType.getNotations()); 111 } 112 113 116 private void processNamedNodeMap(NamedNodeMap nodeMap) { 117 if (nodeMap != null) { 118 int len = nodeMap.getLength(); 119 for (int i = 0; i < len; i++) { 120 processNode(nodeMap.item(i)); 121 } 122 } 123 } 124 125 128 protected void processNode(Node node) { 129 fHandler.handleNode(node); 130 if (node instanceof Document ) { 131 processDocumentType((Document )node); 132 } else if (node instanceof DocumentType ) { 133 DocumentType docType = (DocumentType )node; 134 processNamedNodeMap(docType.getEntities()); 135 processNamedNodeMap(docType.getNotations()); 136 } else if (node instanceof Element ) { 137 processAttributes(node); 138 } 139 140 processChildren(node); 141 } 142 143 147 public static SimpleDOMTraversal getTraverser(SimpleDOMTraversal.Handler handler, 148 Node node) { 149 154 if (DOMOps.isLazyDOMInstance(DOMOps.getDocument(node))) { 155 return new LazyDOMSimpleTraversal(handler); 156 } else { 157 return new SimpleDOMTraversal(handler); 158 } 159 } 160 } 161 | Popular Tags |