Files
mono/mcs/class/System.Web/System.Web.UI/TemplateControl.cs
T
Marek Habersack e71f514f40 2008-01-24 Marek Habersack <[email protected]>
* WebHandlerBuildProvider.cs: derive from the new
	SimpleBuildProvider class.

	* BuildManager.cs: full implementation of the batch compilation
	for the 2.0 profile. The implementation is most probably different
	to MS.NET's but the effects are the same (or nearly the same -
	there are some minor differences). There are missing bits and
	pieces, but minor ones - to be added later.

	* SimpleBuildProvider.cs: new build provider abstract class
	deriving from the GenericBuildProvider that implements common code
	for building web handlers and web services.

	* GenericBuildProvider.cs: new generic abstract class used to
	implement base builder code for all the build provider types and
	maximize code reuse.

	* PageBuildProvider.cs: derive from the new TemplateBuildProvider
	class and implement all the necessary abstract members. 
	Handle "fake" virtual paths, to properly support generating of the
	WSDL helper code.

	* BuildProvider.cs: SetVirtualPath now converts the passed path to
	absolute URL.
	Added internal virtual method GenerateCode, used by derived
	classes.
	SetCommonParameters no longer sets the referenced assemblies, this
	is done in BuildManager now.
	Added an internal virtual property CodeUnit which returns the
	build provider's compilation unit.

	* PageCompiler.cs: fix assignment of base types for asynchronous
	pages, to match MS.NET.
	For asynchronous pages, add the necessary methods generation.

	* BaseCompiler.cs: default namespace is now set using a constant.
	Renamed the Init method into ConstructType, to better reflect its
	purpose and made the method internal - it is used by the build
	providers.
	Added several CreateProvider overloads for easier and more unified
	compilation provider creation.
	Added an internal MainClassType property to enable class type name
	retrieval from other parts of the class hierarchy.

	* ApplicationFileBuildProvider.cs: new build provider deriving
	from the TemplateBuildProvider class to compile the global.asax
	file.

	* AspGenerator.cs: properly handle parsers which have no input
	file path given, but use TextReader instead.
	Dependencies are virtual paths now, convert them to physical ones
	before creating cache dependency.

	* CachingCompiler.cs: parser dependencies are virtual paths now,
	convert them to physical ones before creating cache dependency.

	* AssemblyBuilder.cs: full implementation of the documented
	functionality as well as a lot of internal build code
	added. Produces assemblies using the same style what MS.NET for
	temporary and target file naming. Handles partial type squashing
	if the same partial type is used by several code compile units
	added to the same builder instance.

	* MasterPageBuildProvider.cs: use the new TemplateBuildProvider
	base class and add implementation of all the abstract methods
	required by it.

	* ThemeDirectoryBuildProvider.cs: new build provider to compile
	theme directories. Replaces the old ThemeDirectoryCompiler class.

	* UserControlBuildProvider.cs: use the new TemplateBuildProvider
	base class and add implementation of all the abstract methods
	required by it.

	* TemplateBuildProvider.cs: new internal abstract class which
	implements the common tasks for all the template control build
	providers.

	* WebServiceBuildProvider.cs: use the new SimpleBuildProvider base
	type to implement building.

