ConfigHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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); FIXME: gotta fix NameValueCollection or fill a bug report
  91. foreach (string s in prev.Keys)
  92. coll.Add (s, prev [s]);
  93. }
  94. CollectionWrapper result = new CollectionWrapper (coll);
  95. result = GoGetThem (result, region, nameAtt, valueAtt);
  96. if (result == null)
  97. return null;
  98. return result.UnWrap () as NameValueCollection;
  99. }
  100. private static CollectionWrapper GoGetThem (CollectionWrapper result,
  101. XmlNode region,
  102. string nameAtt,
  103. string valueAtt)
  104. {
  105. if (region.Attributes != null && region.Attributes.Count != 0)
  106. throw new ConfigurationException ("Unknown attribute", region);
  107. XmlNode keyNode;
  108. XmlNode valueNode;
  109. XmlNodeList childs = region.ChildNodes;
  110. foreach (XmlNode node in childs) {
  111. XmlNodeType ntype = node.NodeType;
  112. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  113. continue;
  114. if (ntype != XmlNodeType.Element)
  115. throw new ConfigurationException ("Only XmlElement allowed", node);
  116. string nodeName = node.Name;
  117. if (nodeName == "clear") {
  118. if (node.Attributes != null && node.Attributes.Count != 0)
  119. throw new ConfigurationException ("Unknown attribute", node);
  120. result.Clear ();
  121. } else if (nodeName == "remove") {
  122. keyNode = null;
  123. if (node.Attributes != null)
  124. keyNode = node.Attributes.RemoveNamedItem (nameAtt);
  125. if (keyNode == null)
  126. throw new ConfigurationException ("Required attribute not found",
  127. node);
  128. if (keyNode.Value == String.Empty)
  129. throw new ConfigurationException ("Required attribute is empty",
  130. node);
  131. if (node.Attributes.Count != 0)
  132. throw new ConfigurationException ("Unknown attribute", node);
  133. result.Remove (keyNode.Value);
  134. } else if (nodeName == "add") {
  135. keyNode = null;
  136. if (node.Attributes != null)
  137. keyNode = node.Attributes.RemoveNamedItem (nameAtt);
  138. if (keyNode == null)
  139. throw new ConfigurationException ("Required attribute not found",
  140. node);
  141. if (keyNode.Value == String.Empty)
  142. throw new ConfigurationException ("Required attribute is empty",
  143. node);
  144. valueNode = node.Attributes.RemoveNamedItem (valueAtt);
  145. if (valueNode == null)
  146. throw new ConfigurationException ("Required attribute not found",
  147. node);
  148. if (node.Attributes.Count != 0)
  149. throw new ConfigurationException ("Unknown attribute", node);
  150. result [keyNode.Value] = valueNode.Value;
  151. } else {
  152. throw new ConfigurationException ("Unknown element", node);
  153. }
  154. }
  155. return result;
  156. }
  157. }
  158. }