* MasterPage.cs: Include relative URL of MasterPage in exception
[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 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.ComponentModel;
36 using System.Web.Util;
37 using System.IO;
38
39 namespace System.Web.UI
40 {
41         [ParseChildren (false)]
42 #if notyet
43         [Designer (...)]
44 #endif
45         [ControlBuilder (typeof (MasterPageControlBuilder))]
46         public class MasterPage: UserControl
47         {
48                 Hashtable definedContentTemplates = new Hashtable ();
49                 Hashtable templates = new Hashtable ();
50                 ArrayList placeholders = new ArrayList ();
51
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 { return placeholders; }
69                 }
70                 
71                 [Browsable (false)]
72                 [EditorBrowsable (EditorBrowsableState.Advanced)]
73                 protected internal IDictionary ContentTemplates {
74                         get { return templates; }
75                 }
76                 
77                 [DefaultValueAttribute ("")]
78                 public string MasterPageFile {
79                         get { return parentMasterPageFile; }
80                         set {
81                                 parentMasterPageFile = value;
82                                 parentMasterPage = null;
83                         }
84                 }
85
86                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
87                 [BrowsableAttribute (false)]
88                 public MasterPage Master {
89                         get {
90                                 if (parentMasterPage == null && parentMasterPageFile != null)
91                                         parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
92                         
93                                 return parentMasterPage;
94                         }
95                 }
96
97                 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
98                                                              string masterPageFile, IDictionary contentTemplateCollection)
99                 {
100                         MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
101                                                                                             owner.Page.MapPath (masterPageFile),
102                                                                                             context);
103                         if (contentTemplateCollection != null) {
104                                 foreach (string templateName in contentTemplateCollection.Keys) {
105                                         if (!masterPage.ContentPlaceHolders.Contains (templateName))
106                                                 throw new HttpException (
107                                                         String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
108                                                                 templateName, masterPageFile));
109                                         if (masterPage.ContentTemplates[templateName] == null)
110                                                 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
111                                 }
112                         }
113                         masterPage.Page = owner.Page;
114                         masterPage.InitializeAsUserControlInternal ();
115                         return masterPage;
116                 }
117
118                 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
119                 {
120                         /* XXX need to use virtual paths here? */
121                         if (master.MasterPageFile != null) {
122                                 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
123                                         throw new HttpException ("circular dependency in master page files detected");
124                                 if (master.Master != null) {
125                                         master.Controls.Clear ();
126                                         master.Controls.Add (master.Master);
127                                         appliedMasterPageFiles.Add (master.MasterPageFile);
128                                         MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);
129                                 }
130                         }
131                 }
132         }
133 }
134
135 #endif