AddExtensionNode() public method

Adds an extension node to the module.
This method creates a new Extension object for the provided path if none exist.
public AddExtensionNode ( string path, string nodeName ) : Mono.Addins.Description.ExtensionNodeDescription
path string /// Path that identifies the extension point. ///
nodeName string /// Node name. ///
return Mono.Addins.Description.ExtensionNodeDescription
Ejemplo n.º 1
0
        void ScanAssemblyContents(IAssemblyReflector reflector, AddinDescription config, ModuleDescription module, object asm, AddinScanResult scanResult)
        {
            bool isMainModule = module == config.MainModule;

            // Get dependencies

            object[] deps = reflector.GetCustomAttributes (asm, typeof(AddinDependencyAttribute), false);
            foreach (AddinDependencyAttribute dep in deps) {
                AddinDependency adep = new AddinDependency ();
                adep.AddinId = dep.Id;
                adep.Version = dep.Version;
                module.Dependencies.Add (adep);
            }

            if (isMainModule) {

                // Get properties

                object[] props = reflector.GetCustomAttributes (asm, typeof(AddinPropertyAttribute), false);
                foreach (AddinPropertyAttribute prop in props)
                    config.Properties.SetPropertyValue (prop.Name, prop.Value, prop.Locale);

                // Get extension points

                object[] extPoints = reflector.GetCustomAttributes (asm, typeof(ExtensionPointAttribute), false);
                foreach (ExtensionPointAttribute ext in extPoints) {
                    ExtensionPoint ep = config.AddExtensionPoint (ext.Path);
                    ep.Description = ext.Description;
                    ep.Name = ext.Name;
                    ExtensionNodeType nt = ep.AddExtensionNode (ext.NodeName, ext.NodeTypeName);
                    nt.ExtensionAttributeTypeName = ext.ExtensionAttributeTypeName;
                }
            }

            // Look for extension nodes declared using assembly attributes

            foreach (CustomAttribute att in reflector.GetRawCustomAttributes (asm, typeof(CustomExtensionAttribute), true))
                AddCustomAttributeExtension (module, att, "Type");

            // Get extensions or extension points applied to types

            foreach (object t in reflector.GetAssemblyTypes (asm)) {

                string typeFullName = reflector.GetTypeFullName (t);

                // Look for extensions

                object[] extensionAtts = reflector.GetCustomAttributes (t, typeof(ExtensionAttribute), false);
                if (extensionAtts.Length > 0) {
                    Dictionary<string,ExtensionNodeDescription> nodes = new Dictionary<string, ExtensionNodeDescription> ();
                    ExtensionNodeDescription uniqueNode = null;
                    foreach (ExtensionAttribute eatt in extensionAtts) {
                        string path;
                        string nodeName = eatt.NodeName;

                        if (eatt.TypeName.Length > 0) {
                            path = "$" + eatt.TypeName;
                        }
                        else if (eatt.Path.Length == 0) {
                            path = GetBaseTypeNameList (reflector, t);
                            if (path == "$") {
                                // The type does not implement any interface and has no superclass.
                                // Will be reported later as an error.
                                path = "$" + typeFullName;
                            }
                        } else {
                            path = eatt.Path;
                        }

                        ExtensionNodeDescription elem = module.AddExtensionNode (path, nodeName);
                        nodes [path] = elem;
                        uniqueNode = elem;

                        if (eatt.Id.Length > 0) {
                            elem.SetAttribute ("id", eatt.Id);
                            elem.SetAttribute ("type", typeFullName);
                        } else {
                            elem.SetAttribute ("id", typeFullName);
                        }
                        if (eatt.InsertAfter.Length > 0)
                            elem.SetAttribute ("insertafter", eatt.InsertAfter);
                        if (eatt.InsertBefore.Length > 0)
                            elem.SetAttribute ("insertbefore", eatt.InsertBefore);
                    }

                    // Get the node attributes

                    foreach (ExtensionAttributeAttribute eat in reflector.GetCustomAttributes (t, typeof(ExtensionAttributeAttribute), false)) {
                        ExtensionNodeDescription node;
                        if (!string.IsNullOrEmpty (eat.Path))
                            nodes.TryGetValue (eat.Path, out node);
                        else if (eat.TypeName.Length > 0)
                            nodes.TryGetValue ("$" + eat.TypeName, out node);
                        else {
                            if (nodes.Count > 1)
                                throw new Exception ("Missing type or extension path value in ExtensionAttribute for type '" + typeFullName + "'.");
                            node = uniqueNode;
                        }
                        if (node == null)
                            throw new Exception ("Invalid type or path value in ExtensionAttribute for type '" + typeFullName + "'.");

                        node.SetAttribute (eat.Name ?? string.Empty, eat.Value ?? string.Empty);
                    }
                }
                else {
                    // Look for extension points

                    extensionAtts = reflector.GetCustomAttributes (t, typeof(TypeExtensionPointAttribute), false);
                    if (extensionAtts.Length > 0 && isMainModule) {
                        foreach (TypeExtensionPointAttribute epa in extensionAtts) {
                            ExtensionPoint ep;

                            ExtensionNodeType nt = new ExtensionNodeType ();

                            if (epa.Path.Length > 0) {
                                ep = config.AddExtensionPoint (epa.Path);
                            }
                            else {
                                ep = config.AddExtensionPoint (GetDefaultTypeExtensionPath (config, typeFullName));
                                nt.ObjectTypeName = typeFullName;
                            }
                            nt.Id = epa.NodeName;
                            nt.TypeName = epa.NodeTypeName;
                            nt.ExtensionAttributeTypeName = epa.ExtensionAttributeTypeName;
                            ep.NodeSet.NodeTypes.Add (nt);
                            ep.Description = epa.Description;
                            ep.Name = epa.Name;
                            ep.RootAddin = config.AddinId;
                            ep.SetExtensionsAddinId (config.AddinId);
                        }
                    }
                    else {
                        // Look for custom extension attribtues
                        foreach (CustomAttribute att in reflector.GetRawCustomAttributes (t, typeof(CustomExtensionAttribute), false)) {
                            ExtensionNodeDescription elem = AddCustomAttributeExtension (module, att, "Type");
                            elem.SetAttribute ("type", typeFullName);
                            if (string.IsNullOrEmpty (elem.GetAttribute ("id")))
                                elem.SetAttribute ("id", typeFullName);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
 ExtensionNodeDescription AddCustomAttributeExtension(ModuleDescription module, CustomAttribute att, string nameName)
 {
     string path;
     if (!att.TryGetValue (CustomExtensionAttribute.PathFieldKey, out path))
         path = "%" + att.TypeName;
     ExtensionNodeDescription elem = module.AddExtensionNode (path, nameName);
     foreach (KeyValuePair<string,string> prop in att) {
         if (prop.Key != CustomExtensionAttribute.PathFieldKey)
             elem.SetAttribute (prop.Key, prop.Value);
     }
     return elem;
 }