ControlBuilder.cs 11 KB

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