1 23 24 package org.enhydra.xml.lazydom; 25 26 import java.util.HashMap ; 27 28 import org.enhydra.xml.dom.SimpleDOMTraversal; 29 import org.enhydra.xml.driver.TestException; 30 import org.w3c.dom.Attr ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.Node ; 33 34 38 class DocInfo { 39 42 private class AttrElementId { 43 44 public final String attrName; 45 public final String attrValue; 46 public final int elementNodeId; 47 48 49 public AttrElementId(String name, 50 String value, 51 int id) { 52 attrName = name; 53 attrValue = value; 54 elementNodeId = id; 55 } 56 57 58 public boolean equals(Object obj) { 59 if (obj instanceof AttrElementId) { 60 AttrElementId entry2 = (AttrElementId)obj; 61 return attrName.equals(entry2.attrName) 62 && attrValue.equals(entry2.attrValue); 63 } else { 64 return false; 65 } 66 } 67 68 69 public int hashCode() { 70 return attrName.hashCode() + attrValue.hashCode(); 71 } 72 } 73 74 77 private HashMap fAttrElementIdMap = new HashMap (); 78 79 82 public DocInfo(LazyDocument document) { 83 84 SimpleDOMTraversal traverser 86 = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() { 87 public void handleNode(Node node) { 88 if (node instanceof Attr ) { 89 addAttrElementId((LazyAttr)node); 90 } 91 } 92 }); 93 traverser.traverse(document.getTemplateDocument()); 94 } 95 96 99 private void addAttrElementId(LazyAttr attr) { 100 LazyElement element = (LazyElement)attr.getOwnerElement(); 101 AttrElementId attrElementId = new AttrElementId(attr.getNodeName(), 102 attr.getNodeValue(), 103 element.getNodeId()); 104 if (!fAttrElementIdMap.containsKey(attrElementId)) { 105 fAttrElementIdMap.put(attrElementId, attrElementId); 106 } 107 } 108 109 112 public int getElementId(String attrName, 113 String attrValue) { 114 AttrElementId key = new AttrElementId(attrName, attrValue, 0); 116 AttrElementId entry = (AttrElementId)fAttrElementIdMap.get(key); 117 if (entry == null) { 118 throw new TestException("Attribute/value pair not found: " 119 + attrName + "/" + attrValue); 120 } 121 return entry.elementNodeId; 122 } 123 124 129 public Element getByAttrValue(LazyDocument lazyDoc, 130 String attrName, 131 String attrValue) { 132 133 return (Element )lazyDoc.getNodeById(getElementId(attrName, attrValue)); 134 } 135 } 136 | Popular Tags |