ControlBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //
  2. // System.Web.UI.ControlBuilder.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002, 2003 Ximian, Inc. (http://www.ximian.com)
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections;
  31. using System.CodeDom;
  32. using System.Reflection;
  33. using System.Security.Permissions;
  34. using System.Web.Compilation;
  35. namespace System.Web.UI {
  36. // CAS
  37. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public class ControlBuilder
  40. {
  41. internal static BindingFlags flagsNoCase = BindingFlags.Public |
  42. BindingFlags.Instance |
  43. BindingFlags.Static |
  44. BindingFlags.IgnoreCase;
  45. TemplateParser parser;
  46. internal ControlBuilder parentBuilder;
  47. Type type;
  48. string tagName;
  49. string id;
  50. internal IDictionary attribs;
  51. internal int line;
  52. internal string fileName;
  53. bool childrenAsProperties;
  54. bool isIParserAccessor = true;
  55. bool hasAspCode;
  56. internal ControlBuilder defaultPropertyBuilder;
  57. ArrayList children;
  58. static int nextID;
  59. internal bool haveParserVariable;
  60. internal CodeMemberMethod method;
  61. internal CodeMemberMethod renderMethod;
  62. internal int renderIndex;
  63. internal bool isProperty;
  64. internal ILocation location;
  65. ArrayList otherTags;
  66. public ControlBuilder ()
  67. {
  68. }
  69. internal ControlBuilder (TemplateParser parser,
  70. ControlBuilder parentBuilder,
  71. Type type,
  72. string tagName,
  73. string id,
  74. IDictionary attribs,
  75. int line,
  76. string sourceFileName)
  77. {
  78. this.parser = parser;
  79. this.parentBuilder = parentBuilder;
  80. this.type = type;
  81. this.tagName = tagName;
  82. this.id = id;
  83. this.attribs = attribs;
  84. this.line = line;
  85. this.fileName = sourceFileName;
  86. }
  87. internal void EnsureOtherTags ()
  88. {
  89. if (otherTags == null)
  90. otherTags = new ArrayList ();
  91. }
  92. internal ArrayList OtherTags {
  93. get { return otherTags; }
  94. }
  95. public Type ControlType {
  96. get { return type; }
  97. }
  98. protected bool FChildrenAsProperties {
  99. get { return childrenAsProperties; }
  100. }
  101. protected bool FIsNonParserAccessor {
  102. get { return !isIParserAccessor; }
  103. }
  104. public bool HasAspCode {
  105. get { return hasAspCode; }
  106. }
  107. public string ID {
  108. get { return id; }
  109. set { id = value; }
  110. }
  111. internal ArrayList Children {
  112. get { return children; }
  113. }
  114. internal void SetControlType (Type t)
  115. {
  116. type = t;
  117. }
  118. protected bool InDesigner {
  119. get { return false; }
  120. }
  121. public Type NamingContainerType {
  122. get {
  123. if (parentBuilder == null)
  124. return typeof (Control);
  125. Type ptype = parentBuilder.ControlType;
  126. if (ptype == null)
  127. return parentBuilder.NamingContainerType;
  128. if (!typeof (INamingContainer).IsAssignableFrom (ptype))
  129. return parentBuilder.NamingContainerType;
  130. return ptype;
  131. }
  132. }
  133. #if NET_2_0
  134. public
  135. #else
  136. internal
  137. #endif
  138. Type BindingContainerType {
  139. get {
  140. if (parentBuilder == null)
  141. return typeof (Control);
  142. if (parentBuilder is TemplateBuilder && ((TemplateBuilder)parentBuilder).ContainerType != null)
  143. return ((TemplateBuilder)parentBuilder).ContainerType;
  144. Type ptype = parentBuilder.ControlType;
  145. if (ptype == null)
  146. return parentBuilder.BindingContainerType;
  147. if (!typeof (INamingContainer).IsAssignableFrom (ptype))
  148. return parentBuilder.BindingContainerType;
  149. return ptype;
  150. }
  151. }
  152. internal TemplateBuilder ParentTemplateBuilder {
  153. get {
  154. if (parentBuilder == null)
  155. return null;
  156. else if (parentBuilder is TemplateBuilder)
  157. return (TemplateBuilder) parentBuilder;
  158. else
  159. return parentBuilder.ParentTemplateBuilder;
  160. }
  161. }
  162. protected TemplateParser Parser {
  163. get { return parser; }
  164. }
  165. public string TagName {
  166. get { return tagName; }
  167. }
  168. internal RootBuilder Root {
  169. get {
  170. if (GetType () == typeof (RootBuilder))
  171. return (RootBuilder) this;
  172. return (RootBuilder) parentBuilder.Root;
  173. }
  174. }
  175. internal bool ChildrenAsProperties {
  176. get { return childrenAsProperties; }
  177. }
  178. public virtual bool AllowWhitespaceLiterals ()
  179. {
  180. return true;
  181. }
  182. public virtual void AppendLiteralString (string s)
  183. {
  184. if (s == null || s == "")
  185. return;
  186. if (childrenAsProperties || !isIParserAccessor) {
  187. if (defaultPropertyBuilder != null) {
  188. defaultPropertyBuilder.AppendLiteralString (s);
  189. } else if (s.Trim () != "") {
  190. throw new HttpException ("Literal content not allowed for " + tagName + " " +
  191. GetType () + " \"" + s + "\"");
  192. }
  193. return;
  194. }
  195. if (!AllowWhitespaceLiterals () && s.Trim () == "")
  196. return;
  197. if (HtmlDecodeLiterals ())
  198. s = HttpUtility.HtmlDecode (s);
  199. if (children == null)
  200. children = new ArrayList ();
  201. children.Add (s);
  202. }
  203. public virtual void AppendSubBuilder (ControlBuilder subBuilder)
  204. {
  205. subBuilder.OnAppendToParentBuilder (this);
  206. subBuilder.parentBuilder = this;
  207. if (childrenAsProperties) {
  208. AppendToProperty (subBuilder);
  209. return;
  210. }
  211. if (typeof (CodeRenderBuilder).IsAssignableFrom (subBuilder.GetType ())) {
  212. AppendCode (subBuilder);
  213. return;
  214. }
  215. if (children == null)
  216. children = new ArrayList ();
  217. children.Add (subBuilder);
  218. }
  219. void AppendToProperty (ControlBuilder subBuilder)
  220. {
  221. if (typeof (CodeRenderBuilder) == subBuilder.GetType ())
  222. throw new HttpException ("Code render not supported here.");
  223. if (defaultPropertyBuilder != null) {
  224. defaultPropertyBuilder.AppendSubBuilder (subBuilder);
  225. return;
  226. }
  227. if (children == null)
  228. children = new ArrayList ();
  229. children.Add (subBuilder);
  230. }
  231. void AppendCode (ControlBuilder subBuilder)
  232. {
  233. if (type != null && !(typeof (Control).IsAssignableFrom (type)))
  234. throw new HttpException ("Code render not supported here.");
  235. if (typeof (CodeRenderBuilder) == subBuilder.GetType ())
  236. hasAspCode = true;
  237. if (children == null)
  238. children = new ArrayList ();
  239. children.Add (subBuilder);
  240. }
  241. public virtual void CloseControl ()
  242. {
  243. }
  244. public static ControlBuilder CreateBuilderFromType (TemplateParser parser,
  245. ControlBuilder parentBuilder,
  246. Type type,
  247. string tagName,
  248. string id,
  249. IDictionary attribs,
  250. int line,
  251. string sourceFileName)
  252. {
  253. ControlBuilder builder;
  254. object [] atts = type.GetCustomAttributes (typeof (ControlBuilderAttribute), true);
  255. if (atts != null && atts.Length > 0) {
  256. ControlBuilderAttribute att = (ControlBuilderAttribute) atts [0];
  257. builder = (ControlBuilder) Activator.CreateInstance (att.BuilderType);
  258. } else {
  259. builder = new ControlBuilder ();
  260. }
  261. builder.Init (parser, parentBuilder, type, tagName, id, attribs);
  262. builder.line = line;
  263. builder.fileName = sourceFileName;
  264. return builder;
  265. }
  266. public virtual Type GetChildControlType (string tagName, IDictionary attribs)
  267. {
  268. return null;
  269. }
  270. public virtual bool HasBody ()
  271. {
  272. return true;
  273. }
  274. public virtual bool HtmlDecodeLiterals ()
  275. {
  276. return false;
  277. }
  278. ControlBuilder CreatePropertyBuilder (string propName, TemplateParser parser, IDictionary atts)
  279. {
  280. PropertyInfo prop = type.GetProperty (propName, flagsNoCase);
  281. if (prop == null) {
  282. string msg = String.Format ("Property {0} not found in type {1}", propName, type);
  283. throw new HttpException (msg);
  284. }
  285. Type propType = prop.PropertyType;
  286. ControlBuilder builder = null;
  287. if (typeof (ICollection).IsAssignableFrom (propType)) {
  288. builder = new CollectionBuilder ();
  289. } else if (typeof (ITemplate).IsAssignableFrom (propType)) {
  290. builder = new TemplateBuilder (prop);
  291. } else {
  292. builder = CreateBuilderFromType (parser, parentBuilder, propType, prop.Name,
  293. null, atts, line, fileName);
  294. builder.isProperty = true;
  295. return builder;
  296. }
  297. builder.Init (parser, this, null, prop.Name, null, atts);
  298. builder.fileName = fileName;
  299. builder.line = line;
  300. builder.isProperty = true;
  301. return builder;
  302. }
  303. public virtual void Init (TemplateParser parser,
  304. ControlBuilder parentBuilder,
  305. Type type,
  306. string tagName,
  307. string id,
  308. IDictionary attribs)
  309. {
  310. this.parser = parser;
  311. if (parser != null)
  312. this.location = parser.Location;
  313. this.parentBuilder = parentBuilder;
  314. this.type = type;
  315. this.tagName = tagName;
  316. this.id = id;
  317. this.attribs = attribs;
  318. if (type == null)
  319. return;
  320. if (this is TemplateBuilder)
  321. return;
  322. object [] atts = type.GetCustomAttributes (typeof (ParseChildrenAttribute), true);
  323. if (!typeof (IParserAccessor).IsAssignableFrom (type) && atts.Length == 0) {
  324. isIParserAccessor = false;
  325. childrenAsProperties = true;
  326. } else if (atts.Length > 0) {
  327. ParseChildrenAttribute att = (ParseChildrenAttribute) atts [0];
  328. childrenAsProperties = att.ChildrenAsProperties;
  329. if (childrenAsProperties && att.DefaultProperty != "") {
  330. defaultPropertyBuilder = CreatePropertyBuilder (att.DefaultProperty,
  331. parser, null);
  332. }
  333. }
  334. }
  335. public virtual bool NeedsTagInnerText ()
  336. {
  337. return false;
  338. }
  339. public virtual void OnAppendToParentBuilder (ControlBuilder parentBuilder)
  340. {
  341. if (defaultPropertyBuilder == null)
  342. return;
  343. ControlBuilder old = defaultPropertyBuilder;
  344. defaultPropertyBuilder = null;
  345. AppendSubBuilder (old);
  346. }
  347. internal void SetTagName (string name)
  348. {
  349. tagName = name;
  350. }
  351. public virtual void SetTagInnerText (string text)
  352. {
  353. }
  354. internal string GetNextID (string proposedID)
  355. {
  356. if (proposedID != null && proposedID.Trim () != "")
  357. return proposedID;
  358. return "_bctrl_" + nextID++;
  359. }
  360. internal virtual ControlBuilder CreateSubBuilder (string tagid,
  361. Hashtable atts,
  362. Type childType,
  363. TemplateParser parser,
  364. ILocation location)
  365. {
  366. ControlBuilder childBuilder = null;
  367. if (childrenAsProperties) {
  368. if (defaultPropertyBuilder == null) {
  369. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  370. } else {
  371. childBuilder = defaultPropertyBuilder.CreateSubBuilder (tagid, atts,
  372. null, parser, location);
  373. }
  374. return childBuilder;
  375. }
  376. childType = GetChildControlType (tagid, atts);
  377. if (childType == null)
  378. return null;
  379. childBuilder = CreateBuilderFromType (parser, this, childType, tagid, id, atts,
  380. location.BeginLine, location.Filename);
  381. return childBuilder;
  382. }
  383. internal virtual object CreateInstance ()
  384. {
  385. // HtmlGenericControl, HtmlTableCell...
  386. object [] atts = type.GetCustomAttributes (typeof (ConstructorNeedsTagAttribute), true);
  387. object [] args = null;
  388. if (atts != null && atts.Length > 0) {
  389. ConstructorNeedsTagAttribute att = (ConstructorNeedsTagAttribute) atts [0];
  390. if (att.NeedsTag)
  391. args = new object [] {tagName};
  392. }
  393. return Activator.CreateInstance (type, args);
  394. }
  395. internal virtual void CreateChildren (object parent)
  396. {
  397. if (children == null || children.Count == 0)
  398. return;
  399. IParserAccessor parser = parent as IParserAccessor;
  400. if (parser == null)
  401. return;
  402. foreach (object o in children) {
  403. if (o is string) {
  404. parser.AddParsedSubObject (new LiteralControl ((string) o));
  405. } else {
  406. parser.AddParsedSubObject (((ControlBuilder) o).CreateInstance ());
  407. }
  408. }
  409. }
  410. }
  411. }