RootBuilder.cs 5.8 KB

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