Пример #1
0
        /// <summary>
        /// Registers the namespace of the given Type with the given prefix
        /// </summary>
        /// <param name="service">The service to register to</param>
        /// <param name="prefix">The prefix that should be used</param>
        /// <param name="ns">The namespace that should get registered</param>
        /// <param name="overwrite">The flag if the existing should be overwritten</param>
        /// <returns>The service itself</returns>
        public static XamlService RegisterNamespace(this XamlService service, string prefix, string ns, bool overwrite = false)
        {
            if (string.IsNullOrEmpty(ns))
            {
                return(service);
            }
            if (string.IsNullOrEmpty(prefix))
            {
                return(service);
            }
            if (service is null)
            {
                return(null);
            }

            var prefixExists   = service.XmlnsDeclarations.ContainsKey(prefix);
            var foundNamespace = service.XmlnsDeclarations.FirstOrDefault(o => o.Value.Declaration == ns).Value;
            var newDeclaration = new XmlnsDeclaration(prefix, ns, false);

            // if both (namespace and prefix) exists -> abort
            if (prefixExists && foundNamespace != null)
            {
                return(service);
            }
            // if only namespace exists and overwrite is false
            if (!overwrite && foundNamespace != null)
            {
                return(service);
            }
            // if only namespace exists
            if (foundNamespace != null)
            {
                // delete the found registered
                service.XmlnsDeclarations.Remove(foundNamespace.Prefix);
                service.XmlnsDeclarations.Add(prefix, newDeclaration);
                return(service);
            }
            // if only prefix exists and overwrite is false
            if (!overwrite && prefixExists)
            {
                return(service);
            }
            // if only prefix exists and overwrite is true
            if (overwrite && prefixExists)
            {
                service.XmlnsDeclarations[prefix] = newDeclaration;
            }
            // if nothing exists
            else
            {
                service.XmlnsDeclarations.Add(prefix, newDeclaration);
            }

            return(service);
        }
Пример #2
0
        /// <summary>
        /// Registers the namespaces to the given url with the given prefix
        /// </summary>
        /// <param name="service">The service to register to</param>
        /// <param name="prefix">The prefix that should be used</param>
        /// <param name="url">The url that should get registered</param>
        /// <param name="overwrite">The flag if the existing should be overwritten</param>
        /// <param name="ns">The namespaces that should get registered to the url</param>
        /// <returns>The service itself</returns>
        public static XamlService RegisterUrl(this XamlService service, string prefix, string url, bool overwrite, params string[] ns)
        {
            if (string.IsNullOrEmpty(prefix))
            {
                return(service);
            }
            if (string.IsNullOrEmpty(url))
            {
                return(service);
            }
            if (ns is null || ns.Length == 0)
            {
                return(service);
            }
            if (ns.Any(string.IsNullOrEmpty))
            {
                return(service);
            }

            var existingUrl    = service.XmlnsDeclarations.FirstOrDefault(o => o.Value.IsUrlDefinition && o.Value.Url == url).Value;
            var prefixExists   = service.XmlnsDeclarations.ContainsKey(prefix);
            var newDeclaration = new XmlnsDeclaration(prefix, url, true);

            newDeclaration.RegisterNamespace(ns);

            // url and prefix exists and overwrite false -> only add namespaces
            if (existingUrl != null && prefixExists && existingUrl.Prefix == prefix && !overwrite)
            {
                existingUrl.RegisterNamespace(ns);
                return(service);
            }
            if (existingUrl != null && prefixExists && existingUrl.Prefix != prefix)
            {
                return(service);
            }

            // only url exists and overwrite is false
            if (existingUrl != null && !overwrite)
            {
                return(service);
            }
            // only url exists and overwrite is true
            if (existingUrl != null)
            {
                service.XmlnsDeclarations.Remove(existingUrl.Prefix);
                service.XmlnsDeclarations.Add(newDeclaration.Prefix, newDeclaration);
                return(service);
            }

            // only prefix exists and overwrite is false
            if (prefixExists && !overwrite)
            {
                return(service);
            }
            // only prefix exists and overwrite is true
            if (prefixExists)
            {
                service.XmlnsDeclarations[prefix] = newDeclaration;
                return(service);
            }

            // nothing exists
            service.XmlnsDeclarations.Add(newDeclaration.Prefix, newDeclaration);


            return(service);
        }