ConfigHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // System.Configuration.ConfigHelper
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System.Collections;
  10. using System.Collections.Specialized;
  11. using System.Xml;
  12. namespace System.Configuration
  13. {
  14. class ConfigHelper
  15. {
  16. class CollectionWrapper
  17. {
  18. IDictionary dict;
  19. NameValueCollection collection;
  20. bool isDict;
  21. public CollectionWrapper (IDictionary dict)
  22. {
  23. this.dict = dict;
  24. isDict = true;
  25. }
  26. public CollectionWrapper (NameValueCollection collection)
  27. {
  28. this.collection = collection;
  29. isDict = false;
  30. }
  31. public void Remove (string s)
  32. {
  33. if (isDict)
  34. dict.Remove (s);
  35. else
  36. collection.Remove (s);
  37. }
  38. public void Clear ()
  39. {
  40. if (isDict)
  41. dict.Clear ();
  42. else
  43. collection.Clear ();
  44. }
  45. public string this [string key]
  46. {
  47. set {
  48. if (isDict)
  49. dict [key] = value;
  50. else
  51. collection [key] = value;
  52. }
  53. }
  54. public object UnWrap ()
  55. {
  56. if (isDict)
  57. return dict;
  58. else
  59. return collection;
  60. }
  61. }
  62. internal static IDictionary GetDictionary (IDictionary prev,
  63. XmlNode region,
  64. string nameAtt,
  65. string valueAtt)
  66. {
  67. Hashtable hash;
  68. if (prev == null)
  69. hash = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  70. CaseInsensitiveComparer.Default);
  71. else {
  72. Hashtable aux = (Hashtable) prev;
  73. hash = (Hashtable) aux.Clone ();
  74. }
  75. CollectionWrapper result = new CollectionWrapper (hash);
  76. result = GoGetThem (result, region, nameAtt, valueAtt);
  77. if (result == null)
  78. return null;
  79. return result.UnWrap () as IDictionary;
  80. }
  81. internal static NameValueCollection GetNameValueCollection (NameValueCollection prev,
  82. XmlNode region,
  83. string nameAtt,
  84. string valueAtt)
  85. {
  86. NameValueCollection coll =
  87. new NameValueCollection (CaseInsensitiveHashCodeProvider.Default,
  88. CaseInsensitiveComparer.Default);
  89. if (prev != null)
  90. coll.Add (prev);
  91. CollectionWrapper result = new CollectionWrapper (coll);
  92. result = GoGetThem (result, region, nameAtt, valueAtt);
  93. if (result == null)
  94. return null;
  95. return result.UnWrap () as NameValueCollection;
  96. }
  97. private static CollectionWrapper GoGetThem (CollectionWrapper result,
  98. XmlNode region,
  99. string nameAtt,
  100. string valueAtt)
  101. {
  102. if (region.Attributes != null && region.Attributes.Count != 0)
  103. throw new ConfigurationException ("Unknown attribute", region);
  104. XmlNode keyNode;
  105. XmlNode valueNode;
  106. XmlNodeList childs = region.ChildNodes;
  107. foreach (XmlNode node in childs) {
  108. XmlNodeType ntype = node.NodeType;
  109. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  110. continue;
  111. if (ntype != XmlNodeType.Element)
  112. throw new ConfigurationException ("Only XmlElement allowed", node);
  113. string nodeName = node.Name;
  114. if (nodeName == "clear") {
  115. if (node.Attributes != null && node.Attributes.Count != 0)
  116. throw new ConfigurationException ("Unknown attribute", node);
  117. result.Clear ();
  118. } else if (nodeName == "remove") {
  119. keyNode = null;
  120. if (node.Attributes != null)
  121. keyNode = node.Attributes.RemoveNamedItem (nameAtt);
  122. if (keyNode == null)
  123. throw new ConfigurationException ("Required attribute not found",
  124. node);
  125. if (keyNode.Value == String.Empty)
  126. throw new ConfigurationException ("Required attribute is empty",
  127. node);
  128. if (node.Attributes.Count != 0)
  129. throw new ConfigurationException ("Unknown attribute", node);
  130. result.Remove (keyNode.Value);
  131. } else if (nodeName == "add") {
  132. keyNode = null;
  133. if (node.Attributes != null)
  134. keyNode = node.Attributes.RemoveNamedItem (nameAtt);
  135. if (keyNode == null)
  136. throw new ConfigurationException ("Required attribute not found",
  137. node);
  138. if (keyNode.Value == String.Empty)
  139. throw new ConfigurationException ("Required attribute is empty",
  140. node);
  141. valueNode = node.Attributes.RemoveNamedItem (valueAtt);
  142. if (valueNode == null)
  143. throw new ConfigurationException ("Required attribute not found",
  144. node);
  145. if (node.Attributes.Count != 0)
  146. throw new ConfigurationException ("Unknown attribute", node);
  147. result [keyNode.Value] = valueNode.Value;
  148. } else {
  149. throw new ConfigurationException ("Unknown element", node);
  150. }
  151. }
  152. return result;
  153. }
  154. }
  155. }