ControlBuilder.cs 16 KB

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