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

ExtensionNodeDescription.cs « Mono.Addins.Description « Mono.Addins - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 343563507d0f6f8200e98cf2eba24032ab381877 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// ExtensionNodeDescription.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.Xml;
using System.Collections.Specialized;
using Mono.Addins.Serialization;

namespace Mono.Addins.Description
{
	/// <summary>
	/// An extension node definition.
	/// </summary>
	public class ExtensionNodeDescription: ObjectDescription, NodeElement
	{
		ExtensionNodeDescriptionCollection childNodes;
		string[] attributes;
		string nodeName;
		
		/// <summary>
		/// Initializes a new instance of the <see cref="Mono.Addins.Description.ExtensionNodeDescription"/> class.
		/// </summary>
		/// <param name='nodeName'>
		/// Node name.
		/// </param>
		public ExtensionNodeDescription (string nodeName)
		{
			this.nodeName = nodeName;
		}
		
		internal ExtensionNodeDescription (XmlElement elem)
		{
			Element = elem;
			nodeName = elem.LocalName;
		}
		
		internal ExtensionNodeDescription ()
		{
		}
		
		/// <summary>
		/// Gets the type of the node.
		/// </summary>
		/// <returns>
		/// The node type.
		/// </returns>
		/// <remarks>
		/// This method only works when the add-in description to which the node belongs has been
		/// loaded from an add-in registry.
		/// </remarks>
		public ExtensionNodeType GetNodeType ()
		{
			if (Parent is Extension) {
				Extension ext = (Extension) Parent;
				object ob = ext.GetExtendedObject ();
				if (ob is ExtensionPoint) {
					ExtensionPoint ep = (ExtensionPoint) ob;
					return ep.NodeSet.GetAllowedNodeTypes () [NodeName];
				} else if (ob is ExtensionNodeDescription) {
					ExtensionNodeDescription pn = (ExtensionNodeDescription) ob;
					ExtensionNodeType pt = ((ExtensionNodeDescription) pn).GetNodeType ();
					if (pt != null)
						return pt.GetAllowedNodeTypes () [NodeName];
				}
			}
			else if (Parent is ExtensionNodeDescription) {
				ExtensionNodeType pt = ((ExtensionNodeDescription) Parent).GetNodeType ();
				if (pt != null)
					return pt.GetAllowedNodeTypes () [NodeName];
			}
			return null;
		}
		
		/// <summary>
		/// Gets the extension path under which this node is registered
		/// </summary>
		/// <returns>
		/// The parent path.
		/// </returns>
		/// <remarks>
		/// For example, if the id of the node is 'ThisNode', and the node is a child of another node with id 'ParentNode', and
		/// that parent node is defined in an extension with the path '/Core/MainExtension', then the parent path is 'Core/MainExtension/ParentNode'.
		/// </remarks>
		public string GetParentPath ()
		{
			if (Parent is Extension)
				return ((Extension)Parent).Path;
			else if (Parent is ExtensionNodeDescription) {
				ExtensionNodeDescription pn = (ExtensionNodeDescription) Parent;
				return pn.GetParentPath () + "/" + pn.Id;
			}
			else
				return string.Empty;
		}
		
		internal override void Verify (string location, StringCollection errors)
		{
			if (nodeName == null || nodeName.Length == 0)
				errors.Add (location + "Node: NodeName can't be empty.");
			ChildNodes.Verify (location + NodeName + "/", errors);
		}
		
