2 // System.Web.Compilation.BuildProvider
5 // Chris Toshok (toshok@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.CodeDom.Compiler;
36 using System.Collections;
37 using System.Collections.Specialized;
39 using System.Reflection;
40 using System.Web.Configuration;
41 using System.Web.Hosting;
42 using System.Web.Util;
44 namespace System.Web.Compilation {
46 public abstract class BuildProvider {
48 ArrayList ref_assemblies;
50 ICollection vpath_deps;
52 protected BuildProvider()
54 ref_assemblies = new ArrayList ();
57 internal void SetVirtualPath (string path)
62 public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
66 public virtual string GetCustomString (CompilerResults results)
71 protected CompilerType GetDefaultCompilerType ()
73 CompilationSection config;
74 config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
75 return GetDefaultCompilerTypeForLanguage (config.DefaultLanguage, config);
78 void SetCommonParameters (CompilationSection config, CompilerParameters p)
80 p.IncludeDebugInformation = config.Debug;
81 foreach (AssemblyInfo info in config.Assemblies) {
82 if (info.Assembly != "*") {
83 p.ReferencedAssemblies.Add (info.Assembly);
84 ref_assemblies.Add (info.Assembly);
86 AddAssembliesInBin (p.ReferencedAssemblies);
91 // embedded? linked?/resources
94 void AddAssembliesInBin (StringCollection coll)
96 foreach (string s in HttpApplication.BinDirectoryAssemblies) {
98 ref_assemblies.Add (s);
102 internal CompilerType GetDefaultCompilerTypeForLanguage (string language, CompilationSection configSection)
104 // MS throws when accesing a Hashtable, we do here.
105 if (language == null || language == "")
106 throw new ArgumentNullException ("language");
108 CompilationSection config;
109 if (configSection == null)
110 config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
112 config = configSection;
114 Compiler compiler = config.Compilers.Get (language);
115 CompilerParameters p;
116 if (compiler != null) {
117 Type type = HttpApplication.LoadType (compiler.Type, true);
118 p = new CompilerParameters ();
119 p.CompilerOptions = compiler.CompilerOptions;
120 p.WarningLevel = compiler.WarningLevel;
121 SetCommonParameters (config, p);
122 return new CompilerType (type, p);
125 if (!CodeDomProvider.IsDefinedLanguage (language))
126 throw new HttpException (String.Format ("No compiler for language '{0}'.", language));
128 CompilerInfo info = CodeDomProvider.GetCompilerInfo (language);
129 CompilerParameters par = info.CreateDefaultCompilerParameters ();
130 SetCommonParameters (config, par);
131 return new CompilerType (info.CodeDomProviderType, par);
134 protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
136 return GetDefaultCompilerTypeForLanguage (language, null);
139 public virtual Type GetGeneratedType (CompilerResults results)
144 public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
146 return BuildProviderResultFlags.Default;
149 protected TextReader OpenReader ()
151 return OpenReader (VirtualPath);
154 protected TextReader OpenReader (string virtualPath)
156 Stream st = OpenStream (virtualPath);
157 return new StreamReader (st, WebEncoding.FileEncoding);
160 protected Stream OpenStream ()
162 return OpenStream (VirtualPath);
165 protected Stream OpenStream (string virtualPath)
167 // MS also throws a NullReferenceException here when not hosted.
168 return VirtualPathProvider.OpenFile (virtualPath);
171 public virtual CompilerType CodeCompilerType {
172 get { return null; } // Documented to return null
175 protected ICollection ReferencedAssemblies {
176 get { return ref_assemblies; }
179 protected internal string VirtualPath {
180 get { return virtual_path; }
183 public virtual ICollection VirtualPathDependencies {
185 if (vpath_deps == null)
186 vpath_deps = new OneNullCollection ();
193 class OneNullCollection : ICollection {
198 public bool IsSynchronized {
199 get { return false; }
202 public object SyncRoot {
206 public void CopyTo (Array array, int index)
209 throw new ArgumentNullException ();
212 throw new ArgumentOutOfRangeException ();
215 throw new ArgumentException ();
217 int length = array.Length;
218 if (index >= length || index > length - 1)
219 throw new ArgumentException ();
221 array.SetValue (null, index);
224 public IEnumerator GetEnumerator ()