[System.Web] Public event handlers from reference source
[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.Hosting;
38 using System.Web.Util;
39
40 namespace System.Web.UI
41 {
42         // CAS
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         public abstract class TemplateControlParser : BaseTemplateParser 
46         {
47                 bool autoEventWireup = true;
48                 bool enableViewState = true;
49                 CompilationMode compilationMode = CompilationMode.Always;
50                 ClientIDMode? clientIDMode;
51                 TextReader reader;
52
53                 protected TemplateControlParser ()
54                 {
55                         LoadConfigDefaults ();
56                 }
57                 
58                 internal override void LoadConfigDefaults ()
59                 {
60                         base.LoadConfigDefaults ();
61                         PagesSection ps = PagesConfig;
62                         autoEventWireup = ps.AutoEventWireup;
63                         enableViewState = ps.EnableViewState;
64                         compilationMode = ps.CompilationMode;
65                 }
66                 
67                 internal override void ProcessMainAttributes (IDictionary atts)
68                 {
69                         autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
70                         enableViewState = GetBool (atts, "EnableViewState", enableViewState);
71
72                         string value = GetString (atts, "CompilationMode", compilationMode.ToString ());
73                         if (!String.IsNullOrEmpty (value)) {
74                                 try {
75                                         compilationMode = (CompilationMode) Enum.Parse (typeof (CompilationMode), value, true);
76                                 } catch (Exception ex) {
77                                         ThrowParseException ("Invalid value of the CompilationMode attribute.", ex);
78                                 }
79                         }
80                         
81                         atts.Remove ("TargetSchema"); // Ignored
82                         value = GetString (atts, "ClientIDMode", null);
83                         if (!String.IsNullOrEmpty (value)) {
84                                 try {
85                                         clientIDMode = (ClientIDMode) Enum.Parse (typeof (ClientIDMode), value, true);
86                                 } catch (Exception ex) {
87                                         ThrowParseException ("Invalid value of the ClientIDMode attribute.", ex);
88                                 }
89                         }
90                         base.ProcessMainAttributes (atts);
91                 }
92
93                 internal object GetCompiledInstance ()
94                 {
95                         Type type = CompileIntoType ();
96                         if (type == null)
97                                 return null;
98
99                         object ctrl = Activator.CreateInstance (type);
100                         if (ctrl == null)
101                                 return null;
102
103                         HandleOptions (ctrl);
104                         return ctrl;
105                 }
106
107                 internal override void AddDirective (string directive, IDictionary atts)
108                 {
109                         int cmp = String.Compare ("Register", directive, true, Helpers.InvariantCulture);
110                         if (cmp == 0) {
111                                 string tagprefix = GetString (atts, "TagPrefix", null);
112                                 if (tagprefix == null || tagprefix.Trim () == "")
113                                         ThrowParseException ("No TagPrefix attribute found.");
114
115                                 string ns = GetString (atts, "Namespace", null);
116                                 string assembly = GetString (atts, "Assembly", null);
117
118                                 if (ns == null && assembly != null)
119                                         ThrowParseException ("Need a Namespace attribute with Assembly.");
120                                 
121                                 if (ns != null) {
122                                         if (atts.Count != 0)
123                                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
124
125                                         RegisterNamespace (tagprefix, ns, assembly);
126                                         return;
127                                 }
128
129                                 string tagname = GetString (atts, "TagName", null);
130                                 string src = GetString (atts, "Src", null);
131
132                                 if (tagname == null && src != null)
133                                         ThrowParseException ("Need a TagName attribute with Src.");
134
135                                 if (tagname != null && src == null)
136                                         ThrowParseException ("Need a Src attribute with TagName.");
137
138                                 RegisterCustomControl (tagprefix, tagname, src);
139                                 return;
140                         }
141
142                         cmp = String.Compare ("Reference", directive, true, Helpers.InvariantCulture);
143                         if (cmp == 0) {
144                                 string vp = null;
145                                 string page = GetString (atts, "Page", null);
146                                 bool is_page = (page != null);
147
148                                 if (is_page)
149                                         vp = page;
150
151                                 bool dupe = false;
152                                 string control = GetString (atts, "Control", null);
153                                 if (control != null)
154                                         if (is_page)
155                                                 dupe = true;
156                                         else
157                                                 vp = control;
158                                 
159                                 string virtualPath = GetString (atts, "VirtualPath", null);
160                                 if (virtualPath != null)
161                                         if (vp != null)
162                                                 dupe = true;
163                                         else
164                                                 vp = virtualPath;
165                                 
166                                 if (vp == null)
167                                         ThrowParseException ("Must provide one of the 'page', 'control' or 'virtualPath' attributes");
168                                 
169                                 if (dupe)
170                                         ThrowParseException ("Only one attribute can be specified.");
171
172                                 vp = HostingEnvironment.VirtualPathProvider.CombineVirtualPaths (VirtualPath.Absolute, vp);
173                                 AddDependency (vp, false);
174                                 
175                                 Type ctype;
176                                 ctype = BuildManager.GetCompiledType (vp);
177                                 
178                                 AddAssembly (ctype.Assembly, true);
179                                 if (atts.Count != 0)
180                                         ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
181
182                                 return;
183                         }
184
185                         base.AddDirective (directive, atts);
186                 }
187
188                 internal override void HandleOptions (object obj)
189                 {
190                         base.HandleOptions (obj);
191
192                         Control ctrl = obj as Control;
193                         ctrl.AutoEventWireup = autoEventWireup;
194                         ctrl.EnableViewState = enableViewState;
195                 }
196
197                 internal bool AutoEventWireup {
198                         get { return autoEventWireup; }
199                 }
200
201                 internal bool EnableViewState {
202                         get { return enableViewState; }
203                 }
204                 
205                 internal CompilationMode CompilationMode {
206                         get { return compilationMode; }
207                 }               
208                 internal ClientIDMode? ClientIDMode {
209                         get { return clientIDMode; }
210                 }
211                 internal override TextReader Reader {
212                         get { return reader; }
213                         set { reader = value; }
214                 }
215         }
216 }
217