[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Dotgnu-pnet-commits] pnetlib/System/Configuration AppSettingsReader.cs,
From: |
Rhys Weatherley <address@hidden> |
Subject: |
[Dotgnu-pnet-commits] pnetlib/System/Configuration AppSettingsReader.cs, 1.1, 1.2 ConfigurationSettings.cs, 1.3, 1.4 DictionarySectionHandler.cs, 1.3, 1.4 NameValueFileSectionHandler.cs, 1.3, 1.4 NameValueSectionHandler.cs, 1.3, 1.4 SingleTagSectionHandler.cs, 1.4, 1.5 |
Date: |
Sun, 23 Nov 2003 05:14:31 +0000 |
Update of /cvsroot/dotgnu-pnet/pnetlib/System/Configuration
In directory subversions:/tmp/cvs-serv17645/System/Configuration
Modified Files:
AppSettingsReader.cs ConfigurationSettings.cs
DictionarySectionHandler.cs NameValueFileSectionHandler.cs
NameValueSectionHandler.cs SingleTagSectionHandler.cs
Log Message:
Implement most of the "System.Configuration" namespace.
Index: SingleTagSectionHandler.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/System/Configuration/SingleTagSectionHandler.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** SingleTagSectionHandler.cs 8 Oct 2003 15:29:25 -0000 1.4
--- SingleTagSectionHandler.cs 23 Nov 2003 05:14:29 -0000 1.5
***************
*** 26,29 ****
--- 26,30 ----
using System;
+ using System.Collections;
#if SECOND_PASS
using System.Xml;
***************
*** 38,46 ****
// Create a configuration object for a section.
- [TODO]
public Object Create(Object parent, Object configContext, XmlNode
section)
{
! // TODO
! return null;
}
--- 39,70 ----
// Create a configuration object for a section.
public Object Create(Object parent, Object configContext, XmlNode
section)
{
! // The section must not have child nodes.
! if(section.HasChildNodes)
! {
! throw new ConfigurationException
! (S._("Config_HasChildNodes"),
section.FirstChild);
! }
!
! // Create the hash table to hold the results.
! Hashtable hash;
! if(parent != null)
! {
! hash = new Hashtable((Hashtable)parent);
! }
! else
! {
! hash = new Hashtable();
! }
!
! // Add configuration information from the
specified section.
! foreach(XmlAttribute attr in section.Attributes)
! {
! hash[attr.Name] = attr.Value;
! }
!
! // Return the result hash table to the caller.
! return hash;
}
Index: DictionarySectionHandler.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/System/Configuration/DictionarySectionHandler.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** DictionarySectionHandler.cs 8 Oct 2003 15:29:25 -0000 1.3
--- DictionarySectionHandler.cs 23 Nov 2003 05:14:29 -0000 1.4
***************
*** 26,29 ****
--- 26,31 ----
using System;
+ using System.Collections;
+ using System.Collections.Specialized;
#if SECOND_PASS
using System.Xml;
***************
*** 56,64 ****
// Create a configuration object for a section.
- [TODO]
public Object Create(Object parent, Object configContext, XmlNode
section)
{
! // TODO
! return null;
}
--- 58,133 ----
// Create a configuration object for a section.
public Object Create(Object parent, Object configContext, XmlNode
section)
{
! Hashtable coll;
! String key, value;
!
! // Create the name/value collection for the
result.
! if(parent != null)
! {
! coll = new Hashtable((Hashtable)parent);
! }
! else
! {
! coll =
CollectionsUtil.CreateCaseInsensitiveHashtable();
! }
!
! // Must not be any attributes remaining on the
section node.
! if(section.Attributes.Count != 0)
! {
! throw new ConfigurationException
!
(S._("Config_UnrecognizedAttribute"), section);
! }
!
! // Process the child nodes.
! foreach(XmlNode node in section.ChildNodes)
! {
! // Ignore comments and white space.
! if(node.NodeType == XmlNodeType.Comment
||
! node.NodeType ==
XmlNodeType.Whitespace)
! {
! continue;
! }
!
! // Must be an element node.
! if(node.NodeType != XmlNodeType.Element)
! {
! throw new ConfigurationException
!
(S._("Config_MustBeElement"), node);
! }
!
! // Process "add", "remove", and "clear"
child tags.
! if(node.Name == "add")
! {
! key =
NameValueSectionHandler.GetRequiredAttribute
! (node,
KeyAttributeName, 1);
! value =
NameValueSectionHandler.GetAttribute
! (node,
ValueAttributeName, 0);
! coll[key] = value;
! }
! else if(node.Name == "remove")
! {
! key =
NameValueSectionHandler.GetRequiredAttribute
! (node,
KeyAttributeName, 0);
! coll.Remove(key);
! }
! else if(node.Name == "clear")
! {
! if(node.Attributes.Count != 0)
! {
! throw new
ConfigurationException
!
(S._("Config_UnrecognizedAttribute"), node);
! }
! coll.Clear();
! }
! else
! {
! throw new ConfigurationException
!
(S._("Config_NotRecognized"), node);
! }
! }
!
! // Return the final collection
! return coll;
}
Index: NameValueFileSectionHandler.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/System/Configuration/NameValueFileSectionHandler.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** NameValueFileSectionHandler.cs 8 Oct 2003 15:29:25 -0000 1.3
--- NameValueFileSectionHandler.cs 23 Nov 2003 05:14:29 -0000 1.4
***************
*** 26,29 ****
--- 26,30 ----
using System;
+ using System.IO;
#if SECOND_PASS
using System.Xml;
***************
*** 38,46 ****
// Create a configuration object for a section.
- [TODO]
public Object Create(Object parent, Object configContext, XmlNode
section)
{
! // TODO
! return null;
}
--- 39,98 ----
// Create a configuration object for a section.
public Object Create(Object parent, Object configContext, XmlNode
section)
{
! Object coll;
! String filename;
! ConfigXmlDocument doc;
!
! // Remove the "file" attribute from the section.
! XmlAttribute file =
!
(section.Attributes.RemoveNamedItem("file")
! as XmlAttribute);
!
! // Load the main name/value pairs from the
children.
! coll = NameValueSectionHandler.Create
! (parent, section, "key", "value");
!
! // Process the "file" attribute, if it is
present.
! if(file != null && file.Value != String.Empty)
! {
! // Combine the base filename with the
new filename.
! filename = file.Value;
! if(file is IConfigXmlNode)
! {
! filename = Path.Combine
! (Path.GetDirectoryName
!
(((IConfigXmlNode)file).Filename), filename);
! }
! if(File.Exists(filename))
! {
! // Load the new configuration
file into memory.
! doc = new ConfigXmlDocument();
! try
! {
! doc.Load(filename);
! }
! catch(XmlException xe)
! {
! throw new
ConfigurationException
! (xe.Message,
xe, filename, xe.LineNumber);
! }
!
! // The document root must match
the section name.
! if(doc.DocumentElement.Name !=
section.Name)
! {
! throw new
ConfigurationException
!
(S._("Config_DocNameMatch"),
!
doc.DocumentElement);
! }
!
! // Load the contents of the
file.
! coll =
NameValueSectionHandler.Create
! (coll,
doc.DocumentElement, "key", "value");
! }
! }
!
! // Return the final collection to the caller.
! return coll;
}
Index: NameValueSectionHandler.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/System/Configuration/NameValueSectionHandler.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** NameValueSectionHandler.cs 8 Oct 2003 15:29:25 -0000 1.3
--- NameValueSectionHandler.cs 23 Nov 2003 05:14:29 -0000 1.4
***************
*** 26,29 ****
--- 26,30 ----
using System;
+ using System.Collections.Specialized;
#if SECOND_PASS
using System.Xml;
***************
*** 55,64 ****
#if SECOND_PASS
// Create a configuration object for a section.
- [TODO]
public Object Create(Object parent, Object configContext, XmlNode
section)
{
! // TODO
! return null;
}
--- 56,177 ----
#if SECOND_PASS
+ // Get a required attribute and throw an exception if there are others.
+ internal static String GetRequiredAttribute
+ (XmlNode node, String name, int expected)
+ {
+ XmlAttribute attr =
+ (node.Attributes.RemoveNamedItem(name)
as XmlAttribute);
+ if(attr == null || attr.Value == String.Empty)
+ {
+ throw new ConfigurationException
+
(S._("Config_RequiredAttribute"), node);
+ }
+ if(node.Attributes.Count != expected)
+ {
+ throw new ConfigurationException
+
(S._("Config_UnrecognizedAttribute"), node);
+ }
+ return attr.Value;
+ }
+
+ // Get an attribute and throw an exception if there are others.
+ internal static String GetAttribute
+ (XmlNode node, String name, int expected)
+ {
+ XmlAttribute attr =
+ (node.Attributes.RemoveNamedItem(name)
as XmlAttribute);
+ if(node.Attributes.Count != expected)
+ {
+ throw new ConfigurationException
+
(S._("Config_UnrecognizedAttribute"), node);
+ }
+ if(attr != null)
+ {
+ return attr.Value;
+ }
+ else
+ {
+ return String.Empty;
+ }
+ }
+
// Create a configuration object for a section.
public Object Create(Object parent, Object configContext, XmlNode
section)
{
! return Create(parent, section, KeyAttributeName,
! ValueAttributeName);
! }
! internal static Object Create(Object parent, XmlNode section,
! String
keyName, String valueName)
! {
! ReadOnlyNameValueCollection coll;
! String key, value;
!
! // Create the name/value collection for the
result.
! if(parent != null)
! {
! coll = new ReadOnlyNameValueCollection
! ((NameValueCollection)parent);
! }
! else
! {
! coll = new
ReadOnlyNameValueCollection();
! }
!
! // Must not be any attributes remaining on the
section node.
! if(section.Attributes.Count != 0)
! {
! throw new ConfigurationException
!
(S._("Config_UnrecognizedAttribute"), section);
! }
!
! // Process the child nodes.
! foreach(XmlNode node in section.ChildNodes)
! {
! // Ignore comments and white space.
! if(node.NodeType == XmlNodeType.Comment
||
! node.NodeType ==
XmlNodeType.Whitespace)
! {
! continue;
! }
!
! // Must be an element node.
! if(node.NodeType != XmlNodeType.Element)
! {
! throw new ConfigurationException
!
(S._("Config_MustBeElement"), node);
! }
!
! // Process "add", "remove", and "clear"
child tags.
! if(node.Name == "add")
! {
! key =
GetRequiredAttribute(node, keyName, 1);
! value =
GetRequiredAttribute(node, valueName, 0);
! coll[key] = value;
! }
! else if(node.Name == "remove")
! {
! key =
GetRequiredAttribute(node, keyName, 0);
! coll.Remove(key);
! }
! else if(node.Name == "clear")
! {
! if(node.Attributes.Count != 0)
! {
! throw new
ConfigurationException
!
(S._("Config_UnrecognizedAttribute"), node);
! }
! coll.Clear();
! }
! else
! {
! throw new ConfigurationException
!
(S._("Config_NotRecognized"), node);
! }
! }
!
! // Make the collection read-only and return it.
! coll.MakeReadOnly();
! return coll;
}
Index: AppSettingsReader.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/System/Configuration/AppSettingsReader.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** AppSettingsReader.cs 25 May 2003 07:13:43 -0000 1.1
--- AppSettingsReader.cs 23 Nov 2003 05:14:29 -0000 1.2
***************
*** 26,37 ****
using System;
public class AppSettingsReader
{
// Constructor.
! public AppSettingsReader() {}
// Get a specific application setting value.
- [TODO]
public Object GetValue(String key, Type type)
{
--- 26,44 ----
using System;
+ using System.Collections.Specialized;
+ using System.Globalization;
public class AppSettingsReader
{
+ // Internal state.
+ private NameValueCollection coll;
+
// Constructor.
! public AppSettingsReader()
! {
! coll = ConfigurationSettings.AppSettings;
! }
// Get a specific application setting value.
public Object GetValue(String key, Type type)
{
***************
*** 44,49 ****
throw new ArgumentNullException("type");
}
! // TODO
! return null;
}
--- 51,77 ----
throw new ArgumentNullException("type");
}
! String value = coll[key];
! if(value != null)
! {
! if(String.Compare(value, "None", true,
!
CultureInfo.InvariantCulture) == 0)
! {
! return null;
! }
! try
! {
! return
Convert.ChangeType(value, type);
! }
! catch(Exception)
! {
! throw new
InvalidOperationException
!
(S._("Config_CannotConvert"));
! }
! }
! else
! {
! throw new InvalidOperationException
! (S._("Config_KeyNotPresent"));
! }
}
Index: ConfigurationSettings.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/System/Configuration/ConfigurationSettings.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** ConfigurationSettings.cs 8 Oct 2003 15:29:25 -0000 1.3
--- ConfigurationSettings.cs 23 Nov 2003 05:14:29 -0000 1.4
***************
*** 3,7 ****
* "System.Configuration.ConfigurationSettings" interface.
*
! * Copyright (C) 2002 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
--- 3,7 ----
* "System.Configuration.ConfigurationSettings" interface.
*
! * Copyright (C) 2002, 2003 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
***************
*** 35,38 ****
--- 35,40 ----
{
// Internal state.
+ private static IConfigurationSystem configSystem;
+ private static Exception configError;
// Constructor - cannot be created by external entities.
***************
*** 40,48 ****
// Get a configuration object for a specific section.
- [TODO]
public static Object GetConfig(String sectionName)
{
! // TODO
! return null;
}
--- 42,76 ----
// Get a configuration object for a specific section.
public static Object GetConfig(String sectionName)
{
! // Make sure that the configuration system is
initialized.
! IConfigurationSystem system;
! lock(typeof(ConfigurationSettings))
! {
! if(configSystem != null)
! {
! system = configSystem;
! }
! else if(configError != null)
! {
! throw configError;
! }
! else
! {
! configSystem = new
BuiltinConfigurationSystem();
! try
! {
! configSystem.Init();
! }
! catch(Exception e)
! {
! configError = e;
! throw;
! }
! }
! }
!
! // Look up the specified configuration item.
! return system.GetConfig(sectionName);
}
***************
*** 52,61 ****
get
{
! RONameValueCollection settings;
! settings = (RONameValueCollection)
(GetConfig("appSettings"));
if(settings == null)
{
! settings = new
RONameValueCollection();
}
return settings;
--- 80,90 ----
get
{
! ReadOnlyNameValueCollection settings;
! settings = (ReadOnlyNameValueCollection)
(GetConfig("appSettings"));
if(settings == null)
{
! settings = new
ReadOnlyNameValueCollection();
! settings.MakeReadOnly();
}
return settings;
***************
*** 63,78 ****
}
! // Read-only subclass of "NameValueCollection".
! internal sealed class RONameValueCollection : NameValueCollection
{
// Constructor.
! public RONameValueCollection()
! : base(new CaseInsensitiveHashCodeProvider(),
! new CaseInsensitiveComparer())
! {
! IsReadOnly = true;
! }
! }; // class RONameValueCollection
}; // class ConfigurationSettings
--- 92,115 ----
}
! // The builtin configuration system handler.
! private class BuiltinConfigurationSystem : IConfigurationSystem
{
// Constructor.
! public BuiltinConfigurationSystem() {}
! // Initialize the configuration system.
! public void Init()
! {
! // TODO
! }
!
! // Get the object for a specific configuration key.
! public Object GetConfig(String configKey)
! {
! // TODO
! return null;
! }
!
! }; // class BuiltinConfigurationSystem
}; // class ConfigurationSettings
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Dotgnu-pnet-commits] pnetlib/System/Configuration AppSettingsReader.cs, 1.1, 1.2 ConfigurationSettings.cs, 1.3, 1.4 DictionarySectionHandler.cs, 1.3, 1.4 NameValueFileSectionHandler.cs, 1.3, 1.4 NameValueSectionHandler.cs, 1.3, 1.4 SingleTagSectionHandler.cs, 1.4, 1.5,
Rhys Weatherley <address@hidden> <=