NameValueSectionHandler.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(childNode.Name == "#text")
  50. {
  51. string text = childNode.Value.Trim (' ', '\t', '\n');
  52. if (text == "")
  53. continue;
  54. } else if(childNode.Name == "#whitespace")
  55. continue;
  56. //if the name of this childNode is not 'add' then throw a ConfigurationException.
  57. if(childNode.Name != "add")
  58. {
  59. throw (new ConfigurationException("Unrecognized element"));
  60. }
  61. //Get the attributes for the childNode
  62. XmlAttributeCollection xmlAttributes = childNode.Attributes;
  63. //Get the key and value Attributes by their Name
  64. XmlAttribute keyAttribute = xmlAttributes[keyName];
  65. XmlAttribute valueAttribute = xmlAttributes[valueName];
  66. //Add this Key/Value Pair to the collection
  67. settingsCollection.Add(keyAttribute.Value, valueAttribute.Value);
  68. }
  69. //FIXME: Something is missing here. MS's version of this method returns a System.Configuration.ReadOnlyNameValueCollection type,
  70. //this class id not documented ANYWHERE. This method is curretly returning a NameValueCollection, but it should be ReadOnly.
  71. return settingsCollection;
  72. }
  73. /// <summary>
  74. /// Gets the name of the key in the key-value pair.
  75. /// </summary>
  76. protected virtual string KeyAttributeName
  77. {
  78. get
  79. {
  80. return keyName;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the value for the key in the key-value pair.
  85. /// </summary>
  86. protected virtual string ValueAttributeName
  87. {
  88. get
  89. {
  90. return valueName;
  91. }
  92. }
  93. }
  94. }