ConfigHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.Collections.Specialized;
  31. #if (XML_DEP)
  32. using System.Xml;
  33. #endif
  34. namespace System.Configuration
  35. {
  36. class ConfigHelper
  37. {
  38. class CollectionWrapper
  39. {
  40. IDictionary dict;
  41. NameValueCollection collection;
  42. bool isDict;
  43. public CollectionWrapper (IDictionary dict)
  44. {
  45. this.dict = dict;
  46. isDict = true;
  47. }
  48. public CollectionWrapper (NameValueCollection collection)
  49. {
  50. this.collection = collection;
  51. isDict = false;
  52. }
  53. public void Remove (string s)
  54. {
  55. if (isDict)
  56. dict.Remove (s);
  57. else
  58. collection.Remove (s);
  59. }
  60. public void Clear ()
  61. {
  62. if (isDict)
  63. dict.Clear ();
  64. else
  65. collection.Clear ();
  66. }
  67. public string this [string key]
  68. {
  69. set {
  70. if (isDict)
  71. dict [key] = value;
  72. else
  73. collection [key] = value;
  74. }
  75. }
  76. public object UnWrap ()
  77. {
  78. if (isDict)
  79. return dict;
  80. else
  81. return collection;
  82. }
  83. }
  84. #if (XML_DEP)
  85. internal static IDictionary GetDictionary (IDictionary prev,
  86. XmlNode region,
  87. string nameAtt,
  88. string valueAtt)
  89. {
  90. Hashtable hash;
  91. if (prev == null)
  92. hash = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  93. CaseInsensitiveComparer.Default);
  94. else {
  95. Hashtable aux = (Hashtable) prev;
  96. hash = (Hashtable) aux.Clone ();
  97. }
  98. CollectionWrapper result = new CollectionWrapper (hash);
  99. result = GoGetThem (result, region, nameAtt, valueAtt);
  100. if (result == null)
  101. return null;
  102. return result.UnWrap () as IDictionary;
  103. }
  104. internal static ConfigNameValueCollection GetNameValueCollection (NameValueCollection prev,
  105. XmlNode region,
  106. string nameAtt,
  107. string valueAtt)
  108. {
  109. ConfigNameValueCollection coll =
  110. new ConfigNameValueCollection (CaseInsensitiveHashCodeProvider.Default,
  111. CaseInsensitiveComparer.Default);
  112. if (prev != null)
  113. coll.Add (prev);
  114. CollectionWrapper result = new CollectionWrapper (coll);
  115. result = GoGetThem (result, region, nameAtt, valueAtt);
  116. if (result == null)
  117. return null;
  118. return result.UnWrap () as ConfigNameValueCollection;
  119. }
  120. private static CollectionWrapper GoGetThem (CollectionWrapper result,
  121. XmlNode region,
  122. string nameAtt,
  123. string valueAtt)
  124. {
  125. if (region.Attributes != null && region.Attributes.Count != 0)
  126. throw new ConfigurationException ("Unknown attribute", region);
  127. XmlNode keyNode;
  128. XmlNode valueNode;
  129. XmlNodeList childs = region.ChildNodes;
  130. foreach (XmlNode node in childs) {
  131. XmlNodeType ntype = node.NodeType;
  132. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  133. continue;
  134. if (ntype != XmlNodeType.Element)
  135. throw new ConfigurationException ("Only XmlElement allowed", node);
  136. string nodeName = node.Name;
  137. if (nodeName == "clear") {
  138. if (node.Attributes != null && node.Attributes.Count != 0)
  139. throw new ConfigurationException ("Unknown attribute", node);
  140. result.Clear ();
  141. } else if (nodeName == "remove") {
  142. keyNode = null;
  143. if (node.Attributes != null)
  144. keyNode = node.Attributes.RemoveNamedItem (nameAtt);
  145. if (keyNode == null)
  146. throw new ConfigurationException ("Required attribute not found",
  147. node);
  148. if (keyNode.Value == String.Empty)
  149. throw new ConfigurationException ("Required attribute is empty",
  150. node);
  151. if (node.Attributes.Count != 0)
  152. throw new ConfigurationException ("Unknown attribute", node);
  153. result.Remove (keyNode.Value);
  154. } else if (nodeName == "add") {
  155. keyNode = null;
  156. if (node.Attributes != null)
  157. keyNode = node.Attributes.RemoveNamedItem (nameAtt);
  158. if (keyNode == null)
  159. throw new ConfigurationException ("Required attribute not found",
  160. node);
  161. if (keyNode.Value == String.Empty)
  162. throw new ConfigurationException ("Required attribute is empty",
  163. node);
  164. valueNode = node.Attributes.RemoveNamedItem (valueAtt);
  165. if (valueNode == null)
  166. throw new ConfigurationException ("Required attribute not found",
  167. node);
  168. if (node.Attributes.Count != 0)
  169. throw new ConfigurationException ("Unknown attribute", node);
  170. result [keyNode.Value] = valueNode.Value;
  171. } else {
  172. throw new ConfigurationException ("Unknown element", node);
  173. }
  174. }
  175. return result;
  176. }
  177. #endif
  178. }
  179. internal class ConfigNameValueCollection: NameValueCollection
  180. {
  181. bool modified;
  182. public ConfigNameValueCollection ()
  183. {
  184. }
  185. public ConfigNameValueCollection (ConfigNameValueCollection col)
  186. : base (col.Count, col)
  187. {
  188. }
  189. public ConfigNameValueCollection (IHashCodeProvider hashProvider, IComparer comparer)
  190. : base(hashProvider, comparer)
  191. {
  192. }
  193. public void ResetModified ()
  194. {
  195. modified = false;
  196. }
  197. public bool IsModified {
  198. get { return modified; }
  199. }
  200. public override void Set (string name, string value)
  201. {
  202. base.Set (name, value);
  203. modified = true;
  204. }
  205. }
  206. }