RootBuilder.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Web.Compilation;
  31. using System.Web.UI.HtmlControls;
  32. namespace System.Web.UI
  33. {
  34. public sealed class RootBuilder : TemplateBuilder
  35. {
  36. static Hashtable htmlControls;
  37. static Hashtable htmlInputControls;
  38. AspComponentFoundry foundry;
  39. static RootBuilder ()
  40. {
  41. htmlControls = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
  42. CaseInsensitiveComparer.DefaultInvariant);
  43. htmlControls.Add ("A", typeof (HtmlAnchor));
  44. htmlControls.Add ("BUTTON", typeof (HtmlButton));
  45. htmlControls.Add ("FORM", typeof (HtmlForm));
  46. #if NET_2_0
  47. htmlControls.Add ("HEAD", typeof (HtmlHead));
  48. #endif
  49. htmlControls.Add ("IMG", typeof (HtmlImage));
  50. htmlControls.Add ("INPUT", "INPUT");
  51. #if NET_2_0
  52. htmlControls.Add ("LINK", typeof (HtmlLink));
  53. htmlControls.Add ("META", typeof (HtmlLink));
  54. #endif
  55. htmlControls.Add ("SELECT", typeof (HtmlSelect));
  56. htmlControls.Add ("TABLE", typeof (HtmlTable));
  57. htmlControls.Add ("TD", typeof (HtmlTableCell));
  58. htmlControls.Add ("TH", typeof (HtmlTableCell));
  59. htmlControls.Add ("TR", typeof (HtmlTableRow));
  60. htmlControls.Add ("TEXTAREA", typeof (HtmlTextArea));
  61. htmlInputControls = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
  62. CaseInsensitiveComparer.DefaultInvariant);
  63. htmlInputControls.Add ("BUTTON", typeof (HtmlInputButton));
  64. #if NET_2_0
  65. htmlInputControls.Add ("SUBMIT", typeof (HtmlInputSubmit));
  66. htmlInputControls.Add ("RESET", typeof (HtmlInputReset));
  67. #else
  68. htmlInputControls.Add ("SUBMIT", typeof (HtmlInputButton));
  69. htmlInputControls.Add ("RESET", typeof (HtmlInputButton));
  70. #endif
  71. htmlInputControls.Add ("CHECKBOX", typeof (HtmlInputCheckBox));
  72. htmlInputControls.Add ("FILE", typeof (HtmlInputFile));
  73. htmlInputControls.Add ("HIDDEN", typeof (HtmlInputHidden));
  74. htmlInputControls.Add ("IMAGE", typeof (HtmlInputImage));
  75. htmlInputControls.Add ("RADIO", typeof (HtmlInputRadioButton));
  76. htmlInputControls.Add ("TEXT", typeof (HtmlInputText));
  77. #if NET_2_0
  78. htmlInputControls.Add ("PASSWORD", typeof (HtmlInputPassword));
  79. #else
  80. htmlInputControls.Add ("PASSWORD", typeof (HtmlInputText));
  81. #endif
  82. }
  83. public RootBuilder (TemplateParser parser)
  84. {
  85. foundry = new AspComponentFoundry ();
  86. line = 1;
  87. fileName = parser.InputFile;
  88. Init (parser, null, null, null, null, null);
  89. }
  90. public override Type GetChildControlType (string tagName, IDictionary attribs)
  91. {
  92. string prefix;
  93. string cname;
  94. int colon = tagName.IndexOf (':');
  95. if (colon != -1) {
  96. if (colon == 0)
  97. throw new Exception ("Empty TagPrefix is not valid");
  98. if (colon + 1 == tagName.Length)
  99. return null;
  100. prefix = tagName.Substring (0, colon);
  101. cname = tagName.Substring (colon + 1);
  102. } else {
  103. prefix = "";
  104. cname = tagName;
  105. }
  106. Type t = foundry.GetComponentType (prefix, cname);
  107. if (t != null)
  108. return t;
  109. else if (prefix != "")
  110. throw new Exception ("TagPrefix " + prefix + " not registered");
  111. return LookupHtmlControls (tagName, attribs);
  112. }
  113. static Type LookupHtmlControls (string tagName, IDictionary attribs)
  114. {
  115. object o = htmlControls [tagName];
  116. if (o is string) {
  117. if (attribs == null)
  118. throw new HttpException ("Unable to map input type control to a Type.");
  119. string ctype = attribs ["TYPE"] as string;
  120. if (ctype == null)
  121. ctype = "TEXT"; // The default used by MS
  122. Type t = htmlInputControls [ctype] as Type;
  123. if (t == null)
  124. throw new HttpException ("Unable to map input type control to a Type.");
  125. return t;
  126. }
  127. if (o == null)
  128. o = typeof (HtmlGenericControl);
  129. return (Type) o;
  130. }
  131. internal AspComponentFoundry Foundry {
  132. get { return foundry; }
  133. }
  134. }
  135. }