Esempio n. 1
0
        private static void ContractIncludes(XmlNode section, string filePath, ChoNestedDictionary <string, XmlNode> xmlDocumentList)
        {
            if (section == null)
            {
                return;
            }
            section = section is XmlDocument ? ((XmlDocument)section).DocumentElement : section;

            bool           beginIncludeTokenFound = false;
            string         includeFilePath        = String.Empty;
            List <XmlNode> childNodes             = new List <XmlNode>();
            XmlNode        startCommentNode       = null;

            foreach (XmlNode xmlNode in section.SelectNodes("child::node()"))
            {
                if (!beginIncludeTokenFound)
                {
                    if (xmlNode is XmlComment && xmlNode.Value.StartsWith(BeginIncludeToken))
                    {
                        includeFilePath        = xmlNode.Value.Replace(BeginIncludeToken, String.Empty).Trim();
                        beginIncludeTokenFound = true;
                        startCommentNode       = xmlNode;
                        continue;
                    }
                    else if (xmlNode is XmlElement)
                    {
                        ContractIncludes(xmlNode, filePath, xmlDocumentList);
                    }
                }
                else
                {
                    if (xmlNode is XmlComment && xmlNode.Value.StartsWith(EndIncludeToken) && xmlNode.Value.Replace(EndIncludeToken, String.Empty).Trim() == includeFilePath)
                    {
                        beginIncludeTokenFound = false;
                        XmlNode includeXmlDocument = CreateIncludeDocument(childNodes);

                        //Create INCLUDE node
                        XmlElement includeElement = section.OwnerDocument.CreateElement(IncludePrefixToken, "include", CinchooNSURI);

                        XmlAttribute pathAttribute = section.OwnerDocument.CreateAttribute(PathToken);

                        if (Path.IsPathRooted(includeFilePath))
                        {
                            if (filePath.IsNullOrEmpty() || Path.GetDirectoryName(includeFilePath) != Path.GetDirectoryName(filePath))
                            {
                                pathAttribute.Value = includeFilePath;
                            }
                            else
                            {
                                pathAttribute.Value = Path.GetFileName(includeFilePath);
                            }
                        }
                        else
                        {
                            pathAttribute.Value = ChoPath.GetRelativePath(Path.GetDirectoryName(filePath), includeFilePath);
                        }

                        includeElement.Attributes.Append(pathAttribute);
                        section.InsertBefore(includeElement, startCommentNode);

                        //Remove all the nodes
                        section.RemoveChild(startCommentNode);
                        foreach (XmlNode xmlChildNode in childNodes)
                        {
                            section.RemoveChild(xmlChildNode);
                        }
                        section.RemoveChild(xmlNode);

                        ChoNestedDictionary <string, XmlNode> xmlSubDocumentList = new ChoNestedDictionary <string, XmlNode>();
                        xmlSubDocumentList.Add(includeFilePath, includeXmlDocument as XmlNode);

                        ContractIncludes(includeXmlDocument, filePath, xmlSubDocumentList);

                        if (xmlSubDocumentList.Count > 0)
                        {
                            xmlDocumentList.Add(xmlSubDocumentList);
                        }

                        childNodes.Clear();

                        continue;
                    }

                    childNodes.Add(xmlNode);
                }
            }
        }