2008-11-18 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;
36 using System.CodeDom.Compiler;
37 using System.Collections;
38 using System.Collections.Specialized;
39 using System.IO;
40 using System.Reflection;
41 using System.Web.Configuration;
42 using System.Web.Hosting;
43 using System.Web.Util;
44
45 namespace System.Web.Compilation {
46
47         public abstract class BuildProvider {
48                 string virtual_path;
49                 ArrayList ref_assemblies;
50                 
51                 ICollection vpath_deps;
52
53                 protected BuildProvider()
54                 {
55                         ref_assemblies = new ArrayList ();
56                 }
57
58                 internal void SetVirtualPath (VirtualPath virtualPath)
59                 {
60                         virtual_path = virtualPath.Absolute;
61                 }
62
63                 internal virtual void GenerateCode ()
64                 {
65                 }
66                 
67                 public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
68                 {
69                 }
70
71                 public virtual string GetCustomString (CompilerResults results)
72                 {
73                         return null;
74                 }
75
76                 protected CompilerType GetDefaultCompilerType ()
77                 {
78                         CompilationSection config;
79                         config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
80                         return BuildManager.GetDefaultCompilerTypeForLanguage (config.DefaultLanguage, config);
81                 }
82                 
83                 protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
84                 {
85                         return BuildManager.GetDefaultCompilerTypeForLanguage (language, null);
86                 }
87
88                 public virtual Type GetGeneratedType (CompilerResults results)
89                 {
90                         return null;
91                 }
92
93                 public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
94                 {
95                         return BuildProviderResultFlags.Default;
96                 }
97
98                 protected TextReader OpenReader ()
99                 {
100                         return OpenReader (VirtualPath);
101                 }
102
103                 protected TextReader OpenReader (string virtualPath)
104                 {
105                         Stream st = OpenStream (virtualPath);
106                         return new StreamReader (st, WebEncoding.FileEncoding);
107                 }
108
109                 protected Stream OpenStream ()
110                 {
111                         return OpenStream (VirtualPath);
112                 }
113
114                 protected Stream OpenStream (string virtualPath)
115                 {
116                         // MS also throws a NullReferenceException here when not hosted.
117                         return VirtualPathProvider.OpenFile (virtualPath);
118                 }
119                 
120                 public virtual CompilerType CodeCompilerType {
121                         get { return null; } // Documented to return null
122                 }
123
124                 protected ICollection ReferencedAssemblies {
125                         get { return ref_assemblies; }
126                 }
127
128                 protected internal string VirtualPath {
129                         get { return virtual_path; }
130                 }
131
132                 public virtual ICollection VirtualPathDependencies {
133                         get {
134                                 if (vpath_deps == null)
135                                         vpath_deps = new OneNullCollection ();
136
137                                 return vpath_deps;
138                         }
139                 }
140
141                 internal virtual CodeCompileUnit CodeUnit {
142                         get { return null; }
143                 }
144         }
145
146         class OneNullCollection : ICollection {
147                 public int Count {
148                         get { return 1; }
149                 }
150
151                 public bool IsSynchronized {
152                         get { return false; }
153                 }
154
155                 public object SyncRoot {
156                         get { return this; }
157                 }
158
159                 public void CopyTo (Array array, int index)
160                 {
161                         if (array == null)
162                                 throw new ArgumentNullException ();
163
164                         if (index < 0)
165                                 throw new ArgumentOutOfRangeException ();
166
167                         if (array.Rank > 1)
168                                 throw new ArgumentException ();
169
170                         int length = array.Length;
171                         if (index >= length || index > length - 1)
172                                 throw new ArgumentException ();
173
174                         array.SetValue (null, index);
175                 }
176
177                 public IEnumerator GetEnumerator ()
178                 {
179                         yield return null;
180                 }
181         }
182 }
183 #endif
184