Пример #1
0
 public void ReadSunnyDay()
 {
     using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(SunnyDayXml)))
     {
         NameValueCollection props = ConfigurationReader.Read(new InputStreamResource(stream, ""), "foo");
         Assert.IsNotNull(props,
                          "Failed to read in any properties at all (props is null).");
         Assert.AreEqual(2, props.Count,
                         "Wrong number of properties read in.");
         Assert.AreEqual("kiley",
                         props["rilo"],
                         "Wrong value for second property");
         Assert.AreEqual("lewis",
                         props["jenny"],
                         "Wrong value for second property");
     }
 }
Пример #2
0
        public void ReadWithNoConfigSectionSectionDefaultsToNameValueSectionHandler()
        {
            const string NoConfigSectionXml = @"<?xml version='1.0' encoding='UTF-8' ?>
<configuration>
    <foo>
        <add key='rilo' value='kiley'/>
        <add key='jenny' value='lewis'/>
    </foo>
</configuration>";

            using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(NoConfigSectionXml)))
            {
                NameValueCollection props
                    = ConfigurationReader.Read(new InputStreamResource(stream, ""), "foo", null);
                Assert.IsNotNull(props, "Failed to read in any properties at all (props is null).");
                Assert.AreEqual(2, props.Count, "Wrong number of properties read in.");
                Assert.AreEqual("kiley", props["rilo"], "Wrong value for second property");
                Assert.AreEqual("lewis", props["jenny"], "Wrong value for second property");
            }
        }
Пример #3
0
 /// <summary>
 /// Reads the specified configuration section into a
 /// <see cref="System.Collections.Specialized.NameValueCollection"/>.
 /// </summary>
 /// <param name="resource">The resource to read.</param>
 /// <param name="configSection">The section name.</param>
 /// <returns>
 /// A newly populated
 /// <see cref="System.Collections.Specialized.NameValueCollection"/>.
 /// </returns>
 /// <exception cref="System.IO.IOException">
 /// If any errors are encountered while attempting to open a stream
 /// from the supplied <paramref name="resource"/>.
 /// </exception>
 /// <exception cref="System.Xml.XmlException">
 /// If any errors are encountered while loading or reading (this only applies to
 /// v1.1 and greater of the .NET Framework) the actual XML.
 /// </exception>
 /// <exception cref="System.Exception">
 /// If any errors are encountered while loading or reading (this only applies to
 /// v1.0 of the .NET Framework).
 /// </exception>
 /// <exception cref="Spring.Objects.FatalObjectException">
 /// If the configuration section was otherwise invalid.
 /// </exception>
 public static NameValueCollection Read(IResource resource, string configSection)
 {
     return(ConfigurationReader.Read(resource, configSection, new NameValueCollection()));
 }
Пример #4
0
 /// <summary>
 /// Reads the specified configuration section into the supplied
 /// <see cref="System.Collections.Specialized.NameValueCollection"/>.
 /// </summary>
 /// <param name="resource">The resource to read.</param>
 /// <param name="configSection">The section name.</param>
 /// <param name="properties">
 /// The collection that is to be populated. May be
 /// <see langword="null"/>.
 /// </param>
 /// <returns>
 /// A newly populated
 /// <see cref="System.Collections.Specialized.NameValueCollection"/>.
 /// </returns>
 /// <exception cref="System.IO.IOException">
 /// If any errors are encountered while attempting to open a stream
 /// from the supplied <paramref name="resource"/>.
 /// </exception>
 /// <exception cref="System.Xml.XmlException">
 /// If any errors are encountered while loading or reading (this only applies to
 /// v1.1 and greater of the .NET Framework) the actual XML.
 /// </exception>
 /// <exception cref="System.Exception">
 /// If any errors are encountered while loading or reading (this only applies to
 /// v1.0 of the .NET Framework).
 /// </exception>
 /// <exception cref="Spring.Objects.FatalObjectException">
 /// If the configuration section was otherwise invalid.
 /// </exception>
 public static NameValueCollection Read(
     IResource resource, string configSection, NameValueCollection properties)
 {
     return(ConfigurationReader.Read(resource, configSection, properties, true));
 }
Пример #5
0
        /// <summary>
        /// Loads properties from the configuration sections
        /// specified in <see cref="ConfigSections"/> into <paramref name="properties"/>.
        /// </summary>
        /// <param name="properties">The <see cref="NameValueCollection"/> instance to be filled with properties.</param>
        protected virtual void LoadProperties(NameValueCollection properties)
        {
            string[] configSections = ConfigSections;
            if (_locations != null)
            {
                ValidateConfigSections(configSections);
                bool usingMultipleConfigSections = configSections.Length > 1;
                int  sectionNameIndex            = 0;
                foreach (IResource resource in _locations)
                {
                    #region Instrumentation

                    if (_log.IsDebugEnabled)
                    {
                        _log.Debug(string.Format(
                                       CultureInfo.InvariantCulture,
                                       "Loading configuration from '{0}'.", resource));
                    }

                    #endregion

                    string sectionName = configSections[sectionNameIndex];
                    if (resource is ConfigSectionResource)
                    {
                        ConfigurationReader.PopulateFromAppConfig(
                            properties, sectionName, _lastLocationOverrides);
                    }
                    else
                    {
                        if (resource.Exists)
                        {
                            ConfigurationReader.Read(
                                resource, sectionName, properties, _lastLocationOverrides);
                        }
                        else
                        {
                            string errorMessage = "Could not load configuration from " + resource;
                            if (_ignoreResourceNotFound)
                            {
                                #region Instrumentation

                                if (_log.IsWarnEnabled)
                                {
                                    _log.Warn(errorMessage);
                                }

                                #endregion
                            }
                            else
                            {
                                throw new ObjectInitializationException(errorMessage);
                            }
                        }
                    }
                    if (usingMultipleConfigSections)
                    {
                        ++sectionNameIndex;
                    }
                }
            }
            else
            {
                foreach (string sectionName in configSections)
                {
                    ConfigurationReader.PopulateFromAppConfig(
                        properties, sectionName, _lastLocationOverrides);
                }
            }
        }