BuildProvider.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. ArrayList ref_assemblies;
  46. ICollection vpath_deps;
  47. CompilationSection compilationSection;
  48. VirtualPath vpath;
  49. CompilationSection CompilationConfig {
  50. get {
  51. if (compilationSection == null)
  52. compilationSection = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
  53. return compilationSection;
  54. }
  55. }
  56. internal virtual string LanguageName {
  57. get { return CompilationConfig.DefaultLanguage; }
  58. }
  59. protected BuildProvider()
  60. {
  61. ref_assemblies = new ArrayList ();
  62. }
  63. internal void SetVirtualPath (VirtualPath virtualPath)
  64. {
  65. vpath = virtualPath;
  66. }
  67. internal virtual void GenerateCode ()
  68. {
  69. }
  70. internal virtual IDictionary <string, bool> ExtractDependencies ()
  71. {
  72. return null;
  73. }
  74. public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
  75. {
  76. }
  77. public virtual string GetCustomString (CompilerResults results)
  78. {
  79. return null;
  80. }
  81. protected CompilerType GetDefaultCompilerType ()
  82. {
  83. return BuildManager.GetDefaultCompilerTypeForLanguage (CompilationConfig.DefaultLanguage, CompilationConfig);
  84. }
  85. protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
  86. {
  87. return BuildManager.GetDefaultCompilerTypeForLanguage (language, null);
  88. }
  89. public virtual Type GetGeneratedType (CompilerResults results)
  90. {
  91. return null;
  92. }
  93. public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
  94. {
  95. return BuildProviderResultFlags.Default;
  96. }
  97. protected TextReader OpenReader ()
  98. {
  99. return OpenReader (VirtualPath);
  100. }
  101. protected TextReader OpenReader (string virtualPath)
  102. {
  103. Stream st = OpenStream (virtualPath);
  104. return new StreamReader (st, WebEncoding.FileEncoding);
  105. }
  106. protected Stream OpenStream ()
  107. {
  108. return OpenStream (VirtualPath);
  109. }
  110. protected Stream OpenStream (string virtualPath)
  111. {
  112. // MS also throws a NullReferenceException here when not hosted.
  113. return VirtualPathProvider.OpenFile (virtualPath);
  114. }
  115. public virtual CompilerType CodeCompilerType {
  116. get { return null; } // Documented to return null
  117. }
  118. protected ICollection ReferencedAssemblies {
  119. get { return ref_assemblies; }
  120. }
  121. protected internal string VirtualPath {
  122. get { return vpath != null ? vpath.Absolute : null; }
  123. }
  124. internal VirtualPath VirtualPathInternal {
  125. get { return vpath; }
  126. }
  127. public virtual ICollection VirtualPathDependencies {
  128. get {
  129. if (vpath_deps == null)
  130. vpath_deps = new OneNullCollection ();
  131. return vpath_deps;
  132. }
  133. }
  134. internal virtual CodeCompileUnit CodeUnit {
  135. get { return null; }
  136. }
  137. }
  138. class OneNullCollection : ICollection {
  139. public int Count {
  140. get { return 1; }
  141. }
  142. public bool IsSynchronized {
  143. get { return false; }
  144. }
  145. public object SyncRoot {
  146. get { return this; }
  147. }
  148. public void CopyTo (Array array, int index)
  149. {
  150. if (array == null)
  151. throw new ArgumentNullException ();
  152. if (index < 0)
  153. throw new ArgumentOutOfRangeException ();
  154. if (array.Rank > 1)
  155. throw new ArgumentException ();
  156. int length = array.Length;
  157. if (index >= length || index > length - 1)
  158. throw new ArgumentException ();
  159. array.SetValue (null, index);
  160. }
  161. public IEnumerator GetEnumerator ()
  162. {
  163. yield return null;
  164. }
  165. }
  166. }