Gotten rid of NET_2_0 ifdefs and 1.1 code from System.Web.UI
[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-2010 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         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         public class RootBuilder : TemplateBuilder
40         {
41                 Hashtable built_objects;
42                 static Hashtable htmlControls;
43                 static Hashtable htmlInputControls;
44                 AspComponentFoundry foundry;
45                 
46                 public RootBuilder ()
47                 {
48                         foundry = new AspComponentFoundry ();
49                         Line = 1;
50                 }
51
52                 static RootBuilder ()
53                 {
54                         htmlControls = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
55                         htmlControls.Add ("A", typeof (HtmlAnchor));
56                         htmlControls.Add ("BUTTON", typeof (HtmlButton));
57                         htmlControls.Add ("FORM", typeof (HtmlForm));
58                         htmlControls.Add ("HEAD", typeof (HtmlHead));
59                         htmlControls.Add ("IMG", typeof (HtmlImage));
60                         htmlControls.Add ("INPUT", "INPUT");
61                         htmlControls.Add ("LINK", typeof (HtmlLink));
62                         htmlControls.Add ("META", typeof (HtmlLink));
63                         htmlControls.Add ("SELECT", typeof (HtmlSelect));
64                         htmlControls.Add ("TABLE", typeof (HtmlTable));
65                         htmlControls.Add ("TD", typeof (HtmlTableCell));
66                         htmlControls.Add ("TH", typeof (HtmlTableCell));
67                         htmlControls.Add ("TR", typeof (HtmlTableRow));
68                         htmlControls.Add ("TEXTAREA", typeof (HtmlTextArea));
69
70                         htmlInputControls = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
71
72                         htmlInputControls.Add ("BUTTON", typeof (HtmlInputButton));
73                         htmlInputControls.Add ("SUBMIT", typeof (HtmlInputSubmit));
74                         htmlInputControls.Add ("RESET", typeof (HtmlInputReset));
75                         htmlInputControls.Add ("CHECKBOX", typeof (HtmlInputCheckBox));
76                         htmlInputControls.Add ("FILE", typeof (HtmlInputFile));
77                         htmlInputControls.Add ("HIDDEN", typeof (HtmlInputHidden));
78                         htmlInputControls.Add ("IMAGE", typeof (HtmlInputImage));
79                         htmlInputControls.Add ("RADIO", typeof (HtmlInputRadioButton));
80                         htmlInputControls.Add ("TEXT", typeof (HtmlInputText));
81                         htmlInputControls.Add ("PASSWORD", typeof (HtmlInputPassword));
82                 }
83
84                 public RootBuilder (TemplateParser parser)
85                 {
86                         foundry = new AspComponentFoundry ();
87                         Line = 1;
88                         if (parser != null)
89                                 FileName = parser.InputFile;
90                         Init (parser, null, null, null, null, null);
91                 }
92
93                 public override Type GetChildControlType (string tagName, IDictionary attribs) 
94                 {
95                         if (tagName == null)
96                                 throw new ArgumentNullException ("tagName");
97
98                         AspComponent component = foundry.GetComponent (tagName);
99                         
100                         if (component != null) {
101                                 if (!String.IsNullOrEmpty (component.Source)) {
102                                         TemplateParser parser = Parser;
103
104                                         if (component.FromConfig) {
105                                                 string parserDir = parser.BaseVirtualDir;
106                                                 VirtualPath vp = new VirtualPath (component.Source);
107
108                                                 if (parserDir == vp.Directory)
109                                                         throw new ParseException (parser.Location,
110                                                                                   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));
111                                                 
112                                                 Parser.AddDependency (component.Source);
113                                         }
114                                 }
115                                 return component.Type;
116                         } else if (component != null && component.Prefix != String.Empty)
117                                 throw new Exception ("Unknown server tag '" + tagName + "'");
118                         
119                         return LookupHtmlControls (tagName, attribs);
120                 }
121
122                 static Type LookupHtmlControls (string tagName, IDictionary attribs)
123                 {
124                         object o = htmlControls [tagName];
125                         if (o is string) {
126                                 if (attribs == null)
127                                         throw new HttpException ("Unable to map input type control to a Type.");
128
129                                 string ctype = attribs ["TYPE"] as string;
130                                 if (ctype == null)
131                                         ctype = "TEXT"; // The default used by MS
132
133                                 Type t = htmlInputControls [ctype] as Type;
134                                 if (t == null)
135                                         throw new HttpException ("Unable to map input type control to a Type.");
136
137                                 return t;
138                         }
139
140                         if (o == null)
141                                 o = typeof (HtmlGenericControl);
142
143                         return (Type) o;
144                 }
145
146                 internal AspComponentFoundry Foundry {
147                         get { return foundry; }
148                         set {
149                                 if (value is AspComponentFoundry)
150                                         foundry = value;
151                         }
152                 }
153
154                 // FIXME: it's empty (but not null) when using the new default ctor
155                 // but I'm not sure when something should gets in...
156                 public IDictionary BuiltObjects {
157                         get {
158                                 if (built_objects == null)
159                                         built_objects = new Hashtable ();
160                                 return built_objects;
161                         }
162                 }
163         }
164 }