2
0

ControlBuilder.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 is RootBuilder)) ? this : MyNamingContainer;
  165. if (cb == null) {
  166. if (this is RootBuilder)
  167. return typeof (Page);
  168. return typeof (Control);
  169. }
  170. #if NET_2_0
  171. if (cb != this && cb is ContentBuilderInternal && !typeof (INonBindingContainer).IsAssignableFrom (cb.BindingContainerType))
  172. return cb.BindingContainerType;
  173. #endif
  174. Type ct;
  175. if (cb is TemplateBuilder) {
  176. ct = ((TemplateBuilder) cb).ContainerType;
  177. if (typeof (INonBindingContainer).IsAssignableFrom (ct))
  178. return MyNamingContainer.BindingContainerType;
  179. if (ct != null)
  180. return ct;
  181. ct = cb.ControlType;
  182. if (typeof (INonBindingContainer).IsAssignableFrom (ct) || !typeof (INamingContainer).IsAssignableFrom (ct))
  183. return MyNamingContainer.BindingContainerType;
  184. return ct;
  185. }
  186. ct = cb.ControlType;
  187. if (typeof (INonBindingContainer).IsAssignableFrom (ct) || !typeof (INamingContainer).IsAssignableFrom (ct))
  188. return MyNamingContainer.BindingContainerType;
  189. return cb.ControlType;
  190. }
  191. }
  192. internal TemplateBuilder ParentTemplateBuilder {
  193. get {
  194. if (parentBuilder == null)
  195. return null;
  196. else if (parentBuilder is TemplateBuilder)
  197. return (TemplateBuilder) parentBuilder;
  198. else
  199. return parentBuilder.ParentTemplateBuilder;
  200. }
  201. }
  202. protected TemplateParser Parser {
  203. get { return parser; }
  204. }
  205. public string TagName {
  206. get { return tagName; }
  207. }
  208. internal string OriginalTagName {
  209. get {
  210. if (originalTagName == null || originalTagName.Length == 0)
  211. return TagName;
  212. return originalTagName;
  213. }
  214. }
  215. internal RootBuilder Root {
  216. get {
  217. if (GetType () == typeof (RootBuilder))
  218. return (RootBuilder) this;
  219. return (RootBuilder) parentBuilder.Root;
  220. }
  221. }
  222. internal bool ChildrenAsProperties {
  223. get { return childrenAsProperties; }
  224. }
  225. void AddChild (object child)
  226. {
  227. if (children == null)
  228. children = new ArrayList ();
  229. children.Add (child);
  230. if (child is TemplateBuilder) {
  231. if (templateChildren == null)
  232. templateChildren = new ArrayList ();
  233. templateChildren.Add (child);
  234. }
  235. }
  236. public virtual bool AllowWhitespaceLiterals ()
  237. {
  238. return true;
  239. }
  240. public virtual void AppendLiteralString (string s)
  241. {
  242. if (s == null || s.Length == 0)
  243. return;
  244. if (childrenAsProperties || !isIParserAccessor) {
  245. if (defaultPropertyBuilder != null) {
  246. defaultPropertyBuilder.AppendLiteralString (s);
  247. } else if (s.Trim ().Length != 0) {
  248. throw new HttpException (String.Format ("Literal content not allowed for '{0}' {1} \"{2}\"",
  249. tagName, GetType (), s));
  250. }
  251. return;
  252. }
  253. if (!AllowWhitespaceLiterals () && s.Trim ().Length == 0)
  254. return;
  255. if (HtmlDecodeLiterals ())
  256. s = HttpUtility.HtmlDecode (s);
  257. AddChild (s);
  258. }
  259. public virtual void AppendSubBuilder (ControlBuilder subBuilder)
  260. {
  261. subBuilder.OnAppendToParentBuilder (this);
  262. subBuilder.parentBuilder = this;
  263. if (childrenAsProperties) {
  264. AppendToProperty (subBuilder);
  265. return;
  266. }
  267. if (typeof (CodeRenderBuilder).IsAssignableFrom (subBuilder.GetType ())) {
  268. AppendCode (subBuilder);
  269. return;
  270. }
  271. AddChild (subBuilder);
  272. }
  273. void AppendToProperty (ControlBuilder subBuilder)
  274. {
  275. if (typeof (CodeRenderBuilder) == subBuilder.GetType ())
  276. throw new HttpException ("Code render not supported here.");
  277. if (defaultPropertyBuilder != null) {
  278. defaultPropertyBuilder.AppendSubBuilder (subBuilder);
  279. return;
  280. }
  281. AddChild (subBuilder);
  282. }
  283. void AppendCode (ControlBuilder subBuilder)
  284. {
  285. if (type != null && !(typeof (Control).IsAssignableFrom (type)))
  286. throw new HttpException ("Code render not supported here.");
  287. if (typeof (CodeRenderBuilder) == subBuilder.GetType ())
  288. hasAspCode = true;
  289. AddChild (subBuilder);
  290. }
  291. public virtual void CloseControl ()
  292. {
  293. }
  294. #if NET_2_0
  295. static Type MapTagType (Type tagType)
  296. {
  297. if (tagType == null)
  298. return null;
  299. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  300. if (ps == null)
  301. return tagType;
  302. TagMapCollection tags = ps.TagMapping;
  303. if (tags == null || tags.Count == 0)
  304. return tagType;
  305. string tagTypeName = tagType.ToString ();
  306. Type mappedType, originalType;
  307. string originalTypeName = String.Empty, mappedTypeName = String.Empty;
  308. bool missingType;
  309. Exception error;
  310. foreach (TagMapInfo tmi in tags) {
  311. error = null;
  312. originalType = null;
  313. try {
  314. originalTypeName = tmi.TagType;
  315. originalType = HttpApplication.LoadType (originalTypeName);
  316. if (originalType == null)
  317. missingType = true;
  318. else
  319. missingType = false;
  320. } catch (Exception ex) {
  321. missingType = true;
  322. error = ex;
  323. }
  324. if (missingType)
  325. throw new HttpException (String.Format ("Could not load type {0}", originalTypeName), error);
  326. if (originalTypeName == tagTypeName) {
  327. mappedTypeName = tmi.MappedTagType;
  328. error = null;
  329. mappedType = null;
  330. try {
  331. mappedType = HttpApplication.LoadType (mappedTypeName);
  332. if (mappedType == null)
  333. missingType = true;
  334. else
  335. missingType = false;
  336. } catch (Exception ex) {
  337. missingType = true;
  338. error = ex;
  339. }
  340. if (missingType)
  341. throw new HttpException (String.Format ("Could not load type {0}", mappedTypeName),
  342. error);
  343. if (!mappedType.IsSubclassOf (originalType))
  344. throw new ConfigurationErrorsException (
  345. String.Format ("The specified type '{0}' used for mapping must inherit from the original type '{1}'.", mappedTypeName, originalTypeName));
  346. return mappedType;
  347. }
  348. }
  349. return tagType;
  350. }
  351. #endif
  352. public static ControlBuilder CreateBuilderFromType (TemplateParser parser,
  353. ControlBuilder parentBuilder,
  354. Type type,
  355. string tagName,
  356. string id,
  357. IDictionary attribs,
  358. int line,
  359. string sourceFileName)
  360. {
  361. Type tagType;
  362. #if NET_2_0
  363. tagType = MapTagType (type);
  364. #else
  365. tagType = type;
  366. #endif
  367. ControlBuilder builder;
  368. object [] atts = tagType.GetCustomAttributes (typeof (ControlBuilderAttribute), true);
  369. if (atts != null && atts.Length > 0) {
  370. ControlBuilderAttribute att = (ControlBuilderAttribute) atts [0];
  371. builder = (ControlBuilder) Activator.CreateInstance (att.BuilderType);
  372. } else {
  373. builder = new ControlBuilder ();
  374. }
  375. builder.Init (parser, parentBuilder, tagType, tagName, id, attribs);
  376. builder.line = line;
  377. builder.fileName = sourceFileName;
  378. return builder;
  379. }
  380. public virtual Type GetChildControlType (string tagName, IDictionary attribs)
  381. {
  382. return null;
  383. }
  384. public virtual bool HasBody ()
  385. {
  386. return true;
  387. }
  388. public virtual bool HtmlDecodeLiterals ()
  389. {
  390. return false;
  391. }
  392. ControlBuilder CreatePropertyBuilder (string propName, TemplateParser parser, IDictionary atts)
  393. {
  394. int idx;
  395. string propertyName;
  396. if ((idx = propName.IndexOf (':')) >= 0)
  397. propertyName = propName.Substring (idx + 1);
  398. else
  399. propertyName = propName;
  400. PropertyInfo prop = type.GetProperty (propertyName, flagsNoCase);
  401. if (prop == null) {
  402. string msg = String.Format ("Property {0} not found in type {1}", propertyName, type);
  403. throw new HttpException (msg);
  404. }
  405. Type propType = prop.PropertyType;
  406. ControlBuilder builder = null;
  407. if (typeof (ICollection).IsAssignableFrom (propType)) {
  408. builder = new CollectionBuilder ();
  409. } else if (typeof (ITemplate).IsAssignableFrom (propType)) {
  410. builder = new TemplateBuilder (prop);
  411. } else if (typeof (string) == propType) {
  412. builder = new StringPropertyBuilder (prop.Name);
  413. } else {
  414. builder = CreateBuilderFromType (parser, parentBuilder, propType, prop.Name,
  415. null, atts, line, fileName);
  416. builder.isProperty = true;
  417. if (idx >= 0)
  418. builder.originalTagName = propName;
  419. return builder;
  420. }
  421. builder.Init (parser, this, null, prop.Name, null, atts);
  422. builder.fileName = fileName;
  423. builder.line = line;
  424. builder.isProperty = true;
  425. if (idx >= 0)
  426. builder.originalTagName = propName;
  427. return builder;
  428. }
  429. public virtual void Init (TemplateParser parser,
  430. ControlBuilder parentBuilder,
  431. Type type,
  432. string tagName,
  433. string id,
  434. IDictionary attribs)
  435. {
  436. this.parser = parser;
  437. if (parser != null)
  438. this.location = parser.Location;
  439. this.parentBuilder = parentBuilder;
  440. this.type = type;
  441. this.tagName = tagName;
  442. this.id = id;
  443. this.attribs = attribs;
  444. if (type == null)
  445. return;
  446. if (this is TemplateBuilder)
  447. return;
  448. object [] atts = type.GetCustomAttributes (typeof (ParseChildrenAttribute), true);
  449. if (!typeof (IParserAccessor).IsAssignableFrom (type) && atts.Length == 0) {
  450. isIParserAccessor = false;
  451. childrenAsProperties = true;
  452. } else if (atts.Length > 0) {
  453. ParseChildrenAttribute att = (ParseChildrenAttribute) atts [0];
  454. childrenAsProperties = att.ChildrenAsProperties;
  455. if (childrenAsProperties && att.DefaultProperty.Length != 0)
  456. defaultPropertyBuilder = CreatePropertyBuilder (att.DefaultProperty,
  457. parser, null);
  458. }
  459. }
  460. public virtual bool NeedsTagInnerText ()
  461. {
  462. return false;
  463. }
  464. public virtual void OnAppendToParentBuilder (ControlBuilder parentBuilder)
  465. {
  466. if (defaultPropertyBuilder == null)
  467. return;
  468. ControlBuilder old = defaultPropertyBuilder;
  469. defaultPropertyBuilder = null;
  470. AppendSubBuilder (old);
  471. }
  472. internal void SetTagName (string name)
  473. {
  474. tagName = name;
  475. }
  476. public virtual void SetTagInnerText (string text)
  477. {
  478. }
  479. internal string GetNextID (string proposedID)
  480. {
  481. if (proposedID != null && proposedID.Trim ().Length != 0)
  482. return proposedID;
  483. return "_bctrl_" + nextID++;
  484. }
  485. internal virtual ControlBuilder CreateSubBuilder (string tagid,
  486. Hashtable atts,
  487. Type childType,
  488. TemplateParser parser,
  489. ILocation location)
  490. {
  491. ControlBuilder childBuilder = null;
  492. if (childrenAsProperties) {
  493. if (defaultPropertyBuilder == null)
  494. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  495. else {
  496. if (String.Compare (defaultPropertyBuilder.TagName, tagid, true, CultureInfo.InvariantCulture) == 0) {
  497. // The child tag is the same what our default property name. Act as if there was
  498. // no default property builder, or otherwise we'll end up with invalid nested
  499. // builder call.
  500. defaultPropertyBuilder = null;
  501. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  502. } else
  503. childBuilder = defaultPropertyBuilder.CreateSubBuilder (tagid, atts,
  504. null, parser,
  505. location);
  506. }
  507. return childBuilder;
  508. }
  509. if (String.Compare (tagName, tagid, true, CultureInfo.InvariantCulture) == 0)
  510. return null;
  511. childType = GetChildControlType (tagid, atts);
  512. if (childType == null)
  513. return null;
  514. childBuilder = CreateBuilderFromType (parser, this, childType, tagid, id, atts,
  515. location.BeginLine, location.Filename);
  516. return childBuilder;
  517. }
  518. internal virtual object CreateInstance ()
  519. {
  520. // HtmlGenericControl, HtmlTableCell...
  521. object [] atts = type.GetCustomAttributes (typeof (ConstructorNeedsTagAttribute), true);
  522. object [] args = null;
  523. if (atts != null && atts.Length > 0) {
  524. ConstructorNeedsTagAttribute att = (ConstructorNeedsTagAttribute) atts [0];
  525. if (att.NeedsTag)
  526. args = new object [] {tagName};
  527. }
  528. return Activator.CreateInstance (type, args);
  529. }
  530. internal virtual void CreateChildren (object parent)
  531. {
  532. if (children == null || children.Count == 0)
  533. return;
  534. IParserAccessor parser = parent as IParserAccessor;
  535. if (parser == null)
  536. return;
  537. foreach (object o in children) {
  538. if (o is string) {
  539. parser.AddParsedSubObject (new LiteralControl ((string) o));
  540. } else {
  541. parser.AddParsedSubObject (((ControlBuilder) o).CreateInstance ());
  542. }
  543. }
  544. }
  545. #if NET_2_0
  546. [MonoTODO ("unsure, lack documentation")]
  547. public virtual object BuildObject ()
  548. {
  549. return CreateInstance ();
  550. }
  551. internal void ResetState()
  552. {
  553. renderIndex = 0;
  554. haveParserVariable = false;
  555. if (Children != null) {
  556. foreach (object child in Children) {
  557. ControlBuilder cb = child as ControlBuilder;
  558. if (cb != null)
  559. cb.ResetState ();
  560. }
  561. }
  562. }
  563. #endif
  564. }
  565. }