RootBuilder.cs 6.3 KB

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