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

ExtensionTree.cs « Mono.Addins « Mono.Addins - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 37a16ba58d7e209667428c2f7c30947ea3f21527 (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
//
// ExtensionTree.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.Reflection;
using System.Xml;
using Mono.Addins.Description;
using System.Collections.Generic;
using Mono.Addins.Database;

namespace Mono.Addins
{
	internal class ExtensionTree: TreeNode
	{
		int internalId;
		internal const string AutoIdPrefix = "__nid_";
		readonly ExtensionContext context;
		
		public ExtensionTree (AddinEngine addinEngine, ExtensionContext context): base (addinEngine, "")
		{
			this.context = context;
		}
		
		public override ExtensionContext Context {
			get { return context; }
		}

		
		public void LoadExtension (ExtensionContextTransaction transaction, TreeNode tnode, string addin, Extension extension, List<TreeNode> addedNodes)
		{
			if (tnode == null) {
				addinEngine.ReportError ("Can't load extensions for path '" + extension.Path + "'. Extension point not defined.", addin, null, false);
				return;
			}
			
			int curPos = -1;
			LoadExtensionElement (transaction, tnode, addin, extension.ExtensionNodes, (ModuleDescription) extension.Parent, ref curPos, tnode.Condition, false, addedNodes);
		}

		void LoadExtensionElement (ExtensionContextTransaction transaction, TreeNode parentNode, string addin, ExtensionNodeDescriptionCollection extension, ModuleDescription module, ref int curPos, BaseCondition parentCondition, bool inComplextCondition, List<TreeNode> addedNodes)
		{
			foreach (ExtensionNodeDescription elem in extension) {

				if (inComplextCondition) {
					parentCondition = ReadComplexCondition (elem, parentCondition);
					inComplextCondition = false;
					continue;
				}

				if (elem.NodeName == "ComplexCondition") {
					LoadExtensionElement (transaction, parentNode, addin, elem.ChildNodes, module, ref curPos, parentCondition, true, addedNodes);
					continue;
				}

				if (elem.NodeName == "Condition") {
					Condition cond = new Condition (AddinEngine, elem, parentCondition);
					LoadExtensionElement (transaction, parentNode, addin, elem.ChildNodes, module, ref curPos, cond, false, addedNodes);
					continue;
				}

				var pnode = parentNode;
				ExtensionPoint extensionPoint = null;
				while (pnode != null && (extensionPoint = pnode.ExtensionPoint) == null)
					pnode = pnode.Parent;

				string after = elem.GetAttribute ("insertafter");
				if (after.Length == 0 && extensionPoint != null && curPos == -1)
					after = extensionPoint.DefaultInsertAfter;
				if (after.Length > 0) {
					int i = parentNode.IndexOfChild (after);
					if (i != -1)
						curPos = i + 1;
				}
				string before = elem.GetAttribute ("insertbefore");
				if (before.Length == 0 && extensionPoint != null && curPos == -1)
					before = extensionPoint.DefaultInsertBefore;
				if (before.Length > 0) {
					int i = parentNode.IndexOfChild (before);
					if (i != -1)
						curPos = i;
				}

				// If node position is not explicitly set, add the node at the end
				if (curPos == -1)
					curPos = parentNode.Children.Count;

				// Find the type of the node in this extension
				ExtensionNodeType ntype = addinEngine.FindType (parentNode.ExtensionNodeSet, elem.NodeName, addin);

				if (ntype == null) {
					addinEngine.ReportError ("Node '" + elem.NodeName + "' not allowed in extension: " + parentNode.GetPath (), addin, null, false);
					continue;
				}

				string id = elem.GetAttribute ("id");
				if (id.Length == 0)
					id = AutoIdPrefix + (++internalId);

				TreeNode childNode = new TreeNode (addinEngine, id);

				ExtensionNode enode = ReadNode (childNode, addin, ntype, elem, module);
				if (enode == null)
					continue;

				// Enables bulk update of children
				parentNode.BeginChildrenUpdateTransaction (transaction);

				childNode.Condition = parentCondition;
				childNode.ExtensionNodeSet = ntype;
				parentNode.InsertChild (transaction, curPos, childNode);
				addedNodes.Add (childNode);

				// Load children
				if (elem.ChildNodes.Count > 0) {
					int cp = 0;
					LoadExtensionElement (transaction, childNode, addin, elem.ChildNodes, module, ref cp, parentCondition, false, addedNodes);
				}
				
				curPos++;
			}
		}
		
		BaseCondition ReadComplexCondition (ExtensionNodeDescription elem, BaseCondition parentCondition)
		{
			if (elem.NodeName == "Or" || elem.NodeName == "And" || elem.NodeName == "Not") {
				var conds = new List<BaseCondition> ();
				foreach (ExtensionNodeDescription celem in elem.ChildNodes) {
					conds.Add (ReadComplexCondition (celem, null));
				}
				if (elem.NodeName == "Or")
					return new OrCondition (conds.ToArray (), parentCondition);
				else if (elem.NodeName == "And")
					return new AndCondition (conds.ToArray (), parentCondition);
				else {
					if (conds.Count != 1) {
						addinEngine.ReportError ("Invalid complex condition element '" + elem.NodeName + "'. 'Not' condition can only have one parameter.", null, null, false);
						return new NullCondition ();
					}
					return new NotCondition (conds [0], parentCondition);
				}
			}
			if (elem.NodeName == "Condition") {
				return new Condition (AddinEngine, elem, parentCondition);
			}
			addinEngine.ReportError ("Invalid complex condition element '" + elem.NodeName + "'.", null, null, false);
			return new NullCondition ();
		}
		
		public ExtensionNode ReadNode (TreeNode tnode, string addin, ExtensionNodeType ntype, ExtensionNodeDescription elem, ModuleDescription module)
		{
			try {
				if (ntype.Type == null) {
					if (!InitializeNodeType (ntype))
						return null;
				}

				ExtensionNode node;
				node = Activator.CreateInstance (ntype.Type) as ExtensionNode;
				if (node == null) {
					addinEngine.ReportError ("Extension node type '" + ntype.Type + "' must be a subclass of ExtensionNode", addin, null, false);
					return null;
				}
				
				tnode.AttachExtensionNode (node);
				node.SetData (addinEngine, addin, ntype, module);
				node.Read (elem);
				return node;
			}
			catch (Exception ex) {
				addinEngine.ReportError ("Could not read extension node of type '" + ntype.Type + "' from extension path '" + tnode.GetPath() + "'", addin, ex, false);
				return null;
			}
		}
		
		bool InitializeNodeType (ExtensionNodeType ntype)
		{
			RuntimeAddin p = addinEngine.GetAddin (ntype.AddinId);
			if (p == null) {
				if (!addinEngine.IsAddinLoaded (ntype.AddinId)) {
					if (!addinEngine.LoadAddin (null, ntype.AddinId, false))
						return false;
					p = addinEngine.GetAddin (ntype.AddinId);
					if (p == null) {
						addinEngine.ReportError ("Add-in not found", ntype.AddinId, null, false);
						return false;
					}
				}
			}
			
			// If no type name is provided, use TypeExtensionNode by default
			if (ntype.TypeName == null || ntype.TypeName.Length == 0 || ntype.TypeName == typeof(TypeExtensionNode).AssemblyQualifiedName) {
				// If it has a custom attribute, use the generic version of TypeExtensionNode
				if (ntype.ExtensionAttributeTypeName.Length > 0) {
					Type attType = p.GetType (ntype.ExtensionAttributeTypeName, false);
					if (attType == null) {
						addinEngine.ReportError ("Custom attribute type '" + ntype.ExtensionAttributeTypeName + "' not found.", ntype.AddinId, null, false);
						return false;
					}
					if (ntype.ObjectTypeName.Length > 0 || ntype.TypeName == typeof(TypeExtensionNode).AssemblyQualifiedName)
						ntype.Type = typeof(TypeExtensionNode<>).MakeGenericType (attType);
					else
						ntype.Type = typeof(ExtensionNode<>).MakeGenericType (attType);
				} else {
					ntype.Type = typeof(TypeExtensionNode);
					return true;
				}
			}
			else {
				ntype.Type = p.GetType (ntype.TypeName, false);
				if (ntype.Type == null) {
					addinEngine.ReportError ("Extension node type '" + ntype.TypeName + "' not found.", ntype.AddinId, null, false);
					return false;
				}
			}
			
			// Check if the type has NodeAttribute attributes applied to fields.
			ExtensionNodeType.FieldData boundAttributeType = null;
			Dictionary<string,ExtensionNodeType.FieldData> fields = GetMembersMap (ntype.Type, out boundAttributeType);
			ntype.CustomAttributeMember = boundAttributeType;
			if (fields.Count > 0)
				ntype.Fields = fields;
			
			// If the node type is bound to a custom attribute and there is a member bound to that attribute,
			// get the member map for the attribute.
				
			if (boundAttributeType != null) {
				if (ntype.ExtensionAttributeTypeName.Length == 0)
					throw new InvalidOperationException ("Extension node not bound to a custom attribute.");

				if (!Util.TryParseTypeName (ntype.ExtensionAttributeTypeName, out var type, out _) || type != boundAttributeType.MemberType.FullName)
					throw new InvalidOperationException ("Incorrect custom attribute type declaration in " + ntype.Type + ". Expected '" + ntype.ExtensionAttributeTypeName + "' found '" + boundAttributeType.MemberType.AssemblyQualifiedName + "'");

				fields = GetMembersMap (boundAttributeType.MemberType, out boundAttributeType);
				if (fields.Count > 0)
					ntype.CustomAttributeFields = fields;
			}
			
			return true;
		}
		
		Dictionary<string,ExtensionNodeType.FieldData> GetMembersMap (Type type, out ExtensionNodeType.FieldData boundAttributeType)
		{
			string fname;
			Dictionary<string,ExtensionNodeType.FieldData> fields = new Dictionary<string, ExtensionNodeType.FieldData> ();
			boundAttributeType = null;
			
			while (type != typeof(object) && type != null) {
				foreach (FieldInfo field in type.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
					NodeAttributeAttribute at = (NodeAttributeAttribute) Attribute.GetCustomAttribute (field, typeof(NodeAttributeAttribute), true);
					if (at != null) {
						ExtensionNodeType.FieldData fd = CreateFieldData (field, at, out fname, ref boundAttributeType);
						if (fd != null)
							fields [fname] = fd;
					}
				}
				foreach (PropertyInfo prop in type.GetProperties (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
					NodeAttributeAttribute at = (NodeAttributeAttribute) Attribute.GetCustomAttribute (prop, typeof(NodeAttributeAttribute), true);
					if (at != null) {
						ExtensionNodeType.FieldData fd = CreateFieldData (prop, at, out fname, ref boundAttributeType);
						if (fd != null)
							fields [fname] = fd;
					}
				}
				type = type.BaseType;
			}
			return fields;
		}
		
		ExtensionNodeType.FieldData CreateFieldData (MemberInfo member, NodeAttributeAttribute at, out string name, ref ExtensionNodeType.FieldData boundAttributeType)
		{
			ExtensionNodeType.FieldData fdata = new ExtensionNodeType.FieldData ();
			fdata.Member = member;
			fdata.Required = at.Required;
			fdata.Localizable = at.Localizable;
			
			if (at.Name != null && at.Name.Length > 0)
				name = at.Name;
			else
				name = member.Name;
			
			if (typeof(CustomExtensionAttribute).IsAssignableFrom (fdata.MemberType)) {
				if (boundAttributeType != null)
					throw new InvalidOperationException ("Type '" + member.DeclaringType + "' has two members bound to a custom attribute. There can be only one.");
				boundAttributeType = fdata;
				return null;
			}
			
			return fdata;
		}
	}
}