ControlBuilder.cs 16 KB

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