ControlBuilder.cs 11 KB

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