2005-09-27 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / TemplateControlParser.cs
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 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System.Collections;
31 using System.IO;
32 using System.Reflection;
33 using System.Security.Permissions;
34 using System.Web.Compilation;
35 using System.Web.Util;
36
37 namespace System.Web.UI {
38
39         // CAS
40         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         public abstract class TemplateControlParser : TemplateParser {
43
44                 bool autoEventWireup = true;
45                 bool enableViewState = true;
46
47                 protected TemplateControlParser ()
48                 {
49                 }
50
51                 internal override void ProcessMainAttributes (Hashtable atts)
52                 {
53                         autoEventWireup = GetBool (atts, "AutoEventWireup", PagesConfig.AutoEventWireup);
54                         enableViewState = GetBool (atts, "EnableViewState", PagesConfig.EnableViewState);
55
56                         atts.Remove ("TargetSchema"); // Ignored
57
58                         base.ProcessMainAttributes (atts);
59                 }
60
61                 internal object GetCompiledInstance ()
62                 {
63                         Type type = CompileIntoType ();
64                         if (type == null)
65                                 return null;
66
67                         object ctrl = Activator.CreateInstance (type);
68                         if (ctrl == null)
69                                 return null;
70
71                         HandleOptions (ctrl);
72                         return ctrl;
73                 }
74
75                 internal override void AddDirective (string directive, Hashtable atts)
76                 {
77                         int cmp = String.Compare ("Register", directive, true);
78                         if (cmp == 0) {
79                                 string tagprefix = GetString (atts, "TagPrefix", null);
80                                 if (tagprefix == null || tagprefix.Trim () == "")
81                                         ThrowParseException ("No TagPrefix attribute found.");
82
83                                 string ns = GetString (atts, "Namespace", null);
84                                 string assembly = GetString (atts, "Assembly", null);
85
86                                 if (ns != null && assembly == null)
87                                         ThrowParseException ("Need an Assembly attribute with Namespace.");
88
89                                 if (ns == null && assembly != null)
90                                         ThrowParseException ("Need a Namespace attribute with Assembly.");
91                                 
92                                 if (ns != null) {
93                                         if (atts.Count != 0)
94                                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
95
96                                         AddImport (ns);
97                                         Assembly ass = AddAssemblyByName (assembly);
98                                         AddDependency (ass.Location);
99                                         RootBuilder.Foundry.RegisterFoundry (tagprefix, ass, ns);
100                                         return;
101                                 }
102
103                                 string tagname = GetString (atts, "TagName", null);
104                                 string src = GetString (atts, "Src", null);
105
106                                 if (tagname == null && src != null)
107                                         ThrowParseException ("Need a TagName attribute with Src.");
108
109                                 if (tagname != null && src == null)
110                                         ThrowParseException ("Need a Src attribute with TagName.");
111
112                                 if (!src.EndsWith (".ascx"))
113                                         ThrowParseException ("Source file extension for controls must be .ascx");
114
115                                 string realpath = MapPath (src);
116                                 if (!File.Exists (realpath))
117                                         throw new ParseException (Location, "Could not find file \"" 
118                                                 + realpath + "\".");
119
120                                 string vpath = UrlUtils.Combine (BaseVirtualDir, src);
121                                 Type type = null;
122                                 try {
123                                         type = UserControlParser.GetCompiledType (vpath, realpath, Dependencies, Context);
124                                 } catch (ParseException pe) {
125                                         if (this is UserControlParser)
126                                                 throw new ParseException (Location, pe.Message, pe);
127                                         throw;
128                                 }
129
130                                 AddAssembly (type.Assembly, true);
131                                 RootBuilder.Foundry.RegisterFoundry (tagprefix, tagname, type);
132                                 return;
133                         }
134
135                         cmp = String.Compare ("Reference", directive, true);
136                         if (cmp == 0) {
137                                 string page = GetString (atts, "Page", null);
138                                 string control = GetString (atts, "Control", null);
139
140                                 bool is_page = (page != null);
141                                 if (!is_page && control == null)
142                                         ThrowParseException ("Must provide 'page' or 'control' attribute");
143
144                                 if (is_page && control != null)
145                                         ThrowParseException ("'page' and 'control' are mutually exclusive");
146
147                                 string filepath = (!is_page) ? control : page;
148                                 filepath = MapPath (filepath);
149                                 AddDependency (filepath);
150                                 Type ctype;
151                                 if (is_page) {
152                                         PageParser pp = new PageParser (page, filepath, Context);
153                                         ctype = pp.CompileIntoType ();
154                                 } else {
155                                         ctype = UserControlParser.GetCompiledType (control, filepath, Dependencies, Context);
156                                 }
157
158                                 AddAssembly (ctype.Assembly, true);
159                                 if (atts.Count != 0)
160                                         ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
161
162                                 return;
163                         }
164
165                         base.AddDirective (directive, atts);
166                 }
167
168                 internal override void HandleOptions (object obj)
169                 {
170                         Control ctrl = obj as Control;
171                         ctrl.AutoEventWireup = autoEventWireup;
172                         ctrl.EnableViewState = enableViewState;
173                 }
174
175                 internal bool AutoEventWireup {
176                         get { return autoEventWireup; }
177                 }
178
179                 internal bool EnableViewState {
180                         get { return enableViewState; }
181                 }
182         }
183 }
184