[asp.net] Implemented the 4.0 type, FileLevelMasterPageControlBuilder
[mono.git] / mcs / class / System.Web / System.Web.UI / MasterPage.cs
1 //
2 // System.Web.UI.MasterPage.cs
3 //
4 // Authors:
5 //   Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005-2010 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 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.ComponentModel;
35 using System.Web.Compilation;
36 using System.Web.Util;
37 using System.Web.UI.WebControls;
38 using System.IO;
39
40 namespace System.Web.UI
41 {
42         [ParseChildren (false)]
43 #if notyet
44         [Designer (...)]
45 #endif
46         [ControlBuilder (typeof (MasterPageControlBuilder))]
47         public class MasterPage: UserControl
48         {
49                 Hashtable definedContentTemplates = new Hashtable ();
50                 Hashtable templates = new Hashtable ();
51                 List <string> placeholders;
52                 string parentMasterPageFile = null;
53                 MasterPage parentMasterPage;
54
55                 [EditorBrowsable (EditorBrowsableState.Advanced)]
56                 protected internal void AddContentTemplate (string templateName, ITemplate template)
57                 {
58                         // LAMESPEC: should be ArgumentException
59                         if (definedContentTemplates.ContainsKey (templateName))
60                                 throw new HttpException ("Multiple contents applied to " + templateName);
61
62                         definedContentTemplates [templateName] = template;
63                 }
64                 
65                 [Browsable (false)]
66                 [EditorBrowsable (EditorBrowsableState.Advanced)]
67                 protected internal IList ContentPlaceHolders {
68                         get {
69                                 if (placeholders == null)
70                                         placeholders = new List <string> ();
71                                 return placeholders;
72                         }
73                 }
74                 
75                 [Browsable (false)]
76                 [EditorBrowsable (EditorBrowsableState.Advanced)]
77                 protected internal IDictionary ContentTemplates {
78                         get { return templates; }
79                 }
80                 
81                 [DefaultValueAttribute ("")]
82                 public string MasterPageFile {
83                         get { return parentMasterPageFile; }
84                         set {
85                                 parentMasterPageFile = value;
86                                 parentMasterPage = null;
87                         }
88                 }
89
90                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
91                 [BrowsableAttribute (false)]
92                 public MasterPage Master {
93                         get {
94                                 if (parentMasterPage == null && parentMasterPageFile != null)
95                                         parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
96                         
97                                 return parentMasterPage;
98                         }
99                 }               
100 #if NET_4_0
101                 public void InstantiateInContentPlaceHolder (Control contentPlaceHolder, ITemplate template)
102                 {
103                         // .NET compatibility...
104                         if (contentPlaceHolder == null || template == null)
105                                 throw new NullReferenceException ();
106
107                         if (contentPlaceHolder != null && template != null)
108                                 template.InstantiateIn (contentPlaceHolder);
109                 }
110 #endif
111                 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
112                                                              string masterPageFile, IDictionary contentTemplateCollection)
113                 {
114 #if TARGET_JVM
115                         MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
116                                                                                             owner.Page.MapPath (masterPageFile),
117                                                                                             context);
118 #else
119                         MasterPage masterPage = BuildManager.CreateInstanceFromVirtualPath (masterPageFile, typeof (MasterPage)) as MasterPage;
120 #endif
121                         if (masterPage == null)
122                                 throw new HttpException ("Failed to create MasterPage instance for '" + masterPageFile + "'.");
123
124                         if (contentTemplateCollection != null) {
125                                 foreach (string templateName in contentTemplateCollection.Keys) {
126                                         if (masterPage.ContentTemplates [templateName] == null)
127                                                 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
128                                 }
129                         }
130                         
131                         masterPage.Page = owner.Page;
132                         masterPage.InitializeAsUserControlInternal ();
133
134                         List <string> placeholders = masterPage.placeholders;
135                         if (contentTemplateCollection != null && placeholders != null && placeholders.Count > 0) {
136                                 foreach (string templateName in contentTemplateCollection.Keys) {
137                                         if (!placeholders.Contains (templateName.ToLowerInvariant ())) {
138                                                 throw new HttpException (
139                                                         String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
140                                                                        templateName, masterPageFile));
141                                         }
142                                 }
143                         }
144
145                         return masterPage;
146                 }
147
148                 internal static void ApplyMasterPageRecursive (MasterPage master, List <string> appliedMasterPageFiles)
149                 {
150                         /* XXX need to use virtual paths here? */
151                         if (master.MasterPageFile != null) {
152                                 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
153                                         throw new HttpException ("circular dependency in master page files detected");
154                                 if (master.Master != null) {
155                                         master.Controls.Clear ();
156                                         master.Controls.Add (master.Master);
157                                         appliedMasterPageFiles.Add (master.MasterPageFile);
158                                         MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);
159                                 }
160                         }
161                 }
162         }
163 }