MasterPageParser.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // System.Web.UI.MasterPageParser
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc.
  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. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.IO;
  34. using System.Web;
  35. using System.Web.Compilation;
  36. using System.Web.Hosting;
  37. using System.Web.Util;
  38. namespace System.Web.UI
  39. {
  40. internal sealed class MasterPageParser: UserControlParser
  41. {
  42. Type masterType;
  43. string masterTypeVirtualPath;
  44. List <string> contentPlaceHolderIds;
  45. string cacheEntryName;
  46. internal MasterPageParser (string virtualPath, string inputFile, HttpContext context)
  47. : base (virtualPath, inputFile, context, "System.Web.UI.MasterPage")
  48. {
  49. this.cacheEntryName = String.Concat ("@@MasterPagePHIDS:", virtualPath, ":", inputFile);
  50. contentPlaceHolderIds = HttpRuntime.InternalCache.Get (this.cacheEntryName) as List <string>;
  51. }
  52. internal MasterPageParser (string virtualPath, TextReader reader, HttpContext context)
  53. : this (virtualPath, null, reader, context)
  54. {
  55. }
  56. internal MasterPageParser (string virtualPath, string inputFile, TextReader reader, HttpContext context)
  57. : base (virtualPath, inputFile, reader, context)
  58. {
  59. this.cacheEntryName = String.Concat ("@@MasterPagePHIDS:", virtualPath, ":", InputFile);
  60. contentPlaceHolderIds = HttpRuntime.InternalCache.Get (this.cacheEntryName) as List <string>;
  61. }
  62. public static MasterPage GetCompiledMasterInstance (string virtualPath, string inputFile, HttpContext context)
  63. {
  64. return BuildManager.CreateInstanceFromVirtualPath (virtualPath, typeof (MasterPage)) as MasterPage;
  65. }
  66. public static Type GetCompiledMasterType (string virtualPath, string inputFile, HttpContext context)
  67. {
  68. return BuildManager.GetCompiledType (virtualPath);
  69. }
  70. internal override void HandleOptions (object obj)
  71. {
  72. base.HandleOptions (obj);
  73. MasterPage mp = (MasterPage)obj;
  74. mp.MasterPageFile = MasterPageFile;
  75. }
  76. internal override void AddDirective (string directive, Hashtable atts)
  77. {
  78. if (String.Compare ("MasterType", directive, true) == 0) {
  79. string type = GetString (atts, "TypeName", null);
  80. if (type != null) {
  81. masterType = LoadType (type);
  82. if (masterType == null)
  83. ThrowParseException ("Could not load type '" + type + "'.");
  84. } else {
  85. string path = GetString (atts, "VirtualPath", null);
  86. if (!String.IsNullOrEmpty (path)) {
  87. if (!HostingEnvironment.VirtualPathProvider.FileExists (path))
  88. ThrowParseFileNotFound (path);
  89. masterTypeVirtualPath = path;
  90. AddDependency (path);
  91. } else
  92. ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
  93. }
  94. if (masterType != null)
  95. AddAssembly (masterType.Assembly, true);
  96. }
  97. else
  98. base.AddDirective (directive, atts);
  99. }
  100. internal void AddContentPlaceHolderId (string id)
  101. {
  102. if (contentPlaceHolderIds == null) {
  103. contentPlaceHolderIds = new List <string> (1);
  104. HttpRuntime.InternalCache.Insert (cacheEntryName, contentPlaceHolderIds);
  105. }
  106. contentPlaceHolderIds.Add (id);
  107. }
  108. internal Type MasterType {
  109. get {
  110. if (masterType == null && !String.IsNullOrEmpty (masterTypeVirtualPath))
  111. masterType = BuildManager.GetCompiledType (masterTypeVirtualPath);
  112. return masterType;
  113. }
  114. }
  115. internal override string DefaultBaseTypeName {
  116. get { return "System.Web.UI.MasterPage"; }
  117. }
  118. internal override string DefaultDirectiveName {
  119. get { return "master"; }
  120. }
  121. }
  122. }
  123. #endif