Ejemplo n.º 1
0
        internal void CreateSection(SectionGroupInfo group, string name, ConfigurationSection sec)
        {
            if (group.HasChild(name))
            {
                throw new ConfigurationErrorsException("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'");
            }

            if (!HasFile && !sec.SectionInformation.AllowLocation)
            {
                throw new ConfigurationErrorsException("The configuration section <" + name + "> cannot be defined inside a <location> element.");
            }

            if (!system.Host.IsDefinitionAllowed(configPath, sec.SectionInformation.AllowDefinition, sec.SectionInformation.AllowExeDefinition))
            {
                object ctx = sec.SectionInformation.AllowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object)sec.SectionInformation.AllowExeDefinition : (object)sec.SectionInformation.AllowDefinition;
                throw new ConfigurationErrorsException("The section <" + name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "').");
            }

            if (sec.SectionInformation.Type == null)
            {
                sec.SectionInformation.Type = system.Host.GetConfigTypeName(sec.GetType());
            }

            SectionInfo section = new SectionInfo(name, sec.SectionInformation);

            section.StreamName = streamName;
            section.ConfigHost = system.Host;
            group.AddChild(section);
            elementData [section] = sec;
            sec.Configuration     = this;
        }
Ejemplo n.º 2
0
        public void Remove(string name)
        {
            SectionInfo secData = group.Sections [name] as SectionInfo;

            if (secData != null)
            {
                config.RemoveConfigInfo(secData);
            }
        }
Ejemplo n.º 3
0
        internal ConfigurationSection GetSectionInstance(SectionInfo config, bool createDefaultInstance)
        {
            object data = _elementData[config];
            var    sec  = data as ConfigurationSection;

            if (sec != null || !createDefaultInstance)
            {
                return(sec);
            }

            var secObj = config.CreateInstance();

            sec = secObj as ConfigurationSection;
            if (sec == null)
            {
                var ds = new DefaultSection();
                ds.SectionHandler = secObj as IConfigurationSectionHandler;
                sec = ds;
            }
            sec.Configuration = this;

            ConfigurationSection parentSection = null;

            if (Parent != null)
            {
                parentSection = Parent.GetSectionInstance(config, true);
                sec.SectionInformation.SetParentSection(parentSection);
            }
            sec.SectionInformation.ConfigFilePath = FilePath;

            sec.ConfigContext = _system.Host.CreateDeprecatedConfigContext(_configPath);

            var xml = data as string;

            sec.RawXml = xml;
            sec.Reset(parentSection);

            if (xml != null)
            {
                using (var r = XmlReader.Create(new StringReader(xml)))
                {
                    sec.DeserializeSection(r);
                }

                if (!string.IsNullOrEmpty(sec.SectionInformation.ConfigSource) && !string.IsNullOrEmpty(FilePath))
                {
                    sec.DeserializeConfigSource(Path.GetDirectoryName(FilePath));
                }
            }

            _elementData[config] = sec;
            return(sec);
        }
Ejemplo n.º 4
0
        internal ConfigurationSection GetSectionInstance(SectionInfo config, bool createDefaultInstance)
        {
            object data = elementData [config];
            ConfigurationSection sec = data as ConfigurationSection;

            if (sec != null || !createDefaultInstance)
            {
                return(sec);
            }

            object secObj = config.CreateInstance();

            sec = secObj as ConfigurationSection;
            if (sec == null)
            {
                DefaultSection ds = new DefaultSection();
                ds.SectionHandler = secObj as IConfigurationSectionHandler;
                sec = ds;
            }
            sec.Configuration = this;

            ConfigurationSection parentSection = null;

            if (parent != null)
            {
                parentSection = parent.GetSectionInstance(config, true);
                sec.SectionInformation.SetParentSection(parentSection);
            }
            sec.SectionInformation.ConfigFilePath = FilePath;

            sec.ConfigContext = system.Host.CreateDeprecatedConfigContext(configPath);

            string xml = data as string;

            sec.RawXml = xml;
            sec.Reset(parentSection);

            if (xml != null)
            {
                XmlTextReader r = new ConfigXmlTextReader(new StringReader(xml), FilePath);
                sec.DeserializeSection(r);
                r.Close();

                if (!String.IsNullOrEmpty(sec.SectionInformation.ConfigSource) && !String.IsNullOrEmpty(FilePath))
                {
                    sec.DeserializeConfigSource(Path.GetDirectoryName(FilePath));
                }
            }

            elementData [config] = sec;
            return(sec);
        }
Ejemplo n.º 5
0
        private SectionInfo CreateSectionInfo(IConfigSystem system, string configPath, string sectionName, string sectionType)
        {
            var sectionInformation = new SectionInformation
            {
                Type           = sectionType,
                ConfigFilePath = configPath
            };

            sectionInformation.SetName(sectionName);

            var sectionInfo = new SectionInfo(sectionName, sectionInformation)
            {
                StreamName = FileName,
                ConfigHost = system.Host
            };

            return(sectionInfo);
        }
