TemplateControlParser.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
  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.Collections;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Security.Permissions;
  34. using System.Web.Compilation;
  35. using System.Web.Configuration;
  36. using System.Web.Util;
  37. namespace System.Web.UI
  38. {
  39. // CAS
  40. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. public abstract class TemplateControlParser : BaseTemplateParser
  43. {
  44. bool autoEventWireup = true;
  45. bool enableViewState = true;
  46. CompilationMode compilationMode = CompilationMode.Always;
  47. #if NET_4_0
  48. ClientIDMode? clientIDMode;
  49. #endif
  50. TextReader reader;
  51. protected TemplateControlParser ()
  52. {
  53. LoadConfigDefaults ();
  54. }
  55. internal override void LoadConfigDefaults ()
  56. {
  57. base.LoadConfigDefaults ();
  58. PagesSection ps = PagesConfig;
  59. autoEventWireup = ps.AutoEventWireup;
  60. enableViewState = ps.EnableViewState;
  61. compilationMode = ps.CompilationMode;
  62. }
  63. internal override void ProcessMainAttributes (IDictionary atts)
  64. {
  65. autoEventWireup = GetBool (atts, "AutoEventWireup", autoEventWireup);
  66. enableViewState = GetBool (atts, "EnableViewState", enableViewState);
  67. string value = GetString (atts, "CompilationMode", compilationMode.ToString ());
  68. if (!String.IsNullOrEmpty (value)) {
  69. try {
  70. compilationMode = (CompilationMode) Enum.Parse (typeof (CompilationMode), value, true);
  71. } catch (Exception ex) {
  72. ThrowParseException ("Invalid value of the CompilationMode attribute.", ex);
  73. }
  74. }
  75. atts.Remove ("TargetSchema"); // Ignored
  76. #if NET_4_0
  77. value = GetString (atts, "ClientIDMode", null);
  78. if (!String.IsNullOrEmpty (value)) {
  79. try {
  80. clientIDMode = (ClientIDMode) Enum.Parse (typeof (ClientIDMode), value, true);
  81. } catch (Exception ex) {
  82. ThrowParseException ("Invalid value of the ClientIDMode attribute.", ex);
  83. }
  84. }
  85. #endif
  86. base.ProcessMainAttributes (atts);
  87. }
  88. internal object GetCompiledInstance ()
  89. {
  90. Type type = CompileIntoType ();
  91. if (type == null)
  92. return null;
  93. object ctrl = Activator.CreateInstance (type);
  94. if (ctrl == null)
  95. return null;
  96. HandleOptions (ctrl);
  97. return ctrl;
  98. }
  99. internal override void AddDirective (string directive, IDictionary atts)
  100. {
  101. int cmp = String.Compare ("Register", directive, true, Helpers.InvariantCulture);
  102. if (cmp == 0) {
  103. string tagprefix = GetString (atts, "TagPrefix", null);
  104. if (tagprefix == null || tagprefix.Trim () == "")
  105. ThrowParseException ("No TagPrefix attribute found.");
  106. string ns = GetString (atts, "Namespace", null);
  107. string assembly = GetString (atts, "Assembly", null);
  108. if (ns == null && assembly != null)
  109. ThrowParseException ("Need a Namespace attribute with Assembly.");
  110. if (ns != null) {
  111. if (atts.Count != 0)
  112. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  113. RegisterNamespace (tagprefix, ns, assembly);
  114. return;
  115. }
  116. string tagname = GetString (atts, "TagName", null);
  117. string src = GetString (atts, "Src", null);
  118. if (tagname == null && src != null)
  119. ThrowParseException ("Need a TagName attribute with Src.");
  120. if (tagname != null && src == null)
  121. ThrowParseException ("Need a Src attribute with TagName.");
  122. RegisterCustomControl (tagprefix, tagname, src);
  123. return;
  124. }
  125. cmp = String.Compare ("Reference", directive, true, Helpers.InvariantCulture);
  126. if (cmp == 0) {
  127. string vp = null;
  128. string page = GetString (atts, "Page", null);
  129. bool is_page = (page != null);
  130. if (is_page)
  131. vp = page;
  132. bool dupe = false;
  133. string control = GetString (atts, "Control", null);
  134. if (control != null)
  135. if (is_page)
  136. dupe = true;
  137. else
  138. vp = control;
  139. string virtualPath = GetString (atts, "VirtualPath", null);
  140. if (virtualPath != null)
  141. if (vp != null)
  142. dupe = true;
  143. else
  144. vp = virtualPath;
  145. if (vp == null)
  146. ThrowParseException ("Must provide one of the 'page', 'control' or 'virtualPath' attributes");
  147. if (dupe)
  148. ThrowParseException ("Only one attribute can be specified.");
  149. AddDependency (vp);
  150. Type ctype;
  151. ctype = BuildManager.GetCompiledType (vp);
  152. AddAssembly (ctype.Assembly, true);
  153. if (atts.Count != 0)
  154. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  155. return;
  156. }
  157. base.AddDirective (directive, atts);
  158. }
  159. internal override void HandleOptions (object obj)
  160. {
  161. base.HandleOptions (obj);
  162. Control ctrl = obj as Control;
  163. ctrl.AutoEventWireup = autoEventWireup;
  164. ctrl.EnableViewState = enableViewState;
  165. }
  166. internal bool AutoEventWireup {
  167. get { return autoEventWireup; }
  168. }
  169. internal bool EnableViewState {
  170. get { return enableViewState; }
  171. }
  172. internal CompilationMode CompilationMode {
  173. get { return compilationMode; }
  174. }
  175. #if NET_4_0
  176. internal ClientIDMode? ClientIDMode {
  177. get { return clientIDMode; }
  178. }
  179. #endif
  180. internal override TextReader Reader {
  181. get { return reader; }
  182. set { reader = value; }
  183. }
  184. }
  185. }