GenericBuildProvider.cs 6.1 KB

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