Ejemplo n.º 6
0
 public ConfigurationSection this [string name]
 {
     get {
         ConfigurationSection sec = BaseGet(name) as ConfigurationSection;
         if (sec == null)
         {
             SectionInfo secData = group.Sections [name] as SectionInfo;
             if (secData == null)
             {
                 return(null);
             }
             sec = config.GetSectionInstance(secData, true);
             if (sec == null)
             {
                 return(null);
             }
             BaseSet(name, sec);
         }
         return(sec);
     }
 }
Ejemplo n.º 7
0
		internal void SetSectionXml (SectionInfo config, string data)
		{
			elementData [config] = data;
		}
Ejemplo n.º 8
0
		internal void SetConfigurationSection (SectionInfo config, ConfigurationSection sec)
		{
			elementData [config] = sec;
		}
Ejemplo n.º 9
0
		internal ConfigurationSection GetSectionInstance (SectionInfo config, bool createDefaultInstance)
		{
			object data = elementData [config];
			ConfigurationSection sec = data as ConfigurationSection;
			if (sec != null || !createDefaultInstance) return sec;
			
			object secObj = config.CreateInstance ();
			sec = secObj as ConfigurationSection;
			if (sec == null) {
				DefaultSection ds = new DefaultSection ();
				ds.SectionHandler = secObj as IConfigurationSectionHandler;
				sec = ds;
			}
			sec.Configuration = this;

			ConfigurationSection parentSection = null;
			if (parent != null) {
				parentSection = parent.GetSectionInstance (config, true);
				sec.SectionInformation.SetParentSection (parentSection);
			}
			sec.SectionInformation.ConfigFilePath = FilePath;

			sec.ConfigContext = system.Host.CreateDeprecatedConfigContext(configPath);
			
			string xml = data as string;
			sec.RawXml = xml;
			sec.Reset (parentSection);

			if (xml != null) {
				XmlTextReader r = new ConfigXmlTextReader (new StringReader (xml), FilePath);
				sec.DeserializeSection (r);
				r.Close ();

				if (!String.IsNullOrEmpty (sec.SectionInformation.ConfigSource) && !String.IsNullOrEmpty (FilePath))
					sec.DeserializeConfigSource (Path.GetDirectoryName (FilePath));
			}
			
			elementData [config] = sec;
			return sec;
		}
Ejemplo n.º 10
0
        public override void ReadConfig(Configuration cfg, string streamName, XmlReader reader)
        {
            StreamName = streamName;
            ConfigHost = cfg.ConfigHost;

            if (reader.LocalName != "configSections")
            {
                while (reader.MoveToNextAttribute())
                {
                    if (reader.Name == "name")
                    {
                        Name = reader.Value;
                    }
                    else if (reader.Name == "type")
                    {
                        TypeName = reader.Value;
                        Type     = null;
                    }
                    else
                    {
                        ThrowException("Unrecognized attribute", reader);
                    }
                }

                if (Name == null)
                {
                    ThrowException("sectionGroup must have a 'name' attribute", reader);
                }

                if (Name == "location")
                {
                    ThrowException("location is a reserved section name", reader);
                }
            }

            if (TypeName == null)
            {
                TypeName = "System.Configuration.ConfigurationSectionGroup";
            }

            if (reader.IsEmptyElement)
            {
                reader.Skip();
                return;
            }

            reader.ReadStartElement();
            reader.MoveToContent();

            while (reader.NodeType != XmlNodeType.EndElement)
            {
                if (reader.NodeType != XmlNodeType.Element)
                {
                    reader.Skip();
                    continue;
                }

                string     name  = reader.LocalName;
                ConfigInfo cinfo = null;

                if (name == "remove")
                {
                    ReadRemoveSection(reader);
                    continue;
                }

                if (name == "clear")
                {
                    if (reader.HasAttributes)
                    {
                        ThrowException("Unrecognized attribute.", reader);
                    }

                    Clear();
                    reader.Skip();
                    continue;
                }

                if (name == "section")
                {
                    cinfo = new SectionInfo();
                }
                else if (name == "sectionGroup")
                {
                    cinfo = new SectionGroupInfo();
                }
                else
                {
                    ThrowException("Unrecognized element: " + reader.Name, reader);
                }

                cinfo.ReadConfig(cfg, streamName, reader);
                var actInfo = Groups[cinfo.Name];
                if (actInfo == null)
                {
                    actInfo = Sections[cinfo.Name];
                }

                if (actInfo != null)
                {
                    if (actInfo.GetType() != cinfo.GetType())
                    {
                        ThrowException("A section or section group named '" + cinfo.Name + "' already exists", reader);
                    }
                    // Merge all the new data
                    actInfo.Merge(cinfo);

                    // Make sure that this section is saved in this configuration file:
                    actInfo.StreamName = streamName;
                }
                else
                {
                    AddChild(cinfo);
                }
            }

            reader.ReadEndElement();
        }
