CompilationConfiguration.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // System.Web.Configuration.CompilationConfiguration
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) Copyright 2003 Ximian, Inc (http://www.ximian.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. using System;
  30. using System.CodeDom.Compiler;
  31. using System.Collections;
  32. using System.Configuration;
  33. using System.IO;
  34. using System.Web;
  35. using System.Xml;
  36. namespace System.Web.Configuration
  37. {
  38. sealed class CompilationConfiguration
  39. {
  40. bool debug = false;
  41. bool batch = false;
  42. int batch_timeout;
  43. string default_language = "c#";
  44. bool _explicit = true;
  45. int max_batch_size = 30;
  46. int max_batch_file_size = 3000;
  47. int num_recompiles_before_app_restart = 15;
  48. bool strict = false;
  49. string temp_directory;
  50. CompilerCollection compilers;
  51. ArrayList assemblies;
  52. bool assembliesInBin = false;
  53. /* Only the config. handler should create instances of this. Use GetInstance (context) */
  54. public CompilationConfiguration (object p)
  55. {
  56. CompilationConfiguration parent = p as CompilationConfiguration;
  57. if (parent != null)
  58. Init (parent);
  59. if (compilers == null)
  60. compilers = new CompilerCollection ();
  61. if (assemblies == null)
  62. assemblies = new ArrayList ();
  63. if (temp_directory == null)
  64. temp_directory = Path.GetTempPath ();
  65. }
  66. static public CompilationConfiguration GetInstance (HttpContext context)
  67. {
  68. CompilationConfiguration config;
  69. if (context == null)
  70. context = HttpContext.Context;
  71. config = context.GetConfig ("system.web/compilation") as CompilationConfiguration;
  72. if (config == null)
  73. throw new Exception ("Configuration error.");
  74. return config;
  75. }
  76. public CodeDomProvider GetProvider (string language)
  77. {
  78. WebCompiler compiler = Compilers [language];
  79. if (compiler == null)
  80. return null;
  81. if (compiler.Provider != null)
  82. return compiler.Provider;
  83. Type t = Type.GetType (compiler.Type);
  84. compiler.Provider = Activator.CreateInstance (t) as CodeDomProvider;
  85. return compiler.Provider;
  86. }
  87. public string GetCompilerOptions (string language)
  88. {
  89. WebCompiler compiler = Compilers [language];
  90. if (compiler == null)
  91. return null;
  92. return compiler.CompilerOptions;
  93. }
  94. public int GetWarningLevel (string language)
  95. {
  96. WebCompiler compiler = Compilers [language];
  97. if (compiler == null)
  98. return 0;
  99. return compiler.WarningLevel;
  100. }
  101. void Init (CompilationConfiguration parent)
  102. {
  103. debug = parent.debug;
  104. batch = parent.batch;
  105. batch_timeout = parent.batch_timeout;
  106. default_language = parent.default_language;
  107. _explicit = parent._explicit;
  108. max_batch_size = parent.max_batch_size;
  109. max_batch_file_size = parent.max_batch_file_size;
  110. num_recompiles_before_app_restart = parent.num_recompiles_before_app_restart;
  111. strict = parent.strict;
  112. temp_directory = parent.temp_directory;
  113. compilers = new CompilerCollection (parent.compilers);
  114. ArrayList p = parent.assemblies;
  115. assembliesInBin = parent.assembliesInBin;
  116. ICollection coll = (p == null) ? (ICollection) new object [0] : p;
  117. assemblies = new ArrayList (coll);
  118. }
  119. public bool Debug {
  120. get { return debug; }
  121. set { debug = value; }
  122. }
  123. public bool Batch {
  124. get { return batch; }
  125. set { batch = value; }
  126. }
  127. public int BatchTimeout {
  128. get { return batch_timeout; }
  129. set { batch_timeout = value; }
  130. }
  131. public string DefaultLanguage {
  132. get { return default_language; }
  133. set { default_language = value; }
  134. }
  135. public bool Explicit {
  136. get { return _explicit; }
  137. set { _explicit = value; }
  138. }
  139. public int MaxBatchSize {
  140. get { return max_batch_size; }
  141. set { max_batch_size = value; }
  142. }
  143. public int MaxBatchFileSize {
  144. get { return max_batch_file_size; }
  145. set { max_batch_file_size = value; }
  146. }
  147. public int NumRecompilesBeforeAppRestart {
  148. get { return num_recompiles_before_app_restart; }
  149. set { num_recompiles_before_app_restart = value; }
  150. }
  151. public bool Strict {
  152. get { return strict; }
  153. set { strict = value; }
  154. }
  155. public string TempDirectory {
  156. get { return temp_directory; }
  157. set {
  158. if (value != null && !Directory.Exists (value))
  159. throw new ArgumentException ("Directory does not exist");
  160. temp_directory = value;
  161. }
  162. }
  163. public CompilerCollection Compilers {
  164. get { return compilers; }
  165. }
  166. public ArrayList Assemblies {
  167. get { return assemblies; }
  168. }
  169. public bool AssembliesInBin {
  170. get { return assembliesInBin; }
  171. set { assembliesInBin = value; }
  172. }
  173. }
  174. }