Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExtensionPoint.cs « Mono.Addins.Description « Mono.Addins - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f3f850ab6e386a3b09683b604e04249499e79aa9 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// ExtensionPoint.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 System.Xml;
using Mono.Addins.Serialization;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace Mono.Addins.Description
{
	/// <summary>
	/// An extension point definition.
	/// </summary>
	public sealed class ExtensionPoint: ObjectDescription
	{
		string path;
		string name;
		string description;
		ExtensionNodeSet nodeSet;
		ConditionTypeDescriptionCollection conditions;
		string defaultInsertBefore;
		string defaultInsertAfter;

		// Information gathered from others addins:
		
		List<string> addins;  // Add-ins which extend this extension point
		string rootAddin;     // Add-in which defines this extension point
		
		internal ExtensionPoint (XmlElement elem): base (elem)
		{
			path = elem.GetAttribute ("path");
			name = elem.GetAttribute ("name");
			defaultInsertBefore = elem.GetAttribute ("defaultInsertBefore");
			defaultInsertAfter = elem.GetAttribute ("defaultInsertAfter");
			description = ReadXmlDescription ();
		}
		
		/// <summary>
		/// Initializes a new instance of the <see cref="Mono.Addins.Description.ExtensionPoint"/> class.
		/// </summary>
		public ExtensionPoint ()
		{
		}
		
		/// <summary>
		/// Copies another extension point.
		/// </summary>
		/// <param name='ep'>
		/// Extension point from which to copy.
		/// </param>
		public void CopyFrom (ExtensionPoint ep)
		{
			path = ep.path;
			name = ep.name;
			defaultInsertBefore = ep.defaultInsertBefore;
			defaultInsertAfter = ep.defaultInsertAfter;
			description = ep.description;
			NodeSet.CopyFrom (ep.NodeSet);
			Conditions.Clear ();
			foreach (ConditionTypeDescription cond in ep.Conditions) {
				ConditionTypeDescription cc = new ConditionTypeDescription ();
				cc.CopyFrom (cond);
				Conditions.Add (cc);
			}
			Addins.Clear ();
			foreach (string s in ep.Addins)
				Addins.Add (s);
			rootAddin = ep.rootAddin;
		}
		
		internal override void Verify (string location, StringCollection errors)
		{
			VerifyNotEmpty (location + "ExtensionPoint", errors, Path, "path");
			NodeSet.Verify (location + "ExtensionPoint (" + Path + ")/", errors);
			Conditions.Verify (location + "ExtensionPoint (" + Path + ")/", errors);
		}
		
		internal void SetExtensionsAddinId (string addinId)
		{
			NodeSet.SetExtensionsAddinId (addinId);
			foreach (ConditionTypeDescription cond in Conditions)
				cond.AddinId = addinId;
			Addins.Add (addinId);
		}
		
		internal void MergeWith (string thisAddinId, ExtensionPoint ep)
		{
			NodeSet.MergeWith (thisAddinId, ep.NodeSet);
			
			foreach (ConditionTypeDescription cond in ep.Conditions) {
				if (cond.AddinId != thisAddinId && !Conditions.Contains (cond))
					Conditions.Add (cond);
			}
			foreach (string s in ep.Addins) {
				if (!Addins.Contains (s))
					Addins.Add (s);
			}
		}
		
		internal void UnmergeExternalData (string thisAddinId, Hashtable addinsToUnmerge)
		{
			NodeSet.UnmergeExternalData (thisAddinId, addinsToUnmerge);
			
			var todel = new List<ConditionTypeDescription> ();
			foreach (ConditionTypeDescription cond in Conditions) {
				if (cond.AddinId != thisAddinId && (addinsToUnmerge == null || addinsToUnmerge.Contains (cond.AddinId)))
					todel.Add (cond);
			}
			foreach (ConditionTypeDescription cond in todel)
				Conditions.Remove (cond);
			
			if (addinsToUnmerge == null)
				Addins.Clear ();
			else {
				foreach (string s in addinsToUnmerge.Keys)
					Addins.Remove (s);
			}
			if (thisAddinId != null && !Addins.Contains (thisAddinId))
				Addins.Add (thisAddinId);
		}
		
		internal void Clear ()
		{
			NodeSet.Clear ();
			Conditions.Clear ();
			Addins.Clear ();
		}
		
		internal override void SaveXml (XmlElement parent)
		{
			CreateElement (parent, "ExtensionPoint"); 
			
			Element.SetAttribute ("path", Path);
			
			if (Name.Length > 0)
				Element.SetAttribute ("name", Name);
			else
				Element.RemoveAttribute ("name");
			
			if (DefaultInsertBefore.Length > 0)
				Element.SetAttribute ("defaultInsertBefore", DefaultInsertBefore);
			else
				Element.RemoveAttribute ("defaultInsertBefore");

			if (DefaultInsertAfter.Length > 0)
				Element.SetAttribute ("defaultInsertAfter", DefaultInsertAfter);
			else
				Element.RemoveAttribute ("defaultInsertAfter");

			SaveXmlDescription (Description);
			
			if (nodeSet != null) {
				nodeSet.Element = Element;
				nodeSet.SaveXml (parent);
			}
		}
		
		/// <summary>
		/// Gets or sets the path that identifies the extension point.
		/// </summary>
		/// <value>
		/// The path.
		/// </value>
		public string Path {
			get { return path != null ? path : string.Empty; }
			set { path = value; }
		}
		
		/// <summary>
		/// Gets or sets the display name of the extension point.
		/// </summary>
		/// <value>
		/// The name.
		/// </value>
		public string Name {
			get { return name != null ? name : string.Empty; }
			set { name = value; }
		}
		
		/// <summary>
		/// Gets or sets the description of the extension point.
		/// </summary>
		/// <value>
		/// The description.
		/// </value>
		public string Description {
			get { return description != null ? description : string.Empty; }
			set { description = value; }
		}
		
		/// <summary>
		/// Gets a list of add-ins that extend this extension point.
		/// </summary>
		/// <remarks>
		/// This value is only available when the add-in description is loaded from an add-in registry.
		/// </remarks>
		public string[] ExtenderAddins {
			get {
				return Addins.ToArray ();
			}
		}
		
		internal List<string> Addins {
			get {
				if (addins == null)
					addins = new List<string> ();
				return addins;
			}
		}
		
		internal string RootAddin {
			get { return rootAddin; }
			set { rootAddin = value; }
		}
		
		/// <summary>
		/// A node set which specifies the node types allowed in this extension point.
		/// </summary>
		/// <value>
		/// The node set.
		/// </value>
		public ExtensionNodeSet NodeSet {
			get {
				if (nodeSet == null) {
					if (Element != null)
						nodeSet = new ExtensionNodeSet (Element);
					else
						nodeSet = new ExtensionNodeSet ();
					nodeSet.SetParent (this);
				}
				return nodeSet;
			}
		}
		
		internal void SetNodeSet (ExtensionNodeSet nset)
		{
			// Used only by the addin updater
			nodeSet = nset;
			nodeSet.SetParent (this);
		}
		
		/// <summary>
		/// Gets the conditions available in this node set.
		/// </summary>
		/// <value>
		/// The conditions.
		/// </value>
		public ConditionTypeDescriptionCollection Conditions {
			get {
				if (conditions == null) {
					conditions = new ConditionTypeDescriptionCollection (this);
					if (Element != null) {
						foreach (XmlElement elem in Element.SelectNodes ("ConditionType"))
							conditions.Add (new ConditionTypeDescription (elem));
					}
				}
				return conditions;
			}
		}
		
		/// <summary>
		/// Adds an extension node type.
		/// </summary>
		/// <returns>
		/// The extension node type.
		/// </returns>
		/// <param name='name'>
		/// Name of the node
		/// </param>
		/// <param name='typeName'>
		/// Name of the type that implements the extension node.
		/// </param>
		/// <remarks>
		/// This method can be used to register a new allowed node type for the extension point.
		/// </remarks>
		public ExtensionNodeType AddExtensionNode (string name, string typeName)
		{
			ExtensionNodeType ntype = new ExtensionNodeType();
			ntype.Id = name;
			ntype.TypeName = typeName;
			NodeSet.NodeTypes.Add(ntype);
			return ntype;

		}

		/// <summary>
		/// The id of the extension before which new extensions will be added, unless the extension defines its own InsertBefore value
		/// </summary>
		/// <value>The default insert before.</value>
		public string DefaultInsertBefore {
			get { return defaultInsertBefore ?? ""; }
			set { defaultInsertBefore = value; }
		}

		/// <summary>
		/// The id of the extension after which new extensions will be added, unless the extension defines its own InsertAfter value
		/// </summary>
		/// <value>The default insert before.</value>
		public string DefaultInsertAfter {
			get { return defaultInsertAfter ?? ""; }
			set { defaultInsertAfter = value; }
		}

		internal override void Write (BinaryXmlWriter writer)
		{
			writer.WriteValue ("path", path);
			writer.WriteValue ("name", name);
			writer.WriteValue ("description", Description);
			writer.WriteValue ("rootAddin", rootAddin);
			writer.WriteValue ("addins", Addins);
			writer.WriteValue ("NodeSet", NodeSet);
			writer.WriteValue ("Conditions", Conditions);
			writer.WriteValue ("defaultInsertBefore", defaultInsertBefore);
			writer.WriteValue ("defaultInsertAfter", defaultInsertAfter);
		}
		
		internal override void Read (BinaryXmlReader reader)
		{
			path = reader.ReadStringValue ("path");
			name = reader.ReadStringValue ("name");
			if (!reader.IgnoreDescriptionData)
				description = reader.ReadStringValue ("description");
			rootAddin = reader.ReadStringValue ("rootAddin");
			addins = (List<string>) reader.ReadValue ("addins", new List<string> ());
			nodeSet = (ExtensionNodeSet) reader.ReadValue ("NodeSet");
			conditions = (ConditionTypeDescriptionCollection) reader.ReadValue ("Conditions", new ConditionTypeDescriptionCollection (this));
			defaultInsertBefore = reader.ReadStringValue ("defaultInsertBefore");
			defaultInsertAfter = reader.ReadStringValue ("defaultInsertAfter");
			if (nodeSet != null)
				nodeSet.SetParent (this);
		}
	}
}