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