UserControlParser.cs 4.8 KB

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