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.Configuration;
36 using System.Web.Util;
38 namespace System.Web.UI {
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public abstract class TemplateControlParser
51 bool autoEventWireup = true;
52 bool enableViewState = true;
54 CompilationMode compilationMode = CompilationMode.Always;
58 protected TemplateControlParser ()
60 LoadConfigDefaults ();
63 internal override void LoadConfigDefaults ()
65 base.LoadConfigDefaults ();
67 PagesSection ps = PagesConfig;
69 PagesConfiguration ps = PagesConfig;
73 autoEventWireup = ps.AutoEventWireup;
74 enableViewState = ps.EnableViewState;
77 compilationMode = ps.CompilationMode;
81 internal override void ProcessMainAttributes (Hashtable atts)
83 autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
84 enableViewState = GetBool (atts, "EnableViewState", enableViewState);
86 string cmode = GetString (atts, "CompilationMode", compilationMode.ToString ());
87 if (!String.IsNullOrEmpty (cmode)) {
88 if (String.Compare (cmode, "always", StringComparison.InvariantCultureIgnoreCase) == 0)
89 compilationMode = CompilationMode.Always;
90 else if (String.Compare (cmode, "auto", StringComparison.InvariantCultureIgnoreCase) == 0)
91 compilationMode = CompilationMode.Auto;
92 else if (String.Compare (cmode, "never", StringComparison.InvariantCultureIgnoreCase) == 0)
93 compilationMode = CompilationMode.Never;
95 ThrowParseException ("Invalid value of the CompilationMode attribute");
98 atts.Remove ("TargetSchema"); // Ignored
100 base.ProcessMainAttributes (atts);
103 internal object GetCompiledInstance ()
105 Type type = CompileIntoType ();
109 object ctrl = Activator.CreateInstance (type);
113 HandleOptions (ctrl);
117 internal override void AddDirective (string directive, Hashtable atts)
119 int cmp = String.Compare ("Register", directive, true);
121 string tagprefix = GetString (atts, "TagPrefix", null);
122 if (tagprefix == null || tagprefix.Trim () == "")
123 ThrowParseException ("No TagPrefix attribute found.");
125 string ns = GetString (atts, "Namespace", null);
126 string assembly = GetString (atts, "Assembly", null);
129 if (ns != null && assembly == null)
130 ThrowParseException ("Need an Assembly attribute with Namespace.");
133 if (ns == null && assembly != null)
134 ThrowParseException ("Need a Namespace attribute with Assembly.");
138 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
140 RegisterNamespace (tagprefix, ns, assembly);
144 string tagname = GetString (atts, "TagName", null);
145 string src = GetString (atts, "Src", null);
147 if (tagname == null && src != null)
148 ThrowParseException ("Need a TagName attribute with Src.");
150 if (tagname != null && src == null)
151 ThrowParseException ("Need a Src attribute with TagName.");
154 if (!StrUtils.EndsWith (src, ".ascx", true))
155 ThrowParseException ("Source file extension for controls must be .ascx");
158 RegisterCustomControl (tagprefix, tagname, src);
162 cmp = String.Compare ("Reference", directive, true);
164 string page = GetString (atts, "Page", null);
165 string control = GetString (atts, "Control", null);
167 bool is_page = (page != null);
168 if (!is_page && control == null)
169 ThrowParseException ("Must provide 'page' or 'control' attribute");
171 if (is_page && control != null)
172 ThrowParseException ("'page' and 'control' are mutually exclusive");
174 string filepath = (!is_page) ? control : page;
175 AddDependency (filepath);
177 filepath = MapPath (filepath);
180 PageParser pp = new PageParser (page, filepath, Context);
181 ctype = pp.CompileIntoType ();
184 ctype = BuildManager.GetCompiledType (control);
186 ctype = UserControlParser.GetCompiledType (control, filepath, Dependencies, Context);
190 AddAssembly (ctype.Assembly, true);
192 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
197 base.AddDirective (directive, atts);
200 internal override void HandleOptions (object obj)
202 base.HandleOptions (obj);
204 Control ctrl = obj as Control;
205 ctrl.AutoEventWireup = autoEventWireup;
206 ctrl.EnableViewState = enableViewState;
209 internal bool AutoEventWireup {
210 get { return autoEventWireup; }
213 internal bool EnableViewState {
214 get { return enableViewState; }
218 internal CompilationMode CompilationMode {
219 get { return compilationMode; }
222 internal override TextReader Reader {
223 get { return reader; }
224 set { reader = value; }