GetSectionUpdatesForGroup() private method

private GetSectionUpdatesForGroup ( string group ) : SectionUpdates
group string
return SectionUpdates
        // Copy configuration sections from the original configuration file.
        private bool CopyConfigDefinitionsRecursive(
                ConfigDefinitionUpdates configDefinitionUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter,
                bool locationPathApplies, LocationUpdates locationUpdates, SectionUpdates sectionUpdates,
                bool addNewSections, string group, int parentLinePosition, int parentIndent) {

            bool wroteASection = false;
            XmlTextReader reader = xmlUtil.Reader;
            int linePosition;
            int indent;
            int startingLinePosition;

            indent = UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition);

            if (reader.NodeType == XmlNodeType.Element) {
                linePosition = xmlUtil.TrueLinePosition;
                startingLinePosition = linePosition;
            }
            else if (reader.NodeType == XmlNodeType.EndElement) {
                linePosition = parentLinePosition + indent;
                if (utilWriter.IsLastLineBlank) {
                    startingLinePosition = xmlUtil.TrueLinePosition;
                }
                else {
                    startingLinePosition = parentLinePosition;
                }
            }
            else {
                linePosition = parentLinePosition + indent;
                startingLinePosition = 0;
            }

            //
            // Write any new sections that apply to this group
            //
            if (sectionUpdates != null && addNewSections) {
                // Remove newness, so we won't write again
                sectionUpdates.IsNew = false;

                Debug.Assert(locationPathApplies, "locationPathApplies");
                string[] movedSectionNames = sectionUpdates.GetMovedSectionNames();
                if (movedSectionNames != null) {
                    if (!utilWriter.IsLastLineBlank) {
                        utilWriter.AppendNewLine();
                    }

                    utilWriter.AppendSpacesToLinePosition(linePosition);
                    bool skipFirstIndent = true;

                    foreach (string configKey in movedSectionNames) {
                        DefinitionUpdate update = sectionUpdates.GetDefinitionUpdate(configKey);

                        WriteSectionUpdate(utilWriter, update, linePosition, indent, skipFirstIndent);
                        skipFirstIndent = false;
                        utilWriter.AppendNewLine();
                        wroteASection = true;
                    }

                    // Restore the whitespace we used for the first element, which is either a start or an end element.
                    utilWriter.AppendSpacesToLinePosition(startingLinePosition);
                }
            }

            if (reader.NodeType == XmlNodeType.Element) {
                //
                // For each element at this depth, either:
                // - Write the element verbatim and recurse due to a location section or group hierarchy element.
                // - Write the element verbatim because it is unchanged, or because the current location does
                //   not apply.
                // - Write the updated XML for the section.
                // - Skip it because the section has been removed.
                //
                int depth = reader.Depth;
                while (reader.Depth == depth) {
                    bool                recurse = false;
                    DefinitionUpdate    update = null;
                    bool                elementLocationPathApplies = locationPathApplies;
                    LocationUpdates     recurseLocationUpdates = locationUpdates;
                    SectionUpdates      recurseSectionUpdates = sectionUpdates;
                    bool                recurseAddNewSections = addNewSections;
                    string              recurseGroup = group;
                    bool                removedSectionOrGroup = false;

                    // update the lineposition and indent for each element
                    indent = UpdateIndent(indent, xmlUtil, utilWriter, parentLinePosition);
                    linePosition = xmlUtil.TrueLinePosition;

                    string elementName = reader.Name;
                    if (elementName == KEYWORD_LOCATION) {
                        string locationSubPathAttribute = reader.GetAttribute(KEYWORD_LOCATION_PATH);
                        locationSubPathAttribute = NormalizeLocationSubPath(locationSubPathAttribute, xmlUtil);
                        elementLocationPathApplies = false;
                        OverrideModeSetting overrideMode = OverrideModeSetting.LocationDefault;
                        bool inheritInChildApps = true;

                        if (IsLocationConfig) {
                            // For location config we will compare config paths instead of location strings
                            // so that we dont end up comparing "1" with "Default Web Site" and ending up with the wrong result
                            if (locationSubPathAttribute == null) {
                                elementLocationPathApplies = false;
                            }
                            else {
                                elementLocationPathApplies = StringUtil.EqualsIgnoreCase(ConfigPath, Host.GetConfigPathFromLocationSubPath(Parent.ConfigPath, locationSubPathAttribute));
                            }
                        }
                        else {
                            Debug.Assert(LocationSubPath == null);

                            // This is the same as doing StringUtil.EqualsIgnoreCase(_locationSubPath, locationSubPathAttribute)
                            // but remember the first one is null. Also remember locationSubPathAttribute is already normalized
                            elementLocationPathApplies = (locationSubPathAttribute == null);
                        }

                        if (elementLocationPathApplies) {
                            // Retrieve overrideMode and InheritInChildApps

                            string allowOverrideAttribute = reader.GetAttribute(KEYWORD_LOCATION_ALLOWOVERRIDE);
                            if (allowOverrideAttribute != null) {
                                overrideMode = OverrideModeSetting.CreateFromXmlReadValue(Boolean.Parse(allowOverrideAttribute));
                            }

                            string overrideModeAttribute = reader.GetAttribute(KEYWORD_LOCATION_OVERRIDEMODE);
                            if (overrideModeAttribute != null) {
                                overrideMode = OverrideModeSetting.CreateFromXmlReadValue(OverrideModeSetting.ParseOverrideModeXmlValue(overrideModeAttribute, null));

                                Debug.Assert(allowOverrideAttribute == null, "allowOverride and overrideMode both detected in a <location> tag");
                            }

                            string inheritInChildAppsAttribute = reader.GetAttribute(KEYWORD_LOCATION_INHERITINCHILDAPPLICATIONS);
                            if (inheritInChildAppsAttribute != null) {
                                inheritInChildApps = Boolean.Parse(inheritInChildAppsAttribute);
                            }

                            // Flag that we already have one of these locations
                            configDefinitionUpdates.FlagLocationWritten();
                        }

                        if (reader.IsEmptyElement) {
                            if (elementLocationPathApplies &&
                                 (configDefinitionUpdates.FindLocationUpdates(overrideMode,
                                                                              inheritInChildApps) != null)) {
                                // If we are going to make updates here, then
                                // delete the one that is here (so we can update later)
                                elementLocationPathApplies = true;
                            }
                            else {
                                // If not lets leave it
                                elementLocationPathApplies = false;
                            }
                        }
                        else {
                            // recurse if this location applies to us
                            if (elementLocationPathApplies) {
                                if (configDefinitionUpdates != null) {
                                    recurseLocationUpdates = configDefinitionUpdates.FindLocationUpdates(overrideMode, inheritInChildApps);
                                    if (recurseLocationUpdates != null) {
                                        recurse = true;
                                        recurseSectionUpdates = recurseLocationUpdates.SectionUpdates;

                                        // If this is <location path=".">, we don't want to add moved sections
                                        // to it.
                                        if (_locationSubPath == null && recurseLocationUpdates.IsDefault) {
                                            recurseAddNewSections = false;
                                        }
                                    }
                                }
                            }
                            else {
                                // recurse if necessary to remove items in _removedSections and _removedGroups
                                if (HasRemovedSectionsOrGroups && !IsLocationConfig && Host.SupportsLocation) {
                                    recurse = true;
                                    recurseLocationUpdates = null;
                                    recurseSectionUpdates = null;
                                    recurseAddNewSections = false;
                                }
                            }
                        }
                    }
                    else {
                        string configKey = CombineConfigKey(group, elementName);
                        FactoryRecord factoryRecord = FindFactoryRecord(configKey, false);
                        if (factoryRecord == null) {
                            // The factory was deleted, so regardless of whether this is a
                            // section or sectionGroup, it can be skipped.
                            if (!elementLocationPathApplies && !IsLocationConfig) {
                                removedSectionOrGroup = true;
                            }
                        }
                        else if (factoryRecord.IsGroup) {
                            if (reader.IsEmptyElement) {
                                if (!elementLocationPathApplies && !IsLocationConfig) {
                                    removedSectionOrGroup = true;
                                }
                            }
                            else {
                                // if the location path applies, recurse if there are updates
                                if (sectionUpdates != null) {
                                    SectionUpdates sectionUpdatesChild = sectionUpdates.GetSectionUpdatesForGroup(elementName);
                                    if (sectionUpdatesChild != null) {
                                        recurse = true;
                                        recurseGroup = configKey;
                                        recurseSectionUpdates = sectionUpdatesChild;
                                    }
                                }
                                else if (!elementLocationPathApplies && !IsLocationConfig) {
                                    if (_removedSectionGroups != null && _removedSectionGroups.Contains(configKey)) {
                                        removedSectionOrGroup = true;
                                    }
                                    else {
                                        recurse = true;
                                        recurseGroup = configKey;
                                        recurseLocationUpdates = null;
                                        recurseSectionUpdates = null;
                                        recurseAddNewSections = false;
                                    }
                                }
                            }
                        }
                        else {
                            // it is a section - get the update
                            if (sectionUpdates != null) {
                                update = sectionUpdates.GetDefinitionUpdate(configKey);
                            }
                            else if (!elementLocationPathApplies && !IsLocationConfig) {
                                if (_removedSections != null && _removedSections.Contains(configKey)) {
                                    removedSectionOrGroup = true;
                                }
                            }
                        }
                    }

                    if (recurse) {
#if DBG
                        string startElementName = reader.Name;
#endif

                        // flush, and get length of underlying stream
                        object checkpoint = utilWriter.CreateStreamCheckpoint();

                        // Copy this element node and up to the first subelement
                        xmlUtil.CopyXmlNode(utilWriter);
                        xmlUtil.CopyReaderToNextElement(utilWriter, true);

                        // Recurse
                        bool recurseWroteASection = CopyConfigDefinitionsRecursive(
                                configDefinitionUpdates, xmlUtil, utilWriter, elementLocationPathApplies, recurseLocationUpdates, recurseSectionUpdates,
                                recurseAddNewSections, recurseGroup, linePosition, indent);

                        // Copy the end element
                        xmlUtil.CopyXmlNode(utilWriter);

                        if (recurseWroteASection) {
                            wroteASection = true;
                        }
                        else {
                            // back out the change
                            utilWriter.RestoreStreamCheckpoint(checkpoint);
                        }

                        // Copy up to the next element, or exit this level.
                        xmlUtil.CopyReaderToNextElement(utilWriter, true);
                    }
                    else {
                        bool skip;
                        if (update == null) {
                            // remove the section from the file if we're in the correct location,
                            // or if the section or group should be removed from all locations
                            skip = elementLocationPathApplies || removedSectionOrGroup;
                        }
                        else {
                            // replace the section if the xml for it has been updated
                            // if it is a configSource, don't write it unless the configSource parameters have changed
                            skip = false;
                            if (update.UpdatedXml != null) {
                                ConfigurationSection configSection = (ConfigurationSection) update.SectionRecord.Result;
                                if (    String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource) ||
                                        configSection.SectionInformation.ConfigSourceModified) {
                                    skip = true;
                                    WriteSectionUpdate(utilWriter, update, linePosition, indent, true);
                                    wroteASection = true;
                                }
                            }
                        }

                        if (skip) {
                            //
                            // Skip over the existing element, then
                            // copy up to the next element, or exit this level.
                            //
                            xmlUtil.SkipAndCopyReaderToNextElement(utilWriter, true);
                        }
                        else {
                            // Copy this entire contents of this element and then to the next element, or exit this level.
                            xmlUtil.CopyOuterXmlToNextElement(utilWriter, true);
                            wroteASection = true;
                        }
                    }
                }
            }

            //
            // Write new section groups
            //
            if (sectionUpdates != null && addNewSections && sectionUpdates.HasNewSectionGroups()) {
                // Add whitespace to align us with the other elements in this group
                linePosition = parentLinePosition + indent;
                if (reader.NodeType == XmlNodeType.EndElement) {
                    if (utilWriter.IsLastLineBlank) {
                        startingLinePosition = xmlUtil.TrueLinePosition;
                    }
                    else {
                        startingLinePosition = parentLinePosition;
                    }
                }
                else {
                    startingLinePosition = 0;
                }

                utilWriter.AppendSpacesToLinePosition(linePosition);

                bool wroteNewSection = WriteNewConfigDefinitionsRecursive(utilWriter, sectionUpdates, linePosition, indent, true);
                if (wroteNewSection) {
                    wroteASection = true;
                }

                // Restore the whitespace of the end element
                utilWriter.AppendSpacesToLinePosition(startingLinePosition);
            }

            return wroteASection;
        }
        // Recursively write new sections for each section group.
        private bool WriteNewConfigDefinitionsRecursive(XmlUtilWriter utilWriter, SectionUpdates sectionUpdates, int linePosition, int indent, bool skipFirstIndent) {
            bool wroteASection = false;

            string[] movedSectionNames = sectionUpdates.GetMovedSectionNames();
            if (movedSectionNames != null) {
                wroteASection = true;
                foreach (string configKey in movedSectionNames) {
                    DefinitionUpdate update = sectionUpdates.GetDefinitionUpdate(configKey);
                    WriteSectionUpdate(utilWriter, update, linePosition, indent, skipFirstIndent);
                    utilWriter.AppendNewLine();
                    skipFirstIndent = false;
                }
            }

            string[] newGroupNames = sectionUpdates.GetNewGroupNames();
            if (newGroupNames != null) {
                foreach (string group in newGroupNames) {

                    if (TargetFramework != null) {
                        ConfigurationSectionGroup g = GetSectionGroup(group);
                        if (g != null && !g.ShouldSerializeSectionGroupInTargetVersion(TargetFramework)){
                            sectionUpdates.MarkGroupAsRetrieved(group);
                            continue;
                        }
                    }

                    if (!skipFirstIndent) {
                        utilWriter.AppendSpacesToLinePosition(linePosition);
                    }
                    skipFirstIndent = false;

                    utilWriter.Write("<" + group + ">\r\n");
                    bool recurseWroteASection = WriteNewConfigDefinitionsRecursive(
                            utilWriter, sectionUpdates.GetSectionUpdatesForGroup(group), linePosition + indent, indent, false);

                    if (recurseWroteASection) {
                        wroteASection = true;
                    }

                    utilWriter.AppendSpacesToLinePosition(linePosition);
                    utilWriter.Write("</" + group + ">\r\n");
                }
            }

            sectionUpdates.IsNew = false;

            return wroteASection;
        }
        private bool CopyConfigDeclarationsRecursive(
                SectionUpdates declarationUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter, string group,
                int parentLinePosition, int parentIndent) {

            bool wroteASection = false;
            XmlTextReader reader = xmlUtil.Reader;
            int linePosition;
            int indent;
            int startingLinePosition;

            indent = UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition);

            if (reader.NodeType == XmlNodeType.Element) {
                linePosition = xmlUtil.TrueLinePosition;
                startingLinePosition = linePosition;
            }
            else if (reader.NodeType == XmlNodeType.EndElement) {
                linePosition = parentLinePosition + indent;
                if (utilWriter.IsLastLineBlank) {
                    startingLinePosition = xmlUtil.TrueLinePosition;
                }
                else {
                    startingLinePosition = parentLinePosition;
                }
            }
            else {
                linePosition = parentLinePosition + indent;
                startingLinePosition = 0;
            }

            //
            // Write any new section declarations that apply to this group
            //
            if (declarationUpdates != null) {
                string[] movedSectionNames = declarationUpdates.GetMovedSectionNames();
                if (movedSectionNames != null) {
                    if (!utilWriter.IsLastLineBlank) {
                        utilWriter.AppendNewLine();
                    }

                    foreach (string configKey in movedSectionNames) {
                        DeclarationUpdate sectionUpdate = declarationUpdates.GetDeclarationUpdate(configKey);
                        Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");

                        // Write the one line section declaration.
                        utilWriter.AppendSpacesToLinePosition(linePosition);
                        utilWriter.Write(sectionUpdate.UpdatedXml);
                        utilWriter.AppendNewLine();

                        wroteASection = true;
                    }

                    // Restore the whitespace we used for the first element, which is either a start or an end element.
                    utilWriter.AppendSpacesToLinePosition(startingLinePosition);
                }
            }

            if (reader.NodeType == XmlNodeType.Element) {
                //
                // For each element at this depth, either:
                // - Write the element verbatim and recurse due to a group hierarchy element.
                // - Write the element verbatim because it is unchanged
                // - Write the updated XML for the section.
                // - Skip it because the section has been removed.
                //
                int depth = reader.Depth;
                while (reader.Depth == depth) {
                    bool                recurse = false;
                    DeclarationUpdate   sectionUpdate = null;
                    DeclarationUpdate   groupUpdate = null;
                    SectionUpdates      declarationUpdatesChild = null;
                    SectionUpdates      recurseDeclarationUpdates = declarationUpdates;
                    string              recurseGroup = group;

                    // update the lineposition and indent for each element
                    indent = UpdateIndent(indent, xmlUtil, utilWriter, parentLinePosition);
                    linePosition = xmlUtil.TrueLinePosition;

                    string directive = reader.Name;
                    string name = reader.GetAttribute(KEYWORD_SECTIONGROUP_NAME);
                    string configKey = CombineConfigKey(group, name);
                    if (directive == KEYWORD_SECTIONGROUP) {
                        // it's a group - get the updates for children
                        declarationUpdatesChild = declarationUpdates.GetSectionUpdatesForGroup(name);
                        if (declarationUpdatesChild != null) {
                            // get the group update
                            groupUpdate = declarationUpdatesChild.GetSectionGroupUpdate();

                            // recurse if there are more sections to copy
                            if (declarationUpdatesChild.HasUnretrievedSections()) {
                                recurse = true;
                                recurseGroup = configKey;
                                recurseDeclarationUpdates = declarationUpdatesChild;
                            }
                        }
                    }
                    else {
                        // it is a section - get the update
                        Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");
                        sectionUpdate = declarationUpdates.GetDeclarationUpdate(configKey);
                    }

                    bool writeGroupUpdate = (groupUpdate != null && groupUpdate.UpdatedXml != null);
                    if (recurse) {
#if DBG
                        string startElementName = reader.Name;
#endif

                        // create a checkpoint that we can revert to if no children are written
                        object checkpoint = utilWriter.CreateStreamCheckpoint();
                        string closingElement = null;

                        // Copy this element node and up to the first subelement
                        if (writeGroupUpdate) {
                            // replace the element with the updated xml
                            utilWriter.Write(groupUpdate.UpdatedXml);

                            // skip over the start element
                            reader.Read();
                        }
                        else {
                            closingElement= xmlUtil.UpdateStartElement(utilWriter, null, true, linePosition, indent);
                        }

                        if (closingElement == null) {
                            // Only if there is a closing element should
                            // we move to it
                            xmlUtil.CopyReaderToNextElement(utilWriter, true);
                        }

                        // Recurse
                        bool recurseWroteASection = CopyConfigDeclarationsRecursive(
                                recurseDeclarationUpdates, xmlUtil, utilWriter, recurseGroup, linePosition, indent);

                        if (closingElement != null) {
                            utilWriter.AppendSpacesToLinePosition(linePosition);
                            utilWriter.Write(closingElement);

                            // Since we already got to </configSections> in reader, lets
                            // indent so we can copy the element in the right place
                            utilWriter.AppendSpacesToLinePosition(parentLinePosition);
                        } else {
                            // Copy the end element
                            xmlUtil.CopyXmlNode(utilWriter);
                        }

                        if (recurseWroteASection || writeGroupUpdate) {
                            wroteASection = true;
                        }
                        else {
                            // back out the change
                            utilWriter.RestoreStreamCheckpoint(checkpoint);
                        }

                        // Copy up to the next element, or exit this level.
                        xmlUtil.CopyReaderToNextElement(utilWriter, true);
                    }
                    else {
                        bool    skip;
                        bool    skipChildElements = false;
                        if (sectionUpdate == null) {
                            skip = true;
                            if (writeGroupUpdate) {
                                // Insert an empty <sectionGroup type="typename" > node, to introduce the type
                                wroteASection = true;
                                utilWriter.Write(groupUpdate.UpdatedXml);
                                utilWriter.AppendNewLine();
                                utilWriter.AppendSpacesToLinePosition(linePosition);
                                utilWriter.Write(FORMAT_SECTIONGROUP_ENDELEMENT);
                                utilWriter.AppendNewLine();
                                utilWriter.AppendSpacesToLinePosition(linePosition);
                            }
                            else if (groupUpdate != null) {
                                // VSWhidbey 522450
                                // If groupUpdate exists, that means we've decided in GetConfigDeclarationUpdates
                                // that the section group should stay in the file.
                                Debug.Assert(groupUpdate.UpdatedXml == null, "groupUpdate.UpdatedXml == null");
                                Debug.Assert(!declarationUpdatesChild.HasUnretrievedSections(),
                                    "If the group has any unretrieved section, we should have chosen the recursive code path above.");

                                wroteASection = true;
                                skip = false;

                                // We should skip all the child sections.  If we indeed need to keep any child
                                // section, we should have chosen the recursive code path above.
                                skipChildElements = true;
                            }
                        }
                        else {
                            wroteASection = true;
                            if (sectionUpdate.UpdatedXml == null) {
                                skip = false;
                            }
                            else {
                                skip = true;

                                // Write the updated XML on a single line
                                utilWriter.Write(sectionUpdate.UpdatedXml);
                            }
                        }

                        if (skip) {
                            //
                            // Skip over the existing element, then
                            // copy up to the next element, or exit this level.
                            //
                            xmlUtil.SkipAndCopyReaderToNextElement(utilWriter, true);
                        }
                        else {
                            if (skipChildElements) {
                                xmlUtil.SkipChildElementsAndCopyOuterXmlToNextElement(utilWriter);
                            }
                            else {
                                // Copy this entire contents of this element and then to the next element, or exit this level.
                                xmlUtil.CopyOuterXmlToNextElement(utilWriter, true);
                            }
                        }
                    }
                }
            }

            return wroteASection;
        }
        private void WriteUnwrittenConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) {
            string[] unretrievedSectionNames = declarationUpdates.GetUnretrievedSectionNames();
            if (unretrievedSectionNames != null) {
                foreach (string configKey in unretrievedSectionNames) {
                    Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");
                    if (!skipFirstIndent) {
                        utilWriter.AppendSpacesToLinePosition(linePosition);
                    }
                    skipFirstIndent = false;

                    DeclarationUpdate update = declarationUpdates.GetDeclarationUpdate(configKey);
                    if (update != null && !string.IsNullOrEmpty(update.UpdatedXml)) {
                        utilWriter.Write(update.UpdatedXml);
                        utilWriter.AppendNewLine();
                    }
                }
            }

            string[] unretrievedGroupNames = declarationUpdates.GetUnretrievedGroupNames();
            if (unretrievedGroupNames != null) {
                foreach (string group in unretrievedGroupNames) {
                    if (TargetFramework != null) {
                        ConfigurationSectionGroup g = GetSectionGroup(group);
                        if (g != null && !g.ShouldSerializeSectionGroupInTargetVersion(TargetFramework)){
                            declarationUpdates.MarkGroupAsRetrieved(group);
                            continue;
                        }
                    }
                    if (!skipFirstIndent) {
                        utilWriter.AppendSpacesToLinePosition(linePosition);
                    }
                    skipFirstIndent = false;

                    SectionUpdates declarationUpdatesChild = declarationUpdates.GetSectionUpdatesForGroup(group);
                    DeclarationUpdate groupUpdate = declarationUpdatesChild.GetSectionGroupUpdate();
                    if (groupUpdate == null) {
                        utilWriter.Write("<sectionGroup name=\"" + group + "\">");
                    }
                    else {
                        utilWriter.Write(groupUpdate.UpdatedXml);
                    }
                    utilWriter.AppendNewLine();

                    WriteUnwrittenConfigDeclarationsRecursive(declarationUpdatesChild, utilWriter, linePosition + indent, indent, false);
                    utilWriter.AppendSpacesToLinePosition(linePosition);
                    utilWriter.Write("</sectionGroup>\r\n");
                }
            }
        }
 private bool CopyConfigDefinitionsRecursive(ConfigDefinitionUpdates configDefinitionUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter, bool locationPathApplies, LocationUpdates locationUpdates, SectionUpdates sectionUpdates, bool addNewSections, string group, int parentLinePosition, int parentIndent)
 {
     int trueLinePosition;
     int num3;
     bool flag = false;
     XmlTextReader reader = xmlUtil.Reader;
     int indent = this.UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition);
     if (reader.NodeType == XmlNodeType.Element)
     {
         trueLinePosition = xmlUtil.TrueLinePosition;
         num3 = trueLinePosition;
     }
     else if (reader.NodeType == XmlNodeType.EndElement)
     {
         trueLinePosition = parentLinePosition + indent;
         if (utilWriter.IsLastLineBlank)
         {
             num3 = xmlUtil.TrueLinePosition;
         }
         else
         {
             num3 = parentLinePosition;
         }
     }
     else
     {
         trueLinePosition = parentLinePosition + indent;
         num3 = 0;
     }
     if ((sectionUpdates != null) && addNewSections)
     {
         sectionUpdates.IsNew = false;
         string[] movedSectionNames = sectionUpdates.GetMovedSectionNames();
         if (movedSectionNames != null)
         {
             if (!utilWriter.IsLastLineBlank)
             {
                 utilWriter.AppendNewLine();
             }
             utilWriter.AppendSpacesToLinePosition(trueLinePosition);
             bool skipFirstIndent = true;
             foreach (string str in movedSectionNames)
             {
                 DefinitionUpdate definitionUpdate = sectionUpdates.GetDefinitionUpdate(str);
                 this.WriteSectionUpdate(utilWriter, definitionUpdate, trueLinePosition, indent, skipFirstIndent);
                 skipFirstIndent = false;
                 utilWriter.AppendNewLine();
                 flag = true;
             }
             utilWriter.AppendSpacesToLinePosition(num3);
         }
     }
     if (reader.NodeType == XmlNodeType.Element)
     {
         int depth = reader.Depth;
         while (reader.Depth == depth)
         {
             bool flag3 = false;
             DefinitionUpdate update = null;
             bool flag4 = locationPathApplies;
             LocationUpdates updates = locationUpdates;
             SectionUpdates updates2 = sectionUpdates;
             bool flag5 = addNewSections;
             string str2 = group;
             bool flag6 = false;
             indent = this.UpdateIndent(indent, xmlUtil, utilWriter, parentLinePosition);
             trueLinePosition = xmlUtil.TrueLinePosition;
             string name = reader.Name;
             if (name == "location")
             {
                 string locationSubPath = BaseConfigurationRecord.NormalizeLocationSubPath(reader.GetAttribute("path"), xmlUtil);
                 flag4 = false;
                 OverrideModeSetting locationDefault = OverrideModeSetting.LocationDefault;
                 bool inheritInChildApps = true;
                 if (base.IsLocationConfig)
                 {
                     if (locationSubPath == null)
                     {
                         flag4 = false;
                     }
                     else
                     {
                         flag4 = StringUtil.EqualsIgnoreCase(base.ConfigPath, base.Host.GetConfigPathFromLocationSubPath(base.Parent.ConfigPath, locationSubPath));
                     }
                 }
                 else
                 {
                     flag4 = locationSubPath == null;
                 }
                 if (flag4)
                 {
                     string attribute = reader.GetAttribute("allowOverride");
                     if (attribute != null)
                     {
                         locationDefault = OverrideModeSetting.CreateFromXmlReadValue(bool.Parse(attribute));
                     }
                     string str6 = reader.GetAttribute("overrideMode");
                     if (str6 != null)
                     {
                         locationDefault = OverrideModeSetting.CreateFromXmlReadValue(OverrideModeSetting.ParseOverrideModeXmlValue(str6, null));
                     }
                     string str7 = reader.GetAttribute("inheritInChildApplications");
                     if (str7 != null)
                     {
                         inheritInChildApps = bool.Parse(str7);
                     }
                     configDefinitionUpdates.FlagLocationWritten();
                 }
                 if (reader.IsEmptyElement)
                 {
                     if (flag4 && (configDefinitionUpdates.FindLocationUpdates(locationDefault, inheritInChildApps) != null))
                     {
                         flag4 = true;
                     }
                     else
                     {
                         flag4 = false;
                     }
                 }
                 else if (flag4)
                 {
                     if (configDefinitionUpdates != null)
                     {
                         updates = configDefinitionUpdates.FindLocationUpdates(locationDefault, inheritInChildApps);
                         if (updates != null)
                         {
                             flag3 = true;
                             updates2 = updates.SectionUpdates;
                             if ((base._locationSubPath == null) && updates.IsDefault)
                             {
                                 flag5 = false;
                             }
                         }
                     }
                 }
                 else if ((this.HasRemovedSectionsOrGroups && !base.IsLocationConfig) && base.Host.SupportsLocation)
                 {
                     flag3 = true;
                     updates = null;
                     updates2 = null;
                     flag5 = false;
                 }
             }
             else
             {
                 string configKey = BaseConfigurationRecord.CombineConfigKey(group, name);
                 FactoryRecord record = base.FindFactoryRecord(configKey, false);
                 if (record == null)
                 {
                     if (!flag4 && !base.IsLocationConfig)
                     {
                         flag6 = true;
                     }
                 }
                 else if (record.IsGroup)
                 {
                     if (reader.IsEmptyElement)
                     {
                         if (!flag4 && !base.IsLocationConfig)
                         {
                             flag6 = true;
                         }
                     }
                     else if (sectionUpdates != null)
                     {
                         SectionUpdates sectionUpdatesForGroup = sectionUpdates.GetSectionUpdatesForGroup(name);
                         if (sectionUpdatesForGroup != null)
                         {
                             flag3 = true;
                             str2 = configKey;
                             updates2 = sectionUpdatesForGroup;
                         }
                     }
                     else if (!flag4 && !base.IsLocationConfig)
                     {
                         if ((this._removedSectionGroups != null) && this._removedSectionGroups.Contains(configKey))
                         {
                             flag6 = true;
                         }
                         else
                         {
                             flag3 = true;
                             str2 = configKey;
                             updates = null;
                             updates2 = null;
                             flag5 = false;
                         }
                     }
                 }
                 else if (sectionUpdates != null)
                 {
                     update = sectionUpdates.GetDefinitionUpdate(configKey);
                 }
                 else if ((!flag4 && !base.IsLocationConfig) && ((this._removedSections != null) && this._removedSections.Contains(configKey)))
                 {
                     flag6 = true;
                 }
             }
             if (flag3)
             {
                 object o = utilWriter.CreateStreamCheckpoint();
                 xmlUtil.CopyXmlNode(utilWriter);
                 xmlUtil.CopyReaderToNextElement(utilWriter, true);
                 bool flag8 = this.CopyConfigDefinitionsRecursive(configDefinitionUpdates, xmlUtil, utilWriter, flag4, updates, updates2, flag5, str2, trueLinePosition, indent);
                 xmlUtil.CopyXmlNode(utilWriter);
                 if (flag8)
                 {
                     flag = true;
                 }
                 else
                 {
                     utilWriter.RestoreStreamCheckpoint(o);
                 }
                 xmlUtil.CopyReaderToNextElement(utilWriter, true);
             }
             else
             {
                 bool flag9;
                 if (update == null)
                 {
                     flag9 = flag4 || flag6;
                 }
                 else
                 {
                     flag9 = false;
                     if (update.UpdatedXml != null)
                     {
                         ConfigurationSection result = (ConfigurationSection) update.SectionRecord.Result;
                         if (string.IsNullOrEmpty(result.SectionInformation.ConfigSource) || result.SectionInformation.ConfigSourceModified)
                         {
                             flag9 = true;
                             this.WriteSectionUpdate(utilWriter, update, trueLinePosition, indent, true);
                             flag = true;
                         }
                     }
                 }
                 if (flag9)
                 {
                     xmlUtil.SkipAndCopyReaderToNextElement(utilWriter, true);
                 }
                 else
                 {
                     xmlUtil.CopyOuterXmlToNextElement(utilWriter, true);
                     flag = true;
                 }
             }
         }
     }
     if (((sectionUpdates != null) && addNewSections) && sectionUpdates.HasNewSectionGroups())
     {
         trueLinePosition = parentLinePosition + indent;
         if (reader.NodeType == XmlNodeType.EndElement)
         {
             if (utilWriter.IsLastLineBlank)
             {
                 num3 = xmlUtil.TrueLinePosition;
             }
             else
             {
                 num3 = parentLinePosition;
             }
         }
         else
         {
             num3 = 0;
         }
         utilWriter.AppendSpacesToLinePosition(trueLinePosition);
         if (this.WriteNewConfigDefinitionsRecursive(utilWriter, sectionUpdates, trueLinePosition, indent, true))
         {
             flag = true;
         }
         utilWriter.AppendSpacesToLinePosition(num3);
     }
     return flag;
 }
 private bool CopyConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter, string group, int parentLinePosition, int parentIndent)
 {
     int trueLinePosition;
     int num3;
     bool flag = false;
     XmlTextReader reader = xmlUtil.Reader;
     int oldIndent = this.UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition);
     if (reader.NodeType == XmlNodeType.Element)
     {
         trueLinePosition = xmlUtil.TrueLinePosition;
         num3 = trueLinePosition;
     }
     else if (reader.NodeType == XmlNodeType.EndElement)
     {
         trueLinePosition = parentLinePosition + oldIndent;
         if (utilWriter.IsLastLineBlank)
         {
             num3 = xmlUtil.TrueLinePosition;
         }
         else
         {
             num3 = parentLinePosition;
         }
     }
     else
     {
         trueLinePosition = parentLinePosition + oldIndent;
         num3 = 0;
     }
     if (declarationUpdates != null)
     {
         string[] movedSectionNames = declarationUpdates.GetMovedSectionNames();
         if (movedSectionNames != null)
         {
             if (!utilWriter.IsLastLineBlank)
             {
                 utilWriter.AppendNewLine();
             }
             foreach (string str in movedSectionNames)
             {
                 DeclarationUpdate declarationUpdate = declarationUpdates.GetDeclarationUpdate(str);
                 utilWriter.AppendSpacesToLinePosition(trueLinePosition);
                 utilWriter.Write(declarationUpdate.UpdatedXml);
                 utilWriter.AppendNewLine();
                 flag = true;
             }
             utilWriter.AppendSpacesToLinePosition(num3);
         }
     }
     if (reader.NodeType == XmlNodeType.Element)
     {
         int depth = reader.Depth;
         while (reader.Depth == depth)
         {
             bool flag2 = false;
             DeclarationUpdate update2 = null;
             DeclarationUpdate sectionGroupUpdate = null;
             SectionUpdates sectionUpdatesForGroup = null;
             SectionUpdates updates2 = declarationUpdates;
             string str2 = group;
             oldIndent = this.UpdateIndent(oldIndent, xmlUtil, utilWriter, parentLinePosition);
             trueLinePosition = xmlUtil.TrueLinePosition;
             string name = reader.Name;
             string attribute = reader.GetAttribute("name");
             string configKey = BaseConfigurationRecord.CombineConfigKey(group, attribute);
             if (name == "sectionGroup")
             {
                 sectionUpdatesForGroup = declarationUpdates.GetSectionUpdatesForGroup(attribute);
                 if (sectionUpdatesForGroup != null)
                 {
                     sectionGroupUpdate = sectionUpdatesForGroup.GetSectionGroupUpdate();
                     if (sectionUpdatesForGroup.HasUnretrievedSections())
                     {
                         flag2 = true;
                         str2 = configKey;
                         updates2 = sectionUpdatesForGroup;
                     }
                 }
             }
             else
             {
                 update2 = declarationUpdates.GetDeclarationUpdate(configKey);
             }
             bool flag3 = (sectionGroupUpdate != null) && (sectionGroupUpdate.UpdatedXml != null);
             if (flag2)
             {
                 object o = utilWriter.CreateStreamCheckpoint();
                 string s = null;
                 if (flag3)
                 {
                     utilWriter.Write(sectionGroupUpdate.UpdatedXml);
                     reader.Read();
                 }
                 else
                 {
                     s = xmlUtil.UpdateStartElement(utilWriter, null, true, trueLinePosition, oldIndent);
                 }
                 if (s == null)
                 {
                     xmlUtil.CopyReaderToNextElement(utilWriter, true);
                 }
                 bool flag4 = this.CopyConfigDeclarationsRecursive(updates2, xmlUtil, utilWriter, str2, trueLinePosition, oldIndent);
                 if (s != null)
                 {
                     utilWriter.AppendSpacesToLinePosition(trueLinePosition);
                     utilWriter.Write(s);
                     utilWriter.AppendSpacesToLinePosition(parentLinePosition);
                 }
                 else
                 {
                     xmlUtil.CopyXmlNode(utilWriter);
                 }
                 if (flag4 || flag3)
                 {
                     flag = true;
                 }
                 else
                 {
                     utilWriter.RestoreStreamCheckpoint(o);
                 }
                 xmlUtil.CopyReaderToNextElement(utilWriter, true);
             }
             else
             {
                 bool flag5;
                 bool flag6 = false;
                 if (update2 == null)
                 {
                     flag5 = true;
                     if (flag3)
                     {
                         flag = true;
                         utilWriter.Write(sectionGroupUpdate.UpdatedXml);
                         utilWriter.AppendNewLine();
                         utilWriter.AppendSpacesToLinePosition(trueLinePosition);
                         utilWriter.Write("</sectionGroup>");
                         utilWriter.AppendNewLine();
                         utilWriter.AppendSpacesToLinePosition(trueLinePosition);
                     }
                     else if (sectionGroupUpdate != null)
                     {
                         flag = true;
                         flag5 = false;
                         flag6 = true;
                     }
                 }
                 else
                 {
                     flag = true;
                     if (update2.UpdatedXml == null)
                     {
                         flag5 = false;
                     }
                     else
                     {
                         flag5 = true;
                         utilWriter.Write(update2.UpdatedXml);
                     }
                 }
                 if (flag5)
                 {
                     xmlUtil.SkipAndCopyReaderToNextElement(utilWriter, true);
                 }
                 else
                 {
                     if (flag6)
                     {
                         xmlUtil.SkipChildElementsAndCopyOuterXmlToNextElement(utilWriter);
                         continue;
                     }
                     xmlUtil.CopyOuterXmlToNextElement(utilWriter, true);
                 }
             }
         }
     }
     return flag;
 }
 private void WriteUnwrittenConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent)
 {
     string[] unretrievedSectionNames = declarationUpdates.GetUnretrievedSectionNames();
     if (unretrievedSectionNames != null)
     {
         foreach (string str in unretrievedSectionNames)
         {
             if (!skipFirstIndent)
             {
                 utilWriter.AppendSpacesToLinePosition(linePosition);
             }
             skipFirstIndent = false;
             DeclarationUpdate declarationUpdate = declarationUpdates.GetDeclarationUpdate(str);
             if ((declarationUpdate != null) && !string.IsNullOrEmpty(declarationUpdate.UpdatedXml))
             {
                 utilWriter.Write(declarationUpdate.UpdatedXml);
                 utilWriter.AppendNewLine();
             }
         }
     }
     string[] unretrievedGroupNames = declarationUpdates.GetUnretrievedGroupNames();
     if (unretrievedGroupNames != null)
     {
         foreach (string str2 in unretrievedGroupNames)
         {
             if (base.TargetFramework != null)
             {
                 ConfigurationSectionGroup sectionGroup = this.GetSectionGroup(str2);
                 if ((sectionGroup != null) && !sectionGroup.ShouldSerializeSectionGroupInTargetVersion(base.TargetFramework))
                 {
                     declarationUpdates.MarkGroupAsRetrieved(str2);
                     continue;
                 }
             }
             if (!skipFirstIndent)
             {
                 utilWriter.AppendSpacesToLinePosition(linePosition);
             }
             skipFirstIndent = false;
             SectionUpdates sectionUpdatesForGroup = declarationUpdates.GetSectionUpdatesForGroup(str2);
             DeclarationUpdate sectionGroupUpdate = sectionUpdatesForGroup.GetSectionGroupUpdate();
             if (sectionGroupUpdate == null)
             {
                 utilWriter.Write("<sectionGroup name=\"" + str2 + "\">");
             }
             else
             {
                 utilWriter.Write(sectionGroupUpdate.UpdatedXml);
             }
             utilWriter.AppendNewLine();
             this.WriteUnwrittenConfigDeclarationsRecursive(sectionUpdatesForGroup, utilWriter, linePosition + indent, indent, false);
             utilWriter.AppendSpacesToLinePosition(linePosition);
             utilWriter.Write("</sectionGroup>\r\n");
         }
     }
 }
 private bool WriteNewConfigDefinitionsRecursive(XmlUtilWriter utilWriter, SectionUpdates sectionUpdates, int linePosition, int indent, bool skipFirstIndent)
 {
     bool flag = false;
     string[] movedSectionNames = sectionUpdates.GetMovedSectionNames();
     if (movedSectionNames != null)
     {
         flag = true;
         foreach (string str in movedSectionNames)
         {
             DefinitionUpdate definitionUpdate = sectionUpdates.GetDefinitionUpdate(str);
             this.WriteSectionUpdate(utilWriter, definitionUpdate, linePosition, indent, skipFirstIndent);
             utilWriter.AppendNewLine();
             skipFirstIndent = false;
         }
     }
     string[] newGroupNames = sectionUpdates.GetNewGroupNames();
     if (newGroupNames != null)
     {
         foreach (string str2 in newGroupNames)
         {
             if (base.TargetFramework != null)
             {
                 ConfigurationSectionGroup sectionGroup = this.GetSectionGroup(str2);
                 if ((sectionGroup != null) && !sectionGroup.ShouldSerializeSectionGroupInTargetVersion(base.TargetFramework))
                 {
                     sectionUpdates.MarkGroupAsRetrieved(str2);
                     continue;
                 }
             }
             if (!skipFirstIndent)
             {
                 utilWriter.AppendSpacesToLinePosition(linePosition);
             }
             skipFirstIndent = false;
             utilWriter.Write("<" + str2 + ">\r\n");
             if (this.WriteNewConfigDefinitionsRecursive(utilWriter, sectionUpdates.GetSectionUpdatesForGroup(str2), linePosition + indent, indent, false))
             {
                 flag = true;
             }
             utilWriter.AppendSpacesToLinePosition(linePosition);
             utilWriter.Write("</" + str2 + ">\r\n");
         }
     }
     sectionUpdates.IsNew = false;
     return flag;
 }