~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// ModuleDescription.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.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Specialized;
using Mono.Addins.Serialization;

namespace Mono.Addins.Description
{
	/// <summary>
	/// A module definition.
	/// </summary>
	/// <remarks>
	/// Optional modules can be used to declare extensions which will be registered only if some
	/// specified add-in dependencies can be satisfied.
	/// </remarks>
	public class ModuleDescription: ObjectDescription
	{
		StringCollection assemblies;
		StringCollection dataFiles;
		StringCollection ignorePaths;
		DependencyCollection dependencies;
		ExtensionCollection extensions;
		
		// Used only at run time
		internal RuntimeAddin RuntimeAddin;
		
		internal ModuleDescription (XmlElement element)
		{
			Element = element;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Mono.Addins.Description.ModuleDescription"/> class.
		/// </summary>
		public ModuleDescription ()
		{
		}

		/// <summary>
		/// Checks if this module depends on the specified add-in.
		/// </summary>
		/// <returns>
		/// <c>true</c> if there is a dependency.
		/// </returns>
		/// <param name='addinId'>
		/// Identifier of the add-in
		/// </param>
		public bool DependsOnAddin (string addinId)
		{
			AddinDescription desc = Parent as AddinDescription;
			if (desc == null)
				throw new InvalidOperationException ();
			
			foreach (Dependency dep in Dependencies) {
				AddinDependency adep = dep as AddinDependency;
				if (adep == null) continue;
				if (Addin.GetFullId (desc.Namespace, adep.AddinId, adep.Version) == addinId)
					return true;
			}
			return false;
		}
		
		/// <summary>
		/// Gets the list of paths to be ignored by the add-in scanner.
		/// </summary>
		public StringCollection IgnorePaths {
			get {
				if (ignorePaths == null)
					ignorePaths = new StringCollection ();
				return ignorePaths;
			}
		}
		
		/// <summary>
		/// Gets all external files
		/// </summary>
		/// <value>
		/// All files.
		/// </value>
		/// <remarks>
		/// External files are data files and assemblies explicitly referenced in the Runtime section of the add-in manifest.
		/// </remarks>
		public StringCollection AllFiles {
			get {
				StringCollection col = new StringCollection ();
				foreach (string s in Assemblies)
					col.Add (s);

				foreach (string d in DataFiles)
					col.Add (d);
					
				return col;
			}
		}
		
		/// <summary>
		/// Gets the list of external assemblies used by this module.
		/// </summary>
		public StringCollection Assemblies {
			get {
				if (assemblies == null) {
					if (Element != null)
						InitCollections ();
					else
						assemblies = new StringCollection ();
				}
				return assemblies;
			}
		}
		
		/// <summary>
		/// Gets the list of external data files used by this module
		/// </summary>
		public StringCollection DataFiles {
			get {
				if (dataFiles == null) {
					if (Element != null)
						InitCollections ();
					else
						dataFiles = new StringCollection ();
				}
				return dataFiles;
			}
		}
		
		/// <summary>
		/// Gets the dependencies of this module
		/// </summary>
		public DependencyCollection Dependencies {
			get {
				if (dependencies == null) {
					dependencies = new DependencyCollection (this);
					if (Element != null) {
						XmlNodeList elems = Element.SelectNodes ("Dependencies/*");

						foreach (XmlNode node in elems) {
							XmlElement elem = node as XmlElement;
							if (elem == null) continue;
							
							if (elem.Name == "Addin") {
								AddinDependency dep = new AddinDependency (elem);
								dependencies.Add (dep);
							} else if (elem.Name == "Assembly") {
								AssemblyDependency dep = new AssemblyDependency (elem);
								dependencies.Add (dep);
							}
						}
					}
				}
				return dependencies;
			}
		}
		
		/// <summary>
		/// Gets the extensions of this module
		/// </summary>
		public ExtensionCollection Extensions {
			get {
				if (extensions == null) {
					extensions = new ExtensionCollection (this);
					if (Element != null) {
						foreach (XmlElement elem in Element.SelectNodes ("Extension"))
							extensions.Add (new Extension (elem));
					}
				}
				return extensions;
			}
		}
		
		/// <summary>
		/// Adds an extension node to the module.
		/// </summary>
		/// <returns>
		/// The extension node.
		/// </returns>
		/// <param name='path'>
		/// Path that identifies the extension point.
		/// </param>
		/// <param name='nodeName'>
		/// Node name.
		/// </param>
		/// <remarks>
		/// This method creates a new Extension object for the provided path if none exist.
		/// </remarks>
		public ExtensionNodeDescription AddExtensionNode (string path, string nodeName)
		{
			ExtensionNodeDescription node = new ExtensionNodeDescription (nodeName);
			GetExtension (path).ExtensionNodes.Add (node);
			return node;
		}
		
		/// <summary>
		/// Gets an extension instance.
		/// </summary>
		/// <returns>
		/// The extension instance.
		/// </returns>
		/// <param name='path'>
		/// Path that identifies the extension point that the extension extends.
		/// </param>
		/// <remarks>
		/// This method creates a new Extension object for the provided path if none exist.
		/// </remarks>
		public Extension GetExtension (string path)
		{
			foreach (Extension e in Extensions) {
				if (e.Path == path)
					return e;
			}
			Extension ex = new Extension (path);
			Extensions.Add (ex);
			return ex;
		}
		
		internal override void SaveXml (XmlElement parent)
		{
			CreateElement (parent, "Module");
			
			if (assemblies != null || dataFiles != null || ignorePaths != null) {
				XmlElement runtime = GetRuntimeElement ();
				
				while (runtime.FirstChild != null)
					runtime.RemoveChild (runtime.FirstChild);
					
				if (assemblies != null) {
					foreach (string s in assemblies) {
						XmlElement asm = Element.OwnerDocument.CreateElement ("Import");
						asm.SetAttribute ("assembly", s);
						runtime.AppendChild (asm);
					}
				}
				if (dataFiles != null) {
					foreach (string s in dataFiles) {
						XmlElement asm = Element.OwnerDocument.CreateElement ("Import");
						asm.SetAttribute ("file", s);
						runtime.AppendChild (asm);
					}
				}
				if (ignorePaths != null) {
					foreach (string s in ignorePaths) {
						XmlElement asm = Element.OwnerDocument.CreateElement ("ScanExclude");
						asm.SetAttribute ("path", s);
						runtime.AppendChild (asm);
					}
				}
				runtime.AppendChild (Element.OwnerDocument.CreateTextNode ("\n"));
			}
			
			// Save dependency information
			
			if (dependencies != null) {
				XmlElement deps = GetDependenciesElement ();
				dependencies.SaveXml (deps);
				deps.AppendChild (Element.OwnerDocument.CreateTextNode ("\n"));
				
				if (extensions != null)
					extensions.SaveXml (Element);
			}
		}
		
		/// <summary>
		/// Adds an add-in reference (there is a typo in the method name)
		/// </summary>
		/// <param name='id'>
		/// Identifier of the add-in.
		/// </param>
		/// <param name='version'>
		/// Version of the add-in.
		/// </param>
		public void AddAssemblyReference (string id, string version)
		{
			XmlElement deps = GetDependenciesElement ();
			if (deps.SelectSingleNode ("Addin[@id='" + id + "']") != null)
				return;
			
			XmlElement dep = Element.OwnerDocument.CreateElement ("Addin");
			dep.SetAttribute ("id", id);
			dep.SetAttribute ("version", version);
			deps.AppendChild (dep);
		}
		
		XmlElement GetDependenciesElement ()
		{
			XmlElement de = Element ["Dependencies"];
			if (de != null)
				return de;

			de = Element.OwnerDocument.CreateElement ("Dependencies");
			Element.AppendChild (de);
			return de;
		}
		
		XmlElement GetRuntimeElement ()
		{
			XmlElement de = Element ["Runtime"];
			if (de != null)
				return de;

			de = Element.OwnerDocument.CreateElement ("Runtime");
			Element.AppendChild (de);
			return de;
		}
		
		void InitCollections ()
		{
			dataFiles = new StringCollection ();
			assemblies = new StringCollection ();
			
			XmlNodeList elems = Element.SelectNodes ("Runtime/*");
			foreach (XmlElement elem in elems) {
				if (elem.LocalName == "Import") {
					string asm = elem.GetAttribute ("assembly");
					if (asm.Length > 0) {
						assemblies.Add (asm);
					} else {
						string file = elem.GetAttribute ("file");
						if (file.Length > 0)
							dataFiles.Add (file);
					}
				} else if (elem.LocalName == "ScanExclude") {
					string path = elem.GetAttribute ("path");
					if (path.Length > 0)
						IgnorePaths.Add (path);
				}
			}
		}
		
		internal override void Verify (string location, StringCollection errors)
		{
			Dependencies.Verify (location + "Module/", errors);
			Extensions.Verify (location + "Module/", errors);
		}

		internal override void Write (BinaryXmlWriter writer)
		{
			writer.WriteValue ("Assemblies", Assemblies);
			writer.WriteValue ("DataFiles", DataFiles);
			writer.WriteValue ("Dependencies", Dependencies);
			writer.WriteValue ("Extensions", Extensions);
			writer.WriteValue ("IgnorePaths", ignorePaths);
		}
		
		internal override void Read (BinaryXmlReader reader)
		{
			assemblies = (StringCollection) reader.ReadValue ("Assemblies", new StringCollection ());
			dataFiles = (StringCollection) reader.ReadValue ("DataFiles", new StringCollection ());
			dependencies = (DependencyCollection) reader.ReadValue ("Dependencies", new DependencyCollection (this));
			extensions = (ExtensionCollection) reader.ReadValue ("Extensions", new ExtensionCollection (this));
			ignorePaths = (StringCollection) reader.ReadValue ("IgnorePaths", new StringCollection ());
		}
	}
}