		/// <summary>
		/// Gets or sets the name of the node.
		/// </summary>
		/// <value>
		/// The name of the node.
		/// </value>
		public string NodeName {
			get { return nodeName; }
			internal set {
				if (Element != null)
					throw new InvalidOperationException ("Can't change node name of xml element");
				nodeName = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the identifier of the node.
		/// </summary>
		/// <value>
		/// The identifier.
		/// </value>
		public string Id {
			get { return GetAttribute ("id"); }
			set { SetAttribute ("id", value); }
		}
		
		/// <summary>
		/// Gets or sets the identifier of the node after which this node has to be inserted
		/// </summary>
		/// <value>
		/// The identifier of the reference node
		/// </value>
		public string InsertAfter {
			get { return GetAttribute ("insertafter"); }
			set {
				if (value == null || value.Length == 0)
					RemoveAttribute ("insertafter");
				else
					SetAttribute ("insertafter", value); 
			}
		}
		
		/// <summary>
		/// Gets or sets the identifier of the node before which this node has to be inserted
		/// </summary>
		/// <value>
		/// The identifier of the reference node
		/// </value>
		public string InsertBefore {
			get { return GetAttribute ("insertbefore"); }
			set {
				if (value == null || value.Length == 0)
					RemoveAttribute ("insertbefore");
				else
					SetAttribute ("insertbefore", value); 
			}
		}
		
		/// <summary>
		/// Gets a value indicating whether this node is a condition.
		/// </summary>
		/// <value>
		/// <c>true</c> if this node is a condition; otherwise, <c>false</c>.
		/// </value>
		public bool IsCondition {
			get { return nodeName == "Condition" || nodeName == "ComplexCondition"; }
		}
		
		internal override void SaveXml (XmlElement parent)
		{
			CreateElement (parent, nodeName);

			if (attributes != null) {
				for (int n = 0; n < attributes.Length; n += 2)
					Element.SetAttribute (attributes [n], attributes [n + 1]);
			}

			ChildNodes.SaveXml (Element);
		}
		
		/// <summary>
		/// Gets the value of an attribute.
		/// </summary>
		/// <returns>
		/// The value of the attribute, or an empty string if the attribute is not defined.
		/// </returns>
		/// <param name='key'>
		/// Name of the attribute.
		/// </param>
		public string GetAttribute (string key)
		{
			if (Element != null)
				return Element.GetAttribute (key);

			if (attributes == null)
				return string.Empty;
			for (int n=0; n<attributes.Length; n+=2) {
				if (attributes [n] == key)
					return attributes [n+1];
			}
			return string.Empty;
		}
		
		/// <summary>
		/// Sets the value of an attribute.
		/// </summary>
		/// <param name='key'>
		/// Name of the attribute
		/// </param>
		/// <param name='value'>
		/// The value.
		/// </param>
		public void SetAttribute (string key, string value)
		{
			if (Element != null) {
				Element.SetAttribute (key, value);
				return;
			}
			
			if (value == null)
				value = string.Empty;
			
			if (attributes == null) {
				attributes = new string [2];
				attributes [0] = key;
				attributes [1] = value;
				return;
			}
			
			for (int n=0; n<attributes.Length; n+=2) {
				if (attributes [n] == key) {
					attributes [n+1] = value;
					return;
				}
			}
			string[] newList = new string [attributes.Length + 2];
			attributes.CopyTo (newList, 0);
			attributes = newList;
			attributes [attributes.Length - 2] = key;
			attributes [attributes.Length - 1] = value;
		}
		
		/// <summary>
		/// Removes an attribute.
		/// </summary>
		/// <param name='name'>
		/// Name of the attribute to remove.
		/// </param>
		public void RemoveAttribute (string name)
		{
			if (Element != null) {
				Element.RemoveAttribute (name);
				return;
			}

			if (attributes == null)
				return;
			
			for (int n=0; n<attributes.Length; n+=2) {
				if (attributes [n] == name) {
					string[] newar = new string [attributes.Length - 2];
					Array.Copy (attributes, 0, newar, 0, n);
					Array.Copy (attributes, n+2, newar, n, attributes.Length - n - 2);
					attributes = newar;
					break;
				}
			}
		}
		
		/// <summary>
		/// Gets the attributes of the node.
		/// </summary>
		/// <value>
		/// The attributes.
		/// </value>
		public NodeAttribute[] Attributes {
			get {
				string [] result = SaveXmlAttributes ();
				if (result == null || result.Length == 0)
					return new NodeAttribute [0];
				NodeAttribute[] ats = new NodeAttribute [result.Length / 2];
				for (int n=0; n<ats.Length; n++) {
					NodeAttribute at = new NodeAttribute ();
					at.name = result [n*2];
					at.value = result [n*2 + 1];
					ats [n] = at;
				}
				return ats;
			}
		}
		
		/// <summary>
		/// Gets the child nodes.
		/// </summary>
		/// <value>
		/// The child nodes.
		/// </value>
		public ExtensionNodeDescriptionCollection ChildNodes {
			get {
				if (childNodes == null) {
					childNodes = new ExtensionNodeDescriptionCollection (this);
					if (Element != null) {
						foreach (XmlNode nod in Element.ChildNodes) {
							if (nod is XmlElement)
								childNodes.Add (new ExtensionNodeDescription ((XmlElement)nod));
						}
					}
				}
				return childNodes;
			}
		}
		
		NodeElementCollection NodeElement.ChildNodes {
			get { return ChildNodes; }
		}
		
		string[] SaveXmlAttributes ()
		{
			if (Element != null) {
				var result = new string [Element.Attributes.Count * 2];
				for (int n = 0; n < result.Length; n += 2) {
					XmlAttribute at = Element.Attributes [n / 2];
					result [n] = at.LocalName;
					result [n + 1] = at.Value;
				}
				return result;
			}

			return attributes;
		}
		
		internal override void Write (BinaryXmlWriter writer)
		{
			writer.WriteValue ("nodeName", nodeName);
			writer.WriteValue ("attributes", SaveXmlAttributes ());
			writer.WriteValue ("ChildNodes", ChildNodes);
		}
		
		internal override void Read (BinaryXmlReader reader)
		{
			nodeName = reader.ReadStringValue ("nodeName");
			attributes = (string[]) reader.ReadValue ("attributes");
			childNodes = (ExtensionNodeDescriptionCollection) reader.ReadValue ("ChildNodes", new ExtensionNodeDescriptionCollection (this));
		}
	}
}