* LosFormatter.cs: Avoid using Position when stream is unseekable.
[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.Configuration;
36 using System.Web.Util;
37
38 namespace System.Web.UI {
39
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public abstract class TemplateControlParser
44 #if NET_2_0
45                 : BaseTemplateParser 
46 #else
47                 : TemplateParser
48 #endif
49         {
50
51                 bool autoEventWireup = true;
52                 bool enableViewState = true;
53 #if NET_2_0
54                 CompilationMode compilationMode = CompilationMode.Always;
55                 TextReader reader;
56 #endif
57
58                 protected TemplateControlParser ()
59                 {
60                         LoadConfigDefaults ();
61                 }
62                 
63                 internal override void LoadConfigDefaults ()
64                 {
65                         base.LoadConfigDefaults ();
66 #if NET_2_0
67                         PagesSection ps = PagesConfig;
68 #else
69                         PagesConfiguration ps = PagesConfig;
70 #endif
71
72 #if NET_1_1
73                         autoEventWireup = ps.AutoEventWireup;
74                         enableViewState = ps.EnableViewState;
75 #endif
76 #if NET_2_0
77                         compilationMode = ps.CompilationMode;
78 #endif
79                 }
80                 
81                 internal override void ProcessMainAttributes (Hashtable atts)
82                 {
83                         autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
84                         enableViewState = GetBool (atts, "EnableViewState", enableViewState);
85 #if NET_2_0
86                         string cmode = GetString (atts, "CompilationMode", compilationMode.ToString ());
87                         if (!String.IsNullOrEmpty (cmode)) {
88                                 if (String.Compare (cmode, "always", StringComparison.InvariantCultureIgnoreCase) == 0)
89                                         compilationMode = CompilationMode.Always;
90                                 else if (String.Compare (cmode, "auto", StringComparison.InvariantCultureIgnoreCase) == 0)
91                                         compilationMode = CompilationMode.Auto;
92                                 else if (String.Compare (cmode, "never", StringComparison.InvariantCultureIgnoreCase) == 0)
93                                         compilationMode = CompilationMode.Never;
94                                 else
95                                         ThrowParseException ("Invalid value of the CompilationMode attribute");
96                         }
97 #endif
98                         atts.Remove ("TargetSchema"); // Ignored
99
100                         base.ProcessMainAttributes (atts);
101                 }
102
103                 internal object GetCompiledInstance ()
104                 {
105                         Type type = CompileIntoType ();
106                         if (type == null)
107                                 return null;
108
109                         object ctrl = Activator.CreateInstance (type);
110                         if (ctrl == null)
111                                 return null;
112
113                         HandleOptions (ctrl);
114                         return ctrl;
115                 }
116
117                 internal override void AddDirective (string directive, Hashtable atts)
118                 {
119                         int cmp = String.Compare ("Register", directive, true);
120                         if (cmp == 0) {
121                                 string tagprefix = GetString (atts, "TagPrefix", null);
122                                 if (tagprefix == null || tagprefix.Trim () == "")
123                                         ThrowParseException ("No TagPrefix attribute found.");
124
125                                 string ns = GetString (atts, "Namespace", null);
126                                 string assembly = GetString (atts, "Assembly", null);
127
128 #if !NET_2_0
129                                 if (ns != null && assembly == null)
130                                         ThrowParseException ("Need an Assembly attribute with Namespace.");
131 #endif
132                                 
133                                 if (ns == null && assembly != null)
134                                         ThrowParseException ("Need a Namespace attribute with Assembly.");
135                                 
136                                 if (ns != null) {
137                                         if (atts.Count != 0)
138                                                 ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
139
140                                         RegisterNamespace (tagprefix, ns, assembly);
141                                         return;
142                                 }
143
144                                 string tagname = GetString (atts, "TagName", null);
145                                 string src = GetString (atts, "Src", null);
146
147                                 if (tagname == null && src != null)
148                                         ThrowParseException ("Need a TagName attribute with Src.");
149
150                                 if (tagname != null && src == null)
151                                         ThrowParseException ("Need a Src attribute with TagName.");
152
153 #if !NET_2_0
154                                 if (!StrUtils.EndsWith (src, ".ascx", true))
155                                         ThrowParseException ("Source file extension for controls must be .ascx");
156 #endif
157                                 
158                                 RegisterCustomControl (tagprefix, tagname, src);
159                                 return;
160                         }
161
162                         cmp = String.Compare ("Reference", directive, true);
163                         if (cmp == 0) {
164                                 string page = GetString (atts, "Page", null);
165                                 string control = GetString (atts, "Control", null);
166
167                                 bool is_page = (page != null);
168                                 if (!is_page && control == null)
169                                         ThrowParseException ("Must provide 'page' or 'control' attribute");
170
171                                 if (is_page && control != null)
172                                         ThrowParseException ("'page' and 'control' are mutually exclusive");
173
174                                 string filepath = (!is_page) ? control : page;
175                                 AddDependency (filepath);
176                                 
177                                 filepath = MapPath (filepath);
178                                 Type ctype;
179                                 if (is_page) {
180                                         PageParser pp = new PageParser (page, filepath, Context);
181                                         ctype = pp.CompileIntoType ();
182                                 } else {
183 #if NET_2_0
184                                         ctype = BuildManager.GetCompiledType (control);
185 #else
186                                         ctype = UserControlParser.GetCompiledType (control, filepath, Dependencies, Context);
187 #endif
188                                 }
189
190                                 AddAssembly (ctype.Assembly, true);
191                                 if (atts.Count != 0)
192                                         ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
193
194                                 return;
195                         }
196
197                         base.AddDirective (directive, atts);
198                 }
199
200                 internal override void HandleOptions (object obj)
201                 {
202                         base.HandleOptions (obj);
203
204                         Control ctrl = obj as Control;
205                         ctrl.AutoEventWireup = autoEventWireup;
206                         ctrl.EnableViewState = enableViewState;
207                 }
208
209                 internal bool AutoEventWireup {
210                         get { return autoEventWireup; }
211                 }
212
213                 internal bool EnableViewState {
214                         get { return enableViewState; }
215                 }
216                 
217 #if NET_2_0
218                 internal CompilationMode CompilationMode {
219                         get { return compilationMode; }
220                 }
221                 
222                 internal override TextReader Reader {
223                         get { return reader; }
224                         set { reader = value; }
225                 }
226 #endif
227         }
228 }
229