[asp.net] Implemented the 4.0 type, FileLevelMasterPageControlBuilder
[mono.git] / mcs / class / System.Web / System.Web.UI / TemplateControlParser.cs
1 //
2 // System.Web.UI.TemplateControlParser
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Globalization;
32 using System.IO;
33 using System.Reflection;
34 using System.Security.Permissions;
35 using System.Web.Compilation;
36 using System.Web.Configuration;
37 using System.Web.Util;
38
39 namespace System.Web.UI
40 {
41         // CAS
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public abstract class TemplateControlParser : BaseTemplateParser 
45         {
46                 bool autoEventWireup = true;
47                 bool enableViewState = true;
48                 CompilationMode compilationMode = CompilationMode.Always;
49 #if NET_4_0
50                 ClientIDMode? clientIDMode;
51 #endif
52                 TextReader reader;
53
54                 protected TemplateControlParser ()
55                 {
56                         LoadConfigDefaults ();
57                 }
58                 
59                 internal override void LoadConfigDefaults ()
60                 {
61                         base.LoadConfigDefaults ();
62                         PagesSection ps = PagesConfig;
63                         autoEventWireup = ps.AutoEventWireup;
64                         enableViewState = ps.EnableViewState;
65                         compilationMode = ps.CompilationMode;
66                 }
67                 
68                 internal override void ProcessMainAttributes (IDictionary atts)
69                 {
70                         autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
71                         enableViewState = GetBool (atts, "EnableViewState", enableViewState);
72
73                         string value = GetString (atts, "CompilationMode", compilationMode.ToString ());
74                         if (!String.IsNullOrEmpty (value)) {
75                                 try {
76                                         compilationMode = (CompilationMode) Enum.Parse (typeof (CompilationMode), value, true);
77                                 } catch (Exception ex) {
78                                         ThrowParseException ("Invalid value of the CompilationMode attribute.", ex);
79                                 }
80                         }
81                         
82                         atts.Remove ("TargetSchema"); // Ignored
83 #if NET_4_0
84                         value = GetString (atts, "ClientIDMode", null);
85                         if (!String.IsNullOrEmpty (value)) {
86                                 try {
87                                         clientIDMode = (ClientIDMode) Enum.Parse (typeof (ClientIDMode), value, true);
88                                 } catch (Exception ex) {
89                                         ThrowParseException ("Invalid value of the ClientIDMode attribute.", ex);
90                                 }
91                         }
92 #endif
93                         base.ProcessMainAttributes (atts);
94                 }
95
96                 internal object GetCompiledInstance ()
97                 {
98                         Type type = CompileIntoType ();
99                         if (type == null)
100                                 return null;
101
102                         object ctrl = Activator.CreateInstance (type);
103                         if (ctrl == null)
104                                 return null;
105
106                         HandleOptions (ctrl);
107                         return ctrl;
108                 }
109
110                 internal override void AddDirective (string directive, IDictionary atts)
111                 {
112                         int cmp = String.Compare ("Register", directive, true, Helpers.InvariantCulture);
113                         if (cmp == 0) {
114                                 string tagprefix = GetString (atts, "TagPrefix", null);
115                                 if (tagprefix == null || tagprefix.Trim () == "")
116                                         ThrowParseException ("No TagPrefix attribute found.");
117
118                                 string ns = GetString (atts, "Namespace", null);
119                                 string assembly = GetString (atts, "Assembly", null);
120
121                                 if (ns == null && assembly != null)
122                                         ThrowParseException ("Need a Namespace attribute with Assembly.");
123                                 
124                                 if (ns != null) {
125                                         if (atts.Count != 0)
126                                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
127
128                                         RegisterNamespace (tagprefix, ns, assembly);
129                                         return;
130                                 }
131
132                                 string tagname = GetString (atts, "TagName", null);
133                                 string src = GetString (atts, "Src", null);
134
135                                 if (tagname == null && src != null)
136                                         ThrowParseException ("Need a TagName attribute with Src.");
137
138                                 if (tagname != null && src == null)
139                                         ThrowParseException ("Need a Src attribute with TagName.");
140
141                                 RegisterCustomControl (tagprefix, tagname, src);
142                                 return;
143                         }
144
145                         cmp = String.Compare ("Reference", directive, true, Helpers.InvariantCulture);
146                         if (cmp == 0) {
147                                 string vp = null;
148                                 string page = GetString (atts, "Page", null);
149                                 bool is_page = (page != null);
150
151                                 if (is_page)
152                                         vp = page;
153
154                                 bool dupe = false;
155                                 string control = GetString (atts, "Control", null);
156                                 if (control != null)
157                                         if (is_page)
158                                                 dupe = true;
159                                         else
160                                                 vp = control;
161                                 
162                                 string virtualPath = GetString (atts, "VirtualPath", null);
163                                 if (virtualPath != null)
164                                         if (vp != null)
165                                                 dupe = true;
166                                         else
167                                                 vp = virtualPath;
168                                 
169                                 if (vp == null)
170                                         ThrowParseException ("Must provide one of the 'page', 'control' or 'virtualPath' attributes");
171                                 
172                                 if (dupe)
173                                         ThrowParseException ("Only one attribute can be specified.");
174
175                                 AddDependency (vp);
176                                 
177                                 Type ctype;
178                                 ctype = BuildManager.GetCompiledType (vp);
179                                 
180                                 AddAssembly (ctype.Assembly, true);
181                                 if (atts.Count != 0)
182                                         ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
183
184                                 return;
185                         }
186
187                         base.AddDirective (directive, atts);
188                 }
189
190                 internal override void HandleOptions (object obj)
191                 {
192                         base.HandleOptions (obj);
193
194                         Control ctrl = obj as Control;
195                         ctrl.AutoEventWireup = autoEventWireup;
196                         ctrl.EnableViewState = enableViewState;
197                 }
198
199                 internal bool AutoEventWireup {
200                         get { return autoEventWireup; }
201                 }
202
203                 internal bool EnableViewState {
204                         get { return enableViewState; }
205                 }
206                 
207                 internal CompilationMode CompilationMode {
208                         get { return compilationMode; }
209                 }               
210 #if NET_4_0
211                 internal ClientIDMode? ClientIDMode {
212                         get { return clientIDMode; }
213                 }
214 #endif
215                 internal override TextReader Reader {
216                         get { return reader; }
217                         set { reader = value; }
218                 }
219         }
220 }
221