2009-05-04 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / RootBuilder.cs
1 //
2 // System.Web.UI.RootBuilder
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 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.Security.Permissions;
32 using System.Web.Compilation;
33 using System.Web.UI.HtmlControls;
34
35 namespace System.Web.UI {
36
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 #if NET_2_0
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public class RootBuilder : TemplateBuilder {
41
42                 Hashtable built_objects;
43
44                 public RootBuilder ()
45                 {
46                         foundry = new AspComponentFoundry ();
47                         Line = 1;
48                 }
49 #else
50         public sealed class RootBuilder : TemplateBuilder {
51 #endif
52                 static Hashtable htmlControls;
53                 static Hashtable htmlInputControls;
54                 AspComponentFoundry foundry;
55
56                 static RootBuilder ()
57                 {
58 #if NET_2_0
59                         htmlControls = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
60 #else
61                         htmlControls = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
62                                                       CaseInsensitiveComparer.DefaultInvariant); 
63 #endif
64
65                         htmlControls.Add ("A", typeof (HtmlAnchor));
66                         htmlControls.Add ("BUTTON", typeof (HtmlButton));
67                         htmlControls.Add ("FORM", typeof (HtmlForm));
68 #if NET_2_0
69                         htmlControls.Add ("HEAD", typeof (HtmlHead));
70 #endif
71                         htmlControls.Add ("IMG", typeof (HtmlImage));
72                         htmlControls.Add ("INPUT", "INPUT");
73 #if NET_2_0
74                         htmlControls.Add ("LINK", typeof (HtmlLink));
75                         htmlControls.Add ("META", typeof (HtmlLink));
76 #endif
77                         htmlControls.Add ("SELECT", typeof (HtmlSelect));
78                         htmlControls.Add ("TABLE", typeof (HtmlTable));
79                         htmlControls.Add ("TD", typeof (HtmlTableCell));
80                         htmlControls.Add ("TH", typeof (HtmlTableCell));
81                         htmlControls.Add ("TR", typeof (HtmlTableRow));
82                         htmlControls.Add ("TEXTAREA", typeof (HtmlTextArea));
83
84 #if NET_2_0
85                         htmlInputControls = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
86 #else
87                         htmlInputControls = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
88                                                            CaseInsensitiveComparer.DefaultInvariant);
89 #endif
90
91                         htmlInputControls.Add ("BUTTON", typeof (HtmlInputButton));
92 #if NET_2_0
93                         htmlInputControls.Add ("SUBMIT", typeof (HtmlInputSubmit));
94                         htmlInputControls.Add ("RESET", typeof (HtmlInputReset));
95 #else
96                         htmlInputControls.Add ("SUBMIT", typeof (HtmlInputButton));
97                         htmlInputControls.Add ("RESET", typeof (HtmlInputButton));
98 #endif
99                         htmlInputControls.Add ("CHECKBOX", typeof (HtmlInputCheckBox));
100                         htmlInputControls.Add ("FILE", typeof (HtmlInputFile));
101                         htmlInputControls.Add ("HIDDEN", typeof (HtmlInputHidden));
102                         htmlInputControls.Add ("IMAGE", typeof (HtmlInputImage));
103                         htmlInputControls.Add ("RADIO", typeof (HtmlInputRadioButton));
104                         htmlInputControls.Add ("TEXT", typeof (HtmlInputText));
105 #if NET_2_0
106                         htmlInputControls.Add ("PASSWORD", typeof (HtmlInputPassword));
107 #else
108                         htmlInputControls.Add ("PASSWORD", typeof (HtmlInputText));
109 #endif
110                 }
111
112                 public RootBuilder (TemplateParser parser)
113                 {
114                         foundry = new AspComponentFoundry ();
115                         Line = 1;
116                         if (parser != null)
117                                 FileName = parser.InputFile;
118                         Init (parser, null, null, null, null, null);
119                 }
120
121                 public override Type GetChildControlType (string tagName, IDictionary attribs) 
122                 {
123                         if (tagName == null)
124                                 throw new ArgumentNullException ("tagName");
125
126                         AspComponent component = foundry.GetComponent (tagName);
127                         
128                         if (component != null) {
129 #if NET_2_0
130                                 if (!String.IsNullOrEmpty (component.Source)) {
131                                         TemplateParser parser = Parser;
132
133                                         if (component.FromConfig) {
134                                                 string parserDir = parser.BaseVirtualDir;
135                                                 VirtualPath vp = new VirtualPath (component.Source);
136
137                                                 if (parserDir == vp.Directory)
138                                                         throw new ParseException (parser.Location,
139                                                                                   String.Format ("The page '{0}' cannot use the user control '{1}', because it is registered in web.config and lives in the same directory as the page.", parser.VirtualPath, vp.Absolute));
140                                                 
141                                                 Parser.AddDependency (component.Source);
142                                         }
143                                 }
144 #endif
145                                 return component.Type;
146                         } else if (component != null && component.Prefix != String.Empty)
147                                 throw new Exception ("Unknown server tag '" + tagName + "'");
148                         
149                         return LookupHtmlControls (tagName, attribs);
150                 }
151
152                 static Type LookupHtmlControls (string tagName, IDictionary attribs)
153                 {
154                         object o = htmlControls [tagName];
155                         if (o is string) {
156                                 if (attribs == null)
157                                         throw new HttpException ("Unable to map input type control to a Type.");
158
159                                 string ctype = attribs ["TYPE"] as string;
160                                 if (ctype == null)
161                                         ctype = "TEXT"; // The default used by MS
162
163                                 Type t = htmlInputControls [ctype] as Type;
164                                 if (t == null)
165                                         throw new HttpException ("Unable to map input type control to a Type.");
166
167                                 return t;
168                         }
169
170                         if (o == null)
171                                 o = typeof (HtmlGenericControl);
172
173                         return (Type) o;
174                 }
175
176                 internal AspComponentFoundry Foundry {
177                         get { return foundry; }
178                         set {
179                                 if (value is AspComponentFoundry)
180                                         foundry = value;
181                         }
182                 }
183 #if NET_2_0
184                 // FIXME: it's empty (but not null) when using the new default ctor
185                 // but I'm not sure when something should gets in...
186                 public IDictionary BuiltObjects {
187                         get {
188                                 if (built_objects == null)
189                                         built_objects = new Hashtable ();
190                                 return built_objects;
191                         }
192                 }
193 #endif
194         }
195 }