Ejemplo n.º 1
0
        /// <summary>
        /// Registers a resource in the given recource group with the given name.
        /// </summary>
        /// <param name="group">
        /// The name of the resource group. If no resource group exists with this name, a new group will
        /// be created automatically.
        /// </param>
        /// <param name="name">The name of the resource. This name must be unique across all the resources.</param>
        /// <param name="loader">Reference to the object that will load the resource.</param>
        public static void RegisterResource(string group, string name, UIResourceLoader loader)
        {
            if (string.IsNullOrEmpty(group))
            {
                throw new ArgumentNullException("group");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (loader == null)
            {
                throw new ArgumentNullException("loader");
            }

            if (resources.ContainsKey(name))
            {
                throw new UIException(string.Format("Resource with name '{0}' already registered at the UIResourceManager!", name));
            }
            if (registeredLoaders.Contains(loader))
            {
                throw new UIException("The given UIResourceLoader is already registered at the UIResourceManager!");
            }

            if (!resourceGroups.ContainsKey(group))
            {
                /// A new resource group has to be created.
                resourceGroups.Add(group, new RCSet <string>());
            }

            /// Register the resource loader.
            resourceGroups[group].Add(name);
            registeredLoaders.Add(loader);
            resources.Add(name, loader);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads a resource definition from the given XML-element.
        /// </summary>
        /// <param name="resGroupName">The name of the group of the resource.</param>
        /// <param name="resourceElem">The XML-element to load from.</param>
        private void LoadResourceDef(string resGroupName, XElement resourceElem)
        {
            XAttribute namespaceAttr = resourceElem.Attribute(RESOURCE_NAMESPACE_ATTR);
            XAttribute nameAttr      = resourceElem.Attribute(RESOURCE_NAME_ATTR);
            XAttribute loaderAttr    = resourceElem.Attribute(RESOURCE_LOADER_ATTR);

            if (namespaceAttr != null && nameAttr != null && loaderAttr != null)
            {
                if (!this.loaderAssemblies.ContainsKey(loaderAttr.Value))
                {
                    throw new ConfigurationException(string.Format("Loader with name '{0}' doesn't exist"));
                }

                string resourceName = string.Format("{0}.{1}", namespaceAttr.Value, nameAttr.Value);

                /// Load the defined paths from the XML
                Dictionary <string, FileInfo> paths = new Dictionary <string, FileInfo>();
                foreach (XElement pathElem in resourceElem.Elements(PATH_ELEM))
                {
                    this.LoadPathElement(pathElem, ref paths);
                }

                /// Load the defined parameters from the XML
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                foreach (XElement paramElem in resourceElem.Elements(PARAMETER_ELEM))
                {
                    this.LoadParameterElement(paramElem, ref parameters);
                }

                /// Load the assembly that contains the resource loader class and create an instance of that class.
                Assembly asm = Assembly.Load(this.loaderAssemblies[loaderAttr.Value]);
                if (asm != null)
                {
                    Type resLoaderType = asm.GetType(this.loaderClasses[loaderAttr.Value]);
                    if (resLoaderType != null)
                    {
                        UIResourceLoader resLoader = Activator.CreateInstance(resLoaderType, new object[2] {
                            paths, parameters
                        }) as UIResourceLoader;
                        if (resLoader != null)
                        {
                            /// Register the created trace object to the trace manager.
                            UIResourceManager.RegisterResource(resGroupName, resourceName, resLoader);
                        }
                        else
                        {
                            throw new ConfigurationException(string.Format("Type {0} doesn't implement abstract class RC.UI.UIResourceLoader!", this.loaderClasses[loaderAttr.Value]));
                        }
                    }
                    else
                    {
                        throw new ConfigurationException(string.Format("Unable to load type {0} from assembly {1}!", this.loaderClasses[loaderAttr.Value], this.loaderAssemblies[loaderAttr.Value]));
                    }
                }
                else
                {
                    throw new ConfigurationException(string.Format("Unable to load assembly {0}!", this.loaderAssemblies[loaderAttr.Value]));
                }
            }
            else
            {
                /// Error: no namespace, name or loader defined
                throw new ConfigurationException("No namespace, name or loader defined for resource!");
            }
        }