2006-09-21 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
43 #if NET_2_0
44                 : BaseTemplateParser 
45 #else
46                 : TemplateParser
47 #endif
48         {
49
50                 bool autoEventWireup = true;
51                 bool enableViewState = true;
52 #if NET_2_0
53                 TextReader reader;
54 #endif
55
56                 protected TemplateControlParser ()
57                 {
58                 }
59
60                 internal override void ProcessMainAttributes (Hashtable atts)
61                 {
62                         autoEventWireup = GetBool (atts, "AutoEventWireup", PagesConfig.AutoEventWireup);
63                         enableViewState = GetBool (atts, "EnableViewState", PagesConfig.EnableViewState);
64
65                         atts.Remove ("TargetSchema"); // Ignored
66
67                         base.ProcessMainAttributes (atts);
68                 }
69
70                 internal object GetCompiledInstance ()
71                 {
72                         Type type = CompileIntoType ();
73                         if (type == null)
74                                 return null;
75
76                         object ctrl = Activator.CreateInstance (type);
77                         if (ctrl == null)
78                                 return null;
79
80                         HandleOptions (ctrl);
81                         return ctrl;
82                 }
83
84                 internal override void AddDirective (string directive, Hashtable atts)
85                 {
86                         int cmp = String.Compare ("Register", directive, true);
87                         if (cmp == 0) {
88                                 string tagprefix = GetString (atts, "TagPrefix", null);
89                                 if (tagprefix == null || tagprefix.Trim () == "")
90                                         ThrowParseException ("No TagPrefix attribute found.");
91
92                                 string ns = GetString (atts, "Namespace", null);
93                                 string assembly = GetString (atts, "Assembly", null);
94
95                                 if (ns != null && assembly == null)
96                                         ThrowParseException ("Need an Assembly attribute with Namespace.");
97
98                                 if (ns == null && assembly != null)
99                                         ThrowParseException ("Need a Namespace attribute with Assembly.");
100                                 
101                                 if (ns != null) {
102                                         if (atts.Count != 0)
103                                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
104
105                                         AddImport (ns);
106                                         Assembly ass = AddAssemblyByName (assembly);
107                                         AddDependency (ass.Location);
108                                         RootBuilder.Foundry.RegisterFoundry (tagprefix, ass, ns);
109                                         return;
110                                 }
111
112                                 string tagname = GetString (atts, "TagName", null);
113                                 string src = GetString (atts, "Src", null);
114
115                                 if (tagname == null && src != null)
116                                         ThrowParseException ("Need a TagName attribute with Src.");
117
118                                 if (tagname != null && src == null)
119                                         ThrowParseException ("Need a Src attribute with TagName.");
120
121                                 if (!src.EndsWith (".ascx"))
122                                         ThrowParseException ("Source file extension for controls must be .ascx");
123
124                                 string realpath = MapPath (src);
125                                 if (!File.Exists (realpath))
126                                         throw new ParseException (Location, "Could not find file \"" 
127                                                 + realpath + "\".");
128
129                                 string vpath = UrlUtils.Combine (BaseVirtualDir, src);
130                                 Type type = null;
131                                 AddDependency (realpath);
132                                 try {
133                                         ArrayList other_deps = new ArrayList ();
134                                         type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, Context);
135                                         foreach (string s in other_deps) {
136                                                 AddDependency (s);
137                                         }
138                                 } catch (ParseException pe) {
139                                         if (this is UserControlParser)
140                                                 throw new ParseException (Location, pe.Message, pe);
141                                         throw;
142                                 }
143
144                                 AddAssembly (type.Assembly, true);
145                                 RootBuilder.Foundry.RegisterFoundry (tagprefix, tagname, type);
146                                 return;
147                         }
148
149                         cmp = String.Compare ("Reference", directive, true);
150                         if (cmp == 0) {
151                                 string page = GetString (atts, "Page", null);
152                                 string control = GetString (atts, "Control", null);
153
154                                 bool is_page = (page != null);
155                                 if (!is_page && control == null)
156                                         ThrowParseException ("Must provide 'page' or 'control' attribute");
157
158                                 if (is_page && control != null)
159                                         ThrowParseException ("'page' and 'control' are mutually exclusive");
160
161                                 string filepath = (!is_page) ? control : page;
162                                 filepath = MapPath (filepath);
163                                 AddDependency (filepath);
164                                 Type ctype;
165                                 if (is_page) {
166                                         PageParser pp = new PageParser (page, filepath, Context);
167                                         ctype = pp.CompileIntoType ();
168                                 } else {
169                                         ctype = UserControlParser.GetCompiledType (control, filepath, Dependencies, Context);
170                                 }
171
172                                 AddAssembly (ctype.Assembly, true);
173                                 if (atts.Count != 0)
174                                         ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
175
176                                 return;
177                         }
178
179                         base.AddDirective (directive, atts);
180                 }
181
182                 internal override void HandleOptions (object obj)
183                 {
184                         base.HandleOptions (obj);
185
186                         Control ctrl = obj as Control;
187                         ctrl.AutoEventWireup = autoEventWireup;
188                         ctrl.EnableViewState = enableViewState;
189                 }
190
191                 internal bool AutoEventWireup {
192                         get { return autoEventWireup; }
193                 }
194
195                 internal bool EnableViewState {
196                         get { return enableViewState; }
197                 }
198 #if NET_2_0
199                 internal TextReader Reader {
200                         get { return reader; }
201                         set { reader = value; }
202                 }
203 #endif
204         }
205 }
206