2008-01-24  Marek Habersack  <[email protected]>

	* PageThemeFileParser.cs: the DefaultBaseType property removed.

	* ApplicationFileParser.cs: added constructor which takes a
	TextReader.
	Removed the DefaultBaseType property.
	Added the Reader property.

	* MasterPage.cs: use BuildManager to create page instance.

	* WebHandlerParser.cs: added a compatibility constructor.

	* TemplateControl.cs: GetTypeFromControlPath uses
	BuildManager.GetCompiledType for the 2.0+ profile.

	* WebServiceParser.cs: added a compatibility internal
	constructor.

	* SimpleWebHandlerParser.cs: properly treat context and
	physicalPath parameters to the constructor, especially for the
	2.0+ profile where they are obsolete.
	Dependencies are added using virtual paths.

	* PageThemeParser.cs: DefaultBaseType property removed.

	* Page.cs: Style sheets and themes are compiled using
	BuildManager.
	Previous page instance is compiled using BuildManager for the 2.0+
	profile.

	* MasterPageParser.cs: Use BuildManager to create instance and
	compile types.
	Dependencies are added using virtual paths.

	* SimpleHandlerFactory.cs: Use
	BuildManager.CreateInstanceFromVirtualPath for the 2.0+ profile.

	* UserControlParser.cs: if the parser is created using a
	TextReader, take the input file path from the virtual path.
	Added a new internal constructor which takes both a TextReader and
	an inputFile parameters.
	Set base type to the default in the constructors.
	Use virtual path when setting master page dependency.
	DefaultBaseTypename doesn't use a literal string in the 2.0+
	profile, it gets the base type from the application
	configuration.

	* TemplateControlParser.cs: Dependency on a control/page mentioned
	in the Reference directive is registered using the virtual path.
	TextReader property is an override now.

	* TemplateParser.cs: RegisterCustomControl adds a dependency on
	the virtual path, not the physical path. The custom control is
	compiled using BuildManager.GetCompiledType for the 2.0+ profile.
	RegisterNamespace does not add a dependency on the assembly.
	LoadType does not add a dependency on the assembly.
	Legacy Src attribute is handled properly now.
	Dependency on code-behind (Src and CodeFile directive attributes)
	is added using the code-behind file's virtual path.
	GetAssemblyFromSource adds a dependency using the virtual path.
	DefaultBaseType implemented here, using DefaultBaseTypeName which
	is implemented in the descendant classes.
	Added internal CodeBehindSource property, used by build
	providers.
	ClassName property works correctly if inputFile is missing
	(e.g. when the parser has been created using a TextReader)
	Added a virtual internal property TextReader to return the readed
	which has been used to create the parser instance.

	* PageHandlerFactory.cs: GetHandler uses
	BuildManager.CreateInstanceFromVirtualPath for the 2.0+ profile.

	* PageParser.cs: set the default base type in the constructors.
	If the input file is not passed to a constructor, take the input
	file path from the virtual path.
	GetCompiledPageInstance uses BuildManager.CreateInstanceFromVirtualPath
	for the 2.0+ profile.
	MasterPage dependency is set using the virtual path of the master
	page.
	MasterPage is compiled into a type using BuildManager.GetCompiledType.
	GetCompiledPageType uses BuildManager.GetCompiledType for the 2.0
	profile.
	DefaultBaseType override removed, it is implemented in the
	TemplateParser class.
	DefaultBaseTypename doesn't use a literal string in the 2.0+
	profile, it gets the base type from the application
	configuration.

2008-01-24  Marek Habersack  <[email protected]>

	* HttpApplicationFactory.cs: use BuildManager.GetCompiledType to
	build the application (global.asax) file.

svn path=/trunk/mcs/; revision=93848
2008-01-24 21:55:44 +00:00

456 lines
13 KiB
C#

