CodeDomConfigurationHandler.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // System.Configuration.CodeDomConfigurationHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // Copyright (c) 2005 Novell, Inc (http://www.novell.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. #if NET_2_0 && XML_DEP
  30. using System;
  31. using System.Collections;
  32. using System.Configuration;
  33. using System.IO;
  34. using System.Xml;
  35. namespace System.CodeDom.Compiler
  36. {
  37. class CodeDomConfigurationHandler : IConfigurationSectionHandler
  38. {
  39. public object Create (object parent, object context, XmlNode section)
  40. {
  41. return new CompilationConfigurationHandler ().Create (parent, context, section);
  42. }
  43. }
  44. class CompilationConfigurationHandler : IConfigurationSectionHandler
  45. {
  46. public object Create (object parent, object context, XmlNode section)
  47. {
  48. CompilationConfiguration config = new CompilationConfiguration (parent);
  49. config.TempDirectory = AttValue ("tempDirectory", section, true);
  50. config.DefaultLanguage = AttValue ("defaultLanguage", section);
  51. if (config.DefaultLanguage == null)
  52. config.DefaultLanguage = "c#";
  53. config.Debug = AttBoolValue ("debug", section, false);
  54. config.Batch = AttBoolValue ("batch", section, false);
  55. config.Explicit = AttBoolValue ("explicit", section, true);
  56. config.Strict = AttBoolValue ("strict", section, false);
  57. config.BatchTimeout = AttUIntValue ("batchTimeout", section, 0);
  58. config.MaxBatchSize = AttUIntValue ("maxBatchSize", section, 0);
  59. config.MaxBatchFileSize = AttUIntValue ("maxBatchFileSize", section, 0);
  60. config.NumRecompilesBeforeAppRestart =
  61. AttUIntValue ("numRecompilesBeforeAppRestart", section, 15);
  62. if (section.Attributes != null && section.Attributes.Count != 0)
  63. ThrowException ("Unrecognized attribute.", section);
  64. XmlNodeList authNodes = section.ChildNodes;
  65. foreach (XmlNode child in authNodes) {
  66. XmlNodeType ntype = child.NodeType;
  67. if (ntype != XmlNodeType.Element)
  68. continue;
  69. if (child.Name == "compilers") {
  70. ReadCompilers (child.ChildNodes, config);
  71. continue;
  72. }
  73. ThrowException ("Unexpected element", child);
  74. }
  75. return config;
  76. }
  77. static void ReadCompilers (XmlNodeList nodes, CompilationConfiguration config)
  78. {
  79. foreach (XmlNode child in nodes) {
  80. XmlNodeType ntype = child.NodeType;
  81. if (ntype != XmlNodeType.Element)
  82. continue;
  83. if (child.Name != "compiler")
  84. ThrowException ("Unexpected element", child);
  85. CompilerInfo compiler = new CompilerInfo ();
  86. compiler.Languages = AttValue ("language", child);
  87. compiler.Extensions = AttValue ("extension", child);
  88. compiler.TypeName = AttValue ("type", child);
  89. compiler.CompilerOptions = AttValue ("compilerOptions", child, true, true);
  90. compiler.WarningLevel = AttUIntValue ("warningLevel", child, 0);
  91. config.Compilers [compiler.Languages] = compiler;
  92. }
  93. }
  94. static string AttValue (string name, XmlNode node, bool optional)
  95. {
  96. return AttValue (name, node, optional, false);
  97. }
  98. static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
  99. {
  100. return ExtractAttributeValue (name, node, optional, allowEmpty);
  101. }
  102. static bool AttBoolValue (string name, XmlNode node, bool _default)
  103. {
  104. string v = AttValue (name, node, true);
  105. if (v == null)
  106. return _default;
  107. bool result = (v == "true");
  108. if (!result && v != "false")
  109. ThrowException ("Invalid boolean value in " + name, node);
  110. return result;
  111. }
  112. static int AttUIntValue (string name, XmlNode node, int _default)
  113. {
  114. string v = AttValue (name, node, true);
  115. if (v == null)
  116. return _default;
  117. int result = 0;
  118. try {
  119. result = (int) UInt32.Parse (v);
  120. } catch {
  121. ThrowException ("Invalid number in " + name, node);
  122. }
  123. return result;
  124. }
  125. static string AttValue (string name, XmlNode node)
  126. {
  127. return ExtractAttributeValue (name, node, true);
  128. }
  129. static string ShortAsmName (string long_name)
  130. {
  131. int i = long_name.IndexOf (',');
  132. if (i < 0)
  133. return long_name + ".dll";
  134. return long_name.Substring (0, i) + ".dll";
  135. }
  136. static void ThrowException (string message, XmlNode node)
  137. {
  138. ThrowException (message, node);
  139. }
  140. static internal string ExtractAttributeValue (string attKey, XmlNode node)
  141. {
  142. return ExtractAttributeValue (attKey, node, false);
  143. }
  144. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
  145. {
  146. return ExtractAttributeValue (attKey, node, optional, false);
  147. }
  148. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
  149. bool allowEmpty)
  150. {
  151. if (node.Attributes == null) {
  152. if (optional)
  153. return null;
  154. ThrowException ("Required attribute not found: " + attKey, node);
  155. }
  156. XmlNode att = node.Attributes.RemoveNamedItem (attKey);
  157. if (att == null) {
  158. if (optional)
  159. return null;
  160. ThrowException ("Required attribute not found: " + attKey, node);
  161. }
  162. string value = att.Value;
  163. if (!allowEmpty && value == String.Empty) {
  164. string opt = optional ? "Optional" : "Required";
  165. ThrowException (opt + " attribute is empty: " + attKey, node);
  166. }
  167. return value;
  168. }
  169. }
  170. sealed class CompilationConfiguration
  171. {
  172. bool debug;
  173. bool batch;
  174. int batch_timeout;
  175. string default_language = "c#";
  176. bool _explicit = true;
  177. int max_batch_size = 30;
  178. int max_batch_file_size = 3000;
  179. int num_recompiles_before_app_restart = 15;
  180. bool strict;
  181. string temp_directory;
  182. CompilerCollection compilers;
  183. /* Only the config. handler should create instances of this. Use GetInstance (context) */
  184. public CompilationConfiguration (object p)
  185. {
  186. CompilationConfiguration parent = p as CompilationConfiguration;
  187. if (parent != null)
  188. Init (parent);
  189. if (compilers == null)
  190. compilers = new CompilerCollection ();
  191. if (temp_directory == null)
  192. temp_directory = Path.GetTempPath ();
  193. }
  194. public CompilerInfo GetCompilerInfo (string language)
  195. {
  196. return Compilers [language];
  197. }
  198. void Init (CompilationConfiguration parent)
  199. {
  200. debug = parent.debug;
  201. batch = parent.batch;
  202. batch_timeout = parent.batch_timeout;
  203. default_language = parent.default_language;
  204. _explicit = parent._explicit;
  205. max_batch_size = parent.max_batch_size;
  206. max_batch_file_size = parent.max_batch_file_size;
  207. num_recompiles_before_app_restart = parent.num_recompiles_before_app_restart;
  208. strict = parent.strict;
  209. temp_directory = parent.temp_directory;
  210. compilers = new CompilerCollection (parent.compilers);
  211. }
  212. public bool Debug {
  213. get { return debug; }
  214. set { debug = value; }
  215. }
  216. public bool Batch {
  217. get { return batch; }
  218. set { batch = value; }
  219. }
  220. public int BatchTimeout {
  221. get { return batch_timeout; }
  222. set { batch_timeout = value; }
  223. }
  224. public string DefaultLanguage {
  225. get { return default_language; }
  226. set { default_language = value; }
  227. }
  228. public bool Explicit {
  229. get { return _explicit; }
  230. set { _explicit = value; }
  231. }
  232. public int MaxBatchSize {
  233. get { return max_batch_size; }
  234. set { max_batch_size = value; }
  235. }
  236. public int MaxBatchFileSize {
  237. get { return max_batch_file_size; }
  238. set { max_batch_file_size = value; }
  239. }
  240. public int NumRecompilesBeforeAppRestart {
  241. get { return num_recompiles_before_app_restart; }
  242. set { num_recompiles_before_app_restart = value; }
  243. }
  244. public bool Strict {
  245. get { return strict; }
  246. set { strict = value; }
  247. }
  248. public string TempDirectory {
  249. get { return temp_directory; }
  250. set {
  251. if (value != null && !Directory.Exists (value))
  252. throw new ArgumentException ("Directory does not exist");
  253. temp_directory = value;
  254. }
  255. }
  256. public CompilerCollection Compilers {
  257. get { return compilers; }
  258. }
  259. }
  260. sealed class CompilerCollection
  261. {
  262. Hashtable compilers;
  263. public CompilerCollection () : this (null) {}
  264. public CompilerCollection (CompilerCollection parent)
  265. {
  266. compilers = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  267. CaseInsensitiveComparer.Default);
  268. if (parent != null && parent.compilers != null) {
  269. foreach (DictionaryEntry entry in parent.compilers)
  270. compilers [entry.Key] = entry.Value;
  271. }
  272. }
  273. public CompilerInfo this [string language] {
  274. get { return compilers [language] as CompilerInfo; }
  275. set {
  276. compilers [language] = value;
  277. string [] langs = language.Split (';');
  278. foreach (string s in langs) {
  279. string x = s.Trim ();
  280. if (x != "")
  281. compilers [x] = value;
  282. }
  283. }
  284. }
  285. internal Hashtable Hash {
  286. get { return compilers; }
  287. }
  288. }
  289. }
  290. #endif // NET_2_0 && XML_DEP