2007-09-05 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / TemplateParser.cs
1 //
2 // System.Web.UI.TemplateParser
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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 using System.CodeDom.Compiler;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Globalization;
35 using System.IO;
36 using System.Reflection;
37 using System.Security.Permissions;
38 using System.Web.Compilation;
39 using System.Web.Configuration;
40 using System.Web.Util;
41
42 #if NET_2_0
43 using System.Collections.Generic;
44 #endif
45
46 namespace System.Web.UI {
47
48         // CAS
49         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
50         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
51         public abstract class TemplateParser : BaseParser
52         {
53                 string inputFile;
54                 string text;
55                 string privateBinPath;
56                 Hashtable mainAttributes;
57                 ArrayList dependencies;
58                 ArrayList assemblies;
59                 Hashtable anames;
60                 ArrayList imports;
61                 ArrayList interfaces;
62                 ArrayList scripts;
63                 Type baseType;
64                 bool baseTypeIsGlobal;
65                 string className;
66                 RootBuilder rootBuilder;
67                 bool debug;
68                 string compilerOptions;
69                 string language;
70                 bool implicitLanguage;
71                 bool strictOn = false;
72                 bool explicitOn = false;
73                 bool linePragmasOn = false;
74                 bool output_cache;
75                 int oc_duration;
76                 string oc_header, oc_custom, oc_param, oc_controls;
77                 bool oc_shared;
78                 OutputCacheLocation oc_location;
79                 CultureInfo invariantCulture = CultureInfo.InvariantCulture;
80 #if NET_2_0
81                 string src;
82                 string partialClassName;
83                 string codeFileBaseClass;
84                 string metaResourceKey;
85                 Type codeFileBaseClassType;
86                 List <UnknownAttributeDescriptor> unknownMainAttributes;
87 #endif
88                 Assembly srcAssembly;
89                 int appAssemblyIndex = -1;
90
91                 internal TemplateParser ()
92                 {
93                         imports = new ArrayList ();
94 #if NET_2_0
95                         AddNamespaces (imports);
96 #else
97                         imports.Add ("System");
98                         imports.Add ("System.Collections");
99                         imports.Add ("System.Collections.Specialized");
100                         imports.Add ("System.Configuration");
101                         imports.Add ("System.Text");
102                         imports.Add ("System.Text.RegularExpressions");
103                         imports.Add ("System.Web");
104                         imports.Add ("System.Web.Caching");
105                         imports.Add ("System.Web.Security");
106                         imports.Add ("System.Web.SessionState");
107                         imports.Add ("System.Web.UI");
108                         imports.Add ("System.Web.UI.WebControls");
109                         imports.Add ("System.Web.UI.HtmlControls");
110 #endif
111
112                         assemblies = new ArrayList ();
113 #if NET_2_0
114                         bool addAssembliesInBin = false;
115                         foreach (AssemblyInfo info in CompilationConfig.Assemblies) {
116                                 if (info.Assembly == "*")
117                                         addAssembliesInBin = true;
118                                 else
119                                         AddAssemblyByName (info.Assembly);
120                         }
121                         if (addAssembliesInBin)
122                                 AddAssembliesInBin ();
123
124                         foreach (NamespaceInfo info in PagesConfig.Namespaces) {
125                                 imports.Add (info.Namespace);
126                         }
127 #else
128                         foreach (string a in CompilationConfig.Assemblies)
129                                 AddAssemblyByName (a);
130                         if (CompilationConfig.AssembliesInBin)
131                                 AddAssembliesInBin ();
132 #endif
133
134                         language = CompilationConfig.DefaultLanguage;
135                         implicitLanguage = true;
136                 }
137                 
138                 internal void AddApplicationAssembly ()
139                 {
140                         if (Context.ApplicationInstance == null)
141                                 return; // this may happen if we have Global.asax and have
142                                         // controls registered from Web.Config
143                         string location = Context.ApplicationInstance.AssemblyLocation;
144                         if (location != typeof (TemplateParser).Assembly.Location) {
145                                 appAssemblyIndex = assemblies.Add (location);
146                         }
147                 }
148
149                 protected abstract Type CompileIntoType ();
150
151 #if NET_2_0
152                 void AddNamespaces (ArrayList imports)
153                 {
154                         if (BuildManager.HaveResources)
155                                 imports.Add ("System.Resources");
156                         
157                         PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
158                         if (pages == null)
159                                 return;
160
161                         NamespaceCollection namespaces = pages.Namespaces;
162                         if (namespaces == null || namespaces.Count == 0)
163                                 return;
164
165                         foreach (NamespaceInfo nsi in namespaces)
166                                 imports.Add (nsi.Namespace);
167                 }
168 #endif
169                 
170                 internal void RegisterCustomControl (string tagPrefix, string tagName, string src)
171                 {
172                         string realpath = MapPath (src);
173                         if (String.Compare (realpath, inputFile, false, invariantCulture) == 0)
174                                 return;
175                         
176                         if (!File.Exists (realpath))
177                                 throw new ParseException (Location, "Could not find file \"" + realpath + "\".");
178                         string vpath = VirtualPathUtility.Combine (BaseVirtualDir, src);
179                         Type type = null;
180                         AddDependency (realpath);
181                         try {
182                                 ArrayList other_deps = new ArrayList ();
183                                 type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, Context);
184                                 foreach (string s in other_deps) {
185                                         AddDependency (s);
186                                 }
187                         } catch (ParseException pe) {
188                                 if (this is UserControlParser)
189                                         throw new ParseException (Location, pe.Message, pe);
190                                 throw;
191                         }
192
193                         AddAssembly (type.Assembly, true);
194                         RootBuilder.Foundry.RegisterFoundry (tagPrefix, tagName, type);
195                 }
196
197                 internal void RegisterNamespace (string tagPrefix, string ns, string assembly)
198                 {
199                         AddImport (ns);
200                         Assembly ass = null;
201                         
202                         if (assembly != null && assembly.Length > 0) {
203                                 ass = AddAssemblyByName (assembly);
204                                 AddDependency (ass.Location);
205                         }
206                         
207                         RootBuilder.Foundry.RegisterFoundry (tagPrefix, ass, ns);
208                 }
209
210                 internal virtual void HandleOptions (object obj)
211                 {
212                 }
213
214                 internal static string GetOneKey (Hashtable tbl)
215                 {
216                         foreach (object key in tbl.Keys)
217                                 return key.ToString ();
218
219                         return null;
220                 }
221                 
222                 internal virtual void AddDirective (string directive, Hashtable atts)
223                 {
224                         if (String.Compare (directive, DefaultDirectiveName, true) == 0) {
225                                 if (mainAttributes != null)
226                                         ThrowParseException ("Only 1 " + DefaultDirectiveName + " is allowed");
227
228                                 mainAttributes = atts;
229                                 ProcessMainAttributes (mainAttributes);
230                                 return;
231                         }
232
233                         int cmp = String.Compare ("Assembly", directive, true);
234                         if (cmp == 0) {
235                                 string name = GetString (atts, "Name", null);
236                                 string src = GetString (atts, "Src", null);
237
238                                 if (atts.Count > 0)
239                                         ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
240
241                                 if (name == null && src == null)
242                                         ThrowParseException ("You gotta specify Src or Name");
243                                         
244                                 if (name != null && src != null)
245                                         ThrowParseException ("Src and Name cannot be used together");
246
247                                 if (name != null) {
248                                         AddAssemblyByName (name);
249                                 } else {
250                                         GetAssemblyFromSource (src);
251                                 }
252
253                                 return;
254                         }
255
256                         cmp = String.Compare ("Import", directive, true);
257                         if (cmp == 0) {
258                                 string namesp = GetString (atts, "Namespace", null);
259                                 if (atts.Count > 0)
260                                         ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
261                                 
262                                 if (namesp != null && namesp != "")
263                                         AddImport (namesp);
264                                 return;
265                         }
266
267                         cmp = String.Compare ("Implements", directive, true);
268                         if (cmp == 0) {
269                                 string ifacename = GetString (atts, "Interface", "");
270
271                                 if (atts.Count > 0)
272                                         ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
273                                 
274                                 Type iface = LoadType (ifacename);
275                                 if (iface == null)
276                                         ThrowParseException ("Cannot find type " + ifacename);
277
278                                 if (!iface.IsInterface)
279                                         ThrowParseException (iface + " is not an interface");
280
281                                 AddInterface (iface.FullName);
282                                 return;
283                         }
284
285                         cmp = String.Compare ("OutputCache", directive, true);
286                         if (cmp == 0) {
287                                 HttpResponse response = HttpContext.Current.Response;
288                                 if (response != null)
289                                         response.Cache.SetValidUntilExpires (true);
290                                 
291                                 output_cache = true;
292                                 
293                                 if (atts ["Duration"] == null)
294                                         ThrowParseException ("The directive is missing a 'duration' attribute.");
295                                 if (atts ["VaryByParam"] == null)
296                                         ThrowParseException ("This directive is missing a 'VaryByParam' " +
297                                                         "attribute, which should be set to \"none\", \"*\", " +
298                                                         "or a list of name/value pairs.");
299
300                                 foreach (DictionaryEntry entry in atts) {
301                                         string key = (string) entry.Key;
302                                         switch (key.ToLower ()) {
303                                         case "duration":
304                                                 oc_duration = Int32.Parse ((string) entry.Value);
305                                                 if (oc_duration < 1)
306                                                         ThrowParseException ("The 'duration' attribute must be set " +
307                                                                         "to a positive integer value");
308                                                 break;
309                                         case "varybyparam":
310                                                 oc_param = (string) entry.Value;
311                                                 if (String.Compare (oc_param, "none") == 0)
312                                                         oc_param = null;
313                                                 break;
314                                         case "varybyheader":
315                                                 oc_header = (string) entry.Value;
316                                                 break;
317                                         case "varybycustom":
318                                                 oc_custom = (string) entry.Value;
319                                                 break;
320                                         case "location":
321                                                 if (!(this is PageParser))
322                                                         goto default;
323
324                                                 try {
325                                                         oc_location = (OutputCacheLocation) Enum.Parse (
326                                                                 typeof (OutputCacheLocation), (string) entry.Value, true);
327                                                 } catch {
328                                                         ThrowParseException ("The 'location' attribute is case sensitive and " +
329                                                                         "must be one of the following values: Any, Client, " +
330                                                                         "Downstream, Server, None, ServerAndClient.");
331                                                 }
332                                                 break;
333                                         case "varybycontrol":
334                                                 if (this is PageParser)
335                                                         goto default;
336
337                                                 oc_controls = (string) entry.Value;
338                                                 break;
339                                         case "shared":
340                                                 if (this is PageParser)
341                                                         goto default;
342
343                                                 try {
344                                                         oc_shared = Boolean.Parse ((string) entry.Value);
345                                                 } catch {
346                                                         ThrowParseException ("The 'shared' attribute is case sensitive" +
347                                                                         " and must be set to 'true' or 'false'.");
348                                                 }
349                                                 break;
350                                         default:
351                                                 ThrowParseException ("The '" + key + "' attribute is not " +
352                                                                 "supported by the 'Outputcache' directive.");
353                                                 break;
354                                         }
355                                         
356                                 }
357                                 
358                                 return;
359                         }
360
361                         ThrowParseException ("Unknown directive: " + directive);
362                 }
363
364                 internal Type LoadType (string typeName)
365                 {
366                         Type type = HttpApplication.LoadType (typeName);
367                         if (type == null)
368                                 return null;
369                         Assembly asm = type.Assembly;
370                         string location = asm.Location;
371                         
372                         AddDependency (location);
373                         string dirname = Path.GetDirectoryName (location);
374                         bool doAddAssembly = true;
375                         foreach (string dir in HttpApplication.BinDirectories) {
376                                 if (dirname == dir) {
377                                         doAddAssembly = false;
378                                         break;
379                                 }
380                         }
381
382                         if (doAddAssembly)
383                                 AddAssembly (asm, true);
384
385                         return type;
386                 }
387
388                 void AddAssembliesInBin ()
389                 {
390                         foreach (string s in HttpApplication.BinDirectoryAssemblies)
391                                 assemblies.Add (s);
392                 }
393                 
394                 internal virtual void AddInterface (string iface)
395                 {
396                         if (interfaces == null)
397                                 interfaces = new ArrayList ();
398
399                         if (!interfaces.Contains (iface))
400                                 interfaces.Add (iface);
401                 }
402                 
403                 internal virtual void AddImport (string namesp)
404                 {
405                         if (imports == null)
406                                 imports = new ArrayList ();
407
408                         if (!imports.Contains (namesp))
409                                 imports.Add (namesp);
410                 }
411
412                 internal virtual void AddSourceDependency (string filename)
413                 {
414                         if (dependencies != null && dependencies.Contains (filename)) {
415                                 ThrowParseException ("Circular file references are not allowed. File: " + filename);
416                         }
417
418                         AddDependency (filename);
419                 }
420
421                 internal virtual void AddDependency (string filename)
422                 {
423                         if (filename == "")
424                                 return;
425
426                         if (dependencies == null)
427                                 dependencies = new ArrayList ();
428
429                         if (!dependencies.Contains (filename))
430                                 dependencies.Add (filename);
431                 }
432                 
433                 internal virtual void AddAssembly (Assembly assembly, bool fullPath)
434                 {
435                         if (assembly.Location == "")
436                                 return;
437
438                         if (anames == null)
439                                 anames = new Hashtable ();
440
441                         string name = assembly.GetName ().Name;
442                         string loc = assembly.Location;
443                         if (fullPath) {
444                                 if (!assemblies.Contains (loc)) {
445                                         assemblies.Add (loc);
446                                 }
447
448                                 anames [name] = loc;
449                                 anames [loc] = assembly;
450                         } else {
451                                 if (!assemblies.Contains (name)) {
452                                         assemblies.Add (name);
453                                 }
454
455                                 anames [name] = assembly;
456                         }
457                 }
458
459                 internal virtual Assembly AddAssemblyByFileName (string filename)
460                 {
461                         Assembly assembly = null;
462                         Exception error = null;
463
464                         try {
465                                 assembly = Assembly.LoadFrom (filename);
466                         } catch (Exception e) { error = e; }
467
468                         if (assembly == null)
469                                 ThrowParseException ("Assembly " + filename + " not found", error);
470
471                         AddAssembly (assembly, true);
472                         return assembly;
473                 }
474
475                 internal virtual Assembly AddAssemblyByName (string name)
476                 {
477                         if (anames == null)
478                                 anames = new Hashtable ();
479
480                         if (anames.Contains (name)) {
481                                 object o = anames [name];
482                                 if (o is string)
483                                         o = anames [o];
484
485                                 return (Assembly) o;
486                         }
487
488                         Assembly assembly = null;
489                         Exception error = null;
490                         try {
491                                 assembly = Assembly.Load (name);
492                         } catch (Exception e) { error = e; }
493
494                         if (assembly == null) {
495                                 try {
496                                         assembly = Assembly.LoadWithPartialName (name);
497                                 } catch (Exception e) { error = e; }
498                         }
499                         
500                         if (assembly == null)
501                                 ThrowParseException ("Assembly " + name + " not found", error);
502
503                         AddAssembly (assembly, true);
504                         return assembly;
505                 }
506                 
507                 internal virtual void ProcessMainAttributes (Hashtable atts)
508                 {
509                         atts.Remove ("Description"); // ignored
510 #if NET_1_1
511                         atts.Remove ("CodeBehind");  // ignored
512 #endif
513                         atts.Remove ("AspCompat"); // ignored
514 #if NET_2_0
515                         // these two are ignored for the moment
516                         atts.Remove ("Async");
517                         atts.Remove ("AsyncTimeOut");
518 #endif
519                         
520                         debug = GetBool (atts, "Debug", true);
521                         compilerOptions = GetString (atts, "CompilerOptions", "");
522                         language = GetString (atts, "Language", "");
523                         if (language.Length != 0)
524                                 implicitLanguage = false;
525                         else
526                                 language = CompilationConfig.DefaultLanguage;
527                         
528                         strictOn = GetBool (atts, "Strict", CompilationConfig.Strict);
529                         explicitOn = GetBool (atts, "Explicit", CompilationConfig.Explicit);
530                         linePragmasOn = GetBool (atts, "LinePragmas", false);
531                         
532                         string inherits = GetString (atts, "Inherits", null);
533 #if NET_2_0
534                         // In ASP 2, the source file is actually integrated with
535                         // the generated file via the use of partial classes. This
536                         // means that the code file has to be confirmed, but not
537                         // used at this point.
538                         src = GetString (atts, "CodeFile", null);
539                         codeFileBaseClass = GetString (atts, "CodeFileBaseClass", null);
540
541                         if (src == null && codeFileBaseClass != null)
542                                 ThrowParseException ("The 'CodeFileBaseClass' attribute cannot be used without a 'CodeFile' attribute");
543                         
544                         if (src != null && inherits != null) {
545                                 // Make sure the source exists
546                                 src = UrlUtils.Combine (BaseVirtualDir, src);
547                                 string realPath = MapPath (src, false);
548                                 if (!File.Exists (realPath))
549                                         ThrowParseException ("File " + src + " not found");
550
551                                 // We are going to create a partial class that shares
552                                 // the same name as the inherits tag, so reset the
553                                 // name. The base type is changed because it is the
554                                 // code file's responsibilty to extend the classes
555                                 // needed.
556                                 partialClassName = inherits;
557
558                                 // Add the code file as an option to the
559                                 // compiler. This lets both files be compiled at once.
560                                 compilerOptions += " \"" + realPath + "\"";
561
562                                 if (codeFileBaseClass != null) {
563                                         try {
564                                                 codeFileBaseClassType = LoadType (codeFileBaseClass);
565                                         } catch (Exception) {
566                                         }
567
568                                         if (codeFileBaseClassType == null)
569                                                 ThrowParseException ("Could not load type '{0}'", codeFileBaseClass);
570                                 }
571                         } else if (inherits != null) {
572                                 // We just set the inherits directly because this is a
573                                 // Single-Page model.
574                                 SetBaseType (inherits);
575                         }
576 #else
577                         string src = GetString (atts, "Src", null);
578
579                         if (src != null)
580                                 srcAssembly = GetAssemblyFromSource (src);
581
582                         if (inherits != null)
583                                 SetBaseType (inherits);
584 #endif
585                         className = GetString (atts, "ClassName", null);
586                         if (className != null) {
587 #if NET_2_0
588                                 string [] identifiers = className.Split ('.');
589                                 for (int i = 0; i < identifiers.Length; i++)
590                                         if (!CodeGenerator.IsValidLanguageIndependentIdentifier (identifiers [i]))
591                                                 ThrowParseException (String.Format ("'{0}' is not a valid "
592                                                         + "value for attribute 'classname'.", className));
593 #else
594                                 if (!CodeGenerator.IsValidLanguageIndependentIdentifier (className))
595                                         ThrowParseException (String.Format ("'{0}' is not a valid "
596                                                 + "value for attribute 'classname'.", className));
597 #endif
598                         }
599
600 #if NET_2_0
601                         if (this is TemplateControlParser)
602                                 metaResourceKey = GetString (atts, "meta:resourcekey", null);
603                         
604                         if (inherits != null && (this is PageParser || this is UserControlParser) && atts.Count > 0) {
605                                 if (unknownMainAttributes == null)
606                                         unknownMainAttributes = new List <UnknownAttributeDescriptor> ();
607                                 string key, val;
608                                 
609                                 foreach (DictionaryEntry de in atts) {
610                                         key = de.Key as string;
611                                         val = de.Value as string;
612                                         
613                                         if (String.IsNullOrEmpty (key) || String.IsNullOrEmpty (val))
614                                                 continue;
615                                         CheckUnknownAttribute (key, val, inherits);
616                                 }
617                                 return;
618                         }
619 #endif
620                         if (atts.Count > 0)
621                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
622                 }
623
624 #if NET_2_0
625                 void CheckUnknownAttribute (string name, string val, string inherits)
626                 {
627                         MemberInfo mi = null;
628                         bool missing = false;
629                         string memberName = name.Trim ().ToLower (CultureInfo.InvariantCulture);
630                         Type parent = codeFileBaseClassType;
631
632                         if (parent == null)
633                                 parent = baseType;
634                         
635                         try {
636                                 MemberInfo[] infos = parent.GetMember (memberName,
637                                                                        MemberTypes.Field | MemberTypes.Property,
638                                                                        BindingFlags.Public | BindingFlags.Instance |
639                                                                        BindingFlags.IgnoreCase | BindingFlags.Static);
640                                 if (infos.Length != 0) {
641                                         // prefer public properties to public methods (it's what MS.NET does)
642                                         foreach (MemberInfo tmp in infos) {
643                                                 if (tmp is PropertyInfo) {
644                                                         mi = tmp;
645                                                         break;
646                                                 }
647                                         }
648                                         if (mi == null)
649                                                 mi = infos [0];
650                                 } else
651                                         missing = true;
652                         } catch (Exception) {
653                                 missing = true;
654                         }
655                         if (missing)
656                                 ThrowParseException (
657                                         "Error parsing attribute '{0}': Type '{1}' does not have a public property named '{0}'",
658                                         memberName, inherits);
659                         
660                         Type memberType = null;
661                         if (mi is PropertyInfo) {
662                                 PropertyInfo pi = mi as PropertyInfo;
663                                 
664                                 if (!pi.CanWrite)
665                                         ThrowParseException (
666                                                 "Error parsing attribute '{0}': The '{0}' property is read-only and cannot be set.",
667                                                 memberName);
668                                 memberType = pi.PropertyType;
669                         } else if (mi is FieldInfo) {
670                                 memberType = ((FieldInfo)mi).FieldType;
671                         } else
672                                 ThrowParseException ("Could not determine member the kind of '{0}' in base type '{1}",
673                                                      memberName, inherits);
674                         TypeConverter converter = TypeDescriptor.GetConverter (memberType);
675                         bool convertible = true;
676                         object value = null;
677                         
678                         if (converter == null || !converter.CanConvertFrom (typeof (string)))
679                                 convertible = false;
680
681                         if (convertible) {
682                                 try {
683                                         value = converter.ConvertFromInvariantString (val);
684                                 } catch (Exception) {
685                                         convertible = false;
686                                 }
687                         }
688
689                         if (!convertible)
690                                 ThrowParseException ("Error parsing attribute '{0}': Cannot create an object of type '{1}' from its string representation '{2}' for the '{3}' property.",
691                                                      memberName, memberType, val, mi.Name);
692                         
693                         UnknownAttributeDescriptor desc = new UnknownAttributeDescriptor (mi, value);
694                         unknownMainAttributes.Add (desc);
695                 }
696 #endif
697                 
698                 internal void SetBaseType (string type)
699                 {
700                         if (type == DefaultBaseTypeName)
701                                 return;
702
703                         Type parent = null;
704                         if (srcAssembly != null)
705                                 parent = srcAssembly.GetType (type);
706
707                         if (parent == null)
708                                 parent = LoadType (type);
709
710                         if (parent == null)
711                                 ThrowParseException ("Cannot find type " + type);
712
713                         if (!DefaultBaseType.IsAssignableFrom (parent))
714                                 ThrowParseException ("The parent type does not derive from " + DefaultBaseType);
715
716                         baseType = parent;
717                         if (parent.FullName.IndexOf ('.') == -1)
718                                 baseTypeIsGlobal = true;
719                 }
720
721                 internal void SetLanguage (string language)
722                 {
723                         this.language = language;
724                         implicitLanguage = false;
725                 }
726                 
727                 Assembly GetAssemblyFromSource (string vpath)
728                 {
729                         vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
730                         string realPath = MapPath (vpath, false);
731                         if (!File.Exists (realPath))
732                                 ThrowParseException ("File " + vpath + " not found");
733
734                         AddSourceDependency (realPath);
735
736                         CompilerResults result = CachingCompiler.Compile (language, realPath, realPath, assemblies);
737                         if (result.NativeCompilerReturnValue != 0) {
738                                 StreamReader reader = new StreamReader (realPath);
739                                 throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
740                         }
741
742                         AddAssembly (result.CompiledAssembly, true);
743                         return result.CompiledAssembly;
744                 }
745                 
746                 internal abstract Type DefaultBaseType { get; }
747                 internal abstract string DefaultBaseTypeName { get; }
748                 internal abstract string DefaultDirectiveName { get; }
749
750                 internal string InputFile
751                 {
752                         get { return inputFile; }
753                         set { inputFile = value; }
754                 }
755
756 #if NET_2_0
757                 internal bool IsPartial {
758                         get { return src != null; }
759                 }
760
761                 internal string PartialClassName {
762                         get { return partialClassName; }
763                 }
764
765                 internal string CodeFileBaseClass {
766                         get { return codeFileBaseClass; }
767                 }
768
769                 internal string MetaResourceKey {
770                         get { return metaResourceKey; }
771                 }
772                 
773                 internal Type CodeFileBaseClassType
774                 {
775                         get { return codeFileBaseClassType; }
776                 }
777                 
778                 internal List <UnknownAttributeDescriptor> UnknownMainAttributes
779                 {
780                         get { return unknownMainAttributes; }
781                 }
782 #endif
783
784                 internal string Text
785                 {
786                         get { return text; }
787                         set { text = value; }
788                 }
789
790                 internal Type BaseType
791                 {
792                         get {
793                                 if (baseType == null)
794                                         baseType = DefaultBaseType;
795
796                                 return baseType;
797                         }
798                 }
799                 
800                 internal bool BaseTypeIsGlobal {
801                         get { return baseTypeIsGlobal; }
802                 }
803                 
804                 internal string ClassName {
805                         get {
806                                 if (className != null)
807                                         return className;
808
809 #if NET_2_0
810                                 string physPath = HttpContext.Current.Request.PhysicalApplicationPath;
811                                 
812                                 if (StrUtils.StartsWith (inputFile, physPath)) {
813                                         className = inputFile.Substring (physPath.Length).ToLower (CultureInfo.InvariantCulture);
814                                         className = className.Replace ('.', '_');
815                                         className = className.Replace ('/', '_').Replace ('\\', '_');
816                                 } else
817 #endif
818                                 className = Path.GetFileName (inputFile).Replace ('.', '_');
819                                 className = className.Replace ('-', '_'); 
820                                 className = className.Replace (' ', '_');
821
822                                 if (Char.IsDigit(className[0])) {
823                                         className = "_" + className;
824                                 }
825
826                                 return className;
827                         }
828                 }
829
830                 internal ArrayList Scripts {
831                         get {
832                                 if (scripts == null)
833                                         scripts = new ArrayList ();
834
835                                 return scripts;
836                         }
837                 }
838
839                 internal ArrayList Imports {
840                         get { return imports; }
841                 }
842
843                 internal ArrayList Assemblies {
844                         get {
845                                 if (appAssemblyIndex != -1) {
846                                         object o = assemblies [appAssemblyIndex];
847                                         assemblies.RemoveAt (appAssemblyIndex);
848                                         assemblies.Add (o);
849                                         appAssemblyIndex = -1;
850                                 }
851
852                                 return assemblies;
853                         }
854                 }
855
856                 internal ArrayList Interfaces {
857                         get { return interfaces; }
858                 }
859
860                 internal RootBuilder RootBuilder {
861                         get { return rootBuilder; }
862                         set { rootBuilder = value; }
863                 }
864
865                 internal ArrayList Dependencies {
866                         get { return dependencies; }
867                         set { dependencies = value; }
868                 }
869
870                 internal string CompilerOptions {
871                         get { return compilerOptions; }
872                 }
873
874                 internal string Language {
875                         get { return language; }
876                 }
877
878                 internal bool ImplicitLanguage {
879                         get { return implicitLanguage; }
880                 }
881                 
882                 internal bool StrictOn {
883                         get { return strictOn; }
884                 }
885
886                 internal bool ExplicitOn {
887                         get { return explicitOn; }
888                 }
889                 
890                 internal bool Debug {
891                         get { return debug; }
892                 }
893
894                 internal bool OutputCache {
895                         get { return output_cache; }
896                 }
897
898                 internal int OutputCacheDuration {
899                         get { return oc_duration; }
900                 }
901
902                 internal string OutputCacheVaryByHeader {
903                         get { return oc_header; }
904                 }
905
906                 internal string OutputCacheVaryByCustom {
907                         get { return oc_custom; }
908                 }
909
910                 internal string OutputCacheVaryByControls {
911                         get { return oc_controls; }
912                 }
913                 
914                 internal bool OutputCacheShared {
915                         get { return oc_shared; }
916                 }
917                 
918                 internal OutputCacheLocation OutputCacheLocation {
919                         get { return oc_location; }
920                 }
921
922                 internal string OutputCacheVaryByParam {
923                         get { return oc_param; }
924                 }
925
926 #if NET_2_0
927                 internal PagesSection PagesConfig {
928                         get {
929                                 return WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
930                         }
931                 }
932 #else
933                 internal PagesConfiguration PagesConfig {
934                         get { return PagesConfiguration.GetInstance (Context); }
935                 }
936 #endif
937         }
938 }
939