merge -r 53370:58178
[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                 private Hashtable built_objects;
43
44                 public RootBuilder ()
45                 {
46                 }
47 #else
48         public sealed class RootBuilder : TemplateBuilder {
49 #endif
50                 static Hashtable htmlControls;
51                 static Hashtable htmlInputControls;
52                 AspComponentFoundry foundry;
53
54                 static RootBuilder ()
55                 {
56                         htmlControls = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
57                                                       CaseInsensitiveComparer.DefaultInvariant); 
58
59                         htmlControls.Add ("A", typeof (HtmlAnchor));
60                         htmlControls.Add ("BUTTON", typeof (HtmlButton));
61                         htmlControls.Add ("FORM", typeof (HtmlForm));
62 #if NET_2_0
63                         htmlControls.Add ("HEAD", typeof (HtmlHead));
64 #endif
65                         htmlControls.Add ("IMG", typeof (HtmlImage));
66                         htmlControls.Add ("INPUT", "INPUT");
67 #if NET_2_0
68                         htmlControls.Add ("LINK", typeof (HtmlLink));
69                         htmlControls.Add ("META", typeof (HtmlLink));
70 #endif
71                         htmlControls.Add ("SELECT", typeof (HtmlSelect));
72                         htmlControls.Add ("TABLE", typeof (HtmlTable));
73                         htmlControls.Add ("TD", typeof (HtmlTableCell));
74                         htmlControls.Add ("TH", typeof (HtmlTableCell));
75                         htmlControls.Add ("TR", typeof (HtmlTableRow));
76                         htmlControls.Add ("TEXTAREA", typeof (HtmlTextArea));
77
78                         htmlInputControls = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
79                                                       CaseInsensitiveComparer.DefaultInvariant); 
80
81                         htmlInputControls.Add ("BUTTON", typeof (HtmlInputButton));
82 #if NET_2_0
83                         htmlInputControls.Add ("SUBMIT", typeof (HtmlInputSubmit));
84                         htmlInputControls.Add ("RESET", typeof (HtmlInputReset));
85 #else
86                         htmlInputControls.Add ("SUBMIT", typeof (HtmlInputButton));
87                         htmlInputControls.Add ("RESET", typeof (HtmlInputButton));
88 #endif
89                         htmlInputControls.Add ("CHECKBOX", typeof (HtmlInputCheckBox));
90                         htmlInputControls.Add ("FILE", typeof (HtmlInputFile));
91                         htmlInputControls.Add ("HIDDEN", typeof (HtmlInputHidden));
92                         htmlInputControls.Add ("IMAGE", typeof (HtmlInputImage));
93                         htmlInputControls.Add ("RADIO", typeof (HtmlInputRadioButton));
94                         htmlInputControls.Add ("TEXT", typeof (HtmlInputText));
95 #if NET_2_0
96                         htmlInputControls.Add ("PASSWORD", typeof (HtmlInputPassword));
97 #else
98                         htmlInputControls.Add ("PASSWORD", typeof (HtmlInputText));
99 #endif
100                 }
101
102                 public RootBuilder (TemplateParser parser)
103                 {
104                         foundry = new AspComponentFoundry ();
105                         line = 1;
106                         if (parser != null)
107                                 fileName = parser.InputFile;
108                         Init (parser, null, null, null, null, null);
109                 }
110
111                 public override Type GetChildControlType (string tagName, IDictionary attribs) 
112                 {
113                         if (tagName == null)
114                                 throw new ArgumentNullException ("tagName");
115
116                         string prefix;
117                         string cname;
118                         int colon = tagName.IndexOf (':');
119                         if (colon != -1) {
120                                 if (colon == 0)
121                                         throw new Exception ("Empty TagPrefix is not valid");
122
123                                 if (colon + 1 == tagName.Length)
124                                         return null;
125
126                                 prefix = tagName.Substring (0, colon);
127                                 cname = tagName.Substring (colon + 1);
128                         } else {
129                                 prefix = "";
130                                 cname = tagName;
131                         }
132
133                         Type t = foundry.GetComponentType (prefix, cname);
134                         if (t != null)
135                                 return t;
136                         else if (prefix != "")
137                                 throw new Exception ("TagPrefix " + prefix + " not registered");
138                         
139                         return LookupHtmlControls (tagName, attribs);
140                 }
141
142                 static Type LookupHtmlControls (string tagName, IDictionary attribs)
143                 {
144                         object o = htmlControls [tagName];
145                         if (o is string) {
146                                 if (attribs == null)
147                                         throw new HttpException ("Unable to map input type control to a Type.");
148
149                                 string ctype = attribs ["TYPE"] as string;
150                                 if (ctype == null)
151                                         ctype = "TEXT"; // The default used by MS
152
153                                 Type t = htmlInputControls [ctype] as Type;
154                                 if (t == null)
155                                         throw new HttpException ("Unable to map input type control to a Type.");
156
157                                 return t;
158                         }
159
160                         if (o == null)
161                                 o = typeof (HtmlGenericControl);
162
163                         return (Type) o;
164                 }
165
166                 internal AspComponentFoundry Foundry {
167                         get { return foundry; }
168                 }
169 #if NET_2_0
170                 // FIXME: it's empty (but not null) when using the new default ctor
171                 // but I'm not sure when something should gets in...
172                 public IDictionary BuiltObjects {
173                         get {
174                                 if (built_objects == null)
175                                         built_objects = new Hashtable ();
176                                 return built_objects;
177                         }
178                 }
179 #endif
180         }
181 }