2 // System.Web.UI.TemplateControlParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
32 using System.Reflection;
33 using System.Security.Permissions;
34 using System.Web.Compilation;
35 using System.Web.Util;
37 namespace System.Web.UI {
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 public abstract class TemplateControlParser : TemplateParser {
44 bool autoEventWireup = true;
45 bool enableViewState = true;
47 protected TemplateControlParser ()
51 internal override void ProcessMainAttributes (Hashtable atts)
53 autoEventWireup = GetBool (atts, "AutoEventWireup", PagesConfig.AutoEventWireup);
54 enableViewState = GetBool (atts, "EnableViewState", PagesConfig.EnableViewState);
56 atts.Remove ("TargetSchema"); // Ignored
58 base.ProcessMainAttributes (atts);
61 internal object GetCompiledInstance ()
63 Type type = CompileIntoType ();
67 object ctrl = Activator.CreateInstance (type);
75 internal override void AddDirective (string directive, Hashtable atts)
77 int cmp = String.Compare ("Register", directive, true);
79 string tagprefix = GetString (atts, "TagPrefix", null);
80 if (tagprefix == null || tagprefix.Trim () == "")
81 ThrowParseException ("No TagPrefix attribute found.");
83 string ns = GetString (atts, "Namespace", null);
84 string assembly = GetString (atts, "Assembly", null);
86 if (ns != null && assembly == null)
87 ThrowParseException ("Need an Assembly attribute with Namespace.");
89 if (ns == null && assembly != null)
90 ThrowParseException ("Need a Namespace attribute with Assembly.");
94 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
97 Assembly ass = AddAssemblyByName (assembly);
98 AddDependency (ass.Location);
99 RootBuilder.Foundry.RegisterFoundry (tagprefix, ass, ns);
103 string tagname = GetString (atts, "TagName", null);
104 string src = GetString (atts, "Src", null);
106 if (tagname == null && src != null)
107 ThrowParseException ("Need a TagName attribute with Src.");
109 if (tagname != null && src == null)
110 ThrowParseException ("Need a Src attribute with TagName.");
112 if (!src.EndsWith (".ascx"))
113 ThrowParseException ("Source file extension for controls must be .ascx");
115 string realpath = MapPath (src);
116 if (!File.Exists (realpath))
117 throw new ParseException (Location, "Could not find file \""
120 string vpath = UrlUtils.Combine (BaseVirtualDir, src);
123 type = UserControlParser.GetCompiledType (vpath, realpath, Dependencies, Context);
124 } catch (ParseException pe) {
125 if (this is UserControlParser)
126 throw new ParseException (Location, pe.Message, pe);
130 AddAssembly (type.Assembly, true);
131 RootBuilder.Foundry.RegisterFoundry (tagprefix, tagname, type);
135 cmp = String.Compare ("Reference", directive, true);
137 string page = GetString (atts, "Page", null);
138 string control = GetString (atts, "Control", null);
140 bool is_page = (page != null);
141 if (!is_page && control == null)
142 ThrowParseException ("Must provide 'page' or 'control' attribute");
144 if (is_page && control != null)
145 ThrowParseException ("'page' and 'control' are mutually exclusive");
147 string filepath = (!is_page) ? control : page;
148 filepath = MapPath (filepath);
149 AddDependency (filepath);
152 PageParser pp = new PageParser (page, filepath, Context);
153 ctype = pp.CompileIntoType ();
155 ctype = UserControlParser.GetCompiledType (control, filepath, Dependencies, Context);
158 AddAssembly (ctype.Assembly, true);
160 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
165 base.AddDirective (directive, atts);
168 internal override void HandleOptions (object obj)
170 Control ctrl = obj as Control;
171 ctrl.AutoEventWireup = autoEventWireup;
172 ctrl.EnableViewState = enableViewState;
175 internal bool AutoEventWireup {
176 get { return autoEventWireup; }
179 internal bool EnableViewState {
180 get { return enableViewState; }