CodeDomConfigurationHandler.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. static string ShortAsmName (string long_name)
  133. {
  134. int i = long_name.IndexOf (',');
  135. if (i < 0)
  136. return long_name + ".dll";
  137. return long_name.Substring (0, i) + ".dll";
  138. }
  139. static void ThrowException (string message, XmlNode node)
  140. {
  141. ThrowException (message, node);
  142. }
  143. static internal string ExtractAttributeValue (string attKey, XmlNode node)
  144. {
  145. return ExtractAttributeValue (attKey, node, false);
  146. }
  147. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
  148. {
  149. return ExtractAttributeValue (attKey, node, optional, false);
  150. }
  151. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
  152. bool allowEmpty)
  153. {
  154. if (node.Attributes == null) {
  155. if (optional)
  156. return null;
  157. ThrowException ("Required attribute not found: " + attKey, node);
  158. }
  159. XmlNode att = node.Attributes.RemoveNamedItem (attKey);
  160. if (att == null) {
  161. if (optional)
  162. return null;
  163. ThrowException ("Required attribute not found: " + attKey, node);
  164. }
  165. string value = att.Value;
  166. if (!allowEmpty && value == String.Empty) {
  167. string opt = optional ? "Optional" : "Required";
  168. ThrowException (opt + " attribute is empty: " + attKey, node);
  169. }
  170. return value;
  171. }
  172. }
  173. #endif // XML_DEP
  174. sealed class CompilationConfiguration
  175. {
  176. bool debug;
  177. bool batch;
  178. int batch_timeout;
  179. string default_language = "c#";
  180. bool _explicit = true;
  181. int max_batch_size = 30;
  182. int max_batch_file_size = 3000;
  183. int num_recompiles_before_app_restart = 15;
  184. bool strict;
  185. string temp_directory;
  186. CompilerCollection compilers;
  187. /* Only the config. handler should create instances of this. Use GetInstance (context) */
  188. public CompilationConfiguration (object p)
  189. {
  190. CompilationConfiguration parent = p as CompilationConfiguration;
  191. if (parent != null)
  192. Init (parent);
  193. if (compilers == null)
  194. compilers = new CompilerCollection ();
  195. if (temp_directory == null)
  196. temp_directory = Path.GetTempPath ();
  197. }
  198. public CompilerInfo GetCompilerInfo (string language)
  199. {
  200. return Compilers [language];
  201. }
  202. void Init (CompilationConfiguration parent)
  203. {
  204. debug = parent.debug;
  205. batch = parent.batch;
  206. batch_timeout = parent.batch_timeout;
  207. default_language = parent.default_language;
  208. _explicit = parent._explicit;
  209. max_batch_size = parent.max_batch_size;
  210. max_batch_file_size = parent.max_batch_file_size;
  211. num_recompiles_before_app_restart = parent.num_recompiles_before_app_restart;
  212. strict = parent.strict;
  213. temp_directory = parent.temp_directory;
  214. compilers = new CompilerCollection (parent.compilers);
  215. }
  216. public bool Debug {
  217. get { return debug; }
  218. set { debug = value; }
  219. }
  220. public bool Batch {
  221. get { return batch; }
  222. set { batch = value; }
  223. }
  224. public int BatchTimeout {
  225. get { return batch_timeout; }
  226. set { batch_timeout = value; }
  227. }
  228. public string DefaultLanguage {
  229. get { return default_language; }
  230. set { default_language = value; }
  231. }
  232. public bool Explicit {
  233. get { return _explicit; }
  234. set { _explicit = value; }
  235. }
  236. public int MaxBatchSize {
  237. get { return max_batch_size; }
  238. set { max_batch_size = value; }
  239. }
  240. public int MaxBatchFileSize {
  241. get { return max_batch_file_size; }
  242. set { max_batch_file_size = value; }
  243. }
  244. public int NumRecompilesBeforeAppRestart {
  245. get { return num_recompiles_before_app_restart; }
  246. set { num_recompiles_before_app_restart = value; }
  247. }
  248. public bool Strict {
  249. get { return strict; }
  250. set { strict = value; }
  251. }
  252. public string TempDirectory {
  253. get { return temp_directory; }
  254. set {
  255. if (value != null && !Directory.Exists (value))
  256. throw new ArgumentException ("Directory does not exist");
  257. temp_directory = value;
  258. }
  259. }
  260. public CompilerCollection Compilers {
  261. get { return compilers; }
  262. }
  263. }
  264. sealed class CompilerCollection
  265. {
  266. Hashtable compilers;
  267. public CompilerCollection () : this (null) {}
  268. public CompilerCollection (CompilerCollection parent)
  269. {
  270. compilers = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  271. CaseInsensitiveComparer.Default);
  272. if (parent != null && parent.compilers != null) {
  273. foreach (DictionaryEntry entry in parent.compilers)
  274. compilers [entry.Key] = entry.Value;
  275. }
  276. }
  277. public CompilerInfo this [string language] {
  278. get { return compilers [language] as CompilerInfo; }
  279. set {
  280. compilers [language] = value;
  281. string [] langs = language.Split (';');
  282. foreach (string s in langs) {
  283. string x = s.Trim ();
  284. if (x != "")
  285. compilers [x] = value;
  286. }
  287. }
  288. }
  289. internal Hashtable Hash {
  290. get { return compilers; }
  291. }
  292. }
  293. }
  294. #endif // NET_2_0