Merge branch 'master' of github.com:mono/mono
[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                 
101                 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
102                                                              string masterPageFile, IDictionary contentTemplateCollection)
103                 {
104 #if TARGET_JVM
105                         MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
106                                                                                             owner.Page.MapPath (masterPageFile),
107                                                                                             context);
108 #else
109                         MasterPage masterPage = BuildManager.CreateInstanceFromVirtualPath (masterPageFile, typeof (MasterPage)) as MasterPage;
110 #endif
111                         if (masterPage == null)
112                                 throw new HttpException ("Failed to create MasterPage instance for '" + masterPageFile + "'.");
113
114                         if (contentTemplateCollection != null) {
115                                 foreach (string templateName in contentTemplateCollection.Keys) {
116                                         if (masterPage.ContentTemplates [templateName] == null)
117                                                 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
118                                 }
119                         }
120                         
121                         masterPage.Page = owner.Page;
122                         masterPage.InitializeAsUserControlInternal ();
123
124                         List <string> placeholders = masterPage.placeholders;
125                         if (contentTemplateCollection != null && placeholders != null && placeholders.Count > 0) {
126                                 foreach (string templateName in contentTemplateCollection.Keys) {
127                                         if (!placeholders.Contains (templateName.ToLowerInvariant ())) {
128                                                 throw new HttpException (
129                                                         String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
130                                                                        templateName, masterPageFile));
131                                         }
132                                 }
133                         }
134
135                         return masterPage;
136                 }
137
138                 internal static void ApplyMasterPageRecursive (MasterPage master, List <string> appliedMasterPageFiles)
139                 {
140                         /* XXX need to use virtual paths here? */
141                         if (master.MasterPageFile != null) {
142                                 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
143                                         throw new HttpException ("circular dependency in master page files detected");
144                                 if (master.Master != null) {
145                                         master.Controls.Clear ();
146                                         master.Controls.Add (master.Master);
147                                         appliedMasterPageFiles.Add (master.MasterPageFile);
148                                         MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);
149                                 }
150                         }
151                 }
152         }
153 }