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 LoadPagesConfigDefaults ();
63 internal virtual void LoadPagesConfigDefaults ()
66 PagesSection ps = PagesConfig;
68 PagesConfiguration ps = PagesConfig;
72 autoEventWireup = ps.AutoEventWireup;
73 enableViewState = ps.EnableViewState;
76 compilationMode = ps.CompilationMode;
80 internal override void ProcessMainAttributes (Hashtable atts)
82 autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
83 enableViewState = GetBool (atts, "EnableViewState", enableViewState);
85 string cmode = GetString (atts, "CompilationMode", compilationMode.ToString ());
86 if (!String.IsNullOrEmpty (cmode)) {
87 if (String.Compare (cmode, "always", StringComparison.InvariantCultureIgnoreCase) == 0)
88 compilationMode = CompilationMode.Always;
89 else if (String.Compare (cmode, "auto", StringComparison.InvariantCultureIgnoreCase) == 0)
90 compilationMode = CompilationMode.Auto;
91 else if (String.Compare (cmode, "never", StringComparison.InvariantCultureIgnoreCase) == 0)
92 compilationMode = CompilationMode.Never;
94 ThrowParseException ("Invalid value of the CompilationMode attribute");
97 atts.Remove ("TargetSchema"); // Ignored
99 base.ProcessMainAttributes (atts);
102 internal object GetCompiledInstance ()
104 Type type = CompileIntoType ();
108 object ctrl = Activator.CreateInstance (type);
112 HandleOptions (ctrl);
116 internal override void AddDirective (string directive, Hashtable atts)
118 int cmp = String.Compare ("Register", directive, true);
120 string tagprefix = GetString (atts, "TagPrefix", null);
121 if (tagprefix == null || tagprefix.Trim () == "")
122 ThrowParseException ("No TagPrefix attribute found.");
124 string ns = GetString (atts, "Namespace", null);
125 string assembly = GetString (atts, "Assembly", null);
128 if (ns != null && assembly == null)
129 ThrowParseException ("Need an Assembly attribute with Namespace.");
132 if (ns == null && assembly != null)
133 ThrowParseException ("Need a Namespace attribute with Assembly.");
137 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
139 RegisterNamespace (tagprefix, ns, assembly);
143 string tagname = GetString (atts, "TagName", null);
144 string src = GetString (atts, "Src", null);
146 if (tagname == null && src != null)
147 ThrowParseException ("Need a TagName attribute with Src.");
149 if (tagname != null && src == null)
150 ThrowParseException ("Need a Src attribute with TagName.");
152 if (!src.EndsWith (".ascx"))
153 ThrowParseException ("Source file extension for controls must be .ascx");
155 RegisterCustomControl (tagprefix, tagname, src);
159 cmp = String.Compare ("Reference", directive, true);
161 string page = GetString (atts, "Page", null);
162 string control = GetString (atts, "Control", null);
164 bool is_page = (page != null);
165 if (!is_page && control == null)
166 ThrowParseException ("Must provide 'page' or 'control' attribute");
168 if (is_page && control != null)
169 ThrowParseException ("'page' and 'control' are mutually exclusive");
171 string filepath = (!is_page) ? control : page;
172 filepath = MapPath (filepath);
173 AddDependency (filepath);
176 PageParser pp = new PageParser (page, filepath, Context);
177 ctype = pp.CompileIntoType ();
179 ctype = UserControlParser.GetCompiledType (control, filepath, Dependencies, Context);
182 AddAssembly (ctype.Assembly, true);
184 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
189 base.AddDirective (directive, atts);
192 internal override void HandleOptions (object obj)
194 base.HandleOptions (obj);
196 Control ctrl = obj as Control;
197 ctrl.AutoEventWireup = autoEventWireup;
198 ctrl.EnableViewState = enableViewState;
201 internal bool AutoEventWireup {
202 get { return autoEventWireup; }
205 internal bool EnableViewState {
206 get { return enableViewState; }
210 internal CompilationMode CompilationMode {
211 get { return compilationMode; }
214 internal TextReader Reader {
215 get { return reader; }
216 set { reader = value; }