Пример #1
0
        bool Load()
        {
            if (String.IsNullOrEmpty(streamName))
            {
                return(true);
            }

            Stream stream = null;

            try {
                stream = system.Host.OpenStreamForRead(streamName);
                if (stream == null)
                {
                    return(false);
                }
            } catch {
                return(false);
            }

            using (XmlTextReader reader = new ConfigXmlTextReader(stream, streamName)) {
                ReadConfigFile(reader, streamName);
            }
            ResetModified();
            return(true);
        }
Пример #2
0
        protected internal virtual object GetRuntimeObject()
        {
            if (SectionHandler != null)
            {
                ConfigurationSection parentSection = sectionInformation != null?sectionInformation.GetParentSection() : null;

                object parent = parentSection != null?parentSection.GetRuntimeObject() : null;

                if (RawXml == null)
                {
                    return(parent);
                }

                try
                {
                    // This code requires some re-thinking...
                    XmlReader reader = new ConfigXmlTextReader(
                        new StringReader(RawXml),
                        Configuration.FilePath);

                    DoDeserializeSection(reader);

                    if (!String.IsNullOrEmpty(SectionInformation.ConfigSource))
                    {
                        string fileDir = SectionInformation.ConfigFilePath;
                        if (!String.IsNullOrEmpty(fileDir))
                        {
                            fileDir = Path.GetDirectoryName(fileDir);
                        }
                        else
                        {
                            fileDir = String.Empty;
                        }

                        string path = Path.Combine(fileDir, SectionInformation.ConfigSource);
                        if (File.Exists(path))
                        {
                            RawXml = File.ReadAllText(path);
                            SectionInformation.SetRawXml(RawXml);
                        }
                    }
                }
                catch
                {
                    // ignore, it can fail - we deserialize only in order to get
                    // the configSource attribute
                }
                XmlDocument doc = new ConfigurationXmlDocument();
                doc.LoadXml(RawXml);
                return(SectionHandler.Create(parent, ConfigContext, doc.DocumentElement));
            }
            return(this);
        }
Пример #3
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);
        }
Пример #4
0
		public override void DeserializeElement (XmlReader reader, bool serializeCollectionKey)
		{
			/* need to do this so we pick up the File attribute */
			base.DeserializeElement (reader, serializeCollectionKey);

			if (File != "") {
				try {
					Stream s = System.IO.File.OpenRead (File);
					XmlReader subreader = new ConfigXmlTextReader (s, File);
					base.DeserializeElement (subreader, serializeCollectionKey);
					s.Close ();
				}
				catch {
					// nada, we just ignore a missing/unreadble file
				}
			}
		}
        public override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            /* need to do this so we pick up the File attribute */
            base.DeserializeElement(reader, serializeCollectionKey);

            if (File != "")
            {
                try {
                    Stream    s         = System.IO.File.OpenRead(File);
                    XmlReader subreader = new ConfigXmlTextReader(s, File);
                    base.DeserializeElement(subreader, serializeCollectionKey);
                    s.Close();
                }
                catch {
                    // nada, we just ignore a missing/unreadble file
                }
            }
        }
Пример #6
0
		protected internal override void DeserializeElement (XmlReader reader, bool serializeCollectionKey)
		{
			/* need to do this so we pick up the File attribute */
			base.DeserializeElement (reader, serializeCollectionKey);

			if (File != "") {
				try {
					string filePath = File;
					if (!Path.IsPathRooted (filePath))
						filePath = Path.Combine (Path.GetDirectoryName (Configuration.FilePath), filePath);

					Stream s = System.IO.File.OpenRead (filePath);
					XmlReader subreader = new ConfigXmlTextReader (s, filePath);
					base.DeserializeElement (subreader, serializeCollectionKey);
					s.Close ();
				}
				catch {
					// nada, we just ignore a missing/unreadble file
				}
			}
		}
        public Configuration OpenConfiguration()
        {
            if (configuration == null)
            {
                if (!parentResolved)
                {
                    Configuration parentFile = parent.GetParentWithFile();
                    if (parentFile != null)
                    {
                        string parentRelativePath = parent.ConfigHost.GetConfigPathFromLocationSubPath(parent.LocationConfigPath, path);
                        parent = parentFile.FindLocationConfiguration(parentRelativePath, parent);
                    }
                }

                configuration = new Configuration(parent, path);
                using (XmlTextReader tr = new ConfigXmlTextReader(new StringReader(xmlContent), path))
                    configuration.ReadData(tr, allowOverride);

                xmlContent = null;
            }
            return(configuration);
        }
