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-2010 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;
31 using System.Globalization;
33 using System.Reflection;
34 using System.Security.Permissions;
35 using System.Web.Compilation;
36 using System.Web.Configuration;
37 using System.Web.Util;
39 namespace System.Web.UI
42 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 public abstract class TemplateControlParser : BaseTemplateParser
46 bool autoEventWireup = true;
47 bool enableViewState = true;
48 CompilationMode compilationMode = CompilationMode.Always;
50 ClientIDMode? clientIDMode;
54 protected TemplateControlParser ()
56 LoadConfigDefaults ();
59 internal override void LoadConfigDefaults ()
61 base.LoadConfigDefaults ();
62 PagesSection ps = PagesConfig;
63 autoEventWireup = ps.AutoEventWireup;
64 enableViewState = ps.EnableViewState;
65 compilationMode = ps.CompilationMode;
68 internal override void ProcessMainAttributes (IDictionary atts)
70 autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
71 enableViewState = GetBool (atts, "EnableViewState", enableViewState);
73 string value = GetString (atts, "CompilationMode", compilationMode.ToString ());
74 if (!String.IsNullOrEmpty (value)) {
76 compilationMode = (CompilationMode) Enum.Parse (typeof (CompilationMode), value, true);
77 } catch (Exception ex) {
78 ThrowParseException ("Invalid value of the CompilationMode attribute.", ex);
82 atts.Remove ("TargetSchema"); // Ignored
84 value = GetString (atts, "ClientIDMode", null);
85 if (!String.IsNullOrEmpty (value)) {
87 clientIDMode = (ClientIDMode) Enum.Parse (typeof (ClientIDMode), value, true);
88 } catch (Exception ex) {
89 ThrowParseException ("Invalid value of the ClientIDMode attribute.", ex);
93 base.ProcessMainAttributes (atts);
96 internal object GetCompiledInstance ()
98 Type type = CompileIntoType ();
102 object ctrl = Activator.CreateInstance (type);
106 HandleOptions (ctrl);
110 internal override void AddDirective (string directive, IDictionary atts)
112 int cmp = String.Compare ("Register", directive, true, Helpers.InvariantCulture);
114 string tagprefix = GetString (atts, "TagPrefix", null);
115 if (tagprefix == null || tagprefix.Trim () == "")
116 ThrowParseException ("No TagPrefix attribute found.");
118 string ns = GetString (atts, "Namespace", null);
119 string assembly = GetString (atts, "Assembly", null);
121 if (ns == null && assembly != null)
122 ThrowParseException ("Need a Namespace attribute with Assembly.");
126 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
128 RegisterNamespace (tagprefix, ns, assembly);
132 string tagname = GetString (atts, "TagName", null);
133 string src = GetString (atts, "Src", null);
135 if (tagname == null && src != null)
136 ThrowParseException ("Need a TagName attribute with Src.");
138 if (tagname != null && src == null)
139 ThrowParseException ("Need a Src attribute with TagName.");
141 RegisterCustomControl (tagprefix, tagname, src);
145 cmp = String.Compare ("Reference", directive, true, Helpers.InvariantCulture);
148 string page = GetString (atts, "Page", null);
149 bool is_page = (page != null);
155 string control = GetString (atts, "Control", null);
162 string virtualPath = GetString (atts, "VirtualPath", null);
163 if (virtualPath != null)
170 ThrowParseException ("Must provide one of the 'page', 'control' or 'virtualPath' attributes");
173 ThrowParseException ("Only one attribute can be specified.");
178 ctype = BuildManager.GetCompiledType (vp);
180 AddAssembly (ctype.Assembly, true);
182 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
187 base.AddDirective (directive, atts);
190 internal override void HandleOptions (object obj)
192 base.HandleOptions (obj);
194 Control ctrl = obj as Control;
195 ctrl.AutoEventWireup = autoEventWireup;
196 ctrl.EnableViewState = enableViewState;
199 internal bool AutoEventWireup {
200 get { return autoEventWireup; }
203 internal bool EnableViewState {
204 get { return enableViewState; }
207 internal CompilationMode CompilationMode {
208 get { return compilationMode; }
211 internal ClientIDMode? ClientIDMode {
212 get { return clientIDMode; }
215 internal override TextReader Reader {
216 get { return reader; }
217 set { reader = value; }