ControlBuilder.cs 15 KB

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