**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI / TemplateControlParser.cs
blobf2ab4e458d3ed8708fc0a16781b45391d13a2975
1 //
2 // System.Web.UI.TemplateControlParser
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.Reflection;
34 using System.Web.Compilation;
35 using System.Web.Util;
37 namespace System.Web.UI
39 public abstract class TemplateControlParser : TemplateParser
41 bool autoEventWireup = true;
42 bool enableViewState = true;
44 protected TemplateControlParser ()
48 internal override void ProcessMainAttributes (Hashtable atts)
50 autoEventWireup = GetBool (atts, "AutoEventWireup", PagesConfig.AutoEventWireup);
51 enableViewState = GetBool (atts, "EnableViewState", PagesConfig.EnableViewState);
53 atts.Remove ("TargetSchema"); // Ignored
55 base.ProcessMainAttributes (atts);
58 internal object GetCompiledInstance ()
60 Type type = CompileIntoType ();
61 if (type == null)
62 return null;
64 object ctrl = Activator.CreateInstance (type);
65 if (ctrl == null)
66 return null;
68 HandleOptions (ctrl);
69 return ctrl;
72 internal override void AddDirective (string directive, Hashtable atts)
74 int cmp = String.Compare ("Register", directive, true);
75 if (cmp == 0) {
76 string tagprefix = GetString (atts, "TagPrefix", null);
77 if (tagprefix == null || tagprefix.Trim () == "")
78 ThrowParseException ("No TagPrefix attribute found.");
80 string ns = GetString (atts, "Namespace", null);
81 string assembly = GetString (atts, "Assembly", null);
83 if (ns != null && assembly == null)
84 ThrowParseException ("Need an Assembly attribute with Namespace.");
86 if (ns == null && assembly != null)
87 ThrowParseException ("Need a Namespace attribute with Assembly.");
89 if (ns != null) {
90 if (atts.Count != 0)
91 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
93 AddImport (ns);
94 Assembly ass = AddAssemblyByName (assembly);
95 AddDependency (ass.Location);
96 RootBuilder.Foundry.RegisterFoundry (tagprefix, ass, ns);
97 return;
100 string tagname = GetString (atts, "TagName", null);
101 string src = GetString (atts, "Src", null);
103 if (tagname == null && src != null)
104 ThrowParseException ("Need a TagName attribute with Src.");
106 if (tagname != null && src == null)
107 ThrowParseException ("Need a Src attribute with TagName.");
109 if (!src.EndsWith (".ascx"))
110 ThrowParseException ("Source file extension for controls must be .ascx");
112 string realpath = MapPath (src);
113 if (!File.Exists (realpath))
114 throw new ParseException (Location, "Could not find file \""
115 + realpath + "\".");
117 try {
118 AddDependency (realpath);
119 } catch (Exception e) {
120 throw new ParseException (Location, e.Message);
123 string vpath = UrlUtils.Combine (BaseVirtualDir, src);
124 Type type = UserControlParser.GetCompiledType (vpath, realpath, Context);
125 AddAssembly (type.Assembly, true);
126 RootBuilder.Foundry.RegisterFoundry (tagprefix, tagname, type);
127 return;
130 cmp = String.Compare ("Reference", directive, true);
131 if (cmp == 0) {
132 string page = GetString (atts, "Page", null);
133 string control = GetString (atts, "Control", null);
135 bool is_page = (page != null);
136 if (!is_page && control == null)
137 ThrowParseException ("Must provide 'page' or 'control' attribute");
139 if (is_page && control != null)
140 ThrowParseException ("'page' and 'control' are mutually exclusive");
142 string filepath = (!is_page) ? control : page;
143 filepath = MapPath (filepath);
144 AddDependency (filepath);
145 Type ctype;
146 if (is_page) {
147 PageParser pp = new PageParser (page, filepath, Context);
148 ctype = pp.CompileIntoType ();
149 } else {
150 ctype = UserControlParser.GetCompiledType (control, filepath, Context);
153 AddAssembly (ctype.Assembly, true);
154 if (atts.Count != 0)
155 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
157 return;
160 base.AddDirective (directive, atts);
163 internal override void HandleOptions (object obj)
165 Control ctrl = obj as Control;
166 ctrl.AutoEventWireup = autoEventWireup;
167 ctrl.EnableViewState = enableViewState;
170 internal bool AutoEventWireup {
171 get { return autoEventWireup; }
174 internal bool EnableViewState {
175 get { return enableViewState; }