Пример #8
0
        protected internal override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            /* need to do this so we pick up the File attribute */
            base.DeserializeElement(reader, serializeCollectionKey);

            if (File != "")
            {
                try {
                    string filePath = File;
                    if (!Path.IsPathRooted(filePath))
                    {
                        filePath = Path.Combine(Path.GetDirectoryName(Configuration.FilePath), filePath);
                    }

                    Stream    s         = System.IO.File.OpenRead(filePath);
                    XmlReader subreader = new ConfigXmlTextReader(s, filePath);
                    base.DeserializeElement(subreader, serializeCollectionKey);
                    s.Close();
                }
                catch {
                    // nada, we just ignore a missing/unreadble file
                }
            }
        }
Пример #9
0
		bool Load ()
		{
			if (String.IsNullOrEmpty (streamName))
				return true;

			Stream stream = null;
			try {
				stream = system.Host.OpenStreamForRead (streamName);
				if (stream == null)
					return false;
			} catch {
				return false;
			}

			using (XmlTextReader reader = new ConfigXmlTextReader (stream, streamName)) {
				ReadConfigFile (reader, streamName);
			}
			ResetModified ();
			return true;
		}
Пример #10
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;
		}
Пример #11
0
		public virtual object GetRuntimeObject ()
		{
			if (SectionHandler != null) {
				ConfigurationSection parentSection = sectionInformation != null ? sectionInformation.GetParentSection () : null;
				object parent = parentSection != null ? parentSection.GetRuntimeObject () : null;
				if (RawXml == null)
					return parent;
				
				try {
					// This code requires some re-thinking...
					XmlReader reader = new ConfigXmlTextReader (
						new StringReader (RawXml),
						Configuration.FilePath);

					DoDeserializeSection (reader);
					
					if (!String.IsNullOrEmpty (SectionInformation.ConfigSource)) {
						string fileDir = SectionInformation.ConfigFilePath;
						if (!String.IsNullOrEmpty (fileDir))
							fileDir = Path.GetDirectoryName (fileDir);
						else
							fileDir = String.Empty;
					
						string path = Path.Combine (fileDir, SectionInformation.ConfigSource);
						if (File.Exists (path)) {
							RawXml = File.ReadAllText (path);
							SectionInformation.SetRawXml (RawXml);
						}
					}
				} catch {
					// ignore, it can fail - we deserialize only in order to get
					// the configSource attribute
				}
				XmlDocument doc = new ConfigurationXmlDocument ();
				doc.LoadXml (RawXml);
				return SectionHandler.Create (parent, ConfigContext, doc.DocumentElement);
			}
			return this;
		}
