UserControlParser.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // System.Web.UI.UserControlParser
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  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;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Web;
  33. using System.Web.Compilation;
  34. using System.Web.Util;
  35. namespace System.Web.UI
  36. {
  37. internal class UserControlParser : TemplateControlParser
  38. {
  39. #if NET_2_0
  40. string masterPage;
  41. #endif
  42. #if !NET_2_0
  43. internal UserControlParser (string virtualPath, string inputFile, HttpContext context)
  44. : this (virtualPath, inputFile, context, null)
  45. {
  46. }
  47. internal UserControlParser (string virtualPath, string inputFile, ArrayList deps, HttpContext context)
  48. : this (virtualPath, inputFile, context, null)
  49. {
  50. this.Dependencies = deps;
  51. }
  52. internal UserControlParser (string virtualPath, string inputFile, HttpContext context, string type)
  53. {
  54. Context = context;
  55. BaseVirtualDir = VirtualPathUtility.GetDirectory (virtualPath, false);
  56. InputFile = inputFile;
  57. SetBaseType (type);
  58. AddApplicationAssembly ();
  59. }
  60. #else
  61. internal UserControlParser (VirtualPath virtualPath, string inputFile, HttpContext context)
  62. : this (virtualPath, inputFile, context, null)
  63. {
  64. }
  65. internal UserControlParser (VirtualPath virtualPath, string inputFile, ArrayList deps, HttpContext context)
  66. : this (virtualPath, inputFile, context, null)
  67. {
  68. this.Dependencies = deps;
  69. }
  70. internal UserControlParser (VirtualPath virtualPath, string inputFile, HttpContext context, string type)
  71. {
  72. VirtualPath = virtualPath;
  73. Context = context;
  74. BaseVirtualDir = virtualPath.DirectoryNoNormalize;
  75. InputFile = inputFile;
  76. SetBaseType (type);
  77. AddApplicationAssembly ();
  78. LoadConfigDefaults ();
  79. }
  80. internal UserControlParser (VirtualPath virtualPath, TextReader reader, HttpContext context)
  81. : this (virtualPath, null, reader, context)
  82. {
  83. }
  84. internal UserControlParser (VirtualPath virtualPath, string inputFile, TextReader reader, HttpContext context)
  85. {
  86. VirtualPath = virtualPath;
  87. Context = context;
  88. BaseVirtualDir = virtualPath.DirectoryNoNormalize;
  89. if (String.IsNullOrEmpty (inputFile))
  90. InputFile = virtualPath.PhysicalPath;
  91. else
  92. InputFile = inputFile;
  93. Reader = reader;
  94. SetBaseType (null);
  95. AddApplicationAssembly ();
  96. LoadConfigDefaults ();
  97. }
  98. internal UserControlParser (TextReader reader, int? uniqueSuffix, HttpContext context)
  99. {
  100. Context = context;
  101. string fpath = context.Request.FilePath;
  102. VirtualPath = new VirtualPath (fpath);
  103. BaseVirtualDir = VirtualPathUtility.GetDirectory (fpath, false);
  104. // We're probably being called by ParseControl - let's use the requested
  105. // control's path plus unique suffix as our input file, since that's the
  106. // context we're being invoked from.
  107. InputFile = VirtualPathUtility.GetFileName (fpath) + "#" + (uniqueSuffix != null ? ((int)uniqueSuffix).ToString ("x") : "0");
  108. Reader = reader;
  109. SetBaseType (null);
  110. AddApplicationAssembly ();
  111. LoadConfigDefaults ();
  112. }
  113. internal static Type GetCompiledType (TextReader reader, int? inputHashCode, HttpContext context)
  114. {
  115. UserControlParser ucp = new UserControlParser (reader, inputHashCode, context);
  116. return ucp.CompileIntoType ();
  117. }
  118. #endif
  119. internal static Type GetCompiledType (string virtualPath, string inputFile, ArrayList deps, HttpContext context)
  120. {
  121. #if NET_2_0
  122. UserControlParser ucp = new UserControlParser (new VirtualPath (virtualPath), inputFile, deps, context);
  123. #else
  124. UserControlParser ucp = new UserControlParser (virtualPath, inputFile, deps, context);
  125. #endif
  126. return ucp.CompileIntoType ();
  127. }
  128. public static Type GetCompiledType (string virtualPath, string inputFile, HttpContext context)
  129. {
  130. #if NET_2_0
  131. UserControlParser ucp = new UserControlParser (new VirtualPath (virtualPath), inputFile, context);
  132. #else
  133. UserControlParser ucp = new UserControlParser (virtualPath, inputFile, context);
  134. #endif
  135. return ucp.CompileIntoType ();
  136. }
  137. protected override Type CompileIntoType ()
  138. {
  139. AspGenerator generator = new AspGenerator (this);
  140. return generator.GetCompiledType ();
  141. }
  142. internal override void ProcessMainAttributes (Hashtable atts)
  143. {
  144. #if NET_2_0
  145. masterPage = GetString (atts, "MasterPageFile", null);
  146. if (masterPage != null)
  147. AddDependency (masterPage);
  148. #endif
  149. base.ProcessMainAttributes (atts);
  150. }
  151. #if NET_2_0
  152. internal override string DefaultBaseTypeName {
  153. get { return PagesConfig.UserControlBaseType; }
  154. }
  155. #else
  156. internal override string DefaultBaseTypeName {
  157. get { return "System.Web.UI.UserControl"; }
  158. }
  159. #endif
  160. internal override string DefaultDirectiveName {
  161. get { return "control"; }
  162. }
  163. #if NET_2_0
  164. internal string MasterPageFile {
  165. get { return masterPage; }
  166. }
  167. #endif
  168. }
  169. }