Exemple #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);
        }
Exemple #2
0
        /// <summary>
        /// Generates a unique prefix with the given namespace for the given service
        /// </summary>
        /// <param name="service">The service to register to</param>
        /// <param name="ns">The namespaces that should get registered to the url</param>
        /// <returns>The generated prefix</returns>
        // ReSharper disable once InconsistentNaming
        private static string GeneratePrefix(this XamlService service, string ns)
        {
            // check if namespace is null or empty
            if (string.IsNullOrEmpty(ns))
            {
                return(null);
            }
            // helper lists -> prefixes and declarations
            var declarations = service.XmlnsDeclarations.Values.Select(o => o.Declaration).ToList();
            var prefixes     = service.XmlnsDeclarations.Keys.ToList();

            // if namespace is already used
            if (declarations.Contains(ns))
            {
                return(null);
            }

            var resultPrefix = string.Empty;
            var splitted     = ns.Split('.');
            var exists       = true;
            var iteration    = 0;
            var longest      = splitted.Select(o => o.Length).OrderByDescending(o => o).First();

            // try combinations of the words from the namespaces
            while (exists)
            {
                iteration++;
                resultPrefix = splitted.Aggregate(string.Empty, (current, split) => current + string.Concat(split.Take(iteration)));
                resultPrefix = resultPrefix.ToLower();

                exists = prefixes.Contains(resultPrefix) || iteration == longest;
            }

            // if prefix is not used -> return result
            if (!prefixes.Contains(resultPrefix))
            {
                return(resultPrefix);
            }

            // add x to the end until it is unique :-p
            exists = true;
            while (exists)
            {
                resultPrefix += 'x';
                exists        = prefixes.Contains(resultPrefix);
            }
            return(resultPrefix);
        }
Exemple #3
0
        /// <summary>
        /// Registers the should serialize Function with the given T and TParent
        /// </summary>
        /// <param name="service">The service to register to</param>
        /// <param name="shouldSerialize">The function that defines the shouldSerialize behavior</param>
        /// <typeparam name="T">The object itself</typeparam>
        /// <typeparam name="TParent">The objects parent</typeparam>
        /// <returns>The service itself</returns>
        public static XamlService RegisterShouldSerialize <T, TParent>(this XamlService service, Func <T, TParent, bool> shouldSerialize)
        {
            var objectType = typeof(T);

            if (service.ShouldSerializeSettings.ContainsKey(objectType))
            {
                service.ShouldSerializeSettings[objectType].Register(shouldSerialize);
            }
            else
            {
                var setting = new ShouldSerializeSetting();
                setting.Register(shouldSerialize);
                service.ShouldSerializeSettings.Add(objectType, setting);
            }

            return(service);
        }
Exemple #4
0
 public static XamlService RegisterStringSerializer <T>(this XamlService service, Func <T, string> serialize, RegisterPriority priority)
 {
     return(service);
 }
Exemple #5
0
 public static XamlService RegisterChildrenProperty <T, TParent>(this XamlService service, Func <T, TParent, bool> shouldUseFilter, RegisterPriority priority)
 {
     return(service);
 }
Exemple #6
0
 public static XamlService RegisterChildrenProperty(this XamlService service, Type property, Type parent, RegisterPriority priority)
 {
     return(service);
 }
Exemple #7
0
 public static XamlService RegisterChildrenProperty(this XamlService service, Func <object, object, bool> shouldUseFilter, RegisterPriority priority) => service.RegisterChildrenProperty <object, object>(shouldUseFilter, priority);
Exemple #8
0
 public static XamlService RegisterChildrenProperty <T, TParent>(this XamlService service, RegisterPriority priority) => service.RegisterChildrenProperty(typeof(T), typeof(TParent), priority);
Exemple #9
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="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 ns, bool overwrite = false) =>
 service.RegisterNamespace(service.GeneratePrefix(ns), ns, overwrite);
Exemple #10
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="overwrite">The flag if the existing should be overwritten</param>
 /// <typeparam name="T">The Type of which the namespace should get registered</typeparam>
 /// <returns>The service itself</returns>
 public static XamlService RegisterNamespace <T>(this XamlService service, string prefix, bool overwrite = false) =>
 service.RegisterNamespace(prefix, typeof(T).Namespace, overwrite);
Exemple #11
0
 public static XamlService RegisterThreadExecution(this XamlService service, Action <Action> executeInside, RegisterPriority priority)
 {
     return(service);
 }
Exemple #12
0
 public static XamlService RegisterThreadExecution(this XamlService service, Thread thread, RegisterPriority priority)
 {
     return(service);
 }
Exemple #13
0
 public static XamlService RegisterContentProperty(this XamlService service, Type property, RegisterPriority priority)
 {
     service.RegisterContentProperty <object>(RegisterPriority.Highest);
     return(service);
 }
Exemple #14
0
 public static XamlService RegisterStringSerializer <T, TParent>(this XamlService service, Func <T, TParent, string> serialize, Func <T, TParent, bool> shouldSerialize, RegisterPriority priority)
 {
     return(service);
 }
Exemple #15
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);
        }
Exemple #16
0
 public static XamlService RegisterObjectConverter <T, TParent, TOut>(this XamlService service, Func <T, TParent, TOut> convert, Func <T, TParent, bool> shouldConvert, RegisterPriority priority)
 {
     return(service);
 }
Exemple #17
0
 public static XamlService RegisterContentProperty <T>(this XamlService service, RegisterPriority priority) => service.RegisterContentProperty(typeof(T), priority);