ControlBuilder.cs 13 KB

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