TemplateControlParser.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // System.Web.UI.TemplateControlParser
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Web.Compilation;
  14. using System.Web.Util;
  15. namespace System.Web.UI
  16. {
  17. public abstract class TemplateControlParser : TemplateParser
  18. {
  19. bool autoEventWireup = true;
  20. bool enableViewState = true;
  21. internal override void ProcessMainAttributes (Hashtable atts)
  22. {
  23. autoEventWireup = GetBool (atts, "AutoEventWireup", true);
  24. enableViewState = GetBool (atts, "EnableViewState", true);
  25. atts.Remove ("TargetSchema"); // Ignored
  26. base.ProcessMainAttributes (atts);
  27. }
  28. internal object GetCompiledInstance (string virtualPath, string inputFile, HttpContext context)
  29. {
  30. Context = context;
  31. InputFile = Path.Combine (MapPath (virtualPath), inputFile);
  32. Type type = CompileIntoType ();
  33. if (type == null)
  34. return null;
  35. object ctrl = Activator.CreateInstance (type);
  36. if (ctrl == null)
  37. return null;
  38. HandleOptions (ctrl);
  39. return ctrl;
  40. }
  41. internal override void AddDirective (string directive, Hashtable atts)
  42. {
  43. int cmp = String.Compare ("Register", directive, true);
  44. if (cmp == 0) {
  45. string tagprefix = GetString (atts, "TagPrefix", null);
  46. if (tagprefix == null || tagprefix.Trim () == "")
  47. ThrowParseException ("No TagPrefix attribute found.");
  48. string ns = GetString (atts, "Namespace", null);
  49. string assembly = GetString (atts, "Assembly", null);
  50. if (ns != null && assembly == null)
  51. ThrowParseException ("Need an Assembly attribute with Namespace.");
  52. if (ns == null && assembly != null)
  53. ThrowParseException ("Need a Namespace attribute with Assembly.");
  54. if (ns != null) {
  55. if (atts.Count != 0)
  56. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  57. AddImport (ns);
  58. Assembly ass = AddAssemblyByName (assembly);
  59. AddDependency (ass.Location);
  60. RootBuilder.Foundry.RegisterFoundry (tagprefix, ass, ns);
  61. return;
  62. }
  63. string tagname = GetString (atts, "TagName", null);
  64. string src = GetString (atts, "Src", null);
  65. if (tagname == null && src != null)
  66. ThrowParseException ("Need a TagName attribute with Src.");
  67. if (tagname != null && src == null)
  68. ThrowParseException ("Need a Src attribute with TagName.");
  69. if (!src.EndsWith (".ascx"))
  70. ThrowParseException ("Source file extension for controls must be .ascx");
  71. AddDependency (Path.Combine (MapPath (BaseVirtualDir), src));
  72. Type type = UserControlParser.GetCompiledType (BaseVirtualDir, src, Context);
  73. AddAssembly (type.Assembly, true);
  74. RootBuilder.Foundry.RegisterFoundry (tagprefix, tagname, type);
  75. }
  76. cmp = String.Compare ("Reference", directive, true);
  77. if (cmp == 0) {
  78. string page = GetString (atts, "Page", null);
  79. string control = GetString (atts, "Control", null);
  80. //TODO: compile and store control/page
  81. Console.WriteLine ("WARNING: Reference is not supported yet!");
  82. if (atts.Count != 0)
  83. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  84. return;
  85. }
  86. atts.Remove ("OutputCache"); // ignored
  87. base.AddDirective (directive, atts);
  88. }
  89. protected override void HandleOptions (object obj)
  90. {
  91. Control ctrl = obj as Control;
  92. ctrl.AutoEventWireup = autoEventWireup;
  93. ctrl.EnableViewState = enableViewState;
  94. }
  95. internal bool AutoEventWireup {
  96. get { return autoEventWireup; }
  97. }
  98. internal bool EnableViewState {
  99. get { return enableViewState; }
  100. }
  101. }
  102. }