ControlBuilder.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. //
  2. // System.Web.UI.ControlBuilder.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Marek Habersack <[email protected]>
  8. //
  9. // (C) 2002, 2003 Ximian, Inc. (http://www.ximian.com)
  10. // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. using System.Configuration;
  33. using System.CodeDom;
  34. using System.Globalization;
  35. using System.Reflection;
  36. using System.Security.Permissions;
  37. using System.Web.Compilation;
  38. using System.Web.Configuration;
  39. using System.IO;
  40. using System.Web.UI.WebControls;
  41. using System.Web.Util;
  42. using _Location = System.Web.Compilation.Location;
  43. namespace System.Web.UI {
  44. // CAS
  45. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  46. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  47. public class ControlBuilder
  48. {
  49. internal static readonly BindingFlags FlagsNoCase = BindingFlags.Public |
  50. BindingFlags.Instance |
  51. BindingFlags.Static |
  52. BindingFlags.IgnoreCase;
  53. ControlBuilder myNamingContainer;
  54. TemplateParser parser;
  55. Type parserType;
  56. ControlBuilder parentBuilder;
  57. Type type;
  58. string tagName;
  59. string originalTagName;
  60. string id;
  61. IDictionary attribs;
  62. int line;
  63. string fileName;
  64. bool childrenAsProperties;
  65. bool isIParserAccessor = true;
  66. bool hasAspCode;
  67. ControlBuilder defaultPropertyBuilder;
  68. ArrayList children;
  69. ArrayList templateChildren;
  70. static int nextID;
  71. bool haveParserVariable;
  72. CodeMemberMethod method;
  73. CodeStatementCollection methodStatements;
  74. CodeMemberMethod renderMethod;
  75. int renderIndex;
  76. bool isProperty;
  77. bool isPropertyWritable;
  78. ILocation location;
  79. ArrayList otherTags;
  80. int localVariableCount = 0;
  81. bool? isTemplate;
  82. public ControlBuilder ()
  83. {
  84. }
  85. internal ControlBuilder (TemplateParser parser,
  86. ControlBuilder parentBuilder,
  87. Type type,
  88. string tagName,
  89. string id,
  90. IDictionary attribs,
  91. int line,
  92. string sourceFileName)
  93. {
  94. this.parser = parser;
  95. this.parserType = parser != null ? parser.GetType () : null;
  96. this.parentBuilder = parentBuilder;
  97. this.type = type;
  98. this.tagName = tagName;
  99. this.id = id;
  100. this.attribs = attribs;
  101. this.line = line;
  102. this.fileName = sourceFileName;
  103. }
  104. internal void EnsureOtherTags ()
  105. {
  106. if (otherTags == null)
  107. otherTags = new ArrayList ();
  108. }
  109. internal ControlBuilder ParentBuilder {
  110. get { return parentBuilder; }
  111. }
  112. internal IDictionary Attributes {
  113. get { return attribs; }
  114. }
  115. internal int Line {
  116. get { return line; }
  117. set { line = value; }
  118. }
  119. internal string FileName {
  120. get { return fileName; }
  121. set { fileName = value; }
  122. }
  123. internal ControlBuilder DefaultPropertyBuilder {
  124. get { return defaultPropertyBuilder; }
  125. }
  126. internal bool HaveParserVariable {
  127. get { return haveParserVariable; }
  128. set { haveParserVariable = value; }
  129. }
  130. internal CodeMemberMethod Method {
  131. get { return method; }
  132. set { method = value; }
  133. }
  134. internal CodeMemberMethod DataBindingMethod {
  135. get;
  136. set;
  137. }
  138. internal CodeStatementCollection MethodStatements {
  139. get { return methodStatements; }
  140. set { methodStatements = value; }
  141. }
  142. internal CodeMemberMethod RenderMethod {
  143. get { return renderMethod; }
  144. set { renderMethod = value; }
  145. }
  146. internal int RenderIndex {
  147. get { return renderIndex; }
  148. }
  149. internal bool IsProperty {
  150. get { return isProperty; }
  151. }
  152. internal bool IsPropertyWritable {
  153. get { return isPropertyWritable; }
  154. }
  155. internal ILocation Location {
  156. get { return location; }
  157. set { location = new _Location (value); }
  158. }
  159. internal ArrayList OtherTags {
  160. get { return otherTags; }
  161. }
  162. public Type ControlType {
  163. get { return type; }
  164. }
  165. protected bool FChildrenAsProperties {
  166. get { return childrenAsProperties; }
  167. }
  168. protected bool FIsNonParserAccessor {
  169. get { return !isIParserAccessor; }
  170. }
  171. public bool HasAspCode {
  172. get { return hasAspCode; }
  173. }
  174. public string ID {
  175. get { return id; }
  176. set { id = value; }
  177. }
  178. internal ArrayList Children {
  179. get { return children; }
  180. }
  181. internal ArrayList TemplateChildren {
  182. get { return templateChildren; }
  183. }
  184. internal void SetControlType (Type t)
  185. {
  186. type = t;
  187. }
  188. protected bool InDesigner {
  189. get { return false; }
  190. }
  191. public Type NamingContainerType {
  192. get {
  193. ControlBuilder cb = myNamingContainer;
  194. if (cb == null)
  195. return typeof (Control);
  196. return cb.ControlType;
  197. }
  198. }
  199. internal bool IsNamingContainer {
  200. get {
  201. if (type == null)
  202. return false;
  203. return typeof (INamingContainer).IsAssignableFrom (type);
  204. }
  205. }
  206. internal bool IsTemplate {
  207. get {
  208. if (isTemplate == null)
  209. isTemplate = (typeof (TemplateBuilder).IsAssignableFrom (GetType ()));
  210. return isTemplate.Value;
  211. }
  212. }
  213. internal bool PropertyBuilderShouldReturnValue {
  214. get { return isProperty && isPropertyWritable && RenderMethod == null && !IsTemplate && !(this is CollectionBuilder) && !(this is RootBuilder); }
  215. }
  216. ControlBuilder MyNamingContainer {
  217. get {
  218. if (myNamingContainer == null) {
  219. Type controlType = parentBuilder != null ? parentBuilder.ControlType : null;
  220. if (parentBuilder == null && controlType == null)
  221. myNamingContainer = null;
  222. else if (parentBuilder is TemplateBuilder)
  223. myNamingContainer = parentBuilder;
  224. else if (controlType != null && typeof (INamingContainer).IsAssignableFrom (controlType))
  225. myNamingContainer = parentBuilder;
  226. else
  227. myNamingContainer = parentBuilder.MyNamingContainer;
  228. }
  229. return myNamingContainer;
  230. }
  231. }
  232. public virtual Type BindingContainerType {
  233. get {
  234. ControlBuilder cb = (this is TemplateBuilder && !(this is RootBuilder)) ? this : MyNamingContainer;
  235. if (cb == null) {
  236. if (this is RootBuilder && parserType == typeof (PageParser))
  237. return typeof (Page);
  238. return typeof (Control);
  239. }
  240. if (cb != this && cb is ContentBuilderInternal && !typeof (INonBindingContainer).IsAssignableFrom (cb.BindingContainerType))
  241. return cb.BindingContainerType;
  242. Type ct;
  243. if (cb is TemplateBuilder) {
  244. ct = ((TemplateBuilder) cb).ContainerType;
  245. if (typeof (INonBindingContainer).IsAssignableFrom (ct))
  246. return MyNamingContainer.BindingContainerType;
  247. if (ct != null)
  248. return ct;
  249. ct = cb.ControlType;
  250. if (ct == null)
  251. return typeof (Control);
  252. if (typeof (INonBindingContainer).IsAssignableFrom (ct) || !typeof (INamingContainer).IsAssignableFrom (ct))
  253. return MyNamingContainer.BindingContainerType;
  254. return ct;
  255. }
  256. ct = cb.ControlType;
  257. if (ct == null)
  258. return typeof (Control);
  259. if (typeof (INonBindingContainer).IsAssignableFrom (ct) || !typeof (INamingContainer).IsAssignableFrom (ct))
  260. return MyNamingContainer.BindingContainerType;
  261. return cb.ControlType;
  262. }
  263. }
  264. internal TemplateBuilder ParentTemplateBuilder {
  265. get {
  266. if (parentBuilder == null)
  267. return null;
  268. else if (parentBuilder is TemplateBuilder)
  269. return (TemplateBuilder) parentBuilder;
  270. else
  271. return parentBuilder.ParentTemplateBuilder;
  272. }
  273. }
  274. protected TemplateParser Parser {
  275. get { return parser; }
  276. }
  277. public string TagName {
  278. get { return tagName; }
  279. }
  280. internal string OriginalTagName {
  281. get {
  282. if (originalTagName == null || originalTagName.Length == 0)
  283. return TagName;
  284. return originalTagName;
  285. }
  286. }
  287. internal RootBuilder Root {
  288. get {
  289. if (typeof (RootBuilder).IsAssignableFrom (GetType ()))
  290. return (RootBuilder) this;
  291. return (RootBuilder) parentBuilder.Root;
  292. }
  293. }
  294. internal bool ChildrenAsProperties {
  295. get { return childrenAsProperties; }
  296. }
  297. internal string GetAttribute (string name)
  298. {
  299. if (attribs == null)
  300. return null;
  301. return attribs [name] as string;
  302. }
  303. internal void IncreaseRenderIndex ()
  304. {
  305. renderIndex++;
  306. }
  307. void AddChild (object child)
  308. {
  309. if (children == null)
  310. children = new ArrayList ();
  311. children.Add (child);
  312. ControlBuilder cb = child as ControlBuilder;
  313. if (cb != null && cb is TemplateBuilder) {
  314. if (templateChildren == null)
  315. templateChildren = new ArrayList ();
  316. templateChildren.Add (child);
  317. }
  318. if (parser == null)
  319. return;
  320. string tag = cb != null ? cb.TagName : null;
  321. if (String.IsNullOrEmpty (tag))
  322. return;
  323. RootBuilder rb = Root;
  324. AspComponentFoundry foundry = rb != null ? rb.Foundry : null;
  325. if (foundry == null)
  326. return;
  327. AspComponent component = foundry.GetComponent (tag);
  328. if (component == null || !component.FromConfig)
  329. return;
  330. parser.AddImport (component.Namespace);
  331. parser.AddDependency (component.Source);
  332. }
  333. public virtual bool AllowWhitespaceLiterals ()
  334. {
  335. return true;
  336. }
  337. public virtual void AppendLiteralString (string s)
  338. {
  339. if (s == null || s.Length == 0)
  340. return;
  341. if (childrenAsProperties || !isIParserAccessor) {
  342. if (defaultPropertyBuilder != null) {
  343. defaultPropertyBuilder.AppendLiteralString (s);
  344. } else if (s.Trim ().Length != 0) {
  345. throw new HttpException (String.Format ("Literal content not allowed for '{0}' {1} \"{2}\"",
  346. tagName, GetType (), s));
  347. }
  348. return;
  349. }
  350. if (!AllowWhitespaceLiterals () && s.Trim ().Length == 0)
  351. return;
  352. if (HtmlDecodeLiterals ())
  353. s = HttpUtility.HtmlDecode (s);
  354. AddChild (s);
  355. }
  356. public virtual void AppendSubBuilder (ControlBuilder subBuilder)
  357. {
  358. subBuilder.OnAppendToParentBuilder (this);
  359. subBuilder.parentBuilder = this;
  360. if (childrenAsProperties) {
  361. AppendToProperty (subBuilder);
  362. return;
  363. }
  364. if (typeof (CodeRenderBuilder).IsAssignableFrom (subBuilder.GetType ())) {
  365. AppendCode (subBuilder);
  366. return;
  367. }
  368. AddChild (subBuilder);
  369. }
  370. void AppendToProperty (ControlBuilder subBuilder)
  371. {
  372. if (typeof (CodeRenderBuilder) == subBuilder.GetType ())
  373. throw new HttpException ("Code render not supported here.");
  374. if (defaultPropertyBuilder != null) {
  375. defaultPropertyBuilder.AppendSubBuilder (subBuilder);
  376. return;
  377. }
  378. AddChild (subBuilder);
  379. }
  380. void AppendCode (ControlBuilder subBuilder)
  381. {
  382. if (type != null && !(typeof (Control).IsAssignableFrom (type)))
  383. throw new HttpException ("Code render not supported here.");
  384. if (typeof (CodeRenderBuilder) == subBuilder.GetType ())
  385. hasAspCode = true;
  386. AddChild (subBuilder);
  387. }
  388. public virtual void CloseControl ()
  389. {
  390. }
  391. static Type MapTagType (Type tagType)
  392. {
  393. if (tagType == null)
  394. return null;
  395. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  396. if (ps == null)
  397. return tagType;
  398. TagMapCollection tags = ps.TagMapping;
  399. if (tags == null || tags.Count == 0)
  400. return tagType;
  401. string tagTypeName = tagType.ToString ();
  402. Type mappedType, originalType;
  403. string originalTypeName = String.Empty, mappedTypeName = String.Empty;
  404. bool missingType;
  405. Exception error;
  406. foreach (TagMapInfo tmi in tags) {
  407. error = null;
  408. originalType = null;
  409. try {
  410. originalTypeName = tmi.TagType;
  411. originalType = HttpApplication.LoadType (originalTypeName);
  412. if (originalType == null)
  413. missingType = true;
  414. else
  415. missingType = false;
  416. } catch (Exception ex) {
  417. missingType = true;
  418. error = ex;
  419. }
  420. if (missingType)
  421. throw new HttpException (String.Format ("Could not load type {0}", originalTypeName), error);
  422. if (originalTypeName == tagTypeName) {
  423. mappedTypeName = tmi.MappedTagType;
  424. error = null;
  425. mappedType = null;
  426. try {
  427. mappedType = HttpApplication.LoadType (mappedTypeName);
  428. if (mappedType == null)
  429. missingType = true;
  430. else
  431. missingType = false;
  432. } catch (Exception ex) {
  433. missingType = true;
  434. error = ex;
  435. }
  436. if (missingType)
  437. throw new HttpException (String.Format ("Could not load type {0}", mappedTypeName),
  438. error);
  439. if (!mappedType.IsSubclassOf (originalType))
  440. throw new ConfigurationErrorsException (
  441. String.Format ("The specified type '{0}' used for mapping must inherit from the original type '{1}'.", mappedTypeName, originalTypeName));
  442. return mappedType;
  443. }
  444. }
  445. return tagType;
  446. }
  447. public static ControlBuilder CreateBuilderFromType (TemplateParser parser,
  448. ControlBuilder parentBuilder,
  449. Type type,
  450. string tagName,
  451. string id,
  452. IDictionary attribs,
  453. int line,
  454. string sourceFileName)
  455. {
  456. Type tagType = MapTagType (type);
  457. ControlBuilder builder;
  458. object [] atts = tagType.GetCustomAttributes (typeof (ControlBuilderAttribute), true);
  459. if (atts != null && atts.Length > 0) {
  460. ControlBuilderAttribute att = (ControlBuilderAttribute) atts [0];
  461. builder = (ControlBuilder) Activator.CreateInstance (att.BuilderType);
  462. } else {
  463. builder = new ControlBuilder ();
  464. }
  465. builder.Init (parser, parentBuilder, tagType, tagName, id, attribs);
  466. builder.line = line;
  467. builder.fileName = sourceFileName;
  468. return builder;
  469. }
  470. public virtual Type GetChildControlType (string tagName, IDictionary attribs)
  471. {
  472. return null;
  473. }
  474. public virtual bool HasBody ()
  475. {
  476. return true;
  477. }
  478. public virtual bool HtmlDecodeLiterals ()
  479. {
  480. return false;
  481. }
  482. ControlBuilder CreatePropertyBuilder (string propName, TemplateParser parser, IDictionary atts)
  483. {
  484. int idx;
  485. string propertyName;
  486. if ((idx = propName.IndexOf (':')) >= 0)
  487. propertyName = propName.Substring (idx + 1);
  488. else
  489. propertyName = propName;
  490. PropertyInfo prop = type.GetProperty (propertyName, FlagsNoCase);
  491. if (prop == null) {
  492. string msg = String.Format ("Property {0} not found in type {1}", propertyName, type);
  493. throw new HttpException (msg);
  494. }
  495. Type propType = prop.PropertyType;
  496. ControlBuilder builder = null;
  497. if (typeof (ICollection).IsAssignableFrom (propType)) {
  498. builder = new CollectionBuilder ();
  499. } else if (typeof (ITemplate).IsAssignableFrom (propType)) {
  500. builder = new TemplateBuilder (prop);
  501. } else if (typeof (string) == propType) {
  502. builder = new StringPropertyBuilder (prop.Name);
  503. } else {
  504. builder = CreateBuilderFromType (parser, parentBuilder, propType, prop.Name,
  505. null, atts, line, fileName);
  506. builder.isProperty = true;
  507. builder.isPropertyWritable = prop.CanWrite;
  508. if (idx >= 0)
  509. builder.originalTagName = propName;
  510. return builder;
  511. }
  512. builder.Init (parser, this, null, prop.Name, null, atts);
  513. builder.fileName = fileName;
  514. builder.line = line;
  515. builder.isProperty = true;
  516. builder.isPropertyWritable = prop.CanWrite;
  517. if (idx >= 0)
  518. builder.originalTagName = propName;
  519. return builder;
  520. }
  521. public virtual void Init (TemplateParser parser,
  522. ControlBuilder parentBuilder,
  523. Type type,
  524. string tagName,
  525. string id,
  526. IDictionary attribs)
  527. {
  528. this.parser = parser;
  529. if (parser != null)
  530. this.Location = parser.Location;
  531. this.parentBuilder = parentBuilder;
  532. this.type = type;
  533. this.tagName = tagName;
  534. this.id = id;
  535. this.attribs = attribs;
  536. if (type == null)
  537. return;
  538. if (this is TemplateBuilder)
  539. return;
  540. object [] atts = type.GetCustomAttributes (typeof (ParseChildrenAttribute), true);
  541. if (!typeof (IParserAccessor).IsAssignableFrom (type) && atts.Length == 0) {
  542. isIParserAccessor = false;
  543. childrenAsProperties = true;
  544. } else if (atts.Length > 0) {
  545. ParseChildrenAttribute att = (ParseChildrenAttribute) atts [0];
  546. childrenAsProperties = att.ChildrenAsProperties;
  547. if (childrenAsProperties && att.DefaultProperty.Length != 0)
  548. defaultPropertyBuilder = CreatePropertyBuilder (att.DefaultProperty,
  549. parser, null);
  550. }
  551. }
  552. public virtual bool NeedsTagInnerText ()
  553. {
  554. return false;
  555. }
  556. public virtual void OnAppendToParentBuilder (ControlBuilder parentBuilder)
  557. {
  558. if (defaultPropertyBuilder == null)
  559. return;
  560. ControlBuilder old = defaultPropertyBuilder;
  561. defaultPropertyBuilder = null;
  562. AppendSubBuilder (old);
  563. }
  564. internal void SetTagName (string name)
  565. {
  566. tagName = name;
  567. }
  568. public virtual void SetTagInnerText (string text)
  569. {
  570. }
  571. internal string GetNextID (string proposedID)
  572. {
  573. if (proposedID != null && proposedID.Trim ().Length != 0)
  574. return proposedID;
  575. return "_bctrl_" + nextID++;
  576. }
  577. internal string GetNextLocalVariableName (string baseName)
  578. {
  579. localVariableCount++;
  580. return baseName + localVariableCount.ToString ();
  581. }
  582. internal virtual ControlBuilder CreateSubBuilder (string tagid,
  583. IDictionary atts,
  584. Type childType,
  585. TemplateParser parser,
  586. ILocation location)
  587. {
  588. ControlBuilder childBuilder = null;
  589. if (childrenAsProperties) {
  590. if (defaultPropertyBuilder == null)
  591. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  592. else {
  593. if (String.Compare (defaultPropertyBuilder.TagName, tagid, true, Helpers.InvariantCulture) == 0) {
  594. // The child tag is the same what our default property name. Act as if there was
  595. // no default property builder, or otherwise we'll end up with invalid nested
  596. // builder call.
  597. defaultPropertyBuilder = null;
  598. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  599. } else {
  600. Type ct = ControlType;
  601. MemberInfo[] mems = ct != null ? ct.GetMember (tagid, MemberTypes.Property, FlagsNoCase) : null;
  602. PropertyInfo prop = mems != null && mems.Length > 0 ? mems [0] as PropertyInfo : null;
  603. if (prop != null && typeof (ITemplate).IsAssignableFrom (prop.PropertyType)) {
  604. childBuilder = CreatePropertyBuilder (tagid, parser, atts);
  605. defaultPropertyBuilder = null;
  606. } else
  607. childBuilder = defaultPropertyBuilder.CreateSubBuilder (tagid, atts, null, parser, location);
  608. }
  609. }
  610. return childBuilder;
  611. }
  612. if (String.Compare (tagName, tagid, true, Helpers.InvariantCulture) == 0)
  613. return null;
  614. childType = GetChildControlType (tagid, atts);
  615. if (childType == null)
  616. return null;
  617. childBuilder = CreateBuilderFromType (parser, this, childType, tagid, id, atts,
  618. location.BeginLine, location.Filename);
  619. return childBuilder;
  620. }
  621. internal virtual object CreateInstance ()
  622. {
  623. // HtmlGenericControl, HtmlTableCell...
  624. object [] atts = type.GetCustomAttributes (typeof (ConstructorNeedsTagAttribute), true);
  625. object [] args = null;
  626. if (atts != null && atts.Length > 0) {
  627. ConstructorNeedsTagAttribute att = (ConstructorNeedsTagAttribute) atts [0];
  628. if (att.NeedsTag)
  629. args = new object [] {tagName};
  630. }
  631. return Activator.CreateInstance (type, args);
  632. }
  633. internal virtual void CreateChildren (object parent)
  634. {
  635. if (children == null || children.Count == 0)
  636. return;
  637. IParserAccessor parser = parent as IParserAccessor;
  638. if (parser == null)
  639. return;
  640. foreach (object o in children) {
  641. if (o is string) {
  642. parser.AddParsedSubObject (new LiteralControl ((string) o));
  643. } else {
  644. parser.AddParsedSubObject (((ControlBuilder) o).CreateInstance ());
  645. }
  646. }
  647. }
  648. [MonoTODO ("unsure, lack documentation")]
  649. public virtual object BuildObject ()
  650. {
  651. return CreateInstance ();
  652. }
  653. public virtual void ProcessGeneratedCode(CodeCompileUnit codeCompileUnit,
  654. CodeTypeDeclaration baseType,
  655. CodeTypeDeclaration derivedType,
  656. CodeMemberMethod buildMethod,
  657. CodeMemberMethod dataBindingMethod)
  658. {
  659. // nothing to do
  660. }
  661. internal void ResetState()
  662. {
  663. renderIndex = 0;
  664. haveParserVariable = false;
  665. if (Children != null) {
  666. foreach (object child in Children) {
  667. ControlBuilder cb = child as ControlBuilder;
  668. if (cb != null)
  669. cb.ResetState ();
  670. }
  671. }
  672. }
  673. }
  674. }