/// <summary>Starts indexing the given attribute right now.</summary> public void StartAttributeIndex(string attrib) { // Create the lookup: AttributeLookup lookup = new AttributeLookup(); // Index the document: for (int i = 0; i < childNodes_.length; i++) { Dom.Element e = childNodes_[i] as Dom.Element; if (e == null) { continue; } e.AddToAttributeLookup(attrib, lookup); } if (lookup.Count == 0) { // Well.. that was pointless! Nothing to actually index! return; } if (AttributeIndex == null) { // Create the index now: AttributeIndex = new Dictionary <string, AttributeLookup>(); } AttributeIndex[attrib] = lookup; }
/// <summary>Adds this element to the given attribute lookup.</summary> public void AddToAttributeLookup(string attrib, AttributeLookup lookup) { // Got the attribute? string value; if (Properties.TryGetValue(attrib, out value)) { lookup.Add(value, this); } // Any kids got it? if (childNodes_ == null) { return; } // Add each one.. for (int i = 0; i < childNodes_.length; i++) { // Add child elements too: Dom.Element el = (childNodes_[i] as Dom.Element); if (el == null) { // E.g. text node. continue; } // Add: el.AddToAttributeLookup(attrib, lookup); } }
/// <summary>Removes the given element from any attribute caches. You must check AttributesCached first.</summary> public void RemoveCachedElement(Element ele) { List <string> toRemove = null; foreach (KeyValuePair <string, AttributeLookup> kvp in AttributeIndex) { string attribute = kvp.Key; AttributeLookup lookup = kvp.Value; // Got this attribute? string value = ele.getAttribute(attribute); if (value == null) { continue; } // Remove it: if (lookup.Remove(value, ele)) { // Remove the lookup too! if (toRemove == null) { toRemove = new List <string>(); } toRemove.Add(attribute); } } if (toRemove == null) { return; } if (toRemove.Count == AttributeIndex.Count) { // Remove the whole thing: AttributeIndex = null; return; } for (int i = toRemove.Count - 1; i >= 0; i--) { // Remove from attrib index: AttributeIndex.Remove(toRemove[i]); } }