UserControlParser.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. internal UserControlParser (string virtualPath, string inputFile, HttpContext context)
  43. : this (virtualPath, inputFile, context, null)
  44. {
  45. }
  46. internal UserControlParser (string virtualPath, string inputFile, ArrayList deps, HttpContext context)
  47. : this (virtualPath, inputFile, context, null)
  48. {
  49. this.Dependencies = deps;
  50. }
  51. internal UserControlParser (string virtualPath, string inputFile, HttpContext context, string type)
  52. {
  53. #if NET_2_0
  54. VirtualPath = new VirtualPath (virtualPath);
  55. #endif
  56. Context = context;
  57. BaseVirtualDir = VirtualPathUtility.GetDirectory (virtualPath, false);
  58. InputFile = inputFile;
  59. SetBaseType (type);
  60. AddApplicationAssembly ();
  61. }
  62. #if NET_2_0
  63. internal UserControlParser (string virtualPath, TextReader reader, HttpContext context)
  64. : this (virtualPath, null, reader, context)
  65. {
  66. }
  67. internal UserControlParser (string virtualPath, string inputFile, TextReader reader, HttpContext context)
  68. {
  69. VirtualPath = new VirtualPath (virtualPath);
  70. Context = context;
  71. BaseVirtualDir = VirtualPathUtility.GetDirectory (virtualPath, false);
  72. if (String.IsNullOrEmpty (inputFile)) {
  73. HttpRequest req = context != null ? context.Request : null;
  74. if (req != null)
  75. InputFile = req.MapPath (virtualPath);
  76. } else
  77. InputFile = inputFile;
  78. Reader = reader;
  79. SetBaseType (null);
  80. AddApplicationAssembly ();
  81. }
  82. internal UserControlParser (TextReader reader, int? uniqueSuffix, HttpContext context)
  83. {
  84. Context = context;
  85. string fpath = context.Request.FilePath;
  86. VirtualPath = new VirtualPath (fpath);
  87. BaseVirtualDir = VirtualPathUtility.GetDirectory (fpath, false);
  88. // We're probably being called by ParseControl - let's use the requested
  89. // control's path plus unique suffix as our input file, since that's the
  90. // context we're being invoked from.
  91. InputFile = VirtualPathUtility.GetFileName (fpath) + "#" + (uniqueSuffix != null ? ((int)uniqueSuffix).ToString ("x") : "0");
  92. Reader = reader;
  93. SetBaseType (null);
  94. AddApplicationAssembly ();
  95. }
  96. internal static Type GetCompiledType (TextReader reader, int? inputHashCode, HttpContext context)
  97. {
  98. UserControlParser ucp = new UserControlParser (reader, inputHashCode, context);
  99. return ucp.CompileIntoType ();
  100. }
  101. #endif
  102. internal static Type GetCompiledType (string virtualPath, string inputFile, ArrayList deps, HttpContext context)
  103. {
  104. UserControlParser ucp = new UserControlParser (virtualPath, inputFile, deps, context);
  105. return ucp.CompileIntoType ();
  106. }
  107. public static Type GetCompiledType (string virtualPath, string inputFile, HttpContext context)
  108. {
  109. UserControlParser ucp = new UserControlParser (virtualPath, inputFile, context);
  110. return ucp.CompileIntoType ();
  111. }
  112. protected override Type CompileIntoType ()
  113. {
  114. AspGenerator generator = new AspGenerator (this);
  115. return generator.GetCompiledType ();
  116. }
  117. internal override void ProcessMainAttributes (Hashtable atts)
  118. {
  119. #if NET_2_0
  120. masterPage = GetString (atts, "MasterPageFile", null);
  121. if (masterPage != null)
  122. AddDependency (masterPage);
  123. #endif
  124. base.ProcessMainAttributes (atts);
  125. }
  126. #if NET_2_0
  127. internal override string DefaultBaseTypeName {
  128. get { return PagesConfig.UserControlBaseType; }
  129. }
  130. #else
  131. internal override string DefaultBaseTypeName {
  132. get { return "System.Web.UI.UserControl"; }
  133. }
  134. #endif
  135. internal override string DefaultDirectiveName {
  136. get { return "control"; }
  137. }
  138. #if NET_2_0
  139. internal string MasterPageFile {
  140. get { return masterPage; }
  141. }
  142. #endif
  143. }
  144. }