DiagnosticsConfigurationHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // System.Diagnostics.DiagnosticsConfigurationHandler.cs
  3. //
  4. // Comments from John R. Hicks <[email protected]> original implementation
  5. // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
  6. //
  7. // Authors:
  8. // John R. Hicks <[email protected]>
  9. // Jonathan Pryor <[email protected]>
  10. //
  11. // (C) 2002
  12. //
  13. using System;
  14. using System.Collections;
  15. using System.Configuration;
  16. using System.Xml;
  17. namespace System.Diagnostics
  18. {
  19. internal sealed class DiagnosticsConfiguration
  20. {
  21. private static IDictionary settings = null;
  22. public static IDictionary Settings {
  23. get {
  24. // TODO: Does anybody know if this is actually thread-safe under .NET?
  25. // I've heard that this construct isn't safe under Java, but it's used
  26. // reasonably often under C++, so I'm not sure about .NET.
  27. if (settings == null) {
  28. lock (typeof(DiagnosticsConfiguration)) {
  29. if (settings == null)
  30. settings = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
  31. }
  32. }
  33. return settings;
  34. }
  35. }
  36. }
  37. public class DiagnosticsConfigurationHandler : IConfigurationSectionHandler
  38. {
  39. delegate void ElementHandler (IDictionary d, XmlNode node);
  40. IDictionary elementHandlers = new Hashtable ();
  41. public DiagnosticsConfigurationHandler ()
  42. {
  43. elementHandlers ["assert"] = new ElementHandler (AddAssertNode);
  44. elementHandlers ["switches"] = new ElementHandler (AddSwitchesNode);
  45. elementHandlers ["trace"] = new ElementHandler (AddTraceNode);
  46. }
  47. public virtual object Create (object parent, object configContext, XmlNode section)
  48. {
  49. IDictionary d;
  50. if (parent == null)
  51. d = new Hashtable (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
  52. else
  53. d = (IDictionary) ((ICloneable)parent).Clone();
  54. foreach (XmlNode child in section.ChildNodes) {
  55. XmlNodeType type = child.NodeType;
  56. switch (type) {
  57. /* ignore */
  58. case XmlNodeType.Whitespace:
  59. case XmlNodeType.Comment:
  60. continue;
  61. case XmlNodeType.Element:
  62. ElementHandler eh = (ElementHandler) elementHandlers [child.Name];
  63. if (eh != null)
  64. eh (d, child);
  65. else
  66. ThrowUnrecognizedElement (child);
  67. break;
  68. default:
  69. ThrowUnrecognizedElement (child);
  70. break;
  71. }
  72. }
  73. return d;
  74. }
  75. // Remarks: Both attribute are optional
  76. private void AddAssertNode (IDictionary d, XmlNode node)
  77. {
  78. XmlAttributeCollection c = node.Attributes;
  79. string assertuienabled = GetAttribute (c, "assertuienabled", false, node);
  80. string logfilename = GetAttribute (c, "logfilename", false, node);
  81. ValidateInvalidAttributes (c, node);
  82. if (assertuienabled != null) {
  83. try {
  84. d ["assertuienabled"] = bool.Parse (assertuienabled);
  85. }
  86. catch (Exception e) {
  87. throw new ConfigurationException ("The `assertuienabled' attribute must be `true' or `false'",
  88. e, node);
  89. }
  90. }
  91. if (logfilename != null)
  92. d ["logfilename"] = logfilename;
  93. DefaultTraceListener dtl = (DefaultTraceListener) TraceImpl.Listeners["Default"];
  94. if (dtl != null) {
  95. if (assertuienabled != null)
  96. dtl.AssertUiEnabled = (bool) d ["assertuienabled"];
  97. if (logfilename != null)
  98. dtl.LogFileName = logfilename;
  99. }
  100. if (node.ChildNodes.Count > 0)
  101. ThrowUnrecognizedElement (node.ChildNodes[0]);
  102. }
  103. // name attribute is required, value is optional
  104. // Docs do not define "remove" or "clear" elements, but .NET recognizes
  105. // them
  106. private void AddSwitchesNode (IDictionary d, XmlNode node)
  107. {
  108. // There are no attributes on <switch/>
  109. ValidateInvalidAttributes (node.Attributes, node);
  110. IDictionary newNodes = new Hashtable ();
  111. foreach (XmlNode child in node.ChildNodes) {
  112. XmlNodeType t = child.NodeType;
  113. if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
  114. continue;
  115. if (t == XmlNodeType.Element) {
  116. XmlAttributeCollection attributes = child.Attributes;
  117. string name = null;
  118. string value = null;
  119. switch (child.Name) {
  120. case "add":
  121. name = GetAttribute (attributes, "name", true, child);
  122. value = GetAttribute (attributes, "value", false, child);
  123. newNodes[name] = AsString (value);
  124. break;
  125. case "remove":
  126. name = GetAttribute (attributes, "name", true, child);
  127. newNodes.Remove (name);
  128. break;
  129. case "clear":
  130. newNodes.Clear ();
  131. break;
  132. default:
  133. ThrowUnrecognizedElement (child);
  134. break;
  135. }
  136. ValidateInvalidAttributes (attributes, child);
  137. }
  138. else
  139. ThrowUnrecognizedNode (child);
  140. }
  141. d [node.Name] = newNodes;
  142. }
  143. private void AddTraceNode (IDictionary d, XmlNode node)
  144. {
  145. AddTraceAttributes (d, node);
  146. foreach (XmlNode child in node.ChildNodes) {
  147. XmlNodeType t = child.NodeType;
  148. if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
  149. continue;
  150. if (t == XmlNodeType.Element) {
  151. if (child.Name == "listeners")
  152. AddTraceListeners (child);
  153. else
  154. ThrowUnrecognizedElement (child);
  155. ValidateInvalidAttributes (child.Attributes, child);
  156. }
  157. else
  158. ThrowUnrecognizedNode (child);
  159. }
  160. }
  161. // all attributes are optional
  162. private void AddTraceAttributes (IDictionary d, XmlNode node)
  163. {
  164. XmlAttributeCollection c = node.Attributes;
  165. string autoflush = GetAttribute (c, "autoflush", false, node);
  166. string indentsize = GetAttribute (c, "indentsize", false, node);
  167. ValidateInvalidAttributes (c, node);
  168. if (autoflush != null) {
  169. try {
  170. bool b = bool.Parse (autoflush);
  171. d ["autoflush"] = b;
  172. TraceImpl.AutoFlush = b;
  173. }
  174. catch (Exception e) {
  175. throw new ConfigurationException ("The `autoflush' attribute must be `true' or `false'",
  176. e, node);
  177. }
  178. }
  179. if (indentsize != null) {
  180. try {
  181. int n = int.Parse (indentsize);
  182. d ["indentsize"] = n;
  183. TraceImpl.IndentSize = n;
  184. }
  185. catch (Exception e) {
  186. throw new ConfigurationException ("The `indentsize' attribute must be an integral value.",
  187. e, node);
  188. }
  189. }
  190. }
  191. // only defines "add" and "remove", but "clear" also works
  192. // for add, "name" and "type" are required; initializeData is optional
  193. private void AddTraceListeners (XmlNode listenersNode)
  194. {
  195. // There are no attributes on <listeners/>
  196. ValidateInvalidAttributes (listenersNode.Attributes, listenersNode);
  197. foreach (XmlNode child in listenersNode.ChildNodes) {
  198. XmlNodeType t = child.NodeType;
  199. if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
  200. continue;
  201. if (t == XmlNodeType.Element) {
  202. XmlAttributeCollection attributes = child.Attributes;
  203. string name = null;
  204. string type = null;
  205. string id = null;
  206. switch (child.Name) {
  207. case "add":
  208. name = GetAttribute (attributes, "name", true, child);
  209. type = GetAttribute (attributes, "type", true, child);
  210. id = GetAttribute (attributes, "initializeData", false, child);
  211. AddTraceListener (name, type, id);
  212. break;
  213. case "remove":
  214. name = GetAttribute (attributes, "name", true, child);
  215. RemoveTraceListener (name);
  216. break;
  217. case "clear":
  218. TraceImpl.Listeners.Clear ();
  219. break;
  220. default:
  221. ThrowUnrecognizedElement (child);
  222. break;
  223. }
  224. ValidateInvalidAttributes (attributes, child);
  225. }
  226. else
  227. ThrowUnrecognizedNode (child);
  228. }
  229. }
  230. private void AddTraceListener (string name, string type, string initializeData)
  231. {
  232. Type t = Type.GetType (type);
  233. object[] args = null;
  234. if (initializeData == null)
  235. args = new object[]{name};
  236. else
  237. args = new object[]{initializeData, name};
  238. try {
  239. TraceListener l = (TraceListener) Activator.CreateInstance (t, args);
  240. TraceImpl.Listeners.Add (l);
  241. }
  242. catch (Exception e) {
  243. throw new ConfigurationException (
  244. string.Format ("Invalid Type Specified: {0}", type),
  245. e);
  246. }
  247. }
  248. private void RemoveTraceListener (string name)
  249. {
  250. try {
  251. TraceImpl.Listeners.Remove (name);
  252. }
  253. catch (ArgumentException e) {
  254. // The specified listener wasn't in the collection
  255. // Ignore this; .NET does.
  256. }
  257. catch (Exception e) {
  258. throw new ConfigurationException (
  259. string.Format ("Unknown error removing listener: {0}", name),
  260. e);
  261. }
  262. }
  263. private string GetAttribute (XmlAttributeCollection attrs, string attr, bool required, XmlNode node)
  264. {
  265. XmlAttribute a = attrs[attr];
  266. string r = null;
  267. if (a != null) {
  268. r = a.Value;
  269. if (required)
  270. ValidateAttribute (attr, r, node);
  271. attrs.Remove (a);
  272. }
  273. else if (required)
  274. ThrowMissingAttribute (attr, node);
  275. return r;
  276. }
  277. private string AsString (string s)
  278. {
  279. return s == null ? string.Empty : s;
  280. }
  281. private void ValidateAttribute (string attribute, string value, XmlNode node)
  282. {
  283. if (value == null || value.Length == 0)
  284. throw new ConfigurationException (string.Format ("Required attribute `{0}' cannot be empty.", attribute), node);
  285. }
  286. private void ValidateInvalidAttributes (XmlAttributeCollection c, XmlNode node)
  287. {
  288. if (c.Count != 0)
  289. ThrowUnrecognizedAttribute (c[0].Name, node);
  290. }
  291. private void ThrowMissingAttribute (string attribute, XmlNode node)
  292. {
  293. throw new ConfigurationException (string.Format ("Missing required attribute `{0}'.", attribute), node);
  294. }
  295. private void ThrowUnrecognizedNode (XmlNode node)
  296. {
  297. throw new ConfigurationException (
  298. string.Format ("Unrecognized node `{0}'; nodeType={1}", node.Name, node.NodeType),
  299. node);
  300. }
  301. private void ThrowUnrecognizedElement (XmlNode node)
  302. {
  303. throw new ConfigurationException (
  304. string.Format ("Unrecognized element <{0}/>", node.Name),
  305. node);
  306. }
  307. private void ThrowUnrecognizedAttribute (string attribute, XmlNode node)
  308. {
  309. throw new ConfigurationException (
  310. string.Format ("Unrecognized attribute `{0}' on element <{1}/>.", attribute, node.Name),
  311. node);
  312. }
  313. }
  314. }