* PageThemeCompiler.cs: let property builders through, stop the
[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                 static object locker = new object ();
48                 static string private_bin_path;
49
50                 string virtual_path;
51                 ArrayList ref_assemblies;
52
53                 ICollection vpath_deps;
54
55                 protected BuildProvider()
56                 {
57                         ref_assemblies = new ArrayList ();
58                 }
59
60                 internal void SetVirtualPath (string path)
61                 {
62                         virtual_path = path;
63                 }
64
65                 public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
66                 {
67                 }
68
69                 public virtual string GetCustomString (CompilerResults results)
70                 {
71                         return null;
72                 }
73
74                 protected CompilerType GetDefaultCompilerType ()
75                 {
76                         CompilationSection config;
77                         config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
78                         return GetDefaultCompilerTypeForLanguage (config.DefaultLanguage, config);
79                 }
80
81                 void SetCommonParameters (CompilationSection config, CompilerParameters p)
82                 {
83                         p.IncludeDebugInformation = config.Debug;
84                         foreach (AssemblyInfo info in config.Assemblies) {
85                                 if (info.Assembly != "*") {
86                                         p.ReferencedAssemblies.Add (info.Assembly);
87                                         ref_assemblies.Add (info.Assembly);
88                                 } else {
89                                         AddAssembliesInBin (p.ReferencedAssemblies);
90                                 }
91                         }
92
93                         // explicit, strict?
94                         // embedded? linked?/resources
95                 }
96
97                 
98                 static void InitPath ()
99                 {
100                         lock (locker) {
101                                 if (private_bin_path != null)
102                                         return;
103
104                                 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
105                                 private_bin_path = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
106                         }
107                 }
108
109                 void AddAssembliesInBin (StringCollection coll)
110                 {
111                         InitPath ();
112                         if (!Directory.Exists (private_bin_path))
113                                 return;
114
115                         string [] binDlls = Directory.GetFiles (private_bin_path, "*.dll");
116                         foreach (string s in binDlls) {
117                                 coll.Add (s);
118                                 ref_assemblies.Add (s);
119                         }
120                 }
121
122                 internal CompilerType GetDefaultCompilerTypeForLanguage (string language, CompilationSection configSection)
123                 {
124                         // MS throws when accesing a Hashtable, we do here.
125                         if (language == null || language == "")
126                                 throw new ArgumentNullException ("language");
127
128                         CompilationSection config;
129                         if (configSection == null)
130                                 config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
131                         else
132                                 config = configSection;
133                         
134                         Compiler compiler = config.Compilers.Get (language);
135                         CompilerParameters p;
136                         if (compiler != null) {
137                                 Type type = Type.GetType (compiler.Type, true);
138                                 p = new CompilerParameters ();
139                                 p.CompilerOptions = compiler.CompilerOptions;
140                                 p.WarningLevel = compiler.WarningLevel;
141                                 SetCommonParameters (config, p);
142                                 return new CompilerType (type, p);
143                         }
144
145                         if (!CodeDomProvider.IsDefinedLanguage (language))
146                                 throw new HttpException (String.Format ("No compiler for language '{0}'.", language));
147
148                         CompilerInfo info = CodeDomProvider.GetCompilerInfo (language);
149                         CompilerParameters par = info.CreateDefaultCompilerParameters ();
150                         SetCommonParameters (config, par);
151                         return new CompilerType (info.CodeDomProviderType, par);
152                 }
153                 
154                 protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
155                 {
156                         return GetDefaultCompilerTypeForLanguage (language, null);
157                 }
158
159                 public virtual Type GetGeneratedType (CompilerResults results)
160                 {
161                         return null;
162                 }
163
164                 public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
165                 {
166                         return BuildProviderResultFlags.Default;
167                 }
168
169                 protected TextReader OpenReader ()
170                 {
171                         return OpenReader (VirtualPath);
172                 }
173
174                 protected TextReader OpenReader (string virtualPath)
175                 {
176                         Stream st = OpenStream (virtualPath);
177                         return new StreamReader (st, WebEncoding.FileEncoding);
178                 }
179
180                 protected Stream OpenStream ()
181                 {
182                         return OpenStream (VirtualPath);
183                 }
184
185                 protected Stream OpenStream (string virtualPath)
186                 {
187                         // MS also throws a NullReferenceException here when not hosted.
188                         return VirtualPathProvider.OpenFile (virtualPath);
189                 }
190
191                 public virtual CompilerType CodeCompilerType {
192                         get { return null; } // Documented to return null
193                 }
194
195                 protected ICollection ReferencedAssemblies {
196                         get { return ref_assemblies; }
197                 }
198
199                 protected internal string VirtualPath {
200                         get { return virtual_path; }
201                 }
202
203                 public virtual ICollection VirtualPathDependencies {
204                         get {
205                                 if (vpath_deps == null)
206                                         vpath_deps = new OneNullCollection ();
207
208                                 return vpath_deps;
209                         }
210                 }
211         }
212
213         class OneNullCollection : ICollection {
214                 public int Count {
215                         get { return 1; }
216                 }
217
218                 public bool IsSynchronized {
219                         get { return false; }
220                 }
221
222                 public object SyncRoot {
223                         get { return this; }
224                 }
225
226                 public void CopyTo (Array array, int index)
227                 {
228                         if (array == null)
229                                 throw new ArgumentNullException ();
230
231                         if (index < 0)
232                                 throw new ArgumentOutOfRangeException ();
233
234                         if (array.Rank > 1)
235                                 throw new ArgumentException ();
236
237                         int length = array.Length;
238                         if (index >= length || index > length - 1)
239                                 throw new ArgumentException ();
240
241                         array.SetValue (null, index);
242                 }
243
244                 public IEnumerator GetEnumerator ()
245                 {
246                         yield return null;
247                 }
248         }
249 }
250 #endif
251