ControlBuilder.cs 14 KB

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