2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / MasterPageParser.cs
1 //
2 // System.Web.UI.MasterPageParser
3 //
4 // Authors:
5 //   Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005 Novell, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32
33 using System;
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.IO;
37 using System.Web;
38 using System.Web.Compilation;
39 using System.Web.Util;
40
41 namespace System.Web.UI
42 {
43         internal sealed class MasterPageParser: UserControlParser
44         {
45                 Type masterType;
46                 List <string> contentPlaceHolderIds;
47                 string cacheEntryName;
48                 
49                 internal MasterPageParser (string virtualPath, string inputFile, HttpContext context)
50                         : base (virtualPath, inputFile, context, "System.Web.UI.MasterPage")
51                 {
52                         this.cacheEntryName = String.Concat ("@@MasterPagePHIDS:", virtualPath, ":", inputFile);
53                         
54                         contentPlaceHolderIds = HttpRuntime.InternalCache.Get (this.cacheEntryName) as List <string>;
55                 }
56
57                 internal MasterPageParser (string virtualPath, TextReader reader, HttpContext context)
58                         : this (virtualPath, null, reader, context)
59                 {
60                 }
61                 
62                 internal MasterPageParser (string virtualPath, string inputFile, TextReader reader, HttpContext context)
63                         : base (virtualPath, inputFile, reader, context)
64                 {
65                         this.cacheEntryName = String.Concat ("@@MasterPagePHIDS:", virtualPath, ":", InputFile);
66                         
67                         contentPlaceHolderIds = HttpRuntime.InternalCache.Get (this.cacheEntryName) as List <string>;
68                 }
69                 
70                 public static MasterPage GetCompiledMasterInstance (string virtualPath, string inputFile, HttpContext context)
71                 {
72                         return BuildManager.CreateInstanceFromVirtualPath (virtualPath, typeof (MasterPage)) as MasterPage;
73                 }
74
75                 public static Type GetCompiledMasterType (string virtualPath, string inputFile, HttpContext context)
76                 {
77                         return BuildManager.GetCompiledType (virtualPath);
78                 }
79                 
80                 internal override void HandleOptions (object obj)
81                 {
82                         base.HandleOptions (obj);
83
84                         MasterPage mp = (MasterPage)obj;
85                         mp.MasterPageFile = MasterPageFile;
86                 }
87
88                 internal override void AddDirective (string directive, Hashtable atts)
89                 {
90                         if (String.Compare ("MasterType", directive, true) == 0) {
91                                 string type = GetString (atts, "TypeName", null);
92                                 if (type != null) {
93                                         masterType = LoadType (type);
94                                         if (masterType == null)
95                                                 ThrowParseException ("Could not load type '" + type + "'.");
96                                 } else {
97                                         string path = GetString (atts, "VirtualPath", null);
98                                         if (path != null) {
99                                                 masterType = BuildManager.GetCompiledType (path);
100                                                 AddDependency (path);
101                                         } else
102                                                 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
103                                 }
104                                 AddAssembly (masterType.Assembly, true);
105                         }
106                         else
107                                 base.AddDirective (directive, atts);
108                 }
109
110                 internal void AddContentPlaceHolderId (string id)
111                 {
112                         if (contentPlaceHolderIds == null) {
113                                 contentPlaceHolderIds = new List <string> (1);
114                                 HttpRuntime.InternalCache.Insert (cacheEntryName, contentPlaceHolderIds);
115                         }
116                         
117                         contentPlaceHolderIds.Add (id);
118                 }
119                 
120                 internal Type MasterType {
121                         get { return masterType; }
122                 }
123
124                 internal override string DefaultBaseTypeName {
125                         get { return "System.Web.UI.MasterPage"; }
126                 }
127
128                 internal override string DefaultDirectiveName {
129                         get { return "master"; }
130                 }
131         }
132 }
133
134 #endif