MasterPageParser.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.Util;
  37. namespace System.Web.UI
  38. {
  39. internal sealed class MasterPageParser: UserControlParser
  40. {
  41. Type masterType;
  42. List <string> contentPlaceHolderIds;
  43. string cacheEntryName;
  44. internal MasterPageParser (string virtualPath, string inputFile, HttpContext context)
  45. : base (virtualPath, inputFile, context, "System.Web.UI.MasterPage")
  46. {
  47. this.cacheEntryName = String.Concat ("@@MasterPagePHIDS:", virtualPath, ":", inputFile);
  48. contentPlaceHolderIds = HttpRuntime.InternalCache.Get (this.cacheEntryName) as List <string>;
  49. }
  50. public static MasterPage GetCompiledMasterInstance (string virtualPath, string inputFile, HttpContext context)
  51. {
  52. MasterPageParser mpp = new MasterPageParser (virtualPath, inputFile, context);
  53. MasterPage mp = (MasterPage) mpp.GetCompiledInstance ();
  54. mp.SetPlaceHolderIds (mpp.contentPlaceHolderIds);
  55. return mp;
  56. }
  57. public static Type GetCompiledMasterType (string virtualPath, string inputFile, HttpContext context)
  58. {
  59. MasterPageParser mpp = new MasterPageParser (virtualPath, inputFile, context);
  60. return mpp.CompileIntoType ();
  61. }
  62. internal override void HandleOptions (object obj)
  63. {
  64. base.HandleOptions (obj);
  65. MasterPage mp = (MasterPage)obj;
  66. mp.MasterPageFile = MasterPageFile;
  67. }
  68. internal override void AddDirective (string directive, Hashtable atts)
  69. {
  70. if (String.Compare ("MasterType", directive, true) == 0) {
  71. string type = GetString (atts, "TypeName", null);
  72. if (type != null) {
  73. masterType = LoadType (type);
  74. if (masterType == null)
  75. ThrowParseException ("Could not load type '" + type + "'.");
  76. } else {
  77. string path = GetString (atts, "VirtualPath", null);
  78. if (path != null) {
  79. string mappedPath = MapPath (path);
  80. masterType = MasterPageParser.GetCompiledMasterType (path, mappedPath,
  81. HttpContext.Current);
  82. AddDependency (mappedPath);
  83. } else
  84. ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute."); }
  85. AddAssembly (masterType.Assembly, true);
  86. }
  87. else
  88. base.AddDirective (directive, atts);
  89. }
  90. internal void AddContentPlaceHolderId (string id)
  91. {
  92. if (contentPlaceHolderIds == null) {
  93. contentPlaceHolderIds = new List <string> (1);
  94. HttpRuntime.InternalCache.Insert (cacheEntryName, contentPlaceHolderIds);
  95. }
  96. contentPlaceHolderIds.Add (id);
  97. }
  98. internal Type MasterType {
  99. get { return masterType; }
  100. }
  101. internal override Type DefaultBaseType {
  102. get { return typeof (MasterPage); }
  103. }
  104. internal override string DefaultBaseTypeName {
  105. get { return "System.Web.UI.MasterPage"; }
  106. }
  107. internal override string DefaultDirectiveName {
  108. get { return "master"; }
  109. }
  110. }
  111. }
  112. #endif