//
// System.Web.UI.TemplateControl.cs
//
// Authors:
// Duncan Mak ([email protected])
// Gonzalo Paniagua Javier ([email protected])
// Andreas Nahr ([email protected])
//
// (C) 2002 Ximian, Inc. (http://www.ximian.com)
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Security.Permissions;
using System.Web.Compilation;
using System.Web.Util;
using System.Xml;
namespace System.Web.UI {
// CAS
[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
#if NET_2_0
public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService {
#else
public abstract class TemplateControl : Control, INamingContainer {
#endif
static readonly Assembly _System_Web_Assembly = typeof (TemplateControl).Assembly;
static object abortTransaction = new object ();
static object commitTransaction = new object ();
static object error = new object ();
static string [] methodNames = { "Page_Init",
#if NET_2_0
"Page_PreInit",
"Page_PreLoad",
"Page_LoadComplete",
"Page_PreRenderComplete",
"Page_SaveStateComplete",
"Page_InitComplete",
#endif
"Page_Load",
"Page_DataBind",
"Page_PreRender",
"Page_Disposed",
"Page_Error",
"Page_Unload",
"Page_AbortTransaction",
"Page_CommitTransaction"};
const BindingFlags bflags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance;
#if NET_2_0
string _appRelativeVirtualPath;
#endif
#region Constructor
protected TemplateControl ()
{
#if NET_2_0
TemplateControl = this;
#endif
Construct ();
}
#endregion
#region Properties
[EditorBrowsable (EditorBrowsableState.Never)]
#if NET_2_0
[Obsolete]
#endif
protected virtual int AutoHandlers {
get { return 0; }
set { }
}
[EditorBrowsable (EditorBrowsableState.Never)]
protected virtual bool SupportAutoEvents {
get { return true; }
}
#if NET_2_0
public string AppRelativeVirtualPath {
get { return _appRelativeVirtualPath; }
set { _appRelativeVirtualPath = value; }
}
#endif
#endregion
#region Methods
protected virtual void Construct ()
{
}
[MonoTODO ("Not implemented")]
protected LiteralControl CreateResourceBasedLiteralControl (int offset,
int size,
bool fAsciiOnly)
{
return null;
}
internal void WireupAutomaticEvents ()
{
if (!SupportAutoEvents || !AutoEventWireup)
return;
foreach (string methodName in methodNames) {
MethodInfo method = null;
Type type;
for (type = GetType (); type.Assembly != _System_Web_Assembly; type = type.BaseType) {
method = type.GetMethod (methodName, bflags);
if (method != null)
break;
}
if (method == null)
continue;
if (method.DeclaringType != type) {
if (!method.IsPublic && !method.IsFamilyOrAssembly &&
!method.IsFamilyAndAssembly && !method.IsFamily)
continue;
}
if (method.ReturnType != typeof (void))
continue;
ParameterInfo [] parms = method.GetParameters ();
int length = parms.Length;
bool noParams = (length == 0);
if (!noParams && (length != 2 ||
parms [0].ParameterType != typeof (object) ||
parms [1].ParameterType != typeof (EventArgs)))
continue;
int pos = methodName.IndexOf ("_");
string eventName = methodName.Substring (pos + 1);
EventInfo evt = type.GetEvent (eventName);
if (evt == null) {
/* This should never happen */
continue;
}
if (noParams) {
NoParamsInvoker npi = new NoParamsInvoker (this, method);
evt.AddEventHandler (this, npi.FakeDelegate);
} else {
evt.AddEventHandler (this, Delegate.CreateDelegate (
#if NET_2_0
typeof (EventHandler), this, method));
#else
typeof (EventHandler), this, methodName));
#endif
}
}
}
[EditorBrowsable (EditorBrowsableState.Never)]
protected virtual void FrameworkInitialize ()
{
}
Type GetTypeFromControlPath (string virtualPath)
{
if (virtualPath == null)
throw new ArgumentNullException ("virtualPath");
string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
string realpath = Context.Request.MapPath (vpath);
#if NET_2_0
return BuildManager.GetCompiledType (vpath);
#else
return UserControlParser.GetCompiledType (vpath, realpath, Context);
#endif
}
public Control LoadControl (string virtualPath)
{
#if NET_2_0
if (virtualPath == null)
throw new ArgumentNullException ("virtualPath");
#else
if (virtualPath == null)
throw new HttpException ("virtualPath is null");
#endif
Type type = GetTypeFromControlPath (virtualPath);
return LoadControl (type, null);
}
public Control LoadControl (Type type, object[] parameters)
{
object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
if (attrs != null && attrs.Length == 1) {
PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
PartialCachingControl ctrl = new PartialCachingControl (type, parameters);
ctrl.VaryByParams = attr.VaryByParams;
ctrl.VaryByControls = attr.VaryByControls;
ctrl.VaryByCustom = attr.VaryByCustom;
return ctrl;
}
object control = Activator.CreateInstance (type, parameters);
if (control is UserControl)
((UserControl) control).InitializeAsUserControl (Page);
return (Control) control;
}
public ITemplate LoadTemplate (string virtualPath)
{
#if NET_2_0
if (virtualPath == null)
throw new ArgumentNullException ("virtualPath");
#else
if (virtualPath == null)
throw new HttpException ("virtualPath is null");
#endif
Type t = GetTypeFromControlPath (virtualPath);
return new SimpleTemplate (t);
}
protected virtual void OnAbortTransaction (EventArgs e)
{
EventHandler eh = Events [abortTransaction] as EventHandler;
if (eh != null)
eh (this, e);
}
protected virtual void OnCommitTransaction (EventArgs e)
{
EventHandler eh = Events [commitTransaction] as EventHandler;
if (eh != null)
eh (this, e);
}
protected virtual void OnError (EventArgs e)
{
EventHandler eh = Events [error] as EventHandler;
if (eh != null)
eh (this, e);
}
[MonoTODO ("Not implemented, always returns null")]
public Control ParseControl (string content)
{
if (content == null)
throw new ArgumentNullException ("content");
return null;
}
[EditorBrowsable (EditorBrowsableState.Never)]
public
#if !NET_2_0
static
#endif
object ReadStringResource ()
{
throw new NotSupportedException ();
}
#if NET_2_0
protected object GetGlobalResourceObject (string className, string resourceKey)
{
return HttpContext.GetGlobalResourceObject (className, resourceKey);
}
protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
{
if (String.IsNullOrEmpty (resourceKey) || String.IsNullOrEmpty (propName) ||
String.IsNullOrEmpty (className) || objType == null)
return null;
object globalObject = GetGlobalResourceObject (className, resourceKey);
if (globalObject == null)
return null;
TypeConverter converter = TypeDescriptor.GetProperties (objType) [propName].Converter;
if (converter == null || !converter.CanConvertFrom (globalObject.GetType ()))
return null;
return converter.ConvertFrom (globalObject);
}
protected object GetLocalResourceObject (string resourceKey)
{
return HttpContext.GetLocalResourceObject (VirtualPathUtility.ToAbsolute (this.AppRelativeVirtualPath),
resourceKey);
}
protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
{
if (String.IsNullOrEmpty (resourceKey) || String.IsNullOrEmpty (propName) || objType == null)
return null;
object localObject = GetLocalResourceObject (resourceKey);
if (localObject == null)
return null;
TypeConverter converter = TypeDescriptor.GetProperties (objType) [propName].Converter;
if (converter == null || !converter.CanConvertFrom (localObject.GetType ()))
return null;
return converter.ConvertFrom (localObject);
}
internal override TemplateControl TemplateControlInternal {
get { return this; }
}
#endif
[EditorBrowsable (EditorBrowsableState.Never)]
public static object ReadStringResource (Type t)
{
throw new NotSupportedException ();
}
[MonoTODO ("Not implemented, does nothing")]
[EditorBrowsable (EditorBrowsableState.Never)]
protected void SetStringResourcePointer (object stringResourcePointer,
int maxResourceOffset)
{
}
[MonoTODO ("Not implemented, does nothing")]
[EditorBrowsable (EditorBrowsableState.Never)]
protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
int size, bool fAsciiOnly)
{
}
#endregion
#region Events
[WebSysDescription ("Raised when the user aborts a transaction.")]
public event EventHandler AbortTransaction {
add { Events.AddHandler (abortTransaction, value); }
remove { Events.RemoveHandler (abortTransaction, value); }
}
[WebSysDescription ("Raised when the user initiates a transaction.")]
public event EventHandler CommitTransaction {
add { Events.AddHandler (commitTransaction, value); }
remove { Events.RemoveHandler (commitTransaction, value); }
}
[WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
public event EventHandler Error {
add { Events.AddHandler (error, value); }
remove { Events.RemoveHandler (error, value); }
}
#endregion
class SimpleTemplate : ITemplate
{
Type type;
public SimpleTemplate (Type type)
{
this.type = type;
}
public void InstantiateIn (Control control)
{
Control template = Activator.CreateInstance (type) as Control;
template.SetBindingContainer (false);
control.Controls.Add (template);
}
}
#if NET_2_0
protected internal object Eval (string expression)
{
return DataBinder.Eval (Page.GetDataItem(), expression);
}
protected internal string Eval (string expression, string format)
{
return DataBinder.Eval (Page.GetDataItem(), expression, format);
}
protected internal object XPath (string xpathexpression)
{
return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
}
protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
{
return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
}
protected internal string XPath (string xpathexpression, string format)
{
return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
}
protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
{
return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
}
protected internal IEnumerable XPathSelect (string xpathexpression)
{
return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
}
protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
{
return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
}
// IFilterResolutionService
[MonoTODO ("Not implemented")]
int IFilterResolutionService.CompareFilters (string filter1, string filter2)
{
throw new NotImplementedException ();
}
[MonoTODO ("Not implemented")]
bool IFilterResolutionService.EvaluateFilter (string filterName)
{
throw new NotImplementedException ();
}
#endif
}
}