2
0

BuildProvider.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // System.Web.Compilation.BuildProvider
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.CodeDom;
  33. using System.CodeDom.Compiler;
  34. using System.Collections;
  35. using System.Collections.Specialized;
  36. using System.IO;
  37. using System.Reflection;
  38. using System.Web.Configuration;
  39. using System.Web.Hosting;
  40. using System.Web.Util;
  41. namespace System.Web.Compilation {
  42. public abstract class BuildProvider {
  43. string virtual_path;
  44. ArrayList ref_assemblies;
  45. ICollection vpath_deps;
  46. protected BuildProvider()
  47. {
  48. ref_assemblies = new ArrayList ();
  49. }
  50. internal void SetVirtualPath (VirtualPath virtualPath)
  51. {
  52. virtual_path = virtualPath.Absolute;
  53. }
  54. internal virtual void GenerateCode ()
  55. {
  56. }
  57. public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
  58. {
  59. }
  60. public virtual string GetCustomString (CompilerResults results)
  61. {
  62. return null;
  63. }
  64. protected CompilerType GetDefaultCompilerType ()
  65. {
  66. CompilationSection config;
  67. config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  68. return GetDefaultCompilerTypeForLanguage (config.DefaultLanguage, config);
  69. }
  70. void SetCommonParameters (CompilationSection config, CompilerParameters p)
  71. {
  72. p.IncludeDebugInformation = config.Debug;
  73. }
  74. internal CompilerType GetDefaultCompilerTypeForLanguage (string language, CompilationSection configSection)
  75. {
  76. // MS throws when accesing a Hashtable, we do here.
  77. if (language == null || language == "")
  78. throw new ArgumentNullException ("language");
  79. CompilationSection config;
  80. if (configSection == null)
  81. config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  82. else
  83. config = configSection;
  84. Compiler compiler = config.Compilers.Get (language);
  85. CompilerParameters p;
  86. if (compiler != null) {
  87. Type type = HttpApplication.LoadType (compiler.Type, true);
  88. p = new CompilerParameters ();
  89. p.CompilerOptions = compiler.CompilerOptions;
  90. p.WarningLevel = compiler.WarningLevel;
  91. SetCommonParameters (config, p);
  92. return new CompilerType (type, p);
  93. }
  94. if (CodeDomProvider.IsDefinedLanguage (language)) {
  95. CompilerInfo info = CodeDomProvider.GetCompilerInfo (language);
  96. CompilerParameters par = info.CreateDefaultCompilerParameters ();
  97. SetCommonParameters (config, par);
  98. return new CompilerType (info.CodeDomProviderType, par);
  99. }
  100. throw new HttpException (String.Concat ("No compiler for language '", language, "'."));
  101. }
  102. protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
  103. {
  104. return GetDefaultCompilerTypeForLanguage (language, null);
  105. }
  106. public virtual Type GetGeneratedType (CompilerResults results)
  107. {
  108. return null;
  109. }
  110. public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
  111. {
  112. return BuildProviderResultFlags.Default;
  113. }
  114. protected TextReader OpenReader ()
  115. {
  116. return OpenReader (VirtualPath);
  117. }
  118. protected TextReader OpenReader (string virtualPath)
  119. {
  120. Stream st = OpenStream (virtualPath);
  121. return new StreamReader (st, WebEncoding.FileEncoding);
  122. }
  123. protected Stream OpenStream ()
  124. {
  125. return OpenStream (VirtualPath);
  126. }
  127. protected Stream OpenStream (string virtualPath)
  128. {
  129. // MS also throws a NullReferenceException here when not hosted.
  130. return VirtualPathProvider.OpenFile (virtualPath);
  131. }
  132. public virtual CompilerType CodeCompilerType {
  133. get { return null; } // Documented to return null
  134. }
  135. protected ICollection ReferencedAssemblies {
  136. get { return ref_assemblies; }
  137. }
  138. protected internal string VirtualPath {
  139. get { return virtual_path; }
  140. }
  141. public virtual ICollection VirtualPathDependencies {
  142. get {
  143. if (vpath_deps == null)
  144. vpath_deps = new OneNullCollection ();
  145. return vpath_deps;
  146. }
  147. }
  148. internal virtual CodeCompileUnit CodeUnit {
  149. get { return null; }
  150. }
  151. }
  152. class OneNullCollection : ICollection {
  153. public int Count {
  154. get { return 1; }
  155. }
  156. public bool IsSynchronized {
  157. get { return false; }
  158. }
  159. public object SyncRoot {
  160. get { return this; }
  161. }
  162. public void CopyTo (Array array, int index)
  163. {
  164. if (array == null)
  165. throw new ArgumentNullException ();
  166. if (index < 0)
  167. throw new ArgumentOutOfRangeException ();
  168. if (array.Rank > 1)
  169. throw new ArgumentException ();
  170. int length = array.Length;
  171. if (index >= length || index > length - 1)
  172. throw new ArgumentException ();
  173. array.SetValue (null, index);
  174. }
  175. public IEnumerator GetEnumerator ()
  176. {
  177. yield return null;
  178. }
  179. }
  180. }
  181. #endif