~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlElementPath.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Text;
 
7
 
 
8
namespace ICSharpCode.XmlEditor
 
9
{
 
10
        /// <summary>
 
11
        /// Represents the path to an xml element starting from the root of the
 
12
        /// document.
 
13
        /// </summary>
 
14
        public class XmlElementPath
 
15
        {
 
16
                QualifiedNameCollection elements = new QualifiedNameCollection();
 
17
                XmlNamespaceCollection namespacesInScope = new XmlNamespaceCollection();
 
18
                
 
19
                public XmlElementPath()
 
20
                {
 
21
                }
 
22
                
 
23
                /// <summary>
 
24
                /// Gets the elements specifying the path.
 
25
                /// </summary>
 
26
                /// <remarks>The order of the elements determines the path.</remarks>
 
27
                public QualifiedNameCollection Elements {
 
28
                        get { return elements; }
 
29
                }
 
30
                
 
31
                public void AddElement(QualifiedName elementName)
 
32
                {
 
33
                        elements.Add(elementName);
 
34
                }
 
35
                
 
36
                public bool IsEmpty {
 
37
                        get { return elements.IsEmpty; }
 
38
                }
 
39
                
 
40
                /// <summary>
 
41
                /// Compacts the path so it only contains the elements that are from 
 
42
                /// the namespace of the last element in the path. 
 
43
                /// </summary>
 
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.
 
46
                /// </remarks>
 
47
                public void Compact()
 
48
                {
 
49
                        if (elements.HasItems) {
 
50
                                QualifiedName lastName = Elements.GetLast();
 
51
                                int index = LastIndexNotMatchingNamespace(lastName.Namespace);
 
52
                                if (index != -1) {
 
53
                                        elements.RemoveFirst(index + 1);
 
54
                                }
 
55
                        }
 
56
                }
 
57
                
 
58
                public XmlNamespaceCollection NamespacesInScope {
 
59
                        get { return namespacesInScope; }
 
60
                }
 
61
                
 
62
                public string GetNamespaceForPrefix(string prefix)
 
63
                {
 
64
                        return namespacesInScope.GetNamespaceForPrefix(prefix);
 
65
                }
 
66
                
 
67
                /// <summary>
 
68
                /// An xml element path is considered to be equal if 
 
69
                /// each path item has the same name and namespace.
 
70
                /// </summary>
 
71
                public override bool Equals(object obj) 
 
72
                {
 
73
                        XmlElementPath rhsPath = obj as XmlElementPath;                 
 
74
                        if (rhsPath == null) {
 
75
                                return false;
 
76
                        }
 
77
                        
 
78
                        return elements.Equals(rhsPath.elements);
 
79
                }
 
80
                
 
81
                public override int GetHashCode() 
 
82
                {
 
83
                        return elements.GetHashCode();
 
84
                }
 
85
                
 
86
                public override string ToString()
 
87
                {
 
88
                        return elements.ToString();
 
89
                }
 
90
                
 
91
                public string GetRootNamespace() 
 
92
                {
 
93
                        return elements.GetRootNamespace();
 
94
                }
 
95
                
 
96
                /// <summary>
 
97
                /// Only updates those names without a namespace.
 
98
                /// </summary>
 
99
                public void SetNamespaceForUnqualifiedNames(string namespaceUri)
 
100
                {
 
101
                        foreach (QualifiedName name in elements) {
 
102
                                if (!name.HasNamespace) {
 
103
                                        name.Namespace = namespaceUri;
 
104
                                }
 
105
                        }
 
106
                }
 
107
                                
 
108
                int LastIndexNotMatchingNamespace(string namespaceUri)
 
109
                {
 
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) {
 
115
                                                return i;
 
116
                                        }
 
117
                                }
 
118
                        }
 
119
                        return -1;
 
120
                }
 
121
        }
 
122
}