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