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