1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
8
namespace ICSharpCode.XmlEditor
11
/// Represents the path to an xml element starting from the root of the
14
public class XmlElementPath
16
QualifiedNameCollection elements = new QualifiedNameCollection();
17
XmlNamespaceCollection namespacesInScope = new XmlNamespaceCollection();
19
public XmlElementPath()
24
/// Gets the elements specifying the path.
26
/// <remarks>The order of the elements determines the path.</remarks>
27
public QualifiedNameCollection Elements {
28
get { return elements; }
31
public void AddElement(QualifiedName elementName)
33
elements.Add(elementName);
37
get { return elements.IsEmpty; }
41
/// Compacts the path so it only contains the elements that are from
42
/// the namespace of the last element in the path.
44
/// <remarks>This method is used when we need to know the path for a
45
/// particular namespace and do not care about the complete path.
49
if (elements.HasItems) {
50
QualifiedName lastName = Elements.GetLast();
51
int index = LastIndexNotMatchingNamespace(lastName.Namespace);
53
elements.RemoveFirst(index + 1);
58
public XmlNamespaceCollection NamespacesInScope {
59
get { return namespacesInScope; }
62
public string GetNamespaceForPrefix(string prefix)
64
return namespacesInScope.GetNamespaceForPrefix(prefix);
68
/// An xml element path is considered to be equal if
69
/// each path item has the same name and namespace.
71
public override bool Equals(object obj)
73
XmlElementPath rhsPath = obj as XmlElementPath;
74
if (rhsPath == null) {
78
return elements.Equals(rhsPath.elements);
81
public override int GetHashCode()
83
return elements.GetHashCode();
86
public override string ToString()
88
return elements.ToString();
91
public string GetRootNamespace()
93
return elements.GetRootNamespace();
97
/// Only updates those names without a namespace.
99
public void SetNamespaceForUnqualifiedNames(string namespaceUri)
101
foreach (QualifiedName name in elements) {
102
if (!name.HasNamespace) {
103
name.Namespace = namespaceUri;
108
int LastIndexNotMatchingNamespace(string namespaceUri)
110
if (elements.Count > 1) {
111
// Start the check from the last but one item.
112
for (int i = elements.Count - 2; i >= 0; --i) {
113
QualifiedName name = elements[i];
114
if (name.Namespace != namespaceUri) {