~ted/ubuntu/lucid/tomboy/with-patch

« back to all changes in this revision

Viewing changes to Mono.Addins/Mono.Addins/Mono.Addins.Description/Extension.cs

Tags: upstream-0.7.2
Import upstream version 0.7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Extension.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
using System.Collections.Specialized;
 
33
using Mono.Addins.Serialization;
 
34
 
 
35
namespace Mono.Addins.Description
 
36
{
 
37
        public class Extension: ObjectDescription, IComparable
 
38
        {
 
39
                string path;
 
40
                ExtensionNodeDescriptionCollection nodes;
 
41
                
 
42
                public Extension ()
 
43
                {
 
44
                }
 
45
                
 
46
                public Extension (string path)
 
47
                {
 
48
                        this.path = path;
 
49
                }
 
50
                
 
51
                // Returns the object extended by this Extension. It can be an ExtensionPoint or
 
52
                // an ExtensionNodeDescription.
 
53
                public ObjectDescription GetExtendedObject ()
 
54
                {
 
55
                        AddinDescription desc = ParentAddinDescription;
 
56
                        if (desc == null)
 
57
                                return null;
 
58
                        ExtensionPoint ep = FindExtensionPoint (desc, path);
 
59
                        if (ep == null && desc.OwnerDatabase != null) {
 
60
                                foreach (Dependency dep in desc.MainModule.Dependencies) {
 
61
                                        AddinDependency adep = dep as AddinDependency;
 
62
                                        if (adep == null) continue;
 
63
                                        Addin ad = desc.OwnerDatabase.GetInstalledAddin (adep.FullAddinId);
 
64
                                        if (ad != null && ad.Description != null) {
 
65
                                                ep = FindExtensionPoint (ad.Description, path);
 
66
                                                if (ep != null)
 
67
                                                        break;
 
68
                                        }
 
69
                                }
 
70
                        }
 
71
                        if (ep != null) {
 
72
                                string subp = path.Substring (ep.Path.Length).Trim ('/');
 
73
                                if (subp.Length == 0)
 
74
                                        return ep; // The extension is directly extending the extension point
 
75
                                
 
76
                                // The extension is extending a node of the extension point
 
77
 
 
78
                                return desc.FindExtensionNode (path, true);
 
79
                        }
 
80
                        return null;
 
81
                }
 
82
                
 
83
                public ExtensionNodeTypeCollection GetAllowedNodeTypes ()
 
84
                {
 
85
                        ObjectDescription ob = GetExtendedObject ();
 
86
                        ExtensionPoint ep = ob as ExtensionPoint;
 
87
                        if (ep != null)
 
88
                                return ep.NodeSet.GetAllowedNodeTypes ();
 
89
                        
 
90
                        ExtensionNodeDescription node = ob as ExtensionNodeDescription;
 
91
                        if (node != null) {
 
92
                                ExtensionNodeType nt = node.GetNodeType ();
 
93
                                if (nt != null)
 
94
                                        return nt.GetAllowedNodeTypes ();
 
95
                        }
 
96
                        return new ExtensionNodeTypeCollection ();
 
97
                }
 
98
                
 
99
                ExtensionPoint FindExtensionPoint (AddinDescription desc, string path)
 
100
                {
 
101
                        foreach (ExtensionPoint ep in desc.ExtensionPoints) {
 
102
                                if (ep.Path == path || path.StartsWith (ep.Path + "/"))
 
103
                                        return ep;
 
104
                        }
 
105
                        return null;
 
106
                }
 
107
                
 
108
                internal override void Verify (string location, StringCollection errors)
 
109
                {
 
110
                        VerifyNotEmpty (location + "Extension", errors, path, "path");
 
111
                        ExtensionNodes.Verify (location + "Extension (" + path + ")/", errors);
 
112
                        
 
113
                        foreach (ExtensionNodeDescription cnode in ExtensionNodes)
 
114
                                VerifyNode (location, cnode, errors);
 
115
                }
 
116
                
 
117
                void VerifyNode (string location, ExtensionNodeDescription node, StringCollection errors)
 
118
                {
 
119
                        string id = node.GetAttribute ("id");
 
120
                        if (id.Length > 0)
 
121
                                id = "(" + id + ")";
 
122
                        if (node.NodeName == "Condition" && node.GetAttribute ("id").Length == 0) {
 
123
                                errors.Add (location + node.NodeName + id + ": Missing 'id' attribute in Condition element.");
 
124
                        }
 
125
                        if (node.NodeName == "ComplexCondition") {
 
126
                                if (node.ChildNodes.Count > 0) {
 
127
                                        VerifyConditionNode (location, node.ChildNodes[0], errors);
 
128
                                        for (int n=1; n<node.ChildNodes.Count; n++)
 
129
                                                VerifyNode (location + node.NodeName + id + "/", node.ChildNodes[n], errors);
 
130
                                }
 
131
                                else
 
132
                                        errors.Add (location + "ComplexCondition: Missing child condition in ComplexCondition element.");
 
133
                        }
 
134
                        foreach (ExtensionNodeDescription cnode in node.ChildNodes)
 
135
                                VerifyNode (location + node.NodeName + id + "/", cnode, errors);
 
136
                }
 
137
                
 
138
                void VerifyConditionNode (string location, ExtensionNodeDescription node, StringCollection errors)
 
139
                {
 
140
                        string nodeName = node.NodeName;
 
141
                        if (nodeName != "Or" && nodeName != "And" && nodeName != "Condition") {
 
142
                                errors.Add (location + "ComplexCondition: Invalid condition element: " + nodeName);
 
143
                                return;
 
144
                        }
 
145
                        foreach (ExtensionNodeDescription cnode in node.ChildNodes)
 
146
                                VerifyConditionNode (location, cnode, errors);
 
147
                }
 
148
                
 
149
                public Extension (XmlElement element)
 
150
                {
 
151
                        Element = element;
 
152
                        path = element.GetAttribute ("path");
 
153
                }
 
154
                
 
155
                public string Path {
 
156
                        get { return path; }
 
157
                        set { path = value; }
 
158
                }
 
159
                
 
160
                internal override void SaveXml (XmlElement parent)
 
161
                {
 
162
                        if (Element == null) {
 
163
                                Element = parent.OwnerDocument.CreateElement ("Extension");
 
164
                                parent.AppendChild (Element);
 
165
                        }
 
166
                        Element.SetAttribute ("path", path);
 
167
                        if (nodes != null)
 
168
                                nodes.SaveXml (Element);
 
169
                }
 
170
                
 
171
                public ExtensionNodeDescriptionCollection ExtensionNodes {
 
172
                        get {
 
173
                                if (nodes == null) {
 
174
                                        nodes = new ExtensionNodeDescriptionCollection (this);
 
175
                                        if (Element != null) {
 
176
                                                foreach (XmlNode node in Element.ChildNodes) {
 
177
                                                        XmlElement e = node as XmlElement;
 
178
                                                        if (e != null)
 
179
                                                                nodes.Add (new ExtensionNodeDescription (e));
 
180
                                                }
 
181
                                        }
 
182
                                }
 
183
                                return nodes;
 
184
                        }
 
185
                }
 
186
                
 
187
                int IComparable.CompareTo (object obj)
 
188
                {
 
189
                        Extension other = (Extension) obj;
 
190
                        return Path.CompareTo (other.Path);
 
191
                }
 
192
                
 
193
                internal override void Write (BinaryXmlWriter writer)
 
194
                {
 
195
                        writer.WriteValue ("path", path);
 
196
                        writer.WriteValue ("Nodes", ExtensionNodes);
 
197
                }
 
198
                
 
199
                internal override void Read (BinaryXmlReader reader)
 
200
                {
 
201
                        path = reader.ReadStringValue ("path");
 
202
                        nodes = (ExtensionNodeDescriptionCollection) reader.ReadValue ("Nodes", new ExtensionNodeDescriptionCollection (this));
 
203
                }
 
204
        }
 
205
}