GenericBuildProvider.cs 6.7 KB

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