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

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins.Description/ModuleDescription.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ModuleDescription.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 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
using System;
 
30
using System.Collections;
 
31
using System.IO;
 
32
using System.Xml;
 
33
using System.Xml.Serialization;
 
34
using System.Collections.Specialized;
 
35
using Mono.Addins.Serialization;
 
36
 
 
37
namespace Mono.Addins.Description
 
38
{
 
39
        /// <summary>
 
40
        /// A module definition.
 
41
        /// </summary>
 
42
        /// <remarks>
 
43
        /// Optional modules can be used to declare extensions which will be registered only if some
 
44
        /// specified add-in dependencies can be satisfied.
 
45
        /// </remarks>
 
46
        public class ModuleDescription: ObjectDescription
 
47
        {
 
48
                StringCollection assemblies;
 
49
                StringCollection dataFiles;
 
50
                StringCollection ignorePaths;
 
51
                DependencyCollection dependencies;
 
52
                ExtensionCollection extensions;
 
53
                
 
54
                // Used only at run time
 
55
                internal RuntimeAddin RuntimeAddin;
 
56
                
 
57
                internal ModuleDescription (XmlElement element)
 
58
                {
 
59
                        Element = element;
 
60
                }
 
61
 
 
62
                /// <summary>
 
63
                /// Initializes a new instance of the <see cref="Mono.Addins.Description.ModuleDescription"/> class.
 
64
                /// </summary>
 
65
                public ModuleDescription ()
 
66
                {
 
67
                }
 
68
 
 
69
                /// <summary>
 
70
                /// Checks if this module depends on the specified add-in.
 
71
                /// </summary>
 
72
                /// <returns>
 
73
                /// <c>true</c> if there is a dependency.
 
74
                /// </returns>
 
75
                /// <param name='addinId'>
 
76
                /// Identifier of the add-in
 
77
                /// </param>
 
78
                public bool DependsOnAddin (string addinId)
 
79
                {
 
80
                        AddinDescription desc = Parent as AddinDescription;
 
81
                        if (desc == null)
 
82
                                throw new InvalidOperationException ();
 
83
                        
 
84
                        foreach (Dependency dep in Dependencies) {
 
85
                                AddinDependency adep = dep as AddinDependency;
 
86
                                if (adep == null) continue;
 
87
                                if (Addin.GetFullId (desc.Namespace, adep.AddinId, adep.Version) == addinId)
 
88
                                        return true;
 
89
                        }
 
90
                        return false;
 
91
                }
 
92
                
 
93
                /// <summary>
 
94
                /// Gets the list of paths to be ignored by the add-in scanner.
 
95
                /// </summary>
 
96
                public StringCollection IgnorePaths {
 
97
                        get {
 
98
                                if (ignorePaths == null)
 
99
                                        ignorePaths = new StringCollection ();
 
100
                                return ignorePaths;
 
101
                        }
 
102
                }
 
103
                
 
104
                /// <summary>
 
105
                /// Gets all external files
 
106
                /// </summary>
 
107
                /// <value>
 
108
                /// All files.
 
109
                /// </value>
 
110
                /// <remarks>
 
111
                /// External files are data files and assemblies explicitly referenced in the Runtime section of the add-in manifest.
 
112
                /// </remarks>
 
113
                public StringCollection AllFiles {
 
114
                        get {
 
115
                                StringCollection col = new StringCollection ();
 
116
                                foreach (string s in Assemblies)
 
117
                                        col.Add (s);
 
118
 
 
119
                                foreach (string d in DataFiles)
 
120
                                        col.Add (d);
 
121
                                        
 
122
                                return col;
 
123
                        }
 
124
                }
 
125
                
 
126
                /// <summary>
 
127
                /// Gets the list of external assemblies used by this module.
 
128
                /// </summary>
 
129
                public StringCollection Assemblies {
 
130
                        get {
 
131
                                if (assemblies == null) {
 
132
                                        if (Element != null)
 
133
                                                InitCollections ();
 
134
                                        else
 
135
                                                assemblies = new StringCollection ();
 
136
                                }
 
137
                                return assemblies;
 
138
                        }
 
139
                }
 
140
                
 
141
                /// <summary>
 
142
                /// Gets the list of external data files used by this module
 
143
                /// </summary>
 
144
                public StringCollection DataFiles {
 
145
                        get {
 
146
                                if (dataFiles == null) {
 
147
                                        if (Element != null)
 
148
                                                InitCollections ();
 
149
                                        else
 
150
                                                dataFiles = new StringCollection ();
 
151
                                }
 
152
                                return dataFiles;
 
153
                        }
 
154
                }
 
155
                
 
156
                /// <summary>
 
157
                /// Gets the dependencies of this module
 
158
                /// </summary>
 
159
                public DependencyCollection Dependencies {
 
160
                        get {
 
161
                                if (dependencies == null) {
 
162
                                        dependencies = new DependencyCollection (this);
 
163
                                        if (Element != null) {
 
164
                                                XmlNodeList elems = Element.SelectNodes ("Dependencies/*");
 
165
 
 
166
                                                foreach (XmlNode node in elems) {
 
167
                                                        XmlElement elem = node as XmlElement;
 
168
                                                        if (elem == null) continue;
 
169
                                                        
 
170
                                                        if (elem.Name == "Addin") {
 
171
                                                                AddinDependency dep = new AddinDependency (elem);
 
172
                                                                dependencies.Add (dep);
 
173
                                                        } else if (elem.Name == "Assembly") {
 
174
                                                                AssemblyDependency dep = new AssemblyDependency (elem);
 
175
                                                                dependencies.Add (dep);
 
176
                                                        }
 
177
                                                }
 
178
                                        }
 
179
                                }
 
180
                                return dependencies;
 
181
                        }
 
182
                }
 
183
                
 
184
                /// <summary>
 
185
                /// Gets the extensions of this module
 
186
                /// </summary>
 
187
                public ExtensionCollection Extensions {
 
188
                        get {
 
189
                                if (extensions == null) {
 
190
                                        extensions = new ExtensionCollection (this);
 
191
                                        if (Element != null) {
 
192
                                                foreach (XmlElement elem in Element.SelectNodes ("Extension"))
 
193
                                                        extensions.Add (new Extension (elem));
 
194
                                        }
 
195
                                }
 
196
                                return extensions;
 
197
                        }
 
198
                }
 
199
                
 
200
                /// <summary>
 
201
                /// Adds an extension node to the module.
 
202
                /// </summary>
 
203
                /// <returns>
 
204
                /// The extension node.
 
205
                /// </returns>
 
206
                /// <param name='path'>
 
207
                /// Path that identifies the extension point.
 
208
                /// </param>
 
209
                /// <param name='nodeName'>
 
210
                /// Node name.
 
211
                /// </param>
 
212
                /// <remarks>
 
213
                /// This method creates a new Extension object for the provided path if none exist.
 
214
                /// </remarks>
 
215
                public ExtensionNodeDescription AddExtensionNode (string path, string nodeName)
 
216
                {
 
217
                        ExtensionNodeDescription node = new ExtensionNodeDescription (nodeName);
 
218
                        GetExtension (path).ExtensionNodes.Add (node);
 
219
                        return node;
 
220
                }
 
221
                
 
222
                /// <summary>
 
223
                /// Gets an extension instance.
 
224
                /// </summary>
 
225
                /// <returns>
 
226
                /// The extension instance.
 
227
                /// </returns>
 
228
                /// <param name='path'>
 
229
                /// Path that identifies the extension point that the extension extends.
 
230
                /// </param>
 
231
                /// <remarks>
 
232
                /// This method creates a new Extension object for the provided path if none exist.
 
233
                /// </remarks>
 
234
                public Extension GetExtension (string path)
 
235
                {
 
236
                        foreach (Extension e in Extensions) {
 
237
                                if (e.Path == path)
 
238
                                        return e;
 
239
                        }
 
240
                        Extension ex = new Extension (path);
 
241
                        Extensions.Add (ex);
 
242
                        return ex;
 
243
                }
 
244
                
 
245
                internal override void SaveXml (XmlElement parent)
 
246
                {
 
247
                        CreateElement (parent, "Module");
 
248
                        
 
249
                        if (assemblies != null || dataFiles != null || ignorePaths != null) {
 
250
                                XmlElement runtime = GetRuntimeElement ();
 
251
                                
 
252
                                while (runtime.FirstChild != null)
 
253
                                        runtime.RemoveChild (runtime.FirstChild);
 
254
                                        
 
255
                                if (assemblies != null) {
 
256
                                        foreach (string s in assemblies) {
 
257
                                                XmlElement asm = Element.OwnerDocument.CreateElement ("Import");
 
258
                                                asm.SetAttribute ("assembly", s);
 
259
                                                runtime.AppendChild (asm);
 
260
                                        }
 
261
                                }
 
262
                                if (dataFiles != null) {
 
263
                                        foreach (string s in dataFiles) {
 
264
                                                XmlElement asm = Element.OwnerDocument.CreateElement ("Import");
 
265
                                                asm.SetAttribute ("file", s);
 
266
                                                runtime.AppendChild (asm);
 
267
                                        }
 
268
                                }
 
269
                                if (ignorePaths != null) {
 
270
                                        foreach (string s in ignorePaths) {
 
271
                                                XmlElement asm = Element.OwnerDocument.CreateElement ("ScanExclude");
 
272
                                                asm.SetAttribute ("path", s);
 
273
                                                runtime.AppendChild (asm);
 
274
                                        }
 
275
                                }
 
276
                                runtime.AppendChild (Element.OwnerDocument.CreateTextNode ("\n"));
 
277
                        }
 
278
                        
 
279
                        // Save dependency information
 
280
                        
 
281
                        if (dependencies != null) {
 
282
                                XmlElement deps = GetDependenciesElement ();
 
283
                                dependencies.SaveXml (deps);
 
284
                                deps.AppendChild (Element.OwnerDocument.CreateTextNode ("\n"));
 
285
                                
 
286
                                if (extensions != null)
 
287
                                        extensions.SaveXml (Element);
 
288
                        }
 
289
                }
 
290
                
 
291
                /// <summary>
 
292
                /// Adds an add-in reference (there is a typo in the method name)
 
293
                /// </summary>
 
294
                /// <param name='id'>
 
295
                /// Identifier of the add-in.
 
296
                /// </param>
 
297
                /// <param name='version'>
 
298
                /// Version of the add-in.
 
299
                /// </param>
 
300
                public void AddAssemblyReference (string id, string version)
 
301
                {
 
302
                        XmlElement deps = GetDependenciesElement ();
 
303
                        if (deps.SelectSingleNode ("Addin[@id='" + id + "']") != null)
 
304
                                return;
 
305
                        
 
306
                        XmlElement dep = Element.OwnerDocument.CreateElement ("Addin");
 
307
                        dep.SetAttribute ("id", id);
 
308
                        dep.SetAttribute ("version", version);
 
309
                        deps.AppendChild (dep);
 
310
                }
 
311
                
 
312
                XmlElement GetDependenciesElement ()
 
313
                {
 
314
                        XmlElement de = Element ["Dependencies"];
 
315
                        if (de != null)
 
316
                                return de;
 
317
 
 
318
                        de = Element.OwnerDocument.CreateElement ("Dependencies");
 
319
                        Element.AppendChild (de);
 
320
                        return de;
 
321
                }
 
322
                
 
323
                XmlElement GetRuntimeElement ()
 
324
                {
 
325
                        XmlElement de = Element ["Runtime"];
 
326
                        if (de != null)
 
327
                                return de;
 
328
 
 
329
                        de = Element.OwnerDocument.CreateElement ("Runtime");
 
330
                        Element.AppendChild (de);
 
331
                        return de;
 
332
                }
 
333
                
 
334
                void InitCollections ()
 
335
                {
 
336
                        dataFiles = new StringCollection ();
 
337
                        assemblies = new StringCollection ();
 
338
                        
 
339
                        XmlNodeList elems = Element.SelectNodes ("Runtime/*");
 
340
                        foreach (XmlElement elem in elems) {
 
341
                                if (elem.LocalName == "Import") {
 
342
                                        string asm = elem.GetAttribute ("assembly");
 
343
                                        if (asm.Length > 0) {
 
344
                                                assemblies.Add (asm);
 
345
                                        } else {
 
346
                                                string file = elem.GetAttribute ("file");
 
347
                                                if (file.Length > 0)
 
348
                                                        dataFiles.Add (file);
 
349
                                        }
 
350
                                } else if (elem.LocalName == "ScanExclude") {
 
351
                                        string path = elem.GetAttribute ("path");
 
352
                                        if (path.Length > 0)
 
353
                                                IgnorePaths.Add (path);
 
354
                                }
 
355
                        }
 
356
                }
 
357
                
 
358
                internal override void Verify (string location, StringCollection errors)
 
359
                {
 
360
                        Dependencies.Verify (location + "Module/", errors);
 
361
                        Extensions.Verify (location + "Module/", errors);
 
362
                }
 
363
 
 
364
                internal override void Write (BinaryXmlWriter writer)
 
365
                {
 
366
                        writer.WriteValue ("Assemblies", Assemblies);
 
367
                        writer.WriteValue ("DataFiles", DataFiles);
 
368
                        writer.WriteValue ("Dependencies", Dependencies);
 
369
                        writer.WriteValue ("Extensions", Extensions);
 
370
                        writer.WriteValue ("IgnorePaths", ignorePaths);
 
371
                }
 
372
                
 
373
                internal override void Read (BinaryXmlReader reader)
 
374
                {
 
375
                        assemblies = (StringCollection) reader.ReadValue ("Assemblies", new StringCollection ());
 
376
                        dataFiles = (StringCollection) reader.ReadValue ("DataFiles", new StringCollection ());
 
377
                        dependencies = (DependencyCollection) reader.ReadValue ("Dependencies", new DependencyCollection (this));
 
378
                        extensions = (ExtensionCollection) reader.ReadValue ("Extensions", new ExtensionCollection (this));
 
379
                        ignorePaths = (StringCollection) reader.ReadValue ("IgnorePaths", new StringCollection ());
 
380
                }
 
381
        }
 
382
}