Novacode.DocX.merge_styles C# (CSharp) Method

merge_styles() private method

private merge_styles ( PackagePart remote_pp, PackagePart local_pp, System.Xml.Linq.XDocument remote_mainDoc, DocX remote, System.Xml.Linq.XDocument remote_footnotes, System.Xml.Linq.XDocument remote_endnotes ) : void
remote_pp System.IO.Packaging.PackagePart
local_pp System.IO.Packaging.PackagePart
remote_mainDoc System.Xml.Linq.XDocument
remote DocX
remote_footnotes System.Xml.Linq.XDocument
remote_endnotes System.Xml.Linq.XDocument
return void
        private void merge_styles(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote, XDocument remote_footnotes, XDocument remote_endnotes)
        {
            var local_styles = new Dictionary<string, string>();
            foreach (XElement local_style in styles.Root.Elements(XName.Get("style", w.NamespaceName)))
            {
                XElement temp = new XElement(local_style);
                XAttribute styleId = temp.Attribute(XName.Get("styleId", w.NamespaceName));
                String value = styleId.Value;
                styleId.Remove();
                String key = Regex.Replace(temp.ToString(), @"\s+", "");
                if (!local_styles.ContainsKey(key)) local_styles.Add(key, value);
            }

            // Add each remote style to this document.
            IEnumerable<XElement> remote_styles = remote.styles.Root.Elements(XName.Get("style", w.NamespaceName));
            foreach (XElement remote_style in remote_styles)
            {
                XElement temp = new XElement(remote_style);
                XAttribute styleId = temp.Attribute(XName.Get("styleId", w.NamespaceName));
                String value = styleId.Value;
                styleId.Remove();
                String key = Regex.Replace(temp.ToString(), @"\s+", "");
                String guuid;

                // Check to see if the local document already contains the remote style.
                if (local_styles.ContainsKey(key))
                {
                    String local_value;
                    local_styles.TryGetValue(key, out local_value);

                    // If the styleIds are the same then nothing needs to be done.
                    if (local_value == value)
                        continue;

                    // All we need to do is update the styleId.
                    guuid = local_value;
                }
                else
                {
                    guuid = Guid.NewGuid().ToString();
                    // Set the styleId in the remote_style to this new Guid
                    // [Fixed the issue that my document referred to a new Guid while my styles still had the old value ("Titel")]
                    remote_style.SetAttributeValue(XName.Get("styleId", w.NamespaceName), guuid);
                }

                foreach (XElement e in remote_mainDoc.Root.Descendants(XName.Get("pStyle", w.NamespaceName)))
                {
                    XAttribute e_styleId = e.Attribute(XName.Get("val", w.NamespaceName));
                    if (e_styleId != null && e_styleId.Value.Equals(styleId.Value))
                    {
                        e_styleId.SetValue(guuid);
                    }
                }

                foreach (XElement e in remote_mainDoc.Root.Descendants(XName.Get("rStyle", w.NamespaceName)))
                {
                    XAttribute e_styleId = e.Attribute(XName.Get("val", w.NamespaceName));
                    if (e_styleId != null && e_styleId.Value.Equals(styleId.Value))
                    {
                        e_styleId.SetValue(guuid);
                    }
                }

                foreach (XElement e in remote_mainDoc.Root.Descendants(XName.Get("tblStyle", w.NamespaceName)))
                {
                    XAttribute e_styleId = e.Attribute(XName.Get("val", w.NamespaceName));
                    if (e_styleId != null && e_styleId.Value.Equals(styleId.Value))
                    {
                        e_styleId.SetValue(guuid);
                    }
                }

                if (remote_endnotes != null)
                {
                    foreach (XElement e in remote_endnotes.Root.Descendants(XName.Get("rStyle", w.NamespaceName)))
                    {
                        XAttribute e_styleId = e.Attribute(XName.Get("val", w.NamespaceName));
                        if (e_styleId != null && e_styleId.Value.Equals(styleId.Value))
                        {
                            e_styleId.SetValue(guuid);
                        }
                    }

                    foreach (XElement e in remote_endnotes.Root.Descendants(XName.Get("pStyle", w.NamespaceName)))
                    {
                        XAttribute e_styleId = e.Attribute(XName.Get("val", w.NamespaceName));
                        if (e_styleId != null && e_styleId.Value.Equals(styleId.Value))
                        {
                            e_styleId.SetValue(guuid);
                        }
                    }
                }

                if (remote_footnotes != null)
                {
                    foreach (XElement e in remote_footnotes.Root.Descendants(XName.Get("rStyle", w.NamespaceName)))
                    {
                        XAttribute e_styleId = e.Attribute(XName.Get("val", w.NamespaceName));
                        if (e_styleId != null && e_styleId.Value.Equals(styleId.Value))
                        {
                            e_styleId.SetValue(guuid);
                        }
                    }

                    foreach (XElement e in remote_footnotes.Root.Descendants(XName.Get("pStyle", w.NamespaceName)))
                    {
                        XAttribute e_styleId = e.Attribute(XName.Get("val", w.NamespaceName));
                        if (e_styleId != null && e_styleId.Value.Equals(styleId.Value))
                        {
                            e_styleId.SetValue(guuid);
                        }
                    }
                }

                // Make sure they don't clash by using a uuid.
                styleId.SetValue(guuid);
                styles.Root.Add(remote_style);
            }
        }