New test.
[mono.git] / mcs / class / System.Web / System.Web.Configuration / CompilationConfiguration.cs
1 //
2 // System.Web.Configuration.CompilationConfiguration
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) Copyright 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if !NET_2_0
32 using System;
33 using System.CodeDom.Compiler;
34 using System.Collections;
35 using System.Configuration;
36 using System.IO;
37 using System.Web;
38 using System.Xml;
39
40 namespace System.Web.Configuration
41 {
42         sealed class CompilationConfiguration
43         {
44                 bool debug = false;
45                 bool batch = false;
46                 int batch_timeout;
47                 string default_language = "c#";
48                 bool _explicit = true;
49                 int max_batch_size = 30;
50                 int max_batch_file_size = 3000;
51                 int num_recompiles_before_app_restart = 15;
52                 bool strict = false;
53                 string temp_directory;
54                 CompilerCollection compilers;
55                 ArrayList assemblies;
56                 bool assembliesInBin = false;
57
58                 /* Only the config. handler should create instances of this. Use GetInstance (context) */
59                 public CompilationConfiguration (object p)
60                 {
61                         CompilationConfiguration parent = p as CompilationConfiguration;
62                         if (parent != null)
63                                 Init (parent);
64
65                         if (compilers == null)
66                                 compilers = new CompilerCollection ();
67
68                         if (assemblies == null)
69                                 assemblies = new ArrayList ();
70
71                         if (temp_directory == null || temp_directory == "")
72                                 temp_directory = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
73                 }
74
75                 static public CompilationConfiguration GetInstance (HttpContext context)
76                 {
77                         CompilationConfiguration config;
78                         if (context == null)
79                                 context = HttpContext.Current;
80
81                         if (context != null) {
82                                 config = context.GetConfig ("system.web/compilation") as CompilationConfiguration;
83                                 if (config == null)
84                                         throw new Exception ("Configuration error.");
85                         } else {
86                                 // empty config (as used in unit tests)
87                                 config = new CompilationConfiguration (null);
88                         }
89
90                         return config;
91                 }
92                 
93                 public CodeDomProvider GetProvider (string language)
94                 {
95                         Compiler compiler = Compilers [language];
96                         if (compiler == null)
97                                 return null;
98
99                         if (compiler.Provider != null)
100                                 return compiler.Provider;
101
102                         Type t = Type.GetType (compiler.Type, true);
103                         compiler.Provider = Activator.CreateInstance (t) as CodeDomProvider;
104                         return compiler.Provider;
105                 }
106
107                 public string GetCompilerOptions (string language)
108                 {
109                         Compiler compiler = Compilers [language];
110                         if (compiler == null)
111                                 return null;
112
113                         return compiler.CompilerOptions;
114                 }
115
116                 public int GetWarningLevel (string language)
117                 {
118                         Compiler compiler = Compilers [language];
119                         if (compiler == null)
120                                 return 0;
121
122                         return compiler.WarningLevel;
123                 }
124
125                 void Init (CompilationConfiguration parent)
126                 {
127                         debug = parent.debug;
128                         batch = parent.batch;
129                         batch_timeout = parent.batch_timeout;
130                         default_language = parent.default_language;
131                         _explicit = parent._explicit;
132                         max_batch_size = parent.max_batch_size;
133                         max_batch_file_size = parent.max_batch_file_size;
134                         num_recompiles_before_app_restart = parent.num_recompiles_before_app_restart;
135                         strict = parent.strict;
136                         temp_directory = parent.temp_directory;
137 #if NET_2_0
138                         compilers = new CompilerCollection ();
139 #else
140                         compilers = new CompilerCollection (parent.compilers);
141 #endif
142                         ArrayList p = parent.assemblies;
143                         assembliesInBin = parent.assembliesInBin;
144                         ICollection coll = (p == null) ? (ICollection) new object [0] : p;
145                         assemblies = new ArrayList (coll);
146                 }
147
148                 public bool Debug {
149                         get { return debug; }
150                         set { debug = value; }
151                 }
152
153                 public bool Batch {
154                         get { return batch; }
155                         set { batch = value; }
156                 }
157
158                 public int BatchTimeout {
159                         get { return batch_timeout; }
160                         set { batch_timeout = value; }
161                 }
162
163                 public string DefaultLanguage {
164                         get { return default_language; }
165                         set { default_language = value; }
166                 }
167
168                 public bool Explicit {
169                         get { return _explicit; }
170                         set { _explicit = value; }
171                 }
172
173                 public int MaxBatchSize {
174                         get { return max_batch_size; }
175                         set { max_batch_size = value; }
176                 }
177
178                 public int MaxBatchFileSize {
179                         get { return max_batch_file_size; }
180                         set { max_batch_file_size = value; }
181                 }
182
183                 public int NumRecompilesBeforeAppRestart {
184                         get { return num_recompiles_before_app_restart; }
185                         set { num_recompiles_before_app_restart = value; }
186                 }
187
188                 public bool Strict {
189                         get { return strict; }
190                         set { strict = value; }
191                 }
192
193                 public string TempDirectory {
194                         get { return temp_directory; }
195                         set {
196                                 if (value != null && !Directory.Exists (value))
197                                         throw new ArgumentException ("Directory does not exist");
198
199                                 Console.WriteLine ("Dos: '{0}'\n{1}", value, Environment.StackTrace);
200                                 temp_directory = value;
201                         }
202                 }
203
204                 public CompilerCollection Compilers {
205                         get { return compilers; }
206                 }
207
208                 public ArrayList Assemblies {
209                         get { return assemblies; }
210                 }
211
212                 public bool AssembliesInBin {
213                         get { return assembliesInBin; }
214                         set { assembliesInBin = value; }
215                 }
216         }
217 }
218 #endif