// // 2002-2005 AlphaSierraPapa // GNU General Public License // // $Revision$ // using System; namespace ICSharpCode.XmlEditor { /// /// Represents the path to an xml element starting from the root of the /// document. /// public class XmlElementPath { QualifiedNameCollection elements = new QualifiedNameCollection(); public XmlElementPath() { } /// /// Gets the elements specifying the path. /// /// The order of the elements determines the path. public QualifiedNameCollection Elements { get { return elements; } } /// /// Compacts the path so it only contains the elements that are from /// the namespace of the last element in the path. /// /// This method is used when we need to know the path for a /// particular namespace and do not care about the complete path. /// public void Compact() { if (elements.Count > 0) { QualifiedName lastName = Elements[Elements.Count - 1]; if (lastName != null) { int index = FindNonMatchingParentElement(lastName.Namespace); if (index != -1) { RemoveParentElements(index); } } } } /// /// An xml element path is considered to be equal if /// each path item has the same name and namespace. /// public override bool Equals(object obj) { if (!(obj is XmlElementPath)) return false; if (this == obj) return true; XmlElementPath rhs = (XmlElementPath)obj; if (elements.Count == rhs.elements.Count) { for (int i = 0; i < elements.Count; ++i) { if (!elements[i].Equals(rhs.elements[i])) { return false; } } return true; } return false; } public override int GetHashCode() { return elements.GetHashCode(); } /// /// Removes elements up to and including the specified index. /// void RemoveParentElements(int index) { while (index >= 0) { --index; elements.RemoveFirst(); } } /// /// Finds the first parent that does belong in the specified /// namespace. /// int FindNonMatchingParentElement(string namespaceUri) { int index = -1; if (elements.Count > 1) { // Start the check from the the last but one item. for (int i = elements.Count - 2; i >= 0; --i) { QualifiedName name = elements[i]; if (name.Namespace != namespaceUri) { index = i; break; } } } return index; } } }