Пример #12
0
        protected internal virtual void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            Hashtable readProps = new Hashtable();

            reader.MoveToContent();
            elementPresent = true;

            while (reader.MoveToNextAttribute())
            {
                PropertyInformation prop = ElementInformation.Properties [reader.LocalName];
                if (prop == null || (serializeCollectionKey && !prop.IsKey))
                {
                    /* handle the built in ConfigurationElement attributes here */
                    if (reader.LocalName == "lockAllAttributesExcept")
                    {
                        LockAllAttributesExcept.SetFromList(reader.Value);
                    }
                    else if (reader.LocalName == "lockAllElementsExcept")
                    {
                        LockAllElementsExcept.SetFromList(reader.Value);
                    }
                    else if (reader.LocalName == "lockAttributes")
                    {
                        LockAttributes.SetFromList(reader.Value);
                    }
                    else if (reader.LocalName == "lockElements")
                    {
                        LockElements.SetFromList(reader.Value);
                    }
                    else if (reader.LocalName == "lockItem")
                    {
                        LockItem = (reader.Value.ToLowerInvariant() == "true");
                    }
                    else if (reader.LocalName == "xmlns")
                    {
                        /* ignore */
                    }
                    else if (this is ConfigurationSection && reader.LocalName == "configSource")
                    {
                        /* ignore */
                    }
                    else if (!OnDeserializeUnrecognizedAttribute(reader.LocalName, reader.Value))
                    {
                        throw new ConfigurationErrorsException("Unrecognized attribute '" + reader.LocalName + "'.", reader);
                    }

                    continue;
                }

                if (readProps.ContainsKey(prop))
                {
                    throw new ConfigurationErrorsException("The attribute '" + prop.Name + "' may only appear once in this element.", reader);
                }

                string value = null;
                try {
                    value = reader.Value;
                    ValidateValue(prop.Property, value);
                    prop.SetStringValue(value);
                } catch (ConfigurationErrorsException) {
                    throw;
                } catch (ConfigurationException) {
                    throw;
                } catch (Exception ex) {
                    string msg = String.Format("The value for the property '{0}' is not valid. The error is: {1}", prop.Name, ex.Message);
                    throw new ConfigurationErrorsException(msg, reader);
                }
                readProps [prop] = prop.Name;

                ConfigXmlTextReader _reader = reader as ConfigXmlTextReader;
                if (_reader != null)
                {
                    prop.Source     = _reader.Filename;
                    prop.LineNumber = _reader.LineNumber;
                }
            }

            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                reader.Skip();
            }
            else
            {
                int depth = reader.Depth;

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

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

                    PropertyInformation prop = ElementInformation.Properties [reader.LocalName];
                    if (prop == null || (serializeCollectionKey && !prop.IsKey))
                    {
                        if (!OnDeserializeUnrecognizedElement(reader.LocalName, reader))
                        {
                            if (prop == null)
                            {
                                ConfigurationElementCollection c = GetDefaultCollection();
                                if (c != null && c.OnDeserializeUnrecognizedElement(reader.LocalName, reader))
                                {
                                    continue;
                                }
                            }
                            throw new ConfigurationErrorsException("Unrecognized element '" + reader.LocalName + "'.", reader);
                        }
                        continue;
                    }

                    if (!prop.IsElement)
                    {
                        throw new ConfigurationErrorsException("Property '" + prop.Name + "' is not a ConfigurationElement.");
                    }

                    if (readProps.Contains(prop))
                    {
                        throw new ConfigurationErrorsException("The element <" + prop.Name + "> may only appear once in this section.", reader);
                    }

                    ConfigurationElement val = (ConfigurationElement)prop.Value;
                    val.DeserializeElement(reader, serializeCollectionKey);
                    readProps [prop] = prop.Name;

                    if (depth == reader.Depth)
                    {
                        reader.Read();
                    }
                } while (depth < reader.Depth);
            }

            modified = false;

            foreach (PropertyInformation prop in ElementInformation.Properties)
            {
                if (!String.IsNullOrEmpty(prop.Name) && prop.IsRequired && !readProps.ContainsKey(prop))
                {
                    PropertyInformation p = ElementInformation.Properties [prop.Name];
                    if (p == null)
                    {
                        object val = OnRequiredPropertyNotFound(prop.Name);
                        if (!object.Equals(val, prop.DefaultValue))
                        {
                            prop.Value      = val;
                            prop.IsModified = false;
                        }
                    }
                }
            }

            PostDeserialize();
        }
Пример #13
0
		public Configuration OpenConfiguration ()
		{
			if (configuration == null) {
				if (!parentResolved) {
					Configuration parentFile = parent.GetParentWithFile ();
					if (parentFile != null) {
						string parentRelativePath = parent.ConfigHost.GetConfigPathFromLocationSubPath (parent.LocationConfigPath, path);
						parent = parentFile.FindLocationConfiguration (parentRelativePath, parent);
					}
				}
				
				configuration = new Configuration (parent, path);
				using (XmlTextReader tr = new ConfigXmlTextReader (new StringReader (xmlContent), path))
					configuration.ReadData (tr, allowOverride);

				xmlContent = null;
			}
			return configuration;
		}