~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Core/MonoDevelop.Projects.Dom.Serialization/NamespaceEntry.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// NamespaceEntry.cs
3
 
//
4
 
// Author:
5
 
//   Lluis Sanchez Gual
6
 
//
7
 
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
// 
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
// 
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
 
30
 
using System;
31
 
using System.Collections;
32
 
using System.Collections.Generic;
33
 
using System.Text;
34
 
using MonoDevelop.Projects;
35
 
 
36
 
namespace MonoDevelop.Projects.Dom.Serialization
37
 
{
38
 
        [Serializable]
39
 
        internal class NamespaceEntry
40
 
        {
41
 
                Hashtable contents = new Hashtable ();
42
 
                NamespaceEntry parent;
43
 
                string name;
44
 
 
45
 
                [NonSerialized]
46
 
                string fullName;
47
 
                
48
 
                // This is the case insensitive version of the hashtable.
49
 
                // It is constructed only when needed.
50
 
                [NonSerialized] Hashtable contents_ci;
51
 
                
52
 
                public NamespaceEntry (NamespaceEntry parent, string name)
53
 
                {
54
 
                        this.parent = parent;
55
 
                        this.name = name;
56
 
                }
57
 
                
58
 
                public string FullName {
59
 
                        get {
60
 
                                if (fullName != null)
61
 
                                        return fullName;
62
 
                                
63
 
                                if (parent != null) {
64
 
                                        if (string.IsNullOrEmpty (parent.name))
65
 
                                                return fullName = name;
66
 
                                        NamespaceEntry p = parent;
67
 
                                        StringBuilder sb = new StringBuilder (name);
68
 
                                        do {
69
 
                                                sb.Insert (0, ".").Insert (0, p.name);
70
 
                                                p = p.parent;
71
 
                                        } while (p != null && !string.IsNullOrEmpty (p.name));
72
 
                                        
73
 
                                        return fullName = sb.ToString ();;
74
 
                                }
75
 
                                else
76
 
                                        return string.Empty;
77
 
                        }
78
 
                }
79
 
                
80
 
                public IEnumerable<ClassEntry> GetAllClasses ()
81
 
                {
82
 
                        foreach (object ob in contents.Values) {
83
 
                                if (ob is ClassEntry)
84
 
                                        yield return (ClassEntry)ob;
85
 
                                else if (ob is NamespaceEntry) {
86
 
                                        foreach (ClassEntry ce in ((NamespaceEntry)ob).GetAllClasses ())
87
 
                                                yield return ce;
88
 
                                }
89
 
                        }
90
 
                }
91
 
                
92
 
                // All methods with the caseSensitive parameter, first check for an
93
 
                // exact match, and if not found, they try with the case insensitive table.
94
 
                
95
 
                public NamespaceEntry GetNamespace (string ns, bool caseSensitive)
96
 
                {
97
 
                        NamespaceEntry ne = contents[ns] as NamespaceEntry;
98
 
                        if (ne != null || caseSensitive) return ne;
99
 
                        
100
 
                        if (contents_ci == null) BuildCaseInsensitiveTable ();
101
 
                        return contents_ci[ns] as NamespaceEntry;
102
 
                }
103
 
                
104
 
                public ClassEntry GetClass (string name, int genericArgumentCount, bool caseSensitive)
105
 
                {
106
 
                        string fullName = ParserDatabase.GetDecoratedName (name, genericArgumentCount);
107
 
                        ClassEntry ne = contents[fullName] as ClassEntry;
108
 
                        if (ne != null || caseSensitive) return ne;
109
 
                        
110
 
                        if (contents_ci == null) BuildCaseInsensitiveTable ();
111
 
                        return contents_ci[fullName] as ClassEntry;
112
 
                }
113
 
                
114
 
                public void Add (NamespaceEntry value)
115
 
                {
116
 
                        
117
 
                        contents [value.name] = value;
118
 
                        if (contents_ci != null)
119
 
                                contents_ci [value.name] = value;
120
 
                }
121
 
                
122
 
                public void Add (ClassEntry value)
123
 
                {
124
 
                        string name = ParserDatabase.GetDecoratedName (value);
125
 
                        
126
 
                        contents [name] = value;
127
 
                        if (contents_ci != null)
128
 
                                contents_ci [name] = value;
129
 
                }
130
 
                
131
 
                public void Remove (NamespaceEntry name)
132
 
                {
133
 
                        contents.Remove (name);
134
 
                        contents_ci = null;
135
 
                }
136
 
                
137
 
                public void Remove (ClassEntry entry)
138
 
                {
139
 
                        contents.Remove (ParserDatabase.GetDecoratedName (entry));
140
 
                        contents_ci = null;
141
 
                }
142
 
                
143
 
                public ICollection Contents
144
 
                {
145
 
                        get { return contents; }
146
 
                }
147
 
                
148
 
                public int ContentCount
149
 
                {
150
 
                        get { return contents.Count; }
151
 
                }
152
 
                
153
 
                public void Clean ()
154
 
                {
155
 
                        ArrayList todel = new ArrayList ();
156
 
                        foreach (DictionaryEntry en in contents)
157
 
                        {
158
 
                                NamespaceEntry h = en.Value as NamespaceEntry;
159
 
                                if (h != null) {
160
 
                                        h.Clean ();
161
 
                                        if (h.ContentCount == 0) todel.Add (en.Key);
162
 
                                }
163
 
                        }
164
 
                        
165
 
                        if (todel.Count > 0)
166
 
                        {
167
 
                                contents_ci = null;
168
 
                                foreach (string key in todel)
169
 
                                        contents.Remove (key);
170
 
                        }
171
 
                }
172
 
                
173
 
                void BuildCaseInsensitiveTable ()
174
 
                {
175
 
                        contents_ci = new Hashtable (StringComparer.CurrentCultureIgnoreCase);
176
 
                        foreach (DictionaryEntry en in contents)
177
 
                                contents_ci.Add (en.Key, en.Value);
178
 
                }
179
 
        }
180
 
}