~do-win/do/test-paths

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// AddinUpdateData.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System;
using System.Collections;
using Mono.Addins.Description;

namespace Mono.Addins.Database
{
	class AddinUpdateData
	{
		// This table collects information about extensions. For each path (key)
		// has a ExtensionInfo object with information about the addin that
		// defines the extension point and the addins which extend it
		Hashtable pathHash = new Hashtable ();
		
		// Collects globally defined node sets. Key is node set name. Value is
		// a ExtensionInfo
		Hashtable nodeSetHash = new Hashtable ();
		
		Hashtable objectTypeExtensions = new Hashtable ();
		
		internal int RelExtensionPoints;
		internal int RelExtensions;
		internal int RelNodeSetTypes;
		internal int RelExtensionNodes;
		
		class RootExtensionPoint
		{
			public AddinDescription Description;
			public ExtensionPoint ExtensionPoint;
		}
		
		IProgressStatus monitor;
		
		public AddinUpdateData (AddinDatabase database, IProgressStatus monitor)
		{
			this.monitor = monitor;
		}
		
		public void RegisterNodeSet (AddinDescription description, ExtensionNodeSet nset)
		{
			ArrayList extensions = (ArrayList) nodeSetHash [nset.Id];
			if (extensions != null) {
				// Extension point already registered
				ArrayList compatExtensions = GetCompatibleExtensionPoints (nset.Id, description, description.MainModule, extensions);
				if (compatExtensions.Count > 0) {
					foreach (ExtensionPoint einfo in compatExtensions)
						einfo.NodeSet.MergeWith (null, nset);
					return;
				}
			}
			// Create a new extension set
			RootExtensionPoint rep = new RootExtensionPoint ();
			rep.ExtensionPoint = new ExtensionPoint ();
			rep.ExtensionPoint.SetNodeSet (nset);
			rep.ExtensionPoint.RootAddin = description.AddinId;
			rep.ExtensionPoint.Path = nset.Id;
			rep.Description = description;
			if (extensions == null) {
				extensions = new ArrayList ();
				nodeSetHash [nset.Id] = extensions;
			}
			extensions.Add (rep);
		}
		
		public void RegisterExtensionPoint (AddinDescription description, ExtensionPoint ep)
		{
			ArrayList extensions = (ArrayList) pathHash [ep.Path];
			if (extensions != null) {
				// Extension point already registered
				ArrayList compatExtensions = GetCompatibleExtensionPoints (ep.Path, description, description.MainModule, extensions);
				if (compatExtensions.Count > 0) {
					foreach (ExtensionPoint einfo in compatExtensions)
						einfo.MergeWith (null, ep);
					RegisterObjectTypes (ep);
					return;
				}
			}
			// Create a new extension
			RootExtensionPoint rep = new RootExtensionPoint ();
			rep.ExtensionPoint = ep;
			rep.ExtensionPoint.RootAddin = description.AddinId;
			rep.Description = description;
			if (extensions == null) {
				extensions = new ArrayList ();
				pathHash [ep.Path] = extensions;
			}
			extensions.Add (rep);
			RegisterObjectTypes (ep);
		}
			
		void RegisterObjectTypes (ExtensionPoint ep)
		{
			// Register extension points bound to a node type
			
			foreach (ExtensionNodeType nt in ep.NodeSet.NodeTypes) {
				if (nt.ObjectTypeName.Length > 0) {
					ArrayList list = (ArrayList) objectTypeExtensions [nt.ObjectTypeName];
					if (list == null) {
						list = new ArrayList ();
						objectTypeExtensions [nt.ObjectTypeName] = list;
					}
					list.Add (ep);
				}
			}
		}

		public void RegisterExtension (AddinDescription description, ModuleDescription module, Extension extension)
		{
			if (extension.Path.StartsWith ("$")) {
				string[] objectTypes = extension.Path.Substring (1).Split (',');
				bool found = false;
				foreach (string s in objectTypes) {
					ArrayList list = (ArrayList) objectTypeExtensions [s];
					if (list != null) {
						found = true;
						foreach (ExtensionPoint ep in list) {
							if (IsAddinCompatible (ep.ParentAddinDescription, description, module)) {
								extension.Path = ep.Path;
								RegisterExtension (description, module, ep.Path);
							}
						}
					}
				}
				if (!found)
					monitor.ReportWarning ("The add-in '" + description.AddinId + "' is trying to register the class '" + extension.Path.Substring (1) + "', but there isn't any add-in defining a suitable extension point");
			}
		}
		
		public void RegisterExtension (AddinDescription description, ModuleDescription module, string path)
		{
			ArrayList extensions = (ArrayList) pathHash [path];
			if (extensions == null) {
				// Root add-in extension points are registered before any other kind of extension,
				// so we should find it now.
				extensions = GetParentExtensionInfo (path);
			}
			if (extensions == null) {
				monitor.ReportWarning ("The add-in '" + description.AddinId + "' is trying to extend '" + path + "', but there isn't any add-in defining this extension point");
				return;
			}
			
			bool found = false;
			foreach (RootExtensionPoint einfo in extensions) {
				if (IsAddinCompatible (einfo.Description, description, module)) {
					if (!einfo.ExtensionPoint.Addins.Contains (description.AddinId))
						einfo.ExtensionPoint.Addins.Add (description.AddinId);
					found = true;
				}
			}
			if (!found)
				monitor.ReportWarning ("The add-in '" + description.AddinId + "' is trying to extend '" + path + "', but there isn't any compatible add-in defining this extension point");
		}
		
		ArrayList GetCompatibleExtensionPoints (string path, AddinDescription description, ModuleDescription module, ArrayList rootExtensionPoints)
		{
			ArrayList list = new ArrayList ();
			foreach (RootExtensionPoint rep in rootExtensionPoints) {
				
				// Find an extension point defined in a root add-in which is compatible with the version of the extender dependency
				if (IsAddinCompatible (rep.Description, description, module))
					list.Add (rep.ExtensionPoint);
			}
			return list;
		}
		
		ArrayList GetParentExtensionInfo (string path)
		{
			int i = path.LastIndexOf ('/');
			if (i == -1)
				return null;
			string np = path.Substring (0, i);
			ArrayList ep = (ArrayList) pathHash [np];
			if (ep != null)
				return ep;
			else
				return GetParentExtensionInfo (np);
		}
		
		bool IsAddinCompatible (AddinDescription installedDescription, AddinDescription description, ModuleDescription module)
		{
			if (installedDescription == description)
				return true;
			if (installedDescription.Domain != AddinDatabase.GlobalDomain) {
				if (description.Domain != AddinDatabase.GlobalDomain && description.Domain != installedDescription.Domain)
					return false;
			} else if (description.Domain != AddinDatabase.GlobalDomain)
				return false;
				
			string addinId = Addin.GetFullId (installedDescription.Namespace, installedDescription.LocalId, null);
			string requiredVersion = null;
			
			for (int n = module.Dependencies.Count - 1; n >= 0; n--) {
				AddinDependency adep = module.Dependencies [n] as AddinDependency;
				if (adep != null && Addin.GetFullId (description.Namespace, adep.AddinId, null) == addinId) {
					requiredVersion = adep.Version;
					break;
				}
			}
			if (requiredVersion == null)
				return false;

			// Check if the required version is between rep.Description.CompatVersion and rep.Description.Version
			if (Addin.CompareVersions (installedDescription.Version, requiredVersion) > 0)
				return false;
			if (installedDescription.CompatVersion.Length > 0 && Addin.CompareVersions (installedDescription.CompatVersion, requiredVersion) < 0)
				return false;
			
			return true;
		}
	}
}