ControlBuilder.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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.Configuration;
  32. using System.CodeDom;
  33. using System.Globalization;
  34. using System.Reflection;
  35. using System.Security.Permissions;
  36. using System.Web.Compilation;
  37. using System.Web.Configuration;
  38. using System.IO;
  39. using System.Web.UI.WebControls;
  40. namespace System.Web.UI {
  41. // CAS
  42. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  43. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  44. public class ControlBuilder
  45. {
  46. internal static BindingFlags flagsNoCase = BindingFlags.Public |
  47. BindingFlags.Instance |
  48. BindingFlags.Static |
  49. BindingFlags.IgnoreCase;
  50. ControlBuilder myNamingContainer;
  51. TemplateParser parser;
  52. internal ControlBuilder parentBuilder;
  53. Type type;
  54. string tagName;
  55. string originalTagName;
  56. string id;
  57. internal IDictionary attribs;
  58. internal int line;
  59. internal string fileName;
  60. bool childrenAsProperties;
  61. bool isIParserAccessor = true;
  62. bool hasAspCode;
  63. internal ControlBuilder defaultPropertyBuilder;
  64. ArrayList children;
  65. ArrayList templateChildren;
  66. static int nextID;
  67. internal bool haveParserVariable;
  68. internal CodeMemberMethod method;
  69. internal CodeStatementCollection methodStatements;
  70. internal CodeMemberMethod renderMethod;
  71. internal int renderIndex;
  72. internal bool isProperty;
  73. internal ILocation location;
  74. ArrayList otherTags;
  75. public ControlBuilder ()
  76. {
  77. }
  78. internal ControlBuilder (TemplateParser parser,
  79. ControlBuilder parentBuilder,
  80. Type type,
  81. string tagName,
  82. string id,
  83. IDictionary attribs,
  84. int line,
  85. string sourceFileName)
  86. {
  87. this.parser = parser;
  88. this.parentBuilder = parentBuilder;
  89. this.type = type;
  90. this.tagName = tagName;
  91. this.id = id;
  92. this.attribs = attribs;
  93. this.line = line;
  94. this.fileName = sourceFileName;
  95. }
  96. internal void EnsureOtherTags ()
  97. {
  98. if (otherTags == null)
  99. otherTags = new ArrayList ();
  100. }
  101. internal ArrayList OtherTags {
  102. get { return otherTags; }
  103. }
  104. public Type ControlType {
  105. get { return type; }
  106. }
  107. protected bool FChildrenAsProperties {
  108. get { return childrenAsProperties; }
  109. }
  110. protected bool FIsNonParserAccessor {
  111. get { return !isIParserAccessor; }
  112. }
  113. public bool HasAspCode {
  114. get { return hasAspCode; }
  115. }
  116. public string ID {
  117. get { return id; }
  118. set { id = value; }
  119. }
  120. internal ArrayList Children {
  121. get { return children; }
  122. }
  123. internal ArrayList TemplateChildren {
  124. get { return templateChildren; }
  125. }
  126. internal void SetControlType (Type t)
  127. {
  128. type = t;
  129. }
  130. protected bool InDesigner {
  131. get { return false; }
  132. }
  133. public Type NamingContainerType {
  134. get {
  135. ControlBuilder cb = myNamingContainer;
  136. if (cb == null)
  137. return typeof (Control);
  138. return cb.ControlType;
  139. }
  140. }
  141. ControlBuilder MyNamingContainer {
  142. get {
  143. if (myNamingContainer == null) {
  144. Type controlType = parentBuilder != null ? parentBuilder.ControlType : null;
  145. if (parentBuilder == null && controlType == null)
  146. myNamingContainer = null;
  147. else if (parentBuilder is TemplateBuilder)
  148. myNamingContainer = parentBuilder;
  149. else if (controlType != null && typeof (INamingContainer).IsAssignableFrom (controlType))
  150. myNamingContainer = parentBuilder;
  151. else
  152. myNamingContainer = parentBuilder.MyNamingContainer;
  153. }
  154. return myNamingContainer;
  155. }
  156. }
  157. #if NET_2_0
  158. public virtual
  159. #else
  160. internal
  161. #endif
  162. Type BindingContainerType {
  163. get {
  164. ControlBuilder cb = (this is TemplateBuilder) ? this : MyNamingContainer;
  165. if (cb == null)
  166. return typeof (Control);
  167. #if NET_2_0
  168. if (cb != this && cb is ContentBuilderInternal)
  169. return cb.BindingContainerType;
  170. #endif
  171. if (cb is TemplateBuilder) {
  172. Type ct = ((TemplateBuilder) cb).ContainerType;
  173. if (typeof (INonBindingContainer).IsAssignableFrom (ct))
  174. return MyNamingContainer.BindingContainerType;
  175. if (ct != null)
  176. return ct;
  177. return ControlType;
  178. }
  179. return cb.ControlType;
  180. }
  181. }
  182. internal TemplateBuilder ParentTemplateBuilder {
  183. get {
  184. if (parentBuilder == null)
  185. return null;
  186. else if (parentBuilder is TemplateBuilder)
  187. return (TemplateBuilder) parentBuilder;
  188. else
  189. return parentBuilder.ParentTemplateBuilder;
  190. }
  191. }
  192. protected TemplateParser Parser {
  193. get { return parser; }
  194. }
  195. public string TagName {
  196. get { return tagName; }
  197. }
  198. internal string OriginalTagName {
  199. get {
  200. if (originalTagName == null || originalTagName.Length == 0)
  201. return TagName;
  202. return originalTagName;
  203. }
  204. }
  205. internal RootBuilder Root {
  206. get {
  207. if (GetType () == typeof (RootBuilder))
  208. return (RootBuilder) this;
  209. return (RootBuilder) parentBuilder.Root;
  210. }
  211. }
  212. internal bool ChildrenAsProperties {
  213. get { return childrenAsProperties; }
  214. }
  215. void AddChild (object child)
  216. {
  217. if (children == null)
  218. children = new ArrayList ();
  219. children.Add (child);
  220. if (child is TemplateBuilder) {
  221. if (templateChildren == null)
  222. templateChildren = new ArrayList ();
  223. templateChildren.Add (child);
  224. }
  225. }
  226. public virtual bool AllowWhitespaceLiterals ()
  227. {
  228. return true;
  229. }
  230. public virtual void AppendLiteralString (string s)
  231. {
  232. if (s == null || s.Length == 0)
  233. return;
  234. if (childrenAsProperties || !isIParserAccessor) {
  235. if (defaultPropertyBuilder != null) {
  236. defaultPropertyBuilder.AppendLiteralString (s);
  237. } else if (s.Trim ().Length != 0) {
  238. throw new HttpException (String.Format ("Literal content not allowed for '{0}' {1} \"{2}\"",
  239. tagName, GetType (), s));
  240. }
  241. return;
  242. }
  243. if (!AllowWhitespaceLiterals () && s.Trim ().Length == 0)
  244. return;
  245. if (HtmlDecodeLiterals ())
  246. s = HttpUtility.HtmlDecode (s);
  247. AddChild (s);
  248. }
  249. public virtual void AppendSubBuilder (ControlBuilder subBuilder)
  250. {
  251. subBuilder.OnAppendToParentBuilder (this);
  252. subBuilder.parentBuilder = this;
  253. if (childrenAsProperties) {
  254. AppendToProperty (subBuilder);
  255. return;
  256. }
  257. if (typeof (CodeRenderBuilder).IsAssignableFrom (subBuilder.GetType ())) {
  258. AppendCode (subBuilder);
  259. return;
  260. }
  261. AddChild (subBuilder);
  262. }
  263. void AppendToProperty (ControlBuilder subBuilder)
  264. {
  265. if (typeof (CodeRenderBuilder) == subBuilder.GetType ())
  266. throw new HttpException ("Code render not supported here.");
  267. if (defaultPropertyBuilder != null) {
  268. defaultPropertyBuilder.AppendSubBuilder (subBuilder);
  269. return;
  270. }
  271. AddChild (subBuilder);
  272. }
  273. void AppendCode (ControlBuilder subBuilder)
  274. {
  275. if (type != null && !(typeof (Control).IsAssignableFrom (type)))
  276. throw new HttpException ("Code render not supported here.");
  277. if (typeof (CodeRenderBuilder) == subBuilder.GetType ())
  278. hasAspCode = true;
  279. AddChild (subBuilder);
  280. }
  281. public virtual void CloseControl ()
  282. {
  283. }
  284. #if NET_2_0
  285. static Type MapTagType (Type tagType)
  286. {
  287. if (tagType == null)
  288. return null;
  289. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  290. if (ps == null)
  291. return tagType;
  292. TagMapCollection tags = ps.TagMapping;
  293. if (tags == null || tags.Count == 0)
  294. return tagType;
  295. string tagTypeName = tagType.ToString ();
  296. Type mappedType, originalType;
  297. string originalTypeName = String.Empty, mappedTypeName = String.Empty;
  298. bool missingType;
  299. Exception error;
  300. foreach (TagMapInfo tmi in tags) {
  301. error = null;
  302. originalType = null;
  303. try {
  304. originalTypeName = tmi.TagType;
  305. originalType = HttpApplication.LoadType (originalTypeName);
  306. if (originalType == null)
  307. missingType = true;
  308. else
  309. missingType = false;
  310. } catch (Exception ex) {
  311. missingType = true;
  312. error = ex;
  313. }
  314. if (missingType)
  315. throw new HttpException (String.Format ("Could not load type {0}", originalTypeName), error);
  316. if (originalTypeName == tagTypeName) {
  317. mappedTypeName = tmi.MappedTagType;
  318. error = null;
  319. mappedType = null;
  320. try {
  321. mappedType = HttpApplication.LoadType (mappedTypeName);
  322. if (mappedType == null)
  323. missingType = true;
  324. else
  325. missingType = false;
  326. } catch (Exception ex) {
  327. missingType = true;
  328. error = ex;
  329. }
  330. if (missingType)
  331. throw new HttpException (String.Format ("Could not load type {0}", mappedTypeName),
  332. error);
  333. if (!mappedType.IsSubclassOf (originalType))
  334. throw new ConfigurationErrorsException (
  335. String.Format ("The specified type '{0}' used for mapping must inherit from the original type '{1}'.", mappedTypeName, originalTypeName));
  336. return mappedType;
  337. }
  338. }
  339. return tagType;
  340. }
  341. #endif
  342. public static ControlBuilder CreateBuilderFromType (TemplateParser parser,
  343. ControlBuilder parentBuilder,
  344. Type type,
  345. string tagName,
  346. string id,
  347. IDictionary attribs,
  348. int line,
  349. string sourceFileName)
  350. {
  351. Type tagType;
  352. #if NET_2_0
  353. tagType = MapTagType (type);
  354. #else
  355. tagType = type;
  356. #endif
  357. ControlBuilder builder;
  358. object [] atts = tagType.GetCustomAttributes (typeof (ControlBuilderAttribute), true);
  359. if (atts != null && atts.Length > 0) {
  360. ControlBuilderAttribute att = (ControlBuilderAttribute) atts [0];
  361. builder = (ControlBuilder) Activator.CreateInstance (att.BuilderType);
  362. } else {
  363. builder = new ControlBuilder ();
  364. }
  365. builder.Init (parser, parentBuilder, tagType, tagName, id, attribs);
  366. builder.line = line;
  367. builder.fileName = sourceFileName;
  368. return builder;
  369. }
  370. public virtual Type GetChildControlType (string tagName, IDictionary attribs)
  371. {
  372. return null;
  373. }
  374. public virtual bool HasBody ()
  375. {
  376. return true;
  377. }
  378. public virtual bool HtmlDecodeLiterals ()
  379. {
  380. return false;
  381. }
  382. ControlBuilder CreatePropertyBuilder (string propName, TemplateParser parser, IDictionary atts)
  383. {
  384. int idx;
  385. string propertyName;
  386. if ((idx = propName.IndexOf (':')) >= 0)
  387. propertyName = propName.Substring (idx + 1);
  388. else
  389. propertyName = propName;
  390. PropertyInfo prop = type.GetProperty (propertyName, flagsNoCase);
  391. if (prop == null) {
  392. string msg = String.Format ("Property {0} not found in type {1}", propertyName, type);
  393. throw new HttpException (msg);
  394. }
  395. Type propType = prop.PropertyType;
  396. ControlBuilder builder = null;
  397. if (typeof (ICollection).IsAssignableFrom (propType)) {
  398. builder = new CollectionBuilder ();
  399. } else if (typeof (ITemplate).IsAssignableFrom (propType)) {
  400. builder = new TemplateBuilder (prop);
  401. } else if (typeof (string) == propType) {
  402. builder = new StringPropertyBuilder (prop.Name);
  403. } else {
  404. builder = CreateBuilderFromType (parser, parentBuilder, propType, prop.Name,
  405. null, atts, line, fileName);
  406. builder.isProperty = true;
  407. if (idx >= 0)
  408. builder.originalTagName = propName;
  409. return builder;
  410. }
  411. builder.Init (parser, this, null, prop.Name, null, atts);
  412. builder.fileName = fileName;
  413. builder.line = line;
  414. builder.isProperty = true;
  415. if (idx >= 0)
  416. builder.originalTagName = propName;
  417. return builder;
  418. }
  419. public virtual void Init (TemplateParser parser,
  420. ControlBuilder parentBuilder,
  421. Type type,
  422. string tagName,
  423. string id,
  424. IDictionary attribs)
  425. {
  426. this.parser = parser;
  427. if (parser != null)
  428. this.location = parser.Location;
  429. this.parentBuilder = parentBuilder;
  430. this.type = type;
  431. this.tagName = tagName;
  432. this.id = id;
  433. this.attribs = attribs;
  434. if (type == null)
  435. return;
  436. if (this is TemplateBuilder)
  437. return;
  438. object [] atts = type.GetCustomAttributes (typeof (ParseChildrenAttribute), true);
  439. if (!typeof (IParserAccessor).IsAssignableFrom (type) && atts.Length == 0) {
  440. isIParserAccessor = false;
  441. childrenAsProperties = true;
  442. } else if (atts.Length > 0) {
  443. ParseChildrenAttribute att = (ParseChildrenAttribute) atts [0];
  444. childrenAsProperties = att.ChildrenAsProperties;
  445. if (childrenAsProperties && att.DefaultProperty.Length != 0)
  446. defaultPropertyBuilder = CreatePropertyBuilder (att.DefaultProperty,
  447. parser, null);
  448. }
  449. }
  450. public virtual bool NeedsTagInnerText ()
  451. {
  452. return false;
  453. }
  454. public virtual void OnAppendToParentBuilder (ControlBuilder parentBuilder)
  455. {
  456. if (defaultPropertyBuilder == null)
  457. return;
  458. ControlBuilder old = defaultPropertyBuilder;
  459. defaultPropertyBuilder = null;
  460. AppendSubBuilder (old);
  461. }
  462. internal void SetTagName (string name)
  463. {
  464. tagName = name;
  465. }
  466. public virtual void SetTagInnerText (string text)
  467. {
  468. }
  469. internal string GetNextID (string proposedID)
  470. {
  471. if (proposedID != null && proposedID.Trim ().Length != 0)
  472. return proposedID;
  473. return "_bctrl_" + nextID++;
  474. }
  475. internal virtual ControlBuilder CreateSubBuilder (string tagid,
  476. Hashtable atts,
  477. Type childType,
  478. TemplateParser parser,
  479. ILocation location)
  480. {
  481. ControlBuilder childBuilder = null;
  482. if (childrenAsProperties) {
  483. if (defaultPropertyBuilder == null)
  484. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  485. else {
  486. if (String.Compare (defaultPropertyBuilder.TagName, tagid, true, CultureInfo.InvariantCulture) == 0) {
  487. // The child tag is the same what our default property name. Act as if there was
  488. // no default property builder, or otherwise we'll end up with invalid nested
  489. // builder call.
  490. defaultPropertyBuilder = null;
  491. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  492. } else
  493. childBuilder = defaultPropertyBuilder.CreateSubBuilder (tagid, atts,
  494. null, parser,
  495. location);
  496. }
  497. return childBuilder;
  498. }
  499. if (String.Compare (tagName, tagid, true, CultureInfo.InvariantCulture) == 0)
  500. return null;
  501. childType = GetChildControlType (tagid, atts);
  502. if (childType == null)
  503. return null;
  504. childBuilder = CreateBuilderFromType (parser, this, childType, tagid, id, atts,
  505. location.BeginLine, location.Filename);
  506. return childBuilder;
  507. }
  508. internal virtual object CreateInstance ()
  509. {
  510. // HtmlGenericControl, HtmlTableCell...
  511. object [] atts = type.GetCustomAttributes (typeof (ConstructorNeedsTagAttribute), true);
  512. object [] args = null;
  513. if (atts != null && atts.Length > 0) {
  514. ConstructorNeedsTagAttribute att = (ConstructorNeedsTagAttribute) atts [0];
  515. if (att.NeedsTag)
  516. args = new object [] {tagName};
  517. }
  518. return Activator.CreateInstance (type, args);
  519. }
  520. internal virtual void CreateChildren (object parent)
  521. {
  522. if (children == null || children.Count == 0)
  523. return;
  524. IParserAccessor parser = parent as IParserAccessor;
  525. if (parser == null)
  526. return;
  527. foreach (object o in children) {
  528. if (o is string) {
  529. parser.AddParsedSubObject (new LiteralControl ((string) o));
  530. } else {
  531. parser.AddParsedSubObject (((ControlBuilder) o).CreateInstance ());
  532. }
  533. }
  534. }
  535. #if NET_2_0
  536. [MonoTODO ("unsure, lack documentation")]
  537. public virtual object BuildObject ()
  538. {
  539. return CreateInstance ();
  540. }
  541. internal void ResetState()
  542. {
  543. renderIndex = 0;
  544. haveParserVariable = false;
  545. if (Children != null) {
  546. foreach (object child in Children) {
  547. ControlBuilder cb = child as ControlBuilder;
  548. if (cb != null)
  549. cb.ResetState ();
  550. }
  551. }
  552. }
  553. #endif
  554. }
  555. }