1 23 24 package org.enhydra.xml.lazydom; 25 26 import org.enhydra.xml.dom.SimpleDOMTraversal; 27 import org.w3c.dom.Node ; 28 29 31 36 public final class TemplateDOM { 37 38 private LazyDocument fTemplateDocument; 39 40 41 private int fMaxNodeId = LazyNode.NULL_NODE_ID; 42 43 44 private LazyNode[] fTemplateNodes; 45 46 49 public TemplateDOM(LazyDocument templateDocument) { 50 fTemplateDocument = templateDocument; 51 if (fTemplateDocument.getNodeId() == LazyNode.NULL_NODE_ID) { 53 assignNodeIds(); 54 } else { 55 findMaxNodeId(); 56 } 57 initNodeTable(); 58 templateDocument.setReadOnly(true, true); 59 } 60 61 64 private void assignNodeIds() { 65 fMaxNodeId = LazyNode.DOCUMENT_NODE_ID; 66 SimpleDOMTraversal traverser 67 = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() { 68 public void handleNode(Node node) { 69 ((LazyNode)node).makeTemplateNode(fMaxNodeId++); 70 } 71 }); 72 traverser.traverse(fTemplateDocument); 73 } 74 75 78 private void findMaxNodeId() { 79 SimpleDOMTraversal traverser 80 = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() { 81 public void handleNode(Node node) { 82 int nodeId = ((LazyNode)node).getNodeId(); 83 if (nodeId > fMaxNodeId) { 84 fMaxNodeId = nodeId; 85 } 86 } 87 }); 88 traverser.traverse(fTemplateDocument); 89 } 90 91 94 private void initNodeTable() { 95 fTemplateNodes = new LazyNode[fMaxNodeId+1]; 96 97 SimpleDOMTraversal traverser 98 = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() { 99 public void handleNode(Node node) { 100 LazyNode lazyNode = (LazyNode)node; 101 fTemplateNodes[lazyNode.getNodeId()] = lazyNode; 102 } 103 }); 104 traverser.traverse(fTemplateDocument); 105 } 106 107 110 public final int getMaxNodeId() { 111 return fMaxNodeId; 112 } 113 114 119 public final LazyNode getNode(int nodeId) { 120 if (nodeId == LazyNode.NULL_NODE_ID) { 121 return null; 122 } else { 123 return fTemplateNodes[nodeId]; 124 } 125 } 126 } 127 | Popular Tags |