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
|
//
// 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
{
public class ModuleDescription: ObjectDescription
{
StringCollection assemblies;
StringCollection dataFiles;
StringCollection ignorePaths;
DependencyCollection dependencies;
ExtensionCollection extensions;
internal ModuleDescription (XmlElement element)
{
Element = element;
}
public ModuleDescription ()
{
}
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;
}
public StringCollection IgnorePaths {
get {
if (ignorePaths == null)
ignorePaths = new StringCollection ();
return ignorePaths;
}
}
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;
}
}
public StringCollection Assemblies {
get {
if (assemblies == null) {
if (Element != null)
InitCollections ();
else
assemblies = new StringCollection ();
}
return assemblies;
}
}
public StringCollection DataFiles {
get {
if (dataFiles == null) {
if (Element != null)
InitCollections ();
else
dataFiles = new StringCollection ();
}
return dataFiles;
}
}
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;
}
}
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;
}
}
public ExtensionNodeDescription AddExtensionNode (string path, string nodeName)
{
ExtensionNodeDescription node = new ExtensionNodeDescription (nodeName);
GetExtension (path).ExtensionNodes.Add (node);
return node;
}
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);
}
}
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 ());
}
}
}
|