/// <summary> /// Returns the section from the specified resource with the given section name. Use <paramref name="defaultConfigurationSectionHandlerType"/> /// in case no section handler is specified. /// </summary> public static object GetSection(IResource resource, string configSectionName, Type defaultConfigurationSectionHandlerType) { using (Stream istm = resource.InputStream) { ConfigXmlDocument doc = new ConfigXmlDocument(); doc.Load(istm); return(GetSectionFromXmlDocument(doc, configSectionName, defaultConfigurationSectionHandlerType)); } }
public void LoadTextReaderStoresTextPosition() { ConfigXmlDocument xmlDoc = new ConfigXmlDocument(); Stream istm = TestResourceLoader.GetStream( this, "_SampleConfig.xml" ); xmlDoc.Load( "MYXML", new StreamReader(istm) ); ITextPosition pos = ((ITextPosition)xmlDoc.SelectSingleNode("//property")); Assert.AreEqual("MYXML", pos.Filename); Assert.AreEqual(5, pos.LineNumber); Assert.AreEqual(14, pos.LinePosition); }
public void LoadXmlStoresTextPosition() { ConfigXmlDocument xmlDoc = new ConfigXmlDocument(); string xmlText = TestResourceLoader.GetText( this, "_SampleConfig.xml" ); xmlDoc.LoadXml( "MYXML", xmlText ); ITextPosition pos = ((ITextPosition)xmlDoc.SelectSingleNode("//property")); Assert.AreEqual("MYXML", pos.Filename); Assert.AreEqual(5, pos.LineNumber); Assert.AreEqual(14, pos.LinePosition); }
public void CanConfigFilenameAndLine() { ConfigXmlDocument xmlDoc = new ConfigXmlDocument(); Stream istm = TestResourceLoader.GetStream( this, "_SampleConfig.xml" ); xmlDoc.Load( "MYXML", new StreamReader(istm) ); XmlNode node = xmlDoc.SelectSingleNode("//property"); Assert.AreEqual("MYXML", ConfigurationUtils.GetFileName(node) ); Assert.AreEqual(5, ConfigurationUtils.GetLineNumber(node) ); //Assert.AreEqual(14, pos.LinePosition); <- IConfigErrorInfo/IConfigXmlNode do not support LinePosition }
public void CanLoadSchemaImportingOtherSchemaByRelativePath() { string schemaLocation = TestResourceLoader.GetAssemblyResourceUri( this.GetType(), "NamespaceParserRegistryTests_TestSchema.xsd" ); NamespaceParserRegistry.RegisterParser(new TestNamespaceParser(), "http://www.example.com/brief", schemaLocation); XmlReader vr = XmlUtils.CreateValidatingReader( new StringResource( @"<?xml version='1.0' encoding='UTF-8' ?> <brief class='foo' /> ").InputStream, NamespaceParserRegistry.GetSchemas(), null); ConfigXmlDocument newDoc = new ConfigXmlDocument(); newDoc.Load(vr); }
private ConfigSectionResource CreateConfigSectionResource(string filename) { ConfigXmlDocument xmlDoc = new ConfigXmlDocument(); Uri testUri = TestResourceLoader.GetUri(this, filename); xmlDoc.Load("test config section", testUri.AbsoluteUri); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("od", "http://www.springframework.net"); XmlElement configElement = (XmlElement)xmlDoc.SelectSingleNode("//configuration/spring/od:objects", nsmgr); ConfigSectionResource csr = new ConfigSectionResource( configElement); return csr; }
/// <summary> /// Returns the typed section from the specified resource with the given section name /// </summary> public static TResult GetSection <TResult>(IResource resource, string configSectionName) { using (Stream istm = resource.InputStream) { ConfigXmlDocument doc = new ConfigXmlDocument(); doc.Load(istm); object result = GetSectionFromXmlDocument(doc, configSectionName); if (result != null && !(result is TResult)) { throw new ArgumentException(string.Format("evaluating configuration sectoin {0} does not result in an instance of type {1}", configSectionName, typeof(TResult))); } return((TResult)result); } }
/// <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> /// <param name="overrideValues"> /// If a key already exists, is its value to be appended to the current /// value or replaced? /// </param> /// <returns> /// The 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, bool overrideValues) { if (properties == null) { properties = new NameValueCollection(); } Stream stream = null; try { ConfigXmlDocument doc = new ConfigXmlDocument(); stream = resource.InputStream; doc.Load(stream); NameValueCollection newProperties = ReadFromXmlDocument(doc, configSection); if (newProperties != null) { PopulateProperties(overrideValues, properties, newProperties); } } finally { if (stream != null) { try { stream.Close(); } catch (IOException ex) { #region Instrumentation if (_log.IsWarnEnabled) { _log.Warn("Could not close stream from resource " + resource.Description, ex); } #endregion } } } return(properties); }
/// <summary> /// Actually load object definitions from the specified XML file. /// </summary> /// <param name="stream">The input stream to read from.</param> /// <param name="resource">The resource for the XML data.</param> /// <returns></returns> protected virtual int DoLoadObjectDefinitions(Stream stream, IResource resource) { try { // create local copy of data byte[] xmlData = IOUtils.ToByteArray(stream); XmlDocument doc; // loop until no unregistered, wellknown namespaces left while (true) { XmlReader reader = null; try { MemoryStream xmlDataStream = new MemoryStream(xmlData); reader = CreateValidatingReader(xmlDataStream); doc = new ConfigXmlDocument(); doc.Load(reader); break; } catch (RetryParseException) { if (reader != null) reader.Close(); } } return RegisterObjectDefinitions(doc, resource); } catch (XmlException ex) { throw new ObjectDefinitionStoreException(resource.Description, "Line " + ex.LineNumber + " in XML document from " + resource + " is not well formed. " + ex.Message, ex); } catch (XmlSchemaException ex) { throw new ObjectDefinitionStoreException(resource.Description, "Line " + ex.LineNumber + " in XML document from " + resource + " violates the schema. " + ex.Message, ex); } catch (ObjectDefinitionStoreException) { throw; } catch (Exception ex) { throw new ObjectDefinitionStoreException("Unexpected exception parsing XML document from " + resource.Description + "Inner exception message= " + ex.Message, ex); } }