private XmlDocument MergeConfigFiles()

in src/TestFramework/Core/ConfigurationReader.cs [558:666]


        private XmlDocument MergeConfigFiles(string[] configFileNames)
        {
            try
            {
                if (configFileNames == null || configFileNames.Length == 0)
                {
                    throw new ArgumentException("At least one PTF config file should be passed in.");
                }

                Logging.ApplicationLog.TraceLog("Try to load " + configFileNames.Length + " config files.");

                XmlDocument docBase = new XmlDocument();
                docBase.XmlResolver = null;

                // Load site.ptfconfig
                Logging.ApplicationLog.TraceLog("Loading " + DefaultPtfConfig);
                Stream sitePtfconfigStream = GetBundledResource(DefaultPtfConfig);
                docBase.Load(XmlReader.Create(sitePtfconfigStream, new XmlReaderSettings() { XmlResolver = null }));

                Stack<XmlDocument> xmlDocs = new Stack<XmlDocument>();
                Stack<string> xmlDocsName = new Stack<string>();
                Stack<string> configFiles = new Stack<string>();
                for (int n = 0; n < configFileNames.Length; n++)
                {
                    if (configFileNames[n] != null) configFiles.Push(configFileNames[n]);
                }
                while (configFiles.Count > 0)
                {
                    string fileName = configFiles.Pop();

                    // Ignore multiple reference.
                    if (xmlDocsName.Contains(fileName))
                    {
                        Logging.ApplicationLog.TraceLog("Ignore multiple references: " + fileName);
                        continue;
                    }

                    XmlDocument doc = new XmlDocument();
                    doc.XmlResolver = null;
                    Logging.ApplicationLog.TraceLog("Loading config file:" + fileName);
                    doc.Load(XmlReader.Create(fileName, new XmlReaderSettings() { XmlResolver = null }));
                    xmlDocs.Push(doc);
                    xmlDocsName.Push(fileName);
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                    nsmgr.AddNamespace("tc", DefaultNamespace);
                    XmlNode root = doc.DocumentElement;
                    XmlNode incNode = root.SelectSingleNode("tc:Include", nsmgr);
                    if (incNode != null)
                    {
                        foreach (XmlNode nod in incNode.ChildNodes)
                        {
                            FileInfo fi = new FileInfo(fileName);
                            string path = Path.Combine(fi.DirectoryName, nod.Attributes["name"].Value);
                            if (!ValidateConfigFiles(new string[] { path }))
                            {
                                throw new XmlException(
                                    String.Format("Validating configuration {0} failed: {1}",
                                        invalidFilename,
                                        validateErrorMessages));
                            }
                            configFiles.Push(path);
                        }
                    }
                }
                while (xmlDocs.Count > 0)
                {
                    XmlDocument doc = xmlDocs.Pop();
                    string configFileName = xmlDocsName.Pop();
                    try
                    {
                        MergeXmlDocument(docBase, doc);
                    }
                    catch (XmlException e)
                    {
                        throw new InvalidOperationException(
                            String.Format(
                                "Merging the configuration file ({0}) failed. " +
                                "Please make sure it is valid. Otherwise, please validate the Xml namespace and schema location in this file. " +
                                "To specify the correct Xml namespace and schema location, " +
                                "the TestSite tag in configuration file(s) should be " + DefaultTestSiteTag,
                                configFileName),
                            e);

                    }
                    catch (InvalidOperationException e)
                    {
                        throw new InvalidOperationException(
                            String.Format(
                                "Merging the configuration file ({0}) failed. " +
                                "Please make sure it is valid. Otherwise, please validate the Xml namespace and schema location in this file. " +
                                "To specify the correct Xml namespace and schema location, " +
                                "the TestSite tag in configuration file(s) should be " + DefaultTestSiteTag,
                                configFileName),
                            e);

                    }
                }

                this.Document = docBase;

                Logging.ApplicationLog.TraceLog("Merged config file content: " + docBase.OuterXml);

                return docBase;
            }
            catch (XmlException e)
            {
                throw new InvalidOperationException("Failed to read test configuration file.", e);
            }
        }