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
50 bool autoEventWireup = true;
51 bool enableViewState = true;
56 protected TemplateControlParser ()
60 internal override void ProcessMainAttributes (Hashtable atts)
62 autoEventWireup = GetBool (atts, "AutoEventWireup", PagesConfig.AutoEventWireup);
63 enableViewState = GetBool (atts, "EnableViewState", PagesConfig.EnableViewState);
65 atts.Remove ("TargetSchema"); // Ignored
67 base.ProcessMainAttributes (atts);
70 internal object GetCompiledInstance ()
72 Type type = CompileIntoType ();
76 object ctrl = Activator.CreateInstance (type);
84 internal override void AddDirective (string directive, Hashtable atts)
86 int cmp = String.Compare ("Register", directive, true);
88 string tagprefix = GetString (atts, "TagPrefix", null);
89 if (tagprefix == null || tagprefix.Trim () == "")
90 ThrowParseException ("No TagPrefix attribute found.");
92 string ns = GetString (atts, "Namespace", null);
93 string assembly = GetString (atts, "Assembly", null);
95 if (ns != null && assembly == null)
96 ThrowParseException ("Need an Assembly attribute with Namespace.");
98 if (ns == null && assembly != null)
99 ThrowParseException ("Need a Namespace attribute with Assembly.");
103 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
106 Assembly ass = AddAssemblyByName (assembly);
107 AddDependency (ass.Location);
108 RootBuilder.Foundry.RegisterFoundry (tagprefix, ass, ns);
112 string tagname = GetString (atts, "TagName", null);
113 string src = GetString (atts, "Src", null);
115 if (tagname == null && src != null)
116 ThrowParseException ("Need a TagName attribute with Src.");
118 if (tagname != null && src == null)
119 ThrowParseException ("Need a Src attribute with TagName.");
121 if (!src.EndsWith (".ascx"))
122 ThrowParseException ("Source file extension for controls must be .ascx");
124 string realpath = MapPath (src);
125 if (!File.Exists (realpath))
126 throw new ParseException (Location, "Could not find file \""
129 string vpath = UrlUtils.Combine (BaseVirtualDir, src);
131 AddDependency (realpath);
133 ArrayList other_deps = new ArrayList ();
134 type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, Context);
135 foreach (string s in other_deps) {
138 } catch (ParseException pe) {
139 if (this is UserControlParser)
140 throw new ParseException (Location, pe.Message, pe);
144 AddAssembly (type.Assembly, true);
145 RootBuilder.Foundry.RegisterFoundry (tagprefix, tagname, type);
149 cmp = String.Compare ("Reference", directive, true);
151 string page = GetString (atts, "Page", null);
152 string control = GetString (atts, "Control", null);
154 bool is_page = (page != null);
155 if (!is_page && control == null)
156 ThrowParseException ("Must provide 'page' or 'control' attribute");
158 if (is_page && control != null)
159 ThrowParseException ("'page' and 'control' are mutually exclusive");
161 string filepath = (!is_page) ? control : page;
162 filepath = MapPath (filepath);
163 AddDependency (filepath);
166 PageParser pp = new PageParser (page, filepath, Context);
167 ctype = pp.CompileIntoType ();
169 ctype = UserControlParser.GetCompiledType (control, filepath, Dependencies, Context);
172 AddAssembly (ctype.Assembly, true);
174 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
179 base.AddDirective (directive, atts);
182 internal override void HandleOptions (object obj)
184 base.HandleOptions (obj);
186 Control ctrl = obj as Control;
187 ctrl.AutoEventWireup = autoEventWireup;
188 ctrl.EnableViewState = enableViewState;
191 internal bool AutoEventWireup {
192 get { return autoEventWireup; }
195 internal bool EnableViewState {
196 get { return enableViewState; }
199 internal TextReader Reader {
200 get { return reader; }
201 set { reader = value; }