CodeDomConfigurationHandler.cs 9.4 KB

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