Ejemplo n.º 11
0
 internal string GetSectionXml(SectionInfo config)
 {
     return(elementData [config] as string);
 }
Ejemplo n.º 12
0
 internal void SetConfigurationSection(SectionInfo config, ConfigurationSection sec)
 {
     elementData [config] = sec;
 }
Ejemplo n.º 13
0
        public void RemoveAt(int index)
        {
            SectionInfo secData = group.Sections [index] as SectionInfo;

            config.RemoveConfigInfo(secData);
        }
		public override void ReadConfig (Configuration cfg, string streamName, XmlReader reader)
		{
			StreamName = streamName;
			ConfigHost = cfg.ConfigHost;
			
			if (reader.LocalName != "configSections")
			{
				while (reader.MoveToNextAttribute ()) {
					if (reader.Name == "name")
						Name = reader.Value;
					else if (reader.Name == "type") {
						TypeName = reader.Value;
						Type = null;
					}
					else
						ThrowException ("Unrecognized attribute", reader);
				}
				
				if (Name == null)
					ThrowException ("sectionGroup must have a 'name' attribute", reader);
	
				if (Name == "location")
					ThrowException ("location is a reserved section name", reader);
			}
			
			if (TypeName == null)
				TypeName = "System.Configuration.ConfigurationSectionGroup";
			
			if (reader.IsEmptyElement) {
				reader.Skip ();
				return;
			}
			
			reader.ReadStartElement ();
			reader.MoveToContent ();
			
			while (reader.NodeType != XmlNodeType.EndElement)
			{
				if (reader.NodeType != XmlNodeType.Element) {
					reader.Skip ();
					continue;
				}
				
				string name = reader.LocalName;
				ConfigInfo cinfo = null;
				
				if (name == "remove") {
					ReadRemoveSection (reader);
					continue;
				}

				if (name == "clear") {
					if (reader.HasAttributes)
						ThrowException ("Unrecognized attribute.", reader);

					Clear ();
					reader.Skip ();
					continue;
				}

				if (name == "section")
					cinfo = new SectionInfo ();
				else if (name == "sectionGroup") {
					cinfo = new SectionGroupInfo ();
				} else
					ThrowException ("Unrecognized element: " + reader.Name, reader);
					
				cinfo.ReadConfig (cfg, streamName, reader);
				ConfigInfo actInfo = Groups [cinfo.Name];
				if (actInfo == null) actInfo = Sections [cinfo.Name];
				
				if (actInfo != null) {
					if (actInfo.GetType () != cinfo.GetType ())
						ThrowException ("A section or section group named '" + cinfo.Name + "' already exists", reader);
					// Merge all the new data
					actInfo.Merge (cinfo);
					
					// Make sure that this section is saved in this configuration file:
					actInfo.StreamName = streamName;
				}
				else
					AddChild (cinfo);
			}
			
			reader.ReadEndElement ();
		}
Ejemplo n.º 15
0
		internal string GetSectionXml (SectionInfo config)
		{
			return elementData [config] as string;
		}
Ejemplo n.º 16
0
		internal void CreateSection (SectionGroupInfo group, string name, ConfigurationSection sec)
		{
			if (group.HasChild (name))
				throw new ConfigurationErrorsException ("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'");
				
			if (!HasFile && !sec.SectionInformation.AllowLocation)
				throw new ConfigurationErrorsException ("The configuration section <" + name + "> cannot be defined inside a <location> element."); 

			if (!system.Host.IsDefinitionAllowed (configPath, sec.SectionInformation.AllowDefinition, sec.SectionInformation.AllowExeDefinition)) {
				object ctx = sec.SectionInformation.AllowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object) sec.SectionInformation.AllowExeDefinition : (object) sec.SectionInformation.AllowDefinition;
				throw new ConfigurationErrorsException ("The section <" + name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "').");
			}

			if (sec.SectionInformation.Type == null)
				sec.SectionInformation.Type = system.Host.GetConfigTypeName (sec.GetType ());

			SectionInfo section = new SectionInfo (name, sec.SectionInformation);
			section.StreamName = streamName;
			section.ConfigHost = system.Host;
			group.AddChild (section);
			elementData [section] = sec;
			sec.Configuration = this;
		}
Ejemplo n.º 17
0
 internal void SetSectionXml(SectionInfo config, string data)
 {
     elementData [config] = data;
 }