* MainsoftWebApp20.Tomcat.vmwcsproj: converted to csproj
[mono.git] / mcs / class / System.Web / System.Web.UI / TemplateControl.cs
1 //
2 // System.Web.UI.TemplateControl.cs
3 //
4 // Authors:
5 //   Duncan Mak  (duncan@ximian.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Reflection;
35 using System.Security.Permissions;
36 using System.Web.Compilation;
37 using System.Web.Util;
38 using System.Xml;
39
40 namespace System.Web.UI {
41
42         // CAS
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 #if NET_2_0
46         public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService {
47 #else
48         public abstract class TemplateControl : Control, INamingContainer {
49 #endif
50                 static readonly Assembly _System_Web_Assembly = typeof (TemplateControl).Assembly;
51                 static object abortTransaction = new object ();
52                 static object commitTransaction = new object ();
53                 static object error = new object ();
54                 static string [] methodNames = { "Page_Init",
55 #if NET_2_0
56                                                  "Page_PreInit",
57                                                  "Page_PreLoad",
58                                                  "Page_LoadComplete",
59                                                  "Page_PreRenderComplete",
60                                                  "Page_SaveStateComplete",
61                                                  "Page_InitComplete",
62 #endif
63                                                  "Page_Load",
64                                                  "Page_DataBind",
65                                                  "Page_PreRender",
66                                                  "Page_Disposed",
67                                                  "Page_Error",
68                                                  "Page_Unload",
69                                                  "Page_AbortTransaction",
70                                                  "Page_CommitTransaction"};
71
72                 const BindingFlags bflags = BindingFlags.Public |
73                                             BindingFlags.NonPublic |
74                                             BindingFlags.Instance;
75
76                 #region Constructor
77                 protected TemplateControl ()
78                 {
79                         Construct ();
80                 }
81
82                 #endregion
83
84                 #region Properties
85                 [EditorBrowsable (EditorBrowsableState.Never)]
86 #if NET_2_0
87                 [Obsolete]
88 #endif
89                 protected virtual int AutoHandlers {
90                         get { return 0; }
91                         set { }
92                 }
93
94                 [EditorBrowsable (EditorBrowsableState.Never)]
95                 protected virtual bool SupportAutoEvents {
96                         get { return true; }
97                 }
98
99 #if NET_2_0
100                 
101                 [MonoTODO ("NotImplementedException")]
102                 public string AppRelativeVirtualPath {
103                         get { throw new NotImplementedException(); }
104                         set { throw new NotImplementedException (); }
105                 }
106 #endif
107
108                 #endregion
109
110                 #region Methods
111
112                 protected virtual void Construct ()
113                 {
114                 }
115
116                 [MonoTODO ("Not implemented")]
117                 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
118                                                                                     int size,
119                                                                                     bool fAsciiOnly)
120                 {
121                         return null;
122                 }
123
124                 internal void WireupAutomaticEvents ()
125                 {
126                         if (!SupportAutoEvents || !AutoEventWireup)
127                                 return;
128
129                         foreach (string methodName in methodNames) {
130                                 MethodInfo method = null;
131                                 Type type;
132                                 for (type = GetType (); type.Assembly != _System_Web_Assembly; type = type.BaseType) {
133                                         method = type.GetMethod (methodName, bflags);
134                                         if (method != null)
135                                                 break;
136                                 }
137                                 if (method == null)
138                                         continue;
139
140                                 if (method.DeclaringType != type) {
141                                         if (!method.IsPublic && !method.IsFamilyOrAssembly &&
142                                             !method.IsFamilyAndAssembly && !method.IsFamily)
143                                                 continue;
144                                 }
145
146                                 if (method.ReturnType != typeof (void))
147                                         continue;
148
149                                 ParameterInfo [] parms = method.GetParameters ();
150                                 int length = parms.Length;
151                                 bool noParams = (length == 0);
152                                 if (!noParams && (length != 2 ||
153                                     parms [0].ParameterType != typeof (object) ||
154                                     parms [1].ParameterType != typeof (EventArgs)))
155                                     continue;
156
157                                 int pos = methodName.IndexOf ("_");
158                                 string eventName = methodName.Substring (pos + 1);
159                                 EventInfo evt = type.GetEvent (eventName);
160                                 if (evt == null) {
161                                         /* This should never happen */
162                                         continue;
163                                 }
164
165                                 if (noParams) {
166                                         NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
167                                         evt.AddEventHandler (this, npi.FakeDelegate);
168                                 } else {
169                                         evt.AddEventHandler (this, Delegate.CreateDelegate (
170                                                         typeof (EventHandler), this, methodName));
171                                 }
172                         }
173                 }
174
175                 [EditorBrowsable (EditorBrowsableState.Never)]
176                 protected virtual void FrameworkInitialize ()
177                 {
178                 }
179
180                 Type GetTypeFromControlPath (string virtualPath)
181                 {
182                         if (virtualPath == null)
183                                 throw new ArgumentNullException ("virtualPath");
184
185                         string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
186                         string realpath = Context.Request.MapPath (vpath);
187                         return UserControlParser.GetCompiledType (vpath, realpath, Context);
188                 }
189
190                 public Control LoadControl (string virtualPath)
191                 {
192 #if NET_2_0
193                         if (virtualPath == null)
194                                 throw new ArgumentNullException ("virtualPath");
195 #else
196                         if (virtualPath == null)
197                                 throw new HttpException ("virtualPath is null");
198 #endif
199                         Type type = GetTypeFromControlPath (virtualPath);
200                         object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
201                         if (attrs != null && attrs.Length == 1) {
202                                 PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
203                                 PartialCachingControl ctrl = new PartialCachingControl (type);
204                                 ctrl.VaryByParams = attr.VaryByParams;
205                                 ctrl.VaryByControls = attr.VaryByControls;
206                                 ctrl.VaryByCustom = attr.VaryByCustom;
207                                 return ctrl;
208                         }
209
210                         object control = Activator.CreateInstance (type);
211                         if (control is UserControl)
212                                 ((UserControl) control).InitializeAsUserControl (Page);
213
214                         return (Control) control;
215                 }
216
217                 public ITemplate LoadTemplate (string virtualPath)
218                 {
219 #if NET_2_0
220                         if (virtualPath == null)
221                                 throw new ArgumentNullException ("virtualPath");
222 #else
223                         if (virtualPath == null)
224                                 throw new HttpException ("virtualPath is null");
225 #endif
226                         Type t = GetTypeFromControlPath (virtualPath);
227                         return new SimpleTemplate (t);
228                 }
229
230                 protected virtual void OnAbortTransaction (EventArgs e)
231                 {
232                         EventHandler eh = Events [abortTransaction] as EventHandler;
233                         if (eh != null)
234                                 eh (this, e);
235                 }
236
237                 protected virtual void OnCommitTransaction (EventArgs e)
238                 {
239                         EventHandler eh = Events [commitTransaction] as EventHandler;
240                         if (eh != null)
241                                 eh (this, e);
242                 }
243
244                 protected virtual void OnError (EventArgs e)
245                 {
246                         EventHandler eh = Events [error] as EventHandler;
247                         if (eh != null)
248                                 eh (this, e);
249                 }
250
251                 [MonoTODO ("Not implemented, always returns null")]
252                 public Control ParseControl (string content)
253                 {
254                         if (content == null)
255                                 throw new ArgumentNullException ("content");
256
257                         return null;
258                 }
259
260                 [EditorBrowsable (EditorBrowsableState.Never)]
261                 public 
262 #if !NET_2_0
263                 static
264 #endif
265                 object ReadStringResource ()
266                 {
267                         throw new NotSupportedException ();
268                 }
269
270 #if NET_2_0
271                 protected object GetGlobalResourceObject (string className, string resourceKey)
272                 {
273                         return HttpContext.GetGlobalResourceObject (className, resourceKey);
274                 }
275
276                 [MonoTODO ("Not implemented")]
277                 protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
278                 {
279                         // FIXME: not sure how to implement that one yet
280                         throw new NotSupportedException();
281                 }
282
283                 protected object GetLocalResourceObject (string resourceKey)
284                 {
285                         return HttpContext.GetLocalResourceObject (Context.Request.CurrentExecutionFilePath, resourceKey);
286                 }
287                 
288                 protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
289                 {
290                         // FIXME: not sure how to implement that one yet
291                         throw new NotSupportedException();
292                 }
293 #endif
294                 
295                 [EditorBrowsable (EditorBrowsableState.Never)]
296                 public static object ReadStringResource (Type t)
297                 {
298                         throw new NotSupportedException ();
299                 }
300
301                 [MonoTODO ("Not implemented, does nothing")]
302                 [EditorBrowsable (EditorBrowsableState.Never)]
303                 protected void SetStringResourcePointer (object stringResourcePointer,
304                                                          int maxResourceOffset)
305                 {
306                 }
307
308                 [MonoTODO ("Not implemented, does nothing")]
309                 [EditorBrowsable (EditorBrowsableState.Never)]
310                 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
311                                                         int size, bool fAsciiOnly)
312                 {
313                 }
314
315                 #endregion
316
317                 #region Events
318
319                 [WebSysDescription ("Raised when the user aborts a transaction.")]
320                 public event EventHandler AbortTransaction {
321                         add { Events.AddHandler (abortTransaction, value); }
322                         remove { Events.RemoveHandler (abortTransaction, value); }
323                 }
324
325                 [WebSysDescription ("Raised when the user initiates a transaction.")]
326                 public event EventHandler CommitTransaction {
327                         add { Events.AddHandler (commitTransaction, value); }
328                         remove { Events.RemoveHandler (commitTransaction, value); }
329                 }
330
331                 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
332                 public event EventHandler Error {
333                         add { Events.AddHandler (error, value); }
334                         remove { Events.RemoveHandler (error, value); }
335                 }
336
337                 #endregion
338
339                 class SimpleTemplate : ITemplate
340                 {
341                         Type type;
342
343                         public SimpleTemplate (Type type)
344                         {
345                                 this.type = type;
346                         }
347
348                         public void InstantiateIn (Control control)
349                         {
350                                 Control template = Activator.CreateInstance (type) as Control;
351                                 template.SetBindingContainer (false);
352                                 control.Controls.Add (template);
353                         }
354                 }
355
356 #if NET_2_0
357                 protected internal object Eval (string expression)
358                 {
359                         return DataBinder.Eval (Page.GetDataItem(), expression);
360                 }
361         
362                 protected internal string Eval (string expression, string format)
363                 {
364                         return DataBinder.Eval (Page.GetDataItem(), expression, format);
365                 }
366         
367                 protected internal object XPath (string xpathexpression)
368                 {
369                         return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
370                 }
371         
372                 protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
373                 {
374                         return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
375                 }
376
377                 protected internal string XPath (string xpathexpression, string format)
378                 {
379                         return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
380                 }
381         
382                 protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
383                 {
384                         return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
385                 }
386
387                 protected internal IEnumerable XPathSelect (string xpathexpression)
388                 {
389                         return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
390                 }
391
392                 protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
393                 {
394                         return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
395                 }
396
397                 // IFilterResolutionService
398
399                 [MonoTODO ("Not implemented")]
400                 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
401                 {
402                         throw new NotImplementedException ();
403                 }
404
405                 [MonoTODO ("Not implemented")]
406                 bool IFilterResolutionService.EvaluateFilter (string filterName)
407                 {
408                         throw new NotImplementedException ();
409                 }
410 #endif
411         }
412 }