2008-01-02 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Compilation / BuildProvider.cs
1 //
2 // System.Web.Compilation.BuildProvider
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2006 Novell, Inc (http://www.novell.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 #if NET_2_0
33
34 using System;
35 using System.CodeDom.Compiler;
36 using System.Collections;
37 using System.Collections.Specialized;
38 using System.IO;
39 using System.Reflection;
40 using System.Web.Configuration;
41 using System.Web.Hosting;
42 using System.Web.Util;
43
44 namespace System.Web.Compilation {
45
46         public abstract class BuildProvider {
47                 string virtual_path;
48                 ArrayList ref_assemblies;
49
50                 ICollection vpath_deps;
51
52                 protected BuildProvider()
53                 {
54                         ref_assemblies = new ArrayList ();
55                 }
56
57                 internal void SetVirtualPath (string path)
58                 {
59                         virtual_path = path;
60                 }
61
62                 public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
63                 {
64                 }
65
66                 public virtual string GetCustomString (CompilerResults results)
67                 {
68                         return null;
69                 }
70
71                 protected CompilerType GetDefaultCompilerType ()
72                 {
73                         CompilationSection config;
74                         config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
75                         return GetDefaultCompilerTypeForLanguage (config.DefaultLanguage, config);
76                 }
77
78                 void SetCommonParameters (CompilationSection config, CompilerParameters p)
79                 {
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);
85                                 } else {
86                                         AddAssembliesInBin (p.ReferencedAssemblies);
87                                 }
88                         }
89
90                         // explicit, strict?
91                         // embedded? linked?/resources
92                 }
93
94                 void AddAssembliesInBin (StringCollection coll)
95                 {
96                         foreach (string s in HttpApplication.BinDirectoryAssemblies) {
97                                 coll.Add (s);
98                                 ref_assemblies.Add (s);
99                         }
100                 }
101
102                 internal CompilerType GetDefaultCompilerTypeForLanguage (string language, CompilationSection configSection)
103                 {
104                         // MS throws when accesing a Hashtable, we do here.
105                         if (language == null || language == "")
106                                 throw new ArgumentNullException ("language");
107
108                         CompilationSection config;
109                         if (configSection == null)
110                                 config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
111                         else
112                                 config = configSection;
113                         
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);
123                         }
124
125                         if (!CodeDomProvider.IsDefinedLanguage (language))
126                                 throw new HttpException (String.Format ("No compiler for language '{0}'.", language));
127
128                         CompilerInfo info = CodeDomProvider.GetCompilerInfo (language);
129                         CompilerParameters par = info.CreateDefaultCompilerParameters ();
130                         SetCommonParameters (config, par);
131                         return new CompilerType (info.CodeDomProviderType, par);
132                 }
133                 
134                 protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
135                 {
136                         return GetDefaultCompilerTypeForLanguage (language, null);
137                 }
138
139                 public virtual Type GetGeneratedType (CompilerResults results)
140                 {
141                         return null;
142                 }
143
144                 public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
145                 {
146                         return BuildProviderResultFlags.Default;
147                 }
148
149                 protected TextReader OpenReader ()
150                 {
151                         return OpenReader (VirtualPath);
152                 }
153
154                 protected TextReader OpenReader (string virtualPath)
155                 {
156                         Stream st = OpenStream (virtualPath);
157                         return new StreamReader (st, WebEncoding.FileEncoding);
158                 }
159
160                 protected Stream OpenStream ()
161                 {
162                         return OpenStream (VirtualPath);
163                 }
164
165                 protected Stream OpenStream (string virtualPath)
166                 {
167                         // MS also throws a NullReferenceException here when not hosted.
168                         return VirtualPathProvider.OpenFile (virtualPath);
169                 }
170
171                 public virtual CompilerType CodeCompilerType {
172                         get { return null; } // Documented to return null
173                 }
174
175                 protected ICollection ReferencedAssemblies {
176                         get { return ref_assemblies; }
177                 }
178
179                 protected internal string VirtualPath {
180                         get { return virtual_path; }
181                 }
182
183                 public virtual ICollection VirtualPathDependencies {
184                         get {
185                                 if (vpath_deps == null)
186                                         vpath_deps = new OneNullCollection ();
187
188                                 return vpath_deps;
189                         }
190                 }
191         }
192
193         class OneNullCollection : ICollection {
194                 public int Count {
195                         get { return 1; }
196                 }
197
198                 public bool IsSynchronized {
199                         get { return false; }
200                 }
201
202                 public object SyncRoot {
203                         get { return this; }
204                 }
205
206                 public void CopyTo (Array array, int index)
207                 {
208                         if (array == null)
209                                 throw new ArgumentNullException ();
210
211                         if (index < 0)
212                                 throw new ArgumentOutOfRangeException ();
213
214                         if (array.Rank > 1)
215                                 throw new ArgumentException ();
216
217                         int length = array.Length;
218                         if (index >= length || index > length - 1)
219                                 throw new ArgumentException ();
220
221                         array.SetValue (null, index);
222                 }
223
224                 public IEnumerator GetEnumerator ()
225                 {
226                         yield return null;
227                 }
228         }
229 }
230 #endif
231