CodeDomConfigurationHandler.cs 9.5 KB

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