NameValueSectionHandler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // System.Configuration.NameValueSectionHandler.cs
  3. //
  4. // Author:
  5. // Christopher Podurgiel ([email protected])
  6. //
  7. // (C) Chris Podurgiel
  8. //
  9. using System;
  10. using System.Xml;
  11. using System.Collections.Specialized;
  12. namespace System.Configuration
  13. {
  14. /// <summary>
  15. /// Summary description for NameValueSectionHandler.
  16. /// </summary>
  17. public class NameValueSectionHandler
  18. {
  19. private static string keyName;
  20. private static string valueName;
  21. private static NameValueCollection settingsCollection;
  22. /// <summary>
  23. /// NameValueSectionHandler Constructor
  24. /// </summary>
  25. public NameValueSectionHandler()
  26. {
  27. //Set Default Values.
  28. keyName = "key";
  29. valueName = "value";
  30. settingsCollection = new NameValueCollection();
  31. }
  32. /// <summary>
  33. /// Creates a new configuration handler and adds the specified configuration object to the collection.
  34. /// </summary>
  35. /// <param name="parent">Composed from the configuration settings in a corresponding parent configuration section.</param>
  36. /// <param name="context">Provides access to the virtual path for which the configuration section handler computes configuration values. Normally this parameter is reserved and is null.</param>
  37. /// <param name="section">The XML node that contains the configuration information to be handled. section provides direct access to the XML contents of the configuration section.</param>
  38. /// <returns></returns>
  39. [MonoTODO]
  40. public object Create(object parent, object context, XmlNode section)
  41. {
  42. //FIXME: I'm not quite sure how to implement 'parent' or 'context'.
  43. //Get all of the ChildNodes in the XML section.
  44. XmlNodeList childNodeList = section.ChildNodes;
  45. //loop throught the ChildNodes
  46. for (int i=0; i < childNodeList.Count; i++)
  47. {
  48. XmlNode childNode = childNodeList[i];
  49. //if the name of this childNode is not 'add' then throw a ConfigurationException.
  50. if(childNode.Name != "add")
  51. {
  52. throw (new ConfigurationException("Unrecognized element"));
  53. }
  54. //Get the attributes for the childNode
  55. XmlAttributeCollection xmlAttributes = childNode.Attributes;
  56. //Get the key and value Attributes by their Name
  57. XmlAttribute keyAttribute = xmlAttributes[keyName];
  58. XmlAttribute valueAttribute = xmlAttributes[valueName];
  59. //Add this Key/Value Pair to the collection
  60. settingsCollection.Add(keyAttribute.Value, valueAttribute.Value);
  61. }
  62. //FIXME: Something is missing here. MS's version of this method returns a System.Configuration.ReadOnlyNameValueCollection type,
  63. //this class id not documented ANYWHERE. This method is curretly returning a NameValueCollection, but it should be ReadOnly.
  64. return settingsCollection;
  65. }
  66. /// <summary>
  67. /// Gets the name of the key in the key-value pair.
  68. /// </summary>
  69. protected virtual string KeyAttributeName
  70. {
  71. get
  72. {
  73. return keyName;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets the value for the key in the key-value pair.
  78. /// </summary>
  79. protected virtual string ValueAttributeName
  80. {
  81. get
  82. {
  83. return valueName;
  84. }
  85. }
  86. }
  87. }