BuildProvider.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // System.Web.Compilation.BuildProvider
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2006-2009 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. using System;
  31. using System.CodeDom;
  32. using System.CodeDom.Compiler;
  33. using System.Collections;
  34. using System.Collections.Generic;
  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. {
  43. public abstract class BuildProvider
  44. {
  45. #if NET_4_0
  46. static Dictionary <string, Type> registeredBuildProviderTypes;
  47. #endif
  48. ArrayList ref_assemblies;
  49. ICollection vpath_deps;
  50. CompilationSection compilationSection;
  51. VirtualPath vpath;
  52. CompilationSection CompilationConfig {
  53. get {
  54. if (compilationSection == null)
  55. compilationSection = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
  56. return compilationSection;
  57. }
  58. }
  59. internal virtual string LanguageName {
  60. get { return CompilationConfig.DefaultLanguage; }
  61. }
  62. protected BuildProvider()
  63. {
  64. ref_assemblies = new ArrayList ();
  65. }
  66. internal void SetVirtualPath (VirtualPath virtualPath)
  67. {
  68. vpath = virtualPath;
  69. }
  70. internal virtual void GenerateCode ()
  71. {
  72. }
  73. internal virtual IDictionary <string, bool> ExtractDependencies ()
  74. {
  75. return null;
  76. }
  77. public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
  78. {
  79. }
  80. public virtual string GetCustomString (CompilerResults results)
  81. {
  82. return null;
  83. }
  84. protected CompilerType GetDefaultCompilerType ()
  85. {
  86. return BuildManager.GetDefaultCompilerTypeForLanguage (CompilationConfig.DefaultLanguage, CompilationConfig);
  87. }
  88. protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
  89. {
  90. return BuildManager.GetDefaultCompilerTypeForLanguage (language, null);
  91. }
  92. public virtual Type GetGeneratedType (CompilerResults results)
  93. {
  94. return null;
  95. }
  96. public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
  97. {
  98. return BuildProviderResultFlags.Default;
  99. }
  100. protected TextReader OpenReader ()
  101. {
  102. return OpenReader (VirtualPath);
  103. }
  104. protected TextReader OpenReader (string virtualPath)
  105. {
  106. Stream st = OpenStream (virtualPath);
  107. return new StreamReader (st, WebEncoding.FileEncoding);
  108. }
  109. protected Stream OpenStream ()
  110. {
  111. return OpenStream (VirtualPath);
  112. }
  113. protected Stream OpenStream (string virtualPath)
  114. {
  115. // MS also throws a NullReferenceException here when not hosted.
  116. return VirtualPathProvider.OpenFile (virtualPath);
  117. }
  118. #if NET_4_0
  119. public static void RegisterBuildProvider (string extension, Type providerType)
  120. {
  121. if (String.IsNullOrEmpty (extension))
  122. throw new ArgumentException ("The string parameter 'extension' cannot be null or empty.", "extension");
  123. if (providerType == null)
  124. throw new ArgumentNullException ("providerType");
  125. if (!typeof (BuildProvider).IsAssignableFrom (providerType))
  126. throw new ArgumentException ("The parameter 'providerType' is invalid", "providerType");
  127. BuildManager.AssertPreStartMethodsRunning ();
  128. if (registeredBuildProviderTypes == null)
  129. registeredBuildProviderTypes = new Dictionary <string, Type> (StringComparer.OrdinalIgnoreCase);
  130. registeredBuildProviderTypes [extension] = providerType;
  131. }
  132. internal static Type GetProviderTypeForExtension (string extension)
  133. {
  134. if (String.IsNullOrEmpty (extension))
  135. return null;
  136. Type type = null;
  137. if (registeredBuildProviderTypes == null || !registeredBuildProviderTypes.TryGetValue (extension, out type) || type == null) {
  138. var cs = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  139. BuildProviderCollection bpcoll = cs != null ? cs.BuildProviders : null;
  140. global::System.Web.Configuration.BuildProvider bpcfg = bpcoll != null ? bpcoll [extension] : null;
  141. if (bpcfg != null)
  142. type = HttpApplication.LoadType (bpcfg.Type);
  143. }
  144. return type;
  145. }
  146. internal static BuildProvider GetProviderInstanceForExtension (string extension)
  147. {
  148. Type type = GetProviderTypeForExtension (extension);
  149. if (type == null)
  150. return null;
  151. return Activator.CreateInstance (type, null) as global::System.Web.Compilation.BuildProvider;
  152. }
  153. #endif
  154. public virtual CompilerType CodeCompilerType {
  155. get { return null; } // Documented to return null
  156. }
  157. protected ICollection ReferencedAssemblies {
  158. get { return ref_assemblies; }
  159. }
  160. protected internal string VirtualPath {
  161. get { return vpath != null ? vpath.Absolute : null; }
  162. }
  163. internal VirtualPath VirtualPathInternal {
  164. get { return vpath; }
  165. }
  166. public virtual ICollection VirtualPathDependencies {
  167. get {
  168. if (vpath_deps == null)
  169. vpath_deps = new OneNullCollection ();
  170. return vpath_deps;
  171. }
  172. }
  173. internal virtual CodeCompileUnit CodeUnit {
  174. get { return null; }
  175. }
  176. }
  177. class OneNullCollection : ICollection {
  178. public int Count {
  179. get { return 1; }
  180. }
  181. public bool IsSynchronized {
  182. get { return false; }
  183. }
  184. public object SyncRoot {
  185. get { return this; }
  186. }
  187. public void CopyTo (Array array, int index)
  188. {
  189. if (array == null)
  190. throw new ArgumentNullException ();
  191. if (index < 0)
  192. throw new ArgumentOutOfRangeException ();
  193. if (array.Rank > 1)
  194. throw new ArgumentException ();
  195. int length = array.Length;
  196. if (index >= length || index > length - 1)
  197. throw new ArgumentException ();
  198. array.SetValue (null, index);
  199. }
  200. public IEnumerator GetEnumerator ()
  201. {
  202. yield return null;
  203. }
  204. }
  205. }