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