MasterPageParser.cs 4.8 KB

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