ControlBuilder.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 LoadType (string typeName)
  264. {
  265. Type type = Type.GetType (typeName);
  266. if (type != null)
  267. return type;
  268. IList tla;
  269. if ((tla = BuildManager.TopLevelAssemblies) != null) {
  270. foreach (Assembly asm in tla) {
  271. if (asm == null)
  272. continue;
  273. type = asm.GetType (typeName);
  274. if (type != null)
  275. return type;
  276. }
  277. }
  278. if (!Directory.Exists (PrivateBinPath))
  279. return null;
  280. string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
  281. foreach (string s in binDlls) {
  282. Assembly binA = Assembly.LoadFrom (s);
  283. type = binA.GetType (typeName);
  284. if (type == null)
  285. continue;
  286. return type;
  287. }
  288. return null;
  289. }
  290. static Type MapTagType (Type tagType)
  291. {
  292. if (tagType == null)
  293. return null;
  294. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  295. if (ps == null)
  296. return tagType;
  297. TagMapCollection tags = ps.TagMapping;
  298. if (tags == null || tags.Count == 0)
  299. return tagType;
  300. string tagTypeName = tagType.ToString ();
  301. Type mappedType, originalType;
  302. string originalTypeName = String.Empty, mappedTypeName = String.Empty;
  303. bool missingType;
  304. Exception error;
  305. foreach (TagMapInfo tmi in tags) {
  306. error = null;
  307. originalType = null;
  308. try {
  309. originalTypeName = tmi.TagType;
  310. originalType = LoadType (originalTypeName);
  311. if (originalType == null)
  312. missingType = true;
  313. else
  314. missingType = false;
  315. } catch (Exception ex) {
  316. missingType = true;
  317. error = ex;
  318. }
  319. if (missingType)
  320. throw new HttpException (String.Format ("Could not load type {0}", originalTypeName), error);
  321. if (originalTypeName == tagTypeName) {
  322. mappedTypeName = tmi.MappedTagType;
  323. error = null;
  324. mappedType = null;
  325. try {
  326. mappedType = LoadType (mappedTypeName);
  327. if (mappedType == null)
  328. missingType = true;
  329. else
  330. missingType = false;
  331. } catch (Exception ex) {
  332. missingType = true;
  333. error = ex;
  334. }
  335. if (missingType)
  336. throw new HttpException (String.Format ("Could not load type {0}", mappedTypeName),
  337. error);
  338. if (!mappedType.IsSubclassOf (originalType))
  339. throw new ConfigurationErrorsException (
  340. String.Format ("The specified type '{0}' used for mapping must inherit from the original type '{1}'.", mappedTypeName, originalTypeName));
  341. return mappedType;
  342. }
  343. }
  344. return tagType;
  345. }
  346. #endif
  347. public static ControlBuilder CreateBuilderFromType (TemplateParser parser,
  348. ControlBuilder parentBuilder,
  349. Type type,
  350. string tagName,
  351. string id,
  352. IDictionary attribs,
  353. int line,
  354. string sourceFileName)
  355. {
  356. Type tagType;
  357. #if NET_2_0
  358. tagType = MapTagType (type);
  359. #else
  360. tagType = type;
  361. #endif
  362. ControlBuilder builder;
  363. object [] atts = tagType.GetCustomAttributes (typeof (ControlBuilderAttribute), true);
  364. if (atts != null && atts.Length > 0) {
  365. ControlBuilderAttribute att = (ControlBuilderAttribute) atts [0];
  366. builder = (ControlBuilder) Activator.CreateInstance (att.BuilderType);
  367. } else {
  368. builder = new ControlBuilder ();
  369. }
  370. builder.Init (parser, parentBuilder, tagType, tagName, id, attribs);
  371. builder.line = line;
  372. builder.fileName = sourceFileName;
  373. return builder;
  374. }
  375. public virtual Type GetChildControlType (string tagName, IDictionary attribs)
  376. {
  377. return null;
  378. }
  379. public virtual bool HasBody ()
  380. {
  381. return true;
  382. }
  383. public virtual bool HtmlDecodeLiterals ()
  384. {
  385. return false;
  386. }
  387. ControlBuilder CreatePropertyBuilder (string propName, TemplateParser parser, IDictionary atts)
  388. {
  389. PropertyInfo prop = type.GetProperty (propName, flagsNoCase);
  390. if (prop == null) {
  391. string msg = String.Format ("Property {0} not found in type {1}", propName, type);
  392. throw new HttpException (msg);
  393. }
  394. Type propType = prop.PropertyType;
  395. ControlBuilder builder = null;
  396. if (typeof (ICollection).IsAssignableFrom (propType)) {
  397. builder = new CollectionBuilder ();
  398. } else if (typeof (ITemplate).IsAssignableFrom (propType)) {
  399. builder = new TemplateBuilder (prop);
  400. } else if (typeof (string) == propType) {
  401. builder = new StringPropertyBuilder (prop.Name);
  402. } else {
  403. builder = CreateBuilderFromType (parser, parentBuilder, propType, prop.Name,
  404. null, atts, line, fileName);
  405. builder.isProperty = true;
  406. return builder;
  407. }
  408. builder.Init (parser, this, null, prop.Name, null, atts);
  409. builder.fileName = fileName;
  410. builder.line = line;
  411. builder.isProperty = true;
  412. return builder;
  413. }
  414. public virtual void Init (TemplateParser parser,
  415. ControlBuilder parentBuilder,
  416. Type type,
  417. string tagName,
  418. string id,
  419. IDictionary attribs)
  420. {
  421. this.parser = parser;
  422. if (parser != null)
  423. this.location = parser.Location;
  424. this.parentBuilder = parentBuilder;
  425. this.type = type;
  426. this.tagName = tagName;
  427. this.id = id;
  428. this.attribs = attribs;
  429. if (type == null)
  430. return;
  431. if (this is TemplateBuilder)
  432. return;
  433. object [] atts = type.GetCustomAttributes (typeof (ParseChildrenAttribute), true);
  434. if (!typeof (IParserAccessor).IsAssignableFrom (type) && atts.Length == 0) {
  435. isIParserAccessor = false;
  436. childrenAsProperties = true;
  437. } else if (atts.Length > 0) {
  438. ParseChildrenAttribute att = (ParseChildrenAttribute) atts [0];
  439. childrenAsProperties = att.ChildrenAsProperties;
  440. if (childrenAsProperties && att.DefaultProperty.Length != 0)
  441. defaultPropertyBuilder = CreatePropertyBuilder (att.DefaultProperty,
  442. parser, null);
  443. }
  444. }
  445. public virtual bool NeedsTagInnerText ()
  446. {
  447. return false;
  448. }
  449. public virtual void OnAppendToParentBuilder (ControlBuilder parentBuilder)
  450. {
  451. if (defaultPropertyBuilder == null)
  452. return;
  453. ControlBuilder old = defaultPropertyBuilder;
  454. defaultPropertyBuilder = null;
  455. AppendSubBuilder (old);
  456. }
  457. internal void SetTagName (string name)
  458. {
  459. tagName = name;
  460. }
  461. public virtual void SetTagInnerText (string text)
  462. {
  463. }
  464. internal string GetNextID (string proposedID)
  465. {
  466. if (proposedID != null && proposedID.Trim ().Length != 0)
  467. return proposedID;
  468. return "_bctrl_" + nextID++;
  469. }
  470. internal virtual ControlBuilder CreateSubBuilder (string tagid,
  471. Hashtable atts,
  472. Type childType,
  473. TemplateParser parser,
  474. ILocation location)
  475. {
  476. ControlBuilder childBuilder = null;
  477. if (childrenAsProperties) {
  478. if (defaultPropertyBuilder == null)
  479. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  480. else {
  481. if (defaultPropertyBuilder.TagName == tagid) {
  482. // The child tag is the same what our default property name. Act as if there was
  483. // no default property builder, or otherwise we'll end up with invalid nested
  484. // builder call.
  485. defaultPropertyBuilder = null;
  486. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  487. } else
  488. childBuilder = defaultPropertyBuilder.CreateSubBuilder (tagid, atts,
  489. null, parser,
  490. location);
  491. }
  492. return childBuilder;
  493. }
  494. if (tagName == tagid)
  495. return null;
  496. childType = GetChildControlType (tagid, atts);
  497. if (childType == null)
  498. return null;
  499. childBuilder = CreateBuilderFromType (parser, this, childType, tagid, id, atts,
  500. location.BeginLine, location.Filename);
  501. return childBuilder;
  502. }
  503. internal virtual object CreateInstance ()
  504. {
  505. // HtmlGenericControl, HtmlTableCell...
  506. object [] atts = type.GetCustomAttributes (typeof (ConstructorNeedsTagAttribute), true);
  507. object [] args = null;
  508. if (atts != null && atts.Length > 0) {
  509. ConstructorNeedsTagAttribute att = (ConstructorNeedsTagAttribute) atts [0];
  510. if (att.NeedsTag)
  511. args = new object [] {tagName};
  512. }
  513. return Activator.CreateInstance (type, args);
  514. }
  515. internal virtual void CreateChildren (object parent)
  516. {
  517. if (children == null || children.Count == 0)
  518. return;
  519. IParserAccessor parser = parent as IParserAccessor;
  520. if (parser == null)
  521. return;
  522. foreach (object o in children) {
  523. if (o is string) {
  524. parser.AddParsedSubObject (new LiteralControl ((string) o));
  525. } else {
  526. parser.AddParsedSubObject (((ControlBuilder) o).CreateInstance ());
  527. }
  528. }
  529. }
  530. #if NET_2_0
  531. [MonoTODO ("unsure, lack documentation")]
  532. public virtual object BuildObject ()
  533. {
  534. return CreateInstance ();
  535. }
  536. internal void ResetState()
  537. {
  538. renderIndex = 0;
  539. haveParserVariable = false;
  540. if (Children != null) {
  541. foreach (object child in Children) {
  542. ControlBuilder cb = child as ControlBuilder;
  543. if (cb != null)
  544. cb.ResetState ();
  545. }
  546. }
  547. }
  548. #endif
  549. }
  550. }