/
DictionarySectionHandler.cs
104 lines (88 loc) · 3.48 KB
/
DictionarySectionHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Xml;
namespace System.Configuration
{
/// <summary>
/// Simple dictionary config factory
/// config is a dictionary mapping key->value
///
/// <add key="name" value="text"> sets key=text
/// <remove key="name"> removes the definition of key
/// <clear> removes all definitions
///
/// </summary>
public class DictionarySectionHandler : IConfigurationSectionHandler
{
/// <summary>
/// Given a partially composed config object (possibly null)
/// and some input from the config system, return a
/// further partially composed config object
/// </summary>
public virtual object Create(object parent, object context, XmlNode section)
{
Hashtable res;
// start res off as a shallow clone of the parent
if (parent == null)
res = new Hashtable(StringComparer.OrdinalIgnoreCase);
else
res = (Hashtable)((Hashtable)parent).Clone();
// process XML
HandlerBase.CheckForUnrecognizedAttributes(section);
foreach (XmlNode child in section.ChildNodes)
{
// skip whitespace and comments, throws if non-element
if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child))
continue;
// handle <add>, <remove>, <clear> tags
if (child.Name == "add")
{
HandlerBase.CheckForChildNodes(child);
string key = HandlerBase.RemoveRequiredAttribute(child, KeyAttributeName);
string value = ValueRequired ?
HandlerBase.RemoveRequiredAttribute(child, ValueAttributeName) :
HandlerBase.RemoveAttribute(child, ValueAttributeName);
HandlerBase.CheckForUnrecognizedAttributes(child);
res[key] = value ?? "";
}
else if (child.Name == "remove")
{
HandlerBase.CheckForChildNodes(child);
string key = HandlerBase.RemoveRequiredAttribute(child, KeyAttributeName);
HandlerBase.CheckForUnrecognizedAttributes(child);
res.Remove(key);
}
else if (child.Name.Equals("clear"))
{
HandlerBase.CheckForChildNodes(child);
HandlerBase.CheckForUnrecognizedAttributes(child);
res.Clear();
}
else
{
HandlerBase.ThrowUnrecognizedElement(child);
}
}
return res;
}
/// <summary>
/// Make the name of the key attribute configurable by derived classes.
/// </summary>
protected virtual string KeyAttributeName
{
get { return "key"; }
}
/// <summary>
/// Make the name of the value attribute configurable by derived classes.
/// </summary>
protected virtual string ValueAttributeName
{
get { return "value"; }
}
internal virtual bool ValueRequired
{
get { return false; }
}
}
}