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

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins/TypeExtensionNode.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
// TypeExtensionNode.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
 
 
30
using System;
 
31
using System.Xml;
 
32
 
 
33
namespace Mono.Addins
 
34
{
 
35
        /// <summary>
 
36
        /// An extension node which specifies a type.
 
37
        /// </summary>
 
38
        /// <remarks>
 
39
        /// This class is a kind of Mono.Addins.ExtensionNode which can be used to register
 
40
        /// types in an extension point. This is a very common case: a host application
 
41
        /// defines an interface, and add-ins create classes that implement that interface.
 
42
        /// The host will define an extension point which will use TypeExtensionNode as node
 
43
        /// type. Add-ins will register the classes they implement in that extension point.
 
44
        /// 
 
45
        /// When the nodes of an extension point are of type TypeExtensionNode it is then
 
46
        /// possible to use query methods such as AddinManager.GetExtensionObjects(string),
 
47
        /// which will get all nodes in the provided extension path and will create an object
 
48
        /// for each node.
 
49
        /// 
 
50
        /// When declaring extension nodes in an add-in manifest, the class names can be
 
51
        /// specified using the 'class' or 'type' attribute. If none of those attributes is
 
52
        /// provided, the class name will be taken from the 'id' attribute.
 
53
        /// 
 
54
        /// TypeExtensionNode is the default extension type used when no type is provided
 
55
        /// in the definition of an extension point.
 
56
        /// </remarks>
 
57
        [ExtensionNode ("Type", Description="Specifies a class that will be used to create an extension object.")]
 
58
        [NodeAttribute ("class", typeof(Type), false, ContentType = ContentType.Class, Description="Name of the class. If a value is not provided, the class name will be taken from the 'id' attribute")]
 
59
        public class TypeExtensionNode: InstanceExtensionNode
 
60
        {
 
61
                string typeName;
 
62
                Type type;
 
63
                
 
64
                /// <summary>
 
65
                /// Reads the extension node data
 
66
                /// </summary>
 
67
                /// <param name='elem'>
 
68
                /// The element containing the extension data
 
69
                /// </param>
 
70
                /// <remarks>
 
71
                /// This method can be overriden to provide a custom method for reading extension node data from an element.
 
72
                /// The default implementation reads the attributes if the element and assigns the values to the fields
 
73
                /// and properties of the extension node that have the corresponding [NodeAttribute] decoration.
 
74
                /// </remarks>
 
75
                internal protected override void Read (NodeElement elem)
 
76
                {
 
77
                        base.Read (elem);
 
78
                        typeName = elem.GetAttribute ("type");
 
79
                        if (typeName.Length == 0)
 
80
                                typeName = elem.GetAttribute ("class");
 
81
                        if (typeName.Length == 0)
 
82
                                typeName = elem.GetAttribute ("id");
 
83
                }
 
84
                
 
85
                /// <summary>
 
86
                /// Creates a new extension object
 
87
                /// </summary>
 
88
                /// <returns>
 
89
                /// The extension object
 
90
                /// </returns>
 
91
                public override object CreateInstance ()
 
92
                {
 
93
                        return Activator.CreateInstance (Type);
 
94
                }
 
95
 
 
96
                /// <summary>
 
97
                /// Type of the object that this node creates
 
98
                /// </summary>
 
99
                public Type Type {
 
100
                        get {
 
101
                                if (type == null) {
 
102
                                        if (typeName.Length == 0)
 
103
                                                throw new InvalidOperationException ("Type name not specified.");
 
104
                                        type = Addin.GetType (typeName, true);
 
105
                                }
 
106
                                return type;
 
107
                        }
 
108
                }
 
109
        }
 
110
        
 
111
        /// <summary>
 
112
        /// An extension node which specifies a type with custom extension metadata
 
113
        /// </summary>
 
114
        /// <remarks>
 
115
        /// This is the default type for type extension nodes bound to a custom extension attribute.
 
116
        /// </remarks>
 
117
        public class TypeExtensionNode<T>: TypeExtensionNode where T:CustomExtensionAttribute
 
118
        {
 
119
                T data;
 
120
                
 
121
                /// <summary>
 
122
                /// The custom attribute containing the extension metadata
 
123
                /// </summary>
 
124
                [NodeAttribute]
 
125
                public T Data {
 
126
                        get { return data; }
 
127
                        internal set { data = value; }
 
128
                }
 
129
        }
 
130
}