public void Skip_5()
        {
            var skip = new UriRelative().Skip(1);

            Assert.True
            (
                skip == null,
                "Fehler in der Funktion Skip der UriRelative"
            );
        }
        /// <summary>
        /// Sucht die zur URL gehörende Ressource
        /// </summary>
        /// <param name="uri">Die Uri</param>
        /// <returns>Die Ressource oder null</returns>
        public static SearchResult Find(string url)
        {
            foreach (var module in Dictionary)
            {
                var root        = module.Value;
                var contextPath = module.Key.ContextPath;
                var requestUri  = new UriRelative(url);

                if (requestUri.StartsWith(contextPath))
                {
                    var uri    = new UriRelative(requestUri.ToString().Substring(contextPath.ToString().Length));
                    var result = root.Find(uri);

                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            // 404
            return(null);
        }
        /// <summary>
        /// Fügt Modul-Einträge aus dem angegebenen Plugin hinzu
        /// </summary>
        /// <param name="pluginContext">Der Kontext des zugehörigen Plugins</param>
        public static void Register(IPluginContext pluginContext)
        {
            var assembly = pluginContext.Assembly;

            foreach (var type in assembly.GetExportedTypes().Where(x => x.IsClass && x.IsSealed && x.GetInterface(typeof(IModule).Name) != null))
            {
                var id            = type.Name?.ToLower();
                var name          = type.Name;
                var icon          = string.Empty;
                var description   = string.Empty;
                var contextPath   = string.Empty;
                var assetPath     = string.Empty;
                var applicationID = string.Empty;

                foreach (var customAttribute in type.CustomAttributes.Where(x => x.AttributeType.GetInterfaces().Contains(typeof(IModuleAttribute))))
                {
                    if (customAttribute.AttributeType == typeof(IDAttribute))
                    {
                        id = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                    }

                    if (customAttribute.AttributeType == typeof(NameAttribute))
                    {
                        name = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(IconAttribute))
                    {
                        icon = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(DescriptionAttribute))
                    {
                        description = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(ContextPathAttribute))
                    {
                        contextPath = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(AssetPathAttribute))
                    {
                        assetPath = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(ApplicationAttribute))
                    {
                        applicationID = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                    }
                }

                // Zugehörige Anwendung ermitteln.
                var application = ApplicationManager.GetApplcation(applicationID);
                if (string.IsNullOrWhiteSpace(applicationID))
                {
                    // Es wurde keine Anwendung angebgeben
                    Context.Log.Warning(message: I18N("webexpress:modulemanager.applicationless"), args: id);
                }
                else if (application == null)
                {
                    // Anwendung wurde nicht gefunden
                    Context.Log.Warning(message: I18N("webexpress:modulemanager.applicationnotfound"), args: new object[] { id, applicationID });
                }
                else if (application != null && application.ApplicationID.Equals(applicationID, StringComparison.OrdinalIgnoreCase))
                {
                    var cp = UriRelative.Combine(Context.ContextPath, application.ContextPath);

                    // Kontext erstellen
                    var context = new ModuleContext()
                    {
                        Assembly      = assembly,
                        PluginID      = pluginContext.PluginID,
                        ApplicationID = application.ApplicationID,
                        ModuleID      = id,
                        ModuleName    = name,
                        Description   = description,
                        Icon          = UriRelative.Combine(cp, contextPath, icon),
                        AssetPath     = Path.Combine(Context.AssetPath, assetPath),
                        ContextPath   = UriRelative.Combine(cp, contextPath),
                        Log           = Context.Log
                    };

                    // Modul erstellen
                    var module = (IModule)type.Assembly.CreateInstance(type.FullName);

                    if (!Dictionary.ContainsKey(id))
                    {
                        Dictionary.Add(id, new ModuleItem()
                        {
                            Context = context,
                            Module  = module
                        });

                        Context.Log.Info(message: I18N("webexpress:modulemanager.register"), args: new object[] { id, application.ApplicationID });
                    }
                    else
                    {
                        Context.Log.Warning(message: I18N("webexpress:modulemanager.duplicate"), args: new object[] { id, application.ApplicationID });
                    }
                }
            }
        }
        /// <summary>
        /// Registriert die Ressourcen eines Moduls
        /// </summary>
        /// <param name="moduleContext">Der Kontext des Moduls, welches die Ressourcen enthält</param>
        public static void Register(IModuleContext moduleContext)
        {
            var assembly    = moduleContext?.Assembly;
            var contextPath = moduleContext.ContextPath;
            var root        = new SitemapNode()
            {
                Dummy = true
            };

            foreach (var resource in assembly.GetTypes().Where(x => x.IsClass == true && x.IsSealed && x.GetInterface(typeof(IResource).Name) != null))
            {
                var id              = resource.Name?.ToLower();
                var segment         = null as ISegmentAttribute;
                var title           = resource.Name;
                var paths           = new List <string>();
                var includeSubPaths = false;
                var moduleID        = string.Empty;
                var resourceContext = new List <string>();

                foreach (var customAttribute in resource.CustomAttributes.Where(x => x.AttributeType.GetInterfaces().Contains(typeof(IResourceAttribute))))
                {
                    if (customAttribute.AttributeType == typeof(IDAttribute))
                    {
                        id = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType.GetInterfaces().Contains(typeof(ISegmentAttribute)))
                    {
                        //segment = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                        segment = resource.GetCustomAttributes(customAttribute.AttributeType, false).FirstOrDefault() as ISegmentAttribute;
                    }

                    if (customAttribute.AttributeType == typeof(TitleAttribute))
                    {
                        title = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(PathAttribute))
                    {
                        paths.Add(customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString());
                    }

                    if (customAttribute.AttributeType == typeof(IncludeSubPathsAttribute))
                    {
                        includeSubPaths = Convert.ToBoolean(customAttribute.ConstructorArguments.FirstOrDefault().Value);
                    }

                    if (customAttribute.AttributeType == typeof(ModuleAttribute))
                    {
                        moduleID = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                    }

                    if (customAttribute.AttributeType == typeof(ContextAttribute))
                    {
                        resourceContext.Add(customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower());
                    }
                }

                // Zugehöriges Modul ermitteln.
                var module = ModuleManager.GetModule(moduleID);
                if (string.IsNullOrEmpty(moduleID))
                {
                    // Es wurde kein Modul angebgeben
                    Context.Log.Warning(message: I18N("webexpress:resourcemanager.moduleless"), args: id);
                }
                else if (module == null)
                {
                    // Modul wurde nicht gefunden
                    Context.Log.Warning(message: I18N("webexpress:resourcemanager.modulenotfound"), args: new object[] { id, moduleID });
                }
                else if (paths.Count == 0 || (paths.Count == 1 && string.IsNullOrWhiteSpace(paths.FirstOrDefault())))
                {
                    // Root setzen
                    if (root.Dummy)
                    {
                        root.ID              = id;
                        root.Title           = title;
                        root.Type            = resource;
                        root.ModuleContext   = moduleContext;
                        root.ResourceContext = resourceContext;
                        root.IncludeSubPaths = includeSubPaths;
                        root.PathSegment     = segment.ToPathSegment();
                        root.Dummy           = false;

                        Context.Log.Info(message: I18N("webexpress:resourcemanager.addresource"), args: new object[] { "ROOT", moduleID });
                    }
                }
                else
                {
                    foreach (var p in paths)
                    {
                        var uri  = new UriRelative(p);
                        var skip = uri.Skip(1);

                        Context.Log.Info(message: I18N("webexpress:resourcemanager.addresource"), args: new object[] { id, moduleID });

                        // Ressourceneintrag erstellen und Eigenschaften setzen
                        var node = root.Insert(skip, id);

                        if (node != null)
                        {
                            node.PathSegment     = segment.ToPathSegment();
                            node.Title           = title;
                            node.Type            = resource;
                            node.ModuleContext   = moduleContext;
                            node.ResourceContext = resourceContext;
                            node.IncludeSubPaths = includeSubPaths;
                        }
                        else
                        {
                            Context.Log.Warning(message: I18N("webexpress:resourcemanager.addresource.error"), args: new object[] { id, moduleID });
                        }
                    }
                }
            }

            if (!Dictionary.ContainsKey(moduleContext))
            {
                Dictionary[moduleContext] = root;

                Context.Log.Info(message: I18N("webexpress:resourcemanager.register"), args: new object[] { root.GetPreOrder().Count, moduleContext?.ModuleID?.ToLower() });
            }

            Context.Log.Debug(message: I18N("webexpress:resourcemanager.sitemap"), args: moduleContext.ModuleID);

            foreach (var item in root.GetPreOrder())
            {
                Context.Log.Debug(message: "   {0} => '{1}'", args: new object[] { item.IDPath, item.ExpressionPath });
            }
        }
Example #5
0
        /// <summary>
        /// Lädt und registriert das Plugin aus einem Assembly
        /// </summary>
        /// <param name="assembly">Das Assembly, indem sich das Plugins befindet</param>
        public static void Register(Assembly assembly)
        {
            try
            {
                foreach (var type in assembly.GetExportedTypes())
                {
                    if (type.IsClass && type.IsSealed && type.GetInterface(typeof(IPlugin).Name) != null && type.Name.Equals("Plugin"))
                    {
                        var id          = type.Name?.ToLower();
                        var name        = type.Assembly.GetCustomAttribute <AssemblyTitleAttribute>()?.Title;
                        var icon        = string.Empty;
                        var description = type.Assembly.GetCustomAttribute <AssemblyDescriptionAttribute>()?.Description;

                        foreach (var customAttribute in type.CustomAttributes.Where(x => x.AttributeType.GetInterfaces().Contains(typeof(IPluginAttribute))))
                        {
                            if (customAttribute.AttributeType == typeof(IDAttribute))
                            {
                                id = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                            }

                            if (customAttribute.AttributeType == typeof(NameAttribute))
                            {
                                name = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                            }

                            if (customAttribute.AttributeType == typeof(IconAttribute))
                            {
                                icon = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                            }

                            if (customAttribute.AttributeType == typeof(DescriptionAttribute))
                            {
                                description = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                            }
                        }

                        var context = new PluginContext()
                        {
                            Assembly     = type.Assembly,
                            PluginID     = id,
                            PluginName   = name,
                            Manufacturer = type.Assembly.GetCustomAttribute <AssemblyCompanyAttribute>()?.Company,
                            Copyright    = type.Assembly.GetCustomAttribute <AssemblyCopyrightAttribute>()?.Copyright,
                            Icon         = UriRelative.Combine(Context.ContextPath, icon),
                            Description  = description,
                            Version      = type.Assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion,
                            Host         = Context,
                            Log          = Context.Log
                        };

                        var plugin = (IPlugin)type.Assembly.CreateInstance(type.FullName);

                        if (!Dictionary.ContainsKey(id))
                        {
                            Dictionary.Add(id, new PluginItem()
                            {
                                Context = context,
                                Plugin  = plugin
                            });

                            Context.Log.Info(message: I18N("webexpress:pluginmanager.created"), args: id);
                        }
                        else
                        {
                            Context.Log.Warning(message: I18N("webexpress:pluginmanager.duplicate"), args: id);
                        }
                    }
                    //else
                    //{
                    //    Context.Log.Warning(message: I18N("webexpress:pluginmanager.notfound"), args: assembly.FullName);
                    //}
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// Fügt Anwendungs-Einträge aus dem angegebenen Plugin hinzu
        /// </summary>
        /// <param name="pluginContext">Der Kontext des zugehörigen Plugins</param>
        public static void Register(IPluginContext pluginContext)
        {
            var assembly = pluginContext.Assembly;

            foreach (var type in assembly.GetExportedTypes().Where(x => x.IsClass && x.IsSealed && x.GetInterface(typeof(IApplication).Name) != null))
            {
                var id          = type.Name?.ToLower();
                var name        = type.Name;
                var icon        = string.Empty;
                var description = string.Empty;
                var contextPath = string.Empty;
                var assetPath   = string.Empty;

                // Attribute ermitteln
                foreach (var customAttribute in type.CustomAttributes.Where(x => x.AttributeType.GetInterfaces().Contains(typeof(IApplicationAttribute))))
                {
                    if (customAttribute.AttributeType == typeof(IDAttribute))
                    {
                        id = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                    }

                    if (customAttribute.AttributeType == typeof(NameAttribute))
                    {
                        name = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(IconAttribute))
                    {
                        icon = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(DescriptionAttribute))
                    {
                        description = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(ContextPathAttribute))
                    {
                        contextPath = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(AssetPathAttribute))
                    {
                        assetPath = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }
                }

                // Anwendungskontext erstellen
                var context = new ApplicationContext()
                {
                    Assembly        = assembly,
                    PluginID        = pluginContext.PluginID,
                    ApplicationID   = id,
                    ApplicationName = name,
                    Description     = description,
                    Icon            = UriRelative.Combine(Context.ContextPath, contextPath, icon),
                    AssetPath       = Path.Combine(Context.AssetPath, assetPath),
                    ContextPath     = UriRelative.Combine(Context.ContextPath, contextPath),
                    Log             = Context.Log
                };

                // Anwendung erstellen
                var application = (IApplication)type.Assembly.CreateInstance(type.FullName);

                if (!Dictionary.ContainsKey(id))
                {
                    Dictionary.Add(id, new ApplicationItem()
                    {
                        Context     = context,
                        Application = application
                    });

                    Context.Log.Info(message: I18N("webexpress:applicationmanager.register"), args: id);
                }
                else
                {
                    Context.Log.Warning(message: I18N("webexpress:applicationmanager.duplicate"), args: id);
                }
            }
        }