* ObjectStateFormatter.cs: Avoid NRE in Serialize. Fixes bug #81851.
[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 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.IO;
32 using System.Reflection;
33 using System.Security.Permissions;
34 using System.Web.Compilation;
35 using System.Web.Configuration;
36 using System.Web.Util;
37
38 namespace System.Web.UI {
39
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public abstract class TemplateControlParser
44 #if NET_2_0
45                 : BaseTemplateParser 
46 #else
47                 : TemplateParser
48 #endif
49         {
50
51                 bool autoEventWireup = true;
52                 bool enableViewState = true;
53 #if NET_2_0
54                 CompilationMode compilationMode = CompilationMode.Always;
55                 TextReader reader;
56 #endif
57
58                 protected TemplateControlParser ()
59                 {
60                         LoadPagesConfigDefaults ();
61                 }
62                 
63                 internal virtual void LoadPagesConfigDefaults ()
64                 {
65 #if NET_2_0
66                         PagesSection ps = PagesConfig;
67 #else
68                         PagesConfiguration ps = PagesConfig;
69 #endif
70
71 #if NET_1_1
72                         autoEventWireup = ps.AutoEventWireup;
73                         enableViewState = ps.EnableViewState;
74 #endif
75 #if NET_2_0
76                         compilationMode = ps.CompilationMode;
77 #endif
78                 }
79                 
80                 internal override void ProcessMainAttributes (Hashtable atts)
81                 {
82                         autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
83                         enableViewState = GetBool (atts, "EnableViewState", enableViewState);
84 #if NET_2_0
85                         string cmode = GetString (atts, "CompilationMode", compilationMode.ToString ());
86                         if (!String.IsNullOrEmpty (cmode)) {
87                                 if (String.Compare (cmode, "always", StringComparison.InvariantCultureIgnoreCase) == 0)
88                                         compilationMode = CompilationMode.Always;
89                                 else if (String.Compare (cmode, "auto", StringComparison.InvariantCultureIgnoreCase) == 0)
90                                         compilationMode = CompilationMode.Auto;
91                                 else if (String.Compare (cmode, "never", StringComparison.InvariantCultureIgnoreCase) == 0)
92                                         compilationMode = CompilationMode.Never;
93                                 else
94                                         ThrowParseException ("Invalid value of the CompilationMode attribute");
95                         }
96 #endif
97                         atts.Remove ("TargetSchema"); // Ignored
98
99                         base.ProcessMainAttributes (atts);
100                 }
101
102                 internal object GetCompiledInstance ()
103                 {
104                         Type type = CompileIntoType ();
105                         if (type == null)
106                                 return null;
107
108                         object ctrl = Activator.CreateInstance (type);
109                         if (ctrl == null)
110                                 return null;
111
112                         HandleOptions (ctrl);
113                         return ctrl;
114                 }
115
116                 internal override void AddDirective (string directive, Hashtable atts)
117                 {
118                         int cmp = String.Compare ("Register", directive, true);
119                         if (cmp == 0) {
120                                 string tagprefix = GetString (atts, "TagPrefix", null);
121                                 if (tagprefix == null || tagprefix.Trim () == "")
122                                         ThrowParseException ("No TagPrefix attribute found.");
123
124                                 string ns = GetString (atts, "Namespace", null);
125                                 string assembly = GetString (atts, "Assembly", null);
126
127 #if !NET_2_0
128                                 if (ns != null && assembly == null)
129                                         ThrowParseException ("Need an Assembly attribute with Namespace.");
130 #endif
131                                 
132                                 if (ns == null && assembly != null)
133                                         ThrowParseException ("Need a Namespace attribute with Assembly.");
134                                 
135                                 if (ns != null) {
136                                         if (atts.Count != 0)
137                                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
138
139                                         RegisterNamespace (tagprefix, ns, assembly);
140                                         return;
141                                 }
142
143                                 string tagname = GetString (atts, "TagName", null);
144                                 string src = GetString (atts, "Src", null);
145
146                                 if (tagname == null && src != null)
147                                         ThrowParseException ("Need a TagName attribute with Src.");
148
149                                 if (tagname != null && src == null)
150                                         ThrowParseException ("Need a Src attribute with TagName.");
151
152                                 if (!src.EndsWith (".ascx"))
153                                         ThrowParseException ("Source file extension for controls must be .ascx");
154
155                                 RegisterCustomControl (tagprefix, tagname, src);
156                                 return;
157                         }
158
159                         cmp = String.Compare ("Reference", directive, true);
160                         if (cmp == 0) {
161                                 string page = GetString (atts, "Page", null);
162                                 string control = GetString (atts, "Control", null);
163
164                                 bool is_page = (page != null);
165                                 if (!is_page && control == null)
166                                         ThrowParseException ("Must provide 'page' or 'control' attribute");
167
168                                 if (is_page && control != null)
169                                         ThrowParseException ("'page' and 'control' are mutually exclusive");
170
171                                 string filepath = (!is_page) ? control : page;
172                                 filepath = MapPath (filepath);
173                                 AddDependency (filepath);
174                                 Type ctype;
175                                 if (is_page) {
176                                         PageParser pp = new PageParser (page, filepath, Context);
177                                         ctype = pp.CompileIntoType ();
178                                 } else {
179                                         ctype = UserControlParser.GetCompiledType (control, filepath, Dependencies, Context);
180                                 }
181
182                                 AddAssembly (ctype.Assembly, true);
183                                 if (atts.Count != 0)
184                                         ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
185
186                                 return;
187                         }
188
189                         base.AddDirective (directive, atts);
190                 }
191
192                 internal override void HandleOptions (object obj)
193                 {
194                         base.HandleOptions (obj);
195
196                         Control ctrl = obj as Control;
197                         ctrl.AutoEventWireup = autoEventWireup;
198                         ctrl.EnableViewState = enableViewState;
199                 }
200
201                 internal bool AutoEventWireup {
202                         get { return autoEventWireup; }
203                 }
204
205                 internal bool EnableViewState {
206                         get { return enableViewState; }
207                 }
208                 
209 #if NET_2_0
210                 internal CompilationMode CompilationMode {
211                         get { return compilationMode; }
212                 }
213                 
214                 internal TextReader Reader {
215                         get { return reader; }
216                         set { reader = value; }
217                 }
218 #endif
219         }
220 }
221