The Labs \ Source Viewer \ SSCLI \ System.Configuration \ DictionarySectionHandler

  1. //------------------------------------------------------------------------------
  2. // <copyright file="DictionarySectionHandler.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Configuration
  16. {
  17.     using System.Collections;
  18.     using System.Collections.Specialized;
  19.     using System.Xml;
  20.     using System.Globalization;
  21.    
  22.     /// <devdoc>
  23.     /// Simple dictionary config factory
  24.     /// config is a dictionary mapping key-&gt;value
  25.     ///
  26.     /// &lt;add key="name" value="text"&gt; sets key=text
  27.     /// &lt;remove key="name"&gt; removes the definition of key
  28.     /// &lt;clear&gt; removes all definitions
  29.     ///
  30.     /// </devdoc>
  31.     public class DictionarySectionHandler : IConfigurationSectionHandler
  32.     {
  33.        
  34.         /// <devdoc>
  35.         /// Given a partially composed config object (possibly null)
  36.         /// and some input from the config system, return a
  37.         /// further partially composed config object
  38.         /// </devdoc>
  39.         public virtual object Create(object parent, object context, XmlNode section)
  40.         {
  41.             Hashtable res;
  42.            
  43.             // start res off as a shallow clone of the parent
  44.            
  45.             if (parent == null)
  46.                 res = new Hashtable(StringComparer.OrdinalIgnoreCase);
  47.             else
  48.                 res = (Hashtable)((Hashtable)parent).Clone();
  49.            
  50.             // process XML
  51.            
  52.             HandlerBase.CheckForUnrecognizedAttributes(section);
  53.            
  54.             foreach (XmlNode child in section.ChildNodes) {
  55.                
  56.                 // skip whitespace and comments, throws if non-element
  57.                 if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child))
  58.                     continue;
  59.                
  60.                 // handle <add>, <remove>, <clear> tags
  61.                 if (child.Name == "add") {
  62.                     HandlerBase.CheckForChildNodes(child);
  63.                     string key = HandlerBase.RemoveRequiredAttribute(child, KeyAttributeName);
  64.                     string value;
  65.                     if (ValueRequired)
  66.                         value = HandlerBase.RemoveRequiredAttribute(child, ValueAttributeName);
  67.                     else
  68.                         value = HandlerBase.RemoveAttribute(child, ValueAttributeName);
  69.                     HandlerBase.CheckForUnrecognizedAttributes(child);
  70.                    
  71.                     if (value == null)
  72.                         value = "";
  73.                    
  74.                     res[key] = value;
  75.                 }
  76.                 else if (child.Name == "remove") {
  77.                     HandlerBase.CheckForChildNodes(child);
  78.                     string key = HandlerBase.RemoveRequiredAttribute(child, KeyAttributeName);
  79.                     HandlerBase.CheckForUnrecognizedAttributes(child);
  80.                    
  81.                     res.Remove(key);
  82.                 }
  83.                 else if (child.Name.Equals("clear")) {
  84.                     HandlerBase.CheckForChildNodes(child);
  85.                     HandlerBase.CheckForUnrecognizedAttributes(child);
  86.                     res.Clear();
  87.                 }
  88.                 else {
  89.                     HandlerBase.ThrowUnrecognizedElement(child);
  90.                 }
  91.             }
  92.            
  93.             return res;
  94.         }
  95.        
  96.         /// <devdoc>
  97.         /// Make the name of the key attribute configurable by derived classes.
  98.         /// </devdoc>
  99.         protected virtual string KeyAttributeName {
  100.             get { return "key"; }
  101.         }
  102.        
  103.         /// <devdoc>
  104.         /// Make the name of the value attribute configurable by derived classes.
  105.         /// </devdoc>
  106.         protected virtual string ValueAttributeName {
  107.             get { return "value"; }
  108.         }
  109.        
  110.         internal virtual bool ValueRequired {
  111.             get { return false; }
  112.         }
  113.     }
  114. }

Developer Fusion