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.Hosting;
38 using System.Web.Util;
40 namespace System.Web.UI
43 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public abstract class TemplateControlParser : BaseTemplateParser
47 bool autoEventWireup = true;
48 bool enableViewState = true;
49 CompilationMode compilationMode = CompilationMode.Always;
50 ClientIDMode? clientIDMode;
53 protected TemplateControlParser ()
55 LoadConfigDefaults ();
58 internal override void LoadConfigDefaults ()
60 base.LoadConfigDefaults ();
61 PagesSection ps = PagesConfig;
62 autoEventWireup = ps.AutoEventWireup;
63 enableViewState = ps.EnableViewState;
64 compilationMode = ps.CompilationMode;
67 internal override void ProcessMainAttributes (IDictionary atts)
69 autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
70 enableViewState = GetBool (atts, "EnableViewState", enableViewState);
72 string value = GetString (atts, "CompilationMode", compilationMode.ToString ());
73 if (!String.IsNullOrEmpty (value)) {
75 compilationMode = (CompilationMode) Enum.Parse (typeof (CompilationMode), value, true);
76 } catch (Exception ex) {
77 ThrowParseException ("Invalid value of the CompilationMode attribute.", ex);
81 atts.Remove ("TargetSchema"); // Ignored
82 value = GetString (atts, "ClientIDMode", null);
83 if (!String.IsNullOrEmpty (value)) {
85 clientIDMode = (ClientIDMode) Enum.Parse (typeof (ClientIDMode), value, true);
86 } catch (Exception ex) {
87 ThrowParseException ("Invalid value of the ClientIDMode attribute.", ex);
90 base.ProcessMainAttributes (atts);
93 internal object GetCompiledInstance ()
95 Type type = CompileIntoType ();
99 object ctrl = Activator.CreateInstance (type);
103 HandleOptions (ctrl);
107 internal override void AddDirective (string directive, IDictionary atts)
109 int cmp = String.Compare ("Register", directive, true, Helpers.InvariantCulture);
111 string tagprefix = GetString (atts, "TagPrefix", null);
112 if (tagprefix == null || tagprefix.Trim () == "")
113 ThrowParseException ("No TagPrefix attribute found.");
115 string ns = GetString (atts, "Namespace", null);
116 string assembly = GetString (atts, "Assembly", null);
118 if (ns == null && assembly != null)
119 ThrowParseException ("Need a Namespace attribute with Assembly.");
123 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
125 RegisterNamespace (tagprefix, ns, assembly);
129 string tagname = GetString (atts, "TagName", null);
130 string src = GetString (atts, "Src", null);
132 if (tagname == null && src != null)
133 ThrowParseException ("Need a TagName attribute with Src.");
135 if (tagname != null && src == null)
136 ThrowParseException ("Need a Src attribute with TagName.");
138 RegisterCustomControl (tagprefix, tagname, src);
142 cmp = String.Compare ("Reference", directive, true, Helpers.InvariantCulture);
145 string page = GetString (atts, "Page", null);
146 bool is_page = (page != null);
152 string control = GetString (atts, "Control", null);
159 string virtualPath = GetString (atts, "VirtualPath", null);
160 if (virtualPath != null)
167 ThrowParseException ("Must provide one of the 'page', 'control' or 'virtualPath' attributes");
170 ThrowParseException ("Only one attribute can be specified.");
172 vp = HostingEnvironment.VirtualPathProvider.CombineVirtualPaths (VirtualPath.Absolute, vp);
173 AddDependency (vp, false);
176 ctype = BuildManager.GetCompiledType (vp);
178 AddAssembly (ctype.Assembly, true);
180 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
185 base.AddDirective (directive, atts);
188 internal override void HandleOptions (object obj)
190 base.HandleOptions (obj);
192 Control ctrl = obj as Control;
193 ctrl.AutoEventWireup = autoEventWireup;
194 ctrl.EnableViewState = enableViewState;
197 internal bool AutoEventWireup {
198 get { return autoEventWireup; }
201 internal bool EnableViewState {
202 get { return enableViewState; }
205 internal CompilationMode CompilationMode {
206 get { return compilationMode; }
208 internal ClientIDMode? ClientIDMode {
209 get { return clientIDMode; }
211 internal override TextReader Reader {
212 get { return reader; }
213 set { reader = value; }