97a817dc88aef2bfe9180aff4a4facc39eeae460
[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 && atts ["VaryByControl"] == null)
296                                         ThrowParseException ("This directive is missing 'VaryByParam' " +
297                                                         "or 'VaryByControl' 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 ONLY_1_1
335                                                 if (this is PageParser)
336                                                         goto default;
337 #endif
338                                                 oc_controls = (string) entry.Value;
339                                                 break;
340                                         case "shared":
341                                                 if (this is PageParser)
342                                                         goto default;
343
344                                                 try {
345                                                         oc_shared = Boolean.Parse ((string) entry.Value);
346                                                 } catch {
347                                                         ThrowParseException ("The 'shared' attribute is case sensitive" +
348                                                                         " and must be set to 'true' or 'false'.");
349                                                 }
350                                                 break;
351                                         default:
352                                                 ThrowParseException ("The '" + key + "' attribute is not " +
353                                                                 "supported by the 'Outputcache' directive.");
354                                                 break;
355                                         }
356                                         
357                                 }
358                                 
359                                 return;
360                         }
361
362                         ThrowParseException ("Unknown directive: " + directive);
363                 }
364
365                 internal Type LoadType (string typeName)
366                 {
367                         Type type = HttpApplication.LoadType (typeName);
368                         if (type == null)
369                                 return null;
370                         Assembly asm = type.Assembly;
371                         string location = asm.Location;
372                         
373                         AddDependency (location);
374                         string dirname = Path.GetDirectoryName (location);
375                         bool doAddAssembly = true;
376                         foreach (string dir in HttpApplication.BinDirectories) {
377                                 if (dirname == dir) {
378                                         doAddAssembly = false;
379                                         break;
380                                 }
381                         }
382
383                         if (doAddAssembly)
384                                 AddAssembly (asm, true);
385
386                         return type;
387                 }
388
389                 void AddAssembliesInBin ()
390                 {
391                         foreach (string s in HttpApplication.BinDirectoryAssemblies)
392                                 assemblies.Add (s);
393                 }
394                 
395                 internal virtual void AddInterface (string iface)
396                 {
397                         if (interfaces == null)
398                                 interfaces = new ArrayList ();
399
400                         if (!interfaces.Contains (iface))
401                                 interfaces.Add (iface);
402                 }
403                 
404                 internal virtual void AddImport (string namesp)
405                 {
406                         if (imports == null)
407                                 imports = new ArrayList ();
408
409                         if (!imports.Contains (namesp))
410                                 imports.Add (namesp);
411                 }
412
413                 internal virtual void AddSourceDependency (string filename)
414                 {
415                         if (dependencies != null && dependencies.Contains (filename)) {
416                                 ThrowParseException ("Circular file references are not allowed. File: " + filename);
417                         }
418
419                         AddDependency (filename);
420                 }
421
422                 internal virtual void AddDependency (string filename)
423                 {
424                         if (filename == "")
425                                 return;
426
427                         if (dependencies == null)
428                                 dependencies = new ArrayList ();
429
430                         if (!dependencies.Contains (filename))
431                                 dependencies.Add (filename);
432                 }
433                 
434                 internal virtual void AddAssembly (Assembly assembly, bool fullPath)
435                 {
436                         if (assembly.Location == "")
437                                 return;
438
439                         if (anames == null)
440                                 anames = new Hashtable ();
441
442                         string name = assembly.GetName ().Name;
443                         string loc = assembly.Location;
444                         if (fullPath) {
445                                 if (!assemblies.Contains (loc)) {
446                                         assemblies.Add (loc);
447                                 }
448
449                                 anames [name] = loc;
450                                 anames [loc] = assembly;
451                         } else {
452                                 if (!assemblies.Contains (name)) {
453                                         assemblies.Add (name);
454                                 }
455
456                                 anames [name] = assembly;
457                         }
458                 }
459
460                 internal virtual Assembly AddAssemblyByFileName (string filename)
461                 {
462                         Assembly assembly = null;
463                         Exception error = null;
464
465                         try {
466                                 assembly = Assembly.LoadFrom (filename);
467                         } catch (Exception e) { error = e; }
468
469                         if (assembly == null)
470                                 ThrowParseException ("Assembly " + filename + " not found", error);
471
472                         AddAssembly (assembly, true);
473                         return assembly;
474                 }
475
476                 internal virtual Assembly AddAssemblyByName (string name)
477                 {
478                         if (anames == null)
479                                 anames = new Hashtable ();
480
481                         if (anames.Contains (name)) {
482                                 object o = anames [name];
483                                 if (o is string)
484                                         o = anames [o];
485
486                                 return (Assembly) o;
487                         }
488
489                         Assembly assembly = null;
490                         Exception error = null;
491                         try {
492                                 assembly = Assembly.Load (name);
493                         } catch (Exception e) { error = e; }
494
495                         if (assembly == null) {
496                                 try {
497                                         assembly = Assembly.LoadWithPartialName (name);
498                                 } catch (Exception e) { error = e; }
499                         }
500                         
501                         if (assembly == null)
502                                 ThrowParseException ("Assembly " + name + " not found", error);
503
504                         AddAssembly (assembly, true);
505                         return assembly;
506                 }
507                 
508                 internal virtual void ProcessMainAttributes (Hashtable atts)
509                 {
510                         atts.Remove ("Description"); // ignored
511 #if NET_1_1
512                         atts.Remove ("CodeBehind");  // ignored
513 #endif
514                         atts.Remove ("AspCompat"); // ignored
515 #if NET_2_0
516                         // these two are ignored for the moment
517                         atts.Remove ("Async");
518                         atts.Remove ("AsyncTimeOut");
519 #endif
520                         
521                         debug = GetBool (atts, "Debug", true);
522                         compilerOptions = GetString (atts, "CompilerOptions", "");
523                         language = GetString (atts, "Language", "");
524                         if (language.Length != 0)
525                                 implicitLanguage = false;
526                         else
527                                 language = CompilationConfig.DefaultLanguage;
528                         
529                         strictOn = GetBool (atts, "Strict", CompilationConfig.Strict);
530                         explicitOn = GetBool (atts, "Explicit", CompilationConfig.Explicit);
531                         linePragmasOn = GetBool (atts, "LinePragmas", false);
532                         
533                         string inherits = GetString (atts, "Inherits", null);
534 #if NET_2_0
535                         // In ASP 2, the source file is actually integrated with
536                         // the generated file via the use of partial classes. This
537                         // means that the code file has to be confirmed, but not
538                         // used at this point.
539                         src = GetString (atts, "CodeFile", null);
540                         codeFileBaseClass = GetString (atts, "CodeFileBaseClass", null);
541
542                         if (src == null && codeFileBaseClass != null)
543                                 ThrowParseException ("The 'CodeFileBaseClass' attribute cannot be used without a 'CodeFile' attribute");
544                         
545                         if (src != null && inherits != null) {
546                                 // Make sure the source exists
547                                 src = UrlUtils.Combine (BaseVirtualDir, src);
548                                 string realPath = MapPath (src, false);
549                                 if (!File.Exists (realPath))
550                                         ThrowParseException ("File " + src + " not found");
551
552                                 // We are going to create a partial class that shares
553                                 // the same name as the inherits tag, so reset the
554                                 // name. The base type is changed because it is the
555                                 // code file's responsibilty to extend the classes
556                                 // needed.
557                                 partialClassName = inherits;
558
559                                 // Add the code file as an option to the
560                                 // compiler. This lets both files be compiled at once.
561                                 compilerOptions += " \"" + realPath + "\"";
562
563                                 if (codeFileBaseClass != null) {
564                                         try {
565                                                 codeFileBaseClassType = LoadType (codeFileBaseClass);
566                                         } catch (Exception) {
567                                         }
568
569                                         if (codeFileBaseClassType == null)
570                                                 ThrowParseException ("Could not load type '{0}'", codeFileBaseClass);
571                                 }
572                         } else if (inherits != null) {
573                                 // We just set the inherits directly because this is a
574                                 // Single-Page model.
575                                 SetBaseType (inherits);
576                         }
577 #else
578                         string src = GetString (atts, "Src", null);
579
580                         if (src != null)
581                                 srcAssembly = GetAssemblyFromSource (src);
582
583                         if (inherits != null)
584                                 SetBaseType (inherits);
585 #endif
586                         className = GetString (atts, "ClassName", null);
587                         if (className != null) {
588 #if NET_2_0
589                                 string [] identifiers = className.Split ('.');
590                                 for (int i = 0; i < identifiers.Length; i++)
591                                         if (!CodeGenerator.IsValidLanguageIndependentIdentifier (identifiers [i]))
592                                                 ThrowParseException (String.Format ("'{0}' is not a valid "
593                                                         + "value for attribute 'classname'.", className));
594 #else
595                                 if (!CodeGenerator.IsValidLanguageIndependentIdentifier (className))
596                                         ThrowParseException (String.Format ("'{0}' is not a valid "
597                                                 + "value for attribute 'classname'.", className));
598 #endif
599                         }
600
601 #if NET_2_0
602                         if (this is TemplateControlParser)
603                                 metaResourceKey = GetString (atts, "meta:resourcekey", null);
604                         
605                         if (inherits != null && (this is PageParser || this is UserControlParser) && atts.Count > 0) {
606                                 if (unknownMainAttributes == null)
607                                         unknownMainAttributes = new List <UnknownAttributeDescriptor> ();
608                                 string key, val;
609                                 
610                                 foreach (DictionaryEntry de in atts) {
611                                         key = de.Key as string;
612                                         val = de.Value as string;
613                                         
614                                         if (String.IsNullOrEmpty (key) || String.IsNullOrEmpty (val))
615                                                 continue;
616                                         CheckUnknownAttribute (key, val, inherits);
617                                 }
618                                 return;
619                         }
620 #endif
621                         if (atts.Count > 0)
622                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
623                 }
624
625 #if NET_2_0
626                 void CheckUnknownAttribute (string name, string val, string inherits)
627                 {
628                         MemberInfo mi = null;
629                         bool missing = false;
630                         string memberName = name.Trim ().ToLower (CultureInfo.InvariantCulture);
631                         Type parent = codeFileBaseClassType;
632
633                         if (parent == null)
634                                 parent = baseType;
635                         
636                         try {
637                                 MemberInfo[] infos = parent.GetMember (memberName,
638                                                                        MemberTypes.Field | MemberTypes.Property,
639                                                                        BindingFlags.Public | BindingFlags.Instance |
640                                                                        BindingFlags.IgnoreCase | BindingFlags.Static);
641                                 if (infos.Length != 0) {
642                                         // prefer public properties to public methods (it's what MS.NET does)
643                                         foreach (MemberInfo tmp in infos) {
644                                                 if (tmp is PropertyInfo) {
645                                                         mi = tmp;
646                                                         break;
647                                                 }
648                                         }
649                                         if (mi == null)
650                                                 mi = infos [0];
651                                 } else
652                                         missing = true;
653                         } catch (Exception) {
654                                 missing = true;
655                         }
656                         if (missing)
657                                 ThrowParseException (
658                                         "Error parsing attribute '{0}': Type '{1}' does not have a public property named '{0}'",
659                                         memberName, inherits);
660                         
661                         Type memberType = null;
662                         if (mi is PropertyInfo) {
663                                 PropertyInfo pi = mi as PropertyInfo;
664                                 
665                                 if (!pi.CanWrite)
666                                         ThrowParseException (
667                                                 "Error parsing attribute '{0}': The '{0}' property is read-only and cannot be set.",
668                                                 memberName);
669                                 memberType = pi.PropertyType;
670                         } else if (mi is FieldInfo) {
671                                 memberType = ((FieldInfo)mi).FieldType;
672                         } else
673                                 ThrowParseException ("Could not determine member the kind of '{0}' in base type '{1}",
674                                                      memberName, inherits);
675                         TypeConverter converter = TypeDescriptor.GetConverter (memberType);
676                         bool convertible = true;
677                         object value = null;
678                         
679                         if (converter == null || !converter.CanConvertFrom (typeof (string)))
680                                 convertible = false;
681
682                         if (convertible) {
683                                 try {
684                                         value = converter.ConvertFromInvariantString (val);
685                                 } catch (Exception) {
686                                         convertible = false;
687                                 }
688                         }
689
690                         if (!convertible)
691                                 ThrowParseException ("Error parsing attribute '{0}': Cannot create an object of type '{1}' from its string representation '{2}' for the '{3}' property.",
692                                                      memberName, memberType, val, mi.Name);
693                         
694                         UnknownAttributeDescriptor desc = new UnknownAttributeDescriptor (mi, value);
695                         unknownMainAttributes.Add (desc);
696                 }
697 #endif
698                 
699                 internal void SetBaseType (string type)
700                 {
701                         if (type == DefaultBaseTypeName)
702                                 return;
703
704                         Type parent = null;
705                         if (srcAssembly != null)
706                                 parent = srcAssembly.GetType (type);
707
708                         if (parent == null)
709                                 parent = LoadType (type);
710
711                         if (parent == null)
712                                 ThrowParseException ("Cannot find type " + type);
713
714                         if (!DefaultBaseType.IsAssignableFrom (parent))
715                                 ThrowParseException ("The parent type does not derive from " + DefaultBaseType);
716
717                         baseType = parent;
718                         if (parent.FullName.IndexOf ('.') == -1)
719                                 baseTypeIsGlobal = true;
720                 }
721
722                 internal void SetLanguage (string language)
723                 {
724                         this.language = language;
725                         implicitLanguage = false;
726                 }
727                 
728                 Assembly GetAssemblyFromSource (string vpath)
729                 {
730                         vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
731                         string realPath = MapPath (vpath, false);
732                         if (!File.Exists (realPath))
733                                 ThrowParseException ("File " + vpath + " not found");
734
735                         AddSourceDependency (realPath);
736
737                         CompilerResults result = CachingCompiler.Compile (language, realPath, realPath, assemblies);
738                         if (result.NativeCompilerReturnValue != 0) {
739                                 StreamReader reader = new StreamReader (realPath);
740                                 throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
741                         }
742
743                         AddAssembly (result.CompiledAssembly, true);
744                         return result.CompiledAssembly;
745                 }
746                 
747                 internal abstract Type DefaultBaseType { get; }
748                 internal abstract string DefaultBaseTypeName { get; }
749                 internal abstract string DefaultDirectiveName { get; }
750
751                 internal string InputFile
752                 {
753                         get { return inputFile; }
754                         set { inputFile = value; }
755                 }
756
757 #if NET_2_0
758                 internal bool IsPartial {
759                         get { return src != null; }
760                 }
761
762                 internal string PartialClassName {
763                         get { return partialClassName; }
764                 }
765
766                 internal string CodeFileBaseClass {
767                         get { return codeFileBaseClass; }
768                 }
769
770                 internal string MetaResourceKey {
771                         get { return metaResourceKey; }
772                 }
773                 
774                 internal Type CodeFileBaseClassType
775                 {
776                         get { return codeFileBaseClassType; }
777                 }
778                 
779                 internal List <UnknownAttributeDescriptor> UnknownMainAttributes
780                 {
781                         get { return unknownMainAttributes; }
782                 }
783 #endif
784
785                 internal string Text
786                 {
787                         get { return text; }
788                         set { text = value; }
789                 }
790
791                 internal Type BaseType
792                 {
793                         get {
794                                 if (baseType == null)
795                                         baseType = DefaultBaseType;
796
797                                 return baseType;
798                         }
799                 }
800                 
801                 internal bool BaseTypeIsGlobal {
802                         get { return baseTypeIsGlobal; }
803                 }
804                 
805                 internal string ClassName {
806                         get {
807                                 if (className != null)
808                                         return className;
809
810 #if NET_2_0
811                                 string physPath = HttpContext.Current.Request.PhysicalApplicationPath;
812                                 
813                                 if (StrUtils.StartsWith (inputFile, physPath)) {
814                                         className = inputFile.Substring (physPath.Length).ToLower (CultureInfo.InvariantCulture);
815                                         className = className.Replace ('.', '_');
816                                         className = className.Replace ('/', '_').Replace ('\\', '_');
817                                 } else
818 #endif
819                                 className = Path.GetFileName (inputFile).Replace ('.', '_');
820                                 className = className.Replace ('-', '_'); 
821                                 className = className.Replace (' ', '_');
822
823                                 if (Char.IsDigit(className[0])) {
824                                         className = "_" + className;
825                                 }
826
827                                 return className;
828                         }
829                 }
830
831                 internal ArrayList Scripts {
832                         get {
833                                 if (scripts == null)
834                                         scripts = new ArrayList ();
835
836                                 return scripts;
837                         }
838                 }
839
840                 internal ArrayList Imports {
841                         get { return imports; }
842                 }
843
844                 internal ArrayList Assemblies {
845                         get {
846                                 if (appAssemblyIndex != -1) {
847                                         object o = assemblies [appAssemblyIndex];
848                                         assemblies.RemoveAt (appAssemblyIndex);
849                                         assemblies.Add (o);
850                                         appAssemblyIndex = -1;
851                                 }
852
853                                 return assemblies;
854                         }
855                 }
856
857                 internal ArrayList Interfaces {
858                         get { return interfaces; }
859                 }
860
861                 internal RootBuilder RootBuilder {
862                         get { return rootBuilder; }
863                         set { rootBuilder = value; }
864                 }
865
866                 internal ArrayList Dependencies {
867                         get { return dependencies; }
868                         set { dependencies = value; }
869                 }
870
871                 internal string CompilerOptions {
872                         get { return compilerOptions; }
873                 }
874
875                 internal string Language {
876                         get { return language; }
877                 }
878
879                 internal bool ImplicitLanguage {
880                         get { return implicitLanguage; }
881                 }
882                 
883                 internal bool StrictOn {
884                         get { return strictOn; }
885                 }
886
887                 internal bool ExplicitOn {
888                         get { return explicitOn; }
889                 }
890                 
891                 internal bool Debug {
892                         get { return debug; }
893                 }
894
895                 internal bool OutputCache {
896                         get { return output_cache; }
897                 }
898
899                 internal int OutputCacheDuration {
900                         get { return oc_duration; }
901                 }
902
903                 internal string OutputCacheVaryByHeader {
904                         get { return oc_header; }
905                 }
906
907                 internal string OutputCacheVaryByCustom {
908                         get { return oc_custom; }
909                 }
910
911                 internal string OutputCacheVaryByControls {
912                         get { return oc_controls; }
913                 }
914                 
915                 internal bool OutputCacheShared {
916                         get { return oc_shared; }
917                 }
918                 
919                 internal OutputCacheLocation OutputCacheLocation {
920                         get { return oc_location; }
921                 }
922
923                 internal string OutputCacheVaryByParam {
924                         get { return oc_param; }
925                 }
926
927 #if NET_2_0
928                 internal PagesSection PagesConfig {
929                         get {
930                                 return WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
931                         }
932                 }
933 #else
934                 internal PagesConfiguration PagesConfig {
935                         get { return PagesConfiguration.GetInstance (Context); }
936                 }
937 #endif
938         }
939 }
940