GenericBuildProvider.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // System.Web.Compilation.GenericBuildProvider
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2008 Novell, Inc
  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;
  31. using System.CodeDom.Compiler;
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using System.IO;
  35. using System.Reflection;
  36. using System.Web;
  37. using System.Web.UI;
  38. namespace System.Web.Compilation
  39. {
  40. internal abstract class GenericBuildProvider <TParser> : BuildProvider
  41. {
  42. TParser _parser;
  43. CompilerType _compilerType;
  44. BaseCompiler _compiler;
  45. TextReader _reader;
  46. bool _parsed;
  47. bool _codeGenerated;
  48. protected bool Parsed {
  49. get { return _parsed; }
  50. }
  51. protected abstract TParser CreateParser (VirtualPath virtualPath, string physicalPath, TextReader reader, HttpContext context);
  52. protected abstract TParser CreateParser (VirtualPath virtualPath, string physicalPath, HttpContext context);
  53. protected abstract BaseCompiler CreateCompiler (TParser parser);
  54. protected abstract string GetParserLanguage (TParser parser);
  55. protected abstract ICollection GetParserDependencies (TParser parser);
  56. protected abstract string GetCodeBehindSource (TParser parser);
  57. protected abstract string GetClassType (BaseCompiler compiler, TParser parser);
  58. protected abstract AspGenerator CreateAspGenerator (TParser parser);
  59. protected abstract List <string> GetReferencedAssemblies (TParser parser);
  60. protected virtual string MapPath (VirtualPath virtualPath)
  61. {
  62. HttpContext ctx = HttpContext.Current;
  63. HttpRequest req = ctx != null ? ctx.Request : null;
  64. if (req != null)
  65. return req.MapPath (VirtualPath);
  66. else
  67. return null;
  68. }
  69. protected virtual TParser Parse ()
  70. {
  71. TParser parser = Parser;
  72. if (_parsed)
  73. return parser;
  74. if (!IsDirectoryBuilder) {
  75. AspGenerator generator = CreateAspGenerator (parser);
  76. if (_reader != null)
  77. generator.Parse (_reader, MapPath (VirtualPathInternal), true);
  78. else
  79. generator.Parse ();
  80. }
  81. _parsed = true;
  82. return parser;
  83. }
  84. protected virtual void OverrideAssemblyPrefix (TParser parser, AssemblyBuilder assemblyBuilder)
  85. {
  86. }
  87. internal override void GenerateCode ()
  88. {
  89. TParser parser = Parse ();
  90. _compiler = CreateCompiler (parser);
  91. if (NeedsConstructType)
  92. _compiler.ConstructType ();
  93. _codeGenerated = true;
  94. }
  95. protected virtual void GenerateCode (AssemblyBuilder assemblyBuilder, TParser parser, BaseCompiler compiler)
  96. {
  97. CodeCompileUnit unit = _compiler.CompileUnit;
  98. if (unit == null)
  99. throw new HttpException ("Unable to generate source code.");
  100. assemblyBuilder.AddCodeCompileUnit (this, unit);
  101. }
  102. public override void GenerateCode (AssemblyBuilder assemblyBuilder)
  103. {
  104. if (!_codeGenerated)
  105. GenerateCode ();
  106. TParser parser = Parse ();
  107. OverrideAssemblyPrefix (parser, assemblyBuilder);
  108. string codeBehindSource = GetCodeBehindSource (parser);
  109. if (codeBehindSource != null)
  110. assemblyBuilder.AddCodeFile (codeBehindSource, this, true);
  111. List <string> refasms = GetReferencedAssemblies (parser);
  112. if (refasms != null && refasms.Count > 0) {
  113. foreach (string loc in refasms)
  114. assemblyBuilder.AddAssemblyReference (loc);
  115. }
  116. GenerateCode (assemblyBuilder, parser, _compiler);
  117. }
  118. protected virtual Type LoadTypeFromBin (BaseCompiler compiler, TParser parser)
  119. {
  120. return null;
  121. }
  122. public override Type GetGeneratedType (CompilerResults results)
  123. {
  124. if (NeedsLoadFromBin && _compiler != null)
  125. return LoadTypeFromBin (_compiler, Parser);
  126. // This is not called if compilation failed.
  127. // Returning null makes the caller throw an InvalidCastException
  128. Assembly assembly = results != null ? results.CompiledAssembly : null;
  129. if (assembly == null)
  130. return null;
  131. return assembly.GetType (GetClassType (_compiler, Parser));
  132. }
  133. // This is intended to be used by builders which may need to do special processing
  134. // on the virtualPath before actually opening the reader.
  135. protected virtual TextReader SpecialOpenReader (VirtualPath virtualPath, out string physicalPath)
  136. {
  137. physicalPath = null;
  138. return OpenReader (virtualPath.Original);
  139. }
  140. // FIXME: figure this out.
  141. public override ICollection VirtualPathDependencies {
  142. get {
  143. TParser parser = Parser;
  144. return GetParserDependencies (parser);
  145. }
  146. }
  147. internal override string LanguageName {
  148. get {
  149. TParser parser = Parse ();
  150. if (parser != null)
  151. return GetParserLanguage (parser);
  152. return base.LanguageName;
  153. }
  154. }
  155. public override CompilerType CodeCompilerType {
  156. get {
  157. if (_compilerType == null)
  158. _compilerType = GetDefaultCompilerTypeForLanguage (LanguageName);
  159. return _compilerType;
  160. }
  161. }
  162. public TParser Parser {
  163. get {
  164. if (_parser == null) {
  165. VirtualPath vp = VirtualPathInternal;
  166. if (vp == null)
  167. throw new HttpException ("VirtualPath not set, cannot instantiate parser.");
  168. if (!IsDirectoryBuilder) {
  169. string physicalPath;
  170. _reader = SpecialOpenReader (vp, out physicalPath);
  171. _parser = CreateParser (vp, physicalPath, _reader, HttpContext.Current);
  172. } else
  173. _parser = CreateParser (vp, null, HttpContext.Current);
  174. if (_parser == null)
  175. throw new HttpException ("Unable to create type parser.");
  176. }
  177. return _parser;
  178. }
  179. }
  180. protected virtual bool IsDirectoryBuilder {
  181. get { return false; }
  182. }
  183. protected virtual bool NeedsConstructType {
  184. get { return true; }
  185. }
  186. protected virtual bool NeedsLoadFromBin {
  187. get { return false; }
  188. }
  189. internal override CodeCompileUnit CodeUnit {
  190. get {
  191. if (!_codeGenerated)
  192. GenerateCode ();
  193. return _compiler.CompileUnit;
  194. }
  195. }
  196. }
  197. }