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