AspGenerator.cs 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. //
  2. // System.Web.Compilation.AspGenerator
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Drawing;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Reflection;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using System.Web.UI;
  19. using System.Web.UI.HtmlControls;
  20. using System.Web.UI.WebControls;
  21. using System.Web.Util;
  22. namespace System.Web.Compilation
  23. {
  24. class ControlStack
  25. {
  26. private Stack controls;
  27. private ControlStackData top;
  28. private bool space_between_tags;
  29. private bool sbt_valid;
  30. class ControlStackData
  31. {
  32. public Type controlType;
  33. public string controlID;
  34. public string tagID;
  35. public ChildrenKind childKind;
  36. public string defaultPropertyName;
  37. public int childrenNumber;
  38. public Type container;
  39. public StringBuilder dataBindFunction;
  40. public StringBuilder codeRenderFunction;
  41. public bool useCodeRender;
  42. public ControlStackData (Type controlType,
  43. string controlID,
  44. string tagID,
  45. ChildrenKind childKind,
  46. string defaultPropertyName,
  47. Type container)
  48. {
  49. this.controlType = controlType;
  50. this.controlID = controlID;
  51. this.tagID = tagID;
  52. this.childKind = childKind;
  53. this.defaultPropertyName = defaultPropertyName;
  54. this.container = container;
  55. childrenNumber = 0;
  56. }
  57. public override string ToString ()
  58. {
  59. return controlType + " " + controlID + " " + tagID + " " + childKind + " " + childrenNumber;
  60. }
  61. }
  62. public ControlStack ()
  63. {
  64. controls = new Stack ();
  65. }
  66. private Type GetContainerType (Type type)
  67. {
  68. if (type != typeof (System.Web.UI.Control) &&
  69. !type.IsSubclassOf (typeof (System.Web.UI.Control)))
  70. return null;
  71. Type container_type;
  72. if (type == typeof (System.Web.UI.WebControls.DataList))
  73. container_type = typeof (System.Web.UI.WebControls.DataListItem);
  74. else if (type == typeof (System.Web.UI.WebControls.DataGrid))
  75. container_type = typeof (System.Web.UI.WebControls.DataGridItem);
  76. else if (type == typeof (System.Web.UI.WebControls.Repeater))
  77. container_type = typeof (System.Web.UI.WebControls.RepeaterItem);
  78. else if (type == typeof (ListControl) || type.IsSubclassOf (typeof (ListControl)))
  79. container_type = type;
  80. else
  81. container_type = Container;
  82. return container_type;
  83. }
  84. public void Push (object o)
  85. {
  86. if (!(o is ControlStackData))
  87. return;
  88. controls.Push (o);
  89. top = (ControlStackData) o;
  90. sbt_valid = false;
  91. }
  92. public void Push (Type controlType,
  93. string controlID,
  94. string tagID,
  95. ChildrenKind childKind,
  96. string defaultPropertyName)
  97. {
  98. Type container_type = null;
  99. if (controlType != null){
  100. AddChild ();
  101. container_type = GetContainerType (controlType);
  102. if (container_type == null)
  103. container_type = this.Container;
  104. }
  105. top = new ControlStackData (controlType,
  106. controlID,
  107. tagID,
  108. childKind,
  109. defaultPropertyName,
  110. container_type);
  111. sbt_valid = false;
  112. controls.Push (top);
  113. }
  114. public object Pop ()
  115. {
  116. object item = controls.Pop ();
  117. if (controls.Count != 0)
  118. top = (ControlStackData) controls.Peek ();
  119. sbt_valid = false;
  120. return item;
  121. }
  122. public Type PeekType ()
  123. {
  124. return top.controlType;
  125. }
  126. public string PeekControlID ()
  127. {
  128. return top.controlID;
  129. }
  130. public string PeekTagID ()
  131. {
  132. return top.tagID;
  133. }
  134. public ChildrenKind PeekChildKind ()
  135. {
  136. return top.childKind;
  137. }
  138. public string PeekDefaultPropertyName ()
  139. {
  140. return top.defaultPropertyName;
  141. }
  142. public void AddChild ()
  143. {
  144. if (top != null)
  145. top.childrenNumber++;
  146. }
  147. public bool HasDataBindFunction ()
  148. {
  149. if (top.dataBindFunction == null || top.dataBindFunction.Length == 0)
  150. return false;
  151. return true;
  152. }
  153. public bool UseCodeRender
  154. {
  155. get {
  156. if (top.codeRenderFunction == null || top.codeRenderFunction.Length == 0)
  157. return false;
  158. return top.useCodeRender;
  159. }
  160. set { top.useCodeRender= value; }
  161. }
  162. public bool SpaceBetweenTags
  163. {
  164. get {
  165. if (!sbt_valid){
  166. sbt_valid = true;
  167. Type type = top.controlType;
  168. if (type.Namespace == "System.Web.UI.WebControls")
  169. space_between_tags = true;
  170. else if (type.IsSubclassOf (typeof (System.Web.UI.WebControls.WebControl)))
  171. space_between_tags = true;
  172. else if (type == typeof (System.Web.UI.HtmlControls.HtmlSelect))
  173. space_between_tags = true;
  174. else if (type == typeof (System.Web.UI.HtmlControls.HtmlTable))
  175. space_between_tags = true;
  176. else if (type == typeof (System.Web.UI.HtmlControls.HtmlTableRow))
  177. space_between_tags = true;
  178. else if (type == typeof (System.Web.UI.HtmlControls.HtmlTableCell))
  179. space_between_tags = true;
  180. else
  181. space_between_tags = false;
  182. }
  183. return space_between_tags;
  184. }
  185. }
  186. public Type Container {
  187. get {
  188. if (top == null)
  189. return null;
  190. return top.container;
  191. }
  192. }
  193. public StringBuilder DataBindFunction
  194. {
  195. get {
  196. if (top.dataBindFunction == null)
  197. top.dataBindFunction = new StringBuilder ();
  198. return top.dataBindFunction;
  199. }
  200. }
  201. public StringBuilder CodeRenderFunction
  202. {
  203. get {
  204. if (top.codeRenderFunction == null)
  205. top.codeRenderFunction = new StringBuilder ();
  206. return top.codeRenderFunction;
  207. }
  208. }
  209. public int ChildIndex
  210. {
  211. get { return top.childrenNumber - 1; }
  212. }
  213. public int Count
  214. {
  215. get { return controls.Count; }
  216. }
  217. public override string ToString ()
  218. {
  219. return top.ToString () + " " + top.useCodeRender;
  220. }
  221. }
  222. class ArrayListWrapper
  223. {
  224. private ArrayList list;
  225. private int index;
  226. public ArrayListWrapper (ArrayList list)
  227. {
  228. this.list = list;
  229. index = -1;
  230. }
  231. private void CheckIndex ()
  232. {
  233. if (index == -1 || index == list.Count)
  234. throw new InvalidOperationException ();
  235. }
  236. public object Current
  237. {
  238. get {
  239. CheckIndex ();
  240. return list [index];
  241. }
  242. set {
  243. CheckIndex ();
  244. list [index] = value;
  245. }
  246. }
  247. public bool MoveNext ()
  248. {
  249. if (index < list.Count)
  250. index++;
  251. return index < list.Count;
  252. }
  253. }
  254. class AspGenerator
  255. {
  256. private object [] parts;
  257. private ArrayListWrapper elements;
  258. private StringBuilder prolog;
  259. private StringBuilder declarations;
  260. private StringBuilder script;
  261. private StringBuilder constructor;
  262. private StringBuilder init_funcs;
  263. private StringBuilder epilog;
  264. private StringBuilder current_function;
  265. private Stack functions;
  266. private ControlStack controls;
  267. private bool parse_ok;
  268. private bool has_form_tag;
  269. private AspComponentFoundry aspFoundry;
  270. private string classDecl;
  271. private string className;
  272. private string interfaces;
  273. private string basetype;
  274. private string parent;
  275. private string fullPath;
  276. private static string enableSessionStateLiteral = ", System.Web.SessionState.IRequiresSessionState";
  277. Hashtable options;
  278. string privateBinPath;
  279. string main_directive;
  280. static string app_file_wrong = "The content in the application file is not valid.";
  281. bool isPage;
  282. bool isUserControl;
  283. bool isApplication;
  284. enum UserControlResult
  285. {
  286. OK = 0,
  287. FileNotFound = 1,
  288. CompilationFailed = 2
  289. }
  290. public AspGenerator (string pathToFile, ArrayList elements)
  291. {
  292. if (elements == null)
  293. throw new ArgumentNullException ();
  294. this.elements = new ArrayListWrapper (elements);
  295. string filename = Path.GetFileName (pathToFile);
  296. this.className = filename.Replace ('.', '_'); // Overridden by @ Page classname
  297. this.className = className.Replace ('-', '_');
  298. this.className = className.Replace (' ', '_');
  299. Options ["ClassName"] = this.className;
  300. this.fullPath = Path.GetFullPath (pathToFile);
  301. this.has_form_tag = false;
  302. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  303. privateBinPath = setup.PrivateBinPath;
  304. // This is a hack until we can run stuff in different domains
  305. if (privateBinPath == null || privateBinPath.Length == 0)
  306. privateBinPath = "bin";
  307. if (!Path.IsPathRooted (privateBinPath))
  308. privateBinPath = Path.Combine (setup.ApplicationBase, privateBinPath);
  309. Init ();
  310. }
  311. public string BaseType {
  312. get { return basetype; }
  313. set {
  314. if (parent == null)
  315. parent = value;
  316. basetype = value;
  317. isUserControl = (basetype == "System.Web.UI.UserControl");
  318. isPage = (basetype == "System.Web.UI.Page");
  319. isApplication = (basetype == "System.Web.HttpApplication");
  320. }
  321. }
  322. public bool IsUserControl {
  323. get { return isUserControl; }
  324. }
  325. public bool IsPage {
  326. get { return isPage; }
  327. }
  328. public bool IsApplication {
  329. get { return isApplication; }
  330. }
  331. public string Interfaces {
  332. get { return interfaces; }
  333. }
  334. public Hashtable Options {
  335. get {
  336. if (options == null)
  337. options = new Hashtable ();
  338. return options;
  339. }
  340. }
  341. public void AddInterface (string iface)
  342. {
  343. if (interfaces == "") {
  344. interfaces = iface;
  345. } else {
  346. string s = ", " + iface;
  347. if (interfaces.IndexOf (s) == -1)
  348. interfaces += s;
  349. }
  350. }
  351. private AspComponentFoundry Foundry
  352. {
  353. get {
  354. if (aspFoundry == null)
  355. aspFoundry = new AspComponentFoundry ();
  356. return aspFoundry;
  357. }
  358. }
  359. private void Init ()
  360. {
  361. controls = new ControlStack ();
  362. controls.Push (typeof (System.Web.UI.Control), "Root", null, ChildrenKind.CONTROLS, null);
  363. prolog = new StringBuilder ();
  364. declarations = new StringBuilder ();
  365. script = new StringBuilder ();
  366. constructor = new StringBuilder ();
  367. init_funcs = new StringBuilder ();
  368. epilog = new StringBuilder ();
  369. current_function = new StringBuilder ();
  370. functions = new Stack ();
  371. functions.Push (current_function);
  372. parts = new Object [6];
  373. parts [0] = prolog;
  374. parts [1] = declarations;
  375. parts [2] = script;
  376. parts [3] = constructor;
  377. parts [4] = init_funcs;
  378. parts [5] = epilog;
  379. prolog.Append ("namespace ASP {\n" +
  380. "\tusing System;\n" +
  381. "\tusing System.Collections;\n" +
  382. "\tusing System.Collections.Specialized;\n" +
  383. "\tusing System.Configuration;\n" +
  384. "\tusing System.IO;\n" +
  385. "\tusing System.Text;\n" +
  386. "\tusing System.Text.RegularExpressions;\n" +
  387. "\tusing System.Web;\n" +
  388. "\tusing System.Web.Caching;\n" +
  389. "\tusing System.Web.Security;\n" +
  390. "\tusing System.Web.SessionState;\n" +
  391. "\tusing System.Web.UI;\n" +
  392. "\tusing System.Web.UI.WebControls;\n" +
  393. "\tusing System.Web.UI.HtmlControls;\n");
  394. declarations.Append ("\t\tprivate static int __autoHandlers;\n");
  395. current_function.Append ("\t\tprivate void __BuildControlTree (System.Web.UI.Control __ctrl)\n\t\t{\n");
  396. if (!IsUserControl)
  397. current_function.Append ("\t\t\tSystem.Web.UI.IParserAccessor __parser = " +
  398. "(System.Web.UI.IParserAccessor) __ctrl;\n\n");
  399. else
  400. controls.UseCodeRender = true;
  401. }
  402. public StringReader GetCode ()
  403. {
  404. if (!parse_ok)
  405. throw new ApplicationException ("You gotta call ProcessElements () first!");
  406. StringBuilder code = new StringBuilder ();
  407. for (int i = 0; i < parts.Length; i++)
  408. code.Append ((StringBuilder) parts [i]);
  409. return new StringReader (code.ToString ());
  410. }
  411. public void Print ()
  412. {
  413. if (!parse_ok){
  414. Console.WriteLine ("//Warning!!!: Elements not correctly parsed.");
  415. }
  416. Console.Write (GetCode ().ReadToEnd ());
  417. }
  418. // Regex.Escape () make some illegal escape sequences for a C# source.
  419. private string Escape (string input)
  420. {
  421. if (input == null)
  422. return String.Empty;
  423. string output = input.Replace ("\\", "\\\\");
  424. output = output.Replace ("\"", "\\\"");
  425. output = output.Replace ("\t", "\\t");
  426. output = output.Replace ("\r", "\\r");
  427. output = output.Replace ("\n", "\\n");
  428. output = output.Replace ("\n", "\\n");
  429. return output;
  430. }
  431. private void PageDirective (TagAttributes att)
  432. {
  433. if (att ["ClassName"] != null){
  434. this.className = (string) att ["ClassName"];
  435. Options ["ClassName"] = className;
  436. }
  437. if (att ["EnableSessionState"] != null){
  438. string est = (string) att ["EnableSessionState"];
  439. if (0 == String.Compare (est, "false", true))
  440. interfaces = interfaces.Replace (enableSessionStateLiteral, "");
  441. else if (0 != String.Compare (est, "true", true))
  442. throw new ApplicationException ("EnableSessionState in Page directive not set to " +
  443. "a correct value: " + est);
  444. }
  445. if (att ["Inherits"] != null)
  446. parent = (string) att ["Inherits"];
  447. if (att ["CompilerOptions"] != null)
  448. Options ["CompilerOptions"] = (string) att ["CompilerOptions"];
  449. if (att ["AutoEventWireup"] != null) {
  450. if (options ["AutoEventWireup"] != null)
  451. throw new ApplicationException ("Already have an AutoEventWireup attribute");
  452. bool autoevent = true;
  453. string v = att ["AutoEventWireup"] as string;
  454. try {
  455. autoevent = Convert.ToBoolean (v);
  456. } catch (Exception) {
  457. throw new ApplicationException ("'" + v + "' is not a valid value for AutoEventWireup");
  458. }
  459. options ["AutoEventWireup"] = autoevent;
  460. }
  461. //FIXME: add support for more attributes.
  462. }
  463. void AddReference (string dll)
  464. {
  465. string references = Options ["References"] as string;
  466. if (references == null)
  467. references = dll;
  468. else
  469. references = references + " " + dll;
  470. Options ["References"] = references;
  471. }
  472. private void RegisterDirective (TagAttributes att)
  473. {
  474. string tag_prefix = (string) (att ["tagprefix"] == null ? "" : att ["tagprefix"]);
  475. string name_space = (string) (att ["namespace"] == null ? "" : att ["namespace"]);
  476. string assembly_name = (string) (att ["assembly"] == null ? "" : att ["assembly"]);
  477. string tag_name = (string) (att ["tagname"] == null ? "" : att ["tagname"]);
  478. string src = (string) (att ["src"] == null ? "" : att ["src"]);
  479. if (tag_prefix != "" && name_space != "" && assembly_name != ""){
  480. if (tag_name != "" || src != "")
  481. throw new ApplicationException ("Invalid attributes for @ Register: " +
  482. att.ToString ());
  483. prolog.AppendFormat ("\tusing {0};\n", name_space);
  484. string dll = privateBinPath + Path.DirectorySeparatorChar + assembly_name + ".dll";
  485. Foundry.RegisterFoundry (tag_prefix, dll, name_space);
  486. AddReference (dll);
  487. return;
  488. }
  489. if (tag_prefix != "" && tag_name != "" && src != ""){
  490. if (name_space != "" && assembly_name != "")
  491. throw new ApplicationException ("Invalid attributes for @ Register: " +
  492. att.ToString ());
  493. if (!src.EndsWith (".ascx"))
  494. throw new ApplicationException ("Source file extension for controls " +
  495. "must be .ascx");
  496. string srcLocation = PathUtil.Combine (null, src);
  497. UserControlData data = GenerateUserControl (srcLocation);
  498. switch (data.result) {
  499. case UserControlResult.OK:
  500. prolog.AppendFormat ("\tusing {0};\n", "ASP");
  501. string dll = "output" + Path.DirectorySeparatorChar + data.assemblyName + ".dll";
  502. Foundry.RegisterFoundry (tag_prefix, data.assemblyName, "ASP", data.className);
  503. AddReference (data.assemblyName);
  504. break;
  505. case UserControlResult.FileNotFound:
  506. throw new ApplicationException ("File '" + src + "' not found.");
  507. case UserControlResult.CompilationFailed:
  508. //TODO: should say where the generated .cs file is for the server to
  509. //show the source and the compiler error
  510. throw new NotImplementedException ();
  511. }
  512. return;
  513. }
  514. throw new ApplicationException ("Invalid combination of attributes in " +
  515. "@ Register: " + att.ToString ());
  516. }
  517. private void ProcessDirective ()
  518. {
  519. Directive directive = (Directive) elements.Current;
  520. TagAttributes att = directive.Attributes;
  521. if (att == null)
  522. return;
  523. string value;
  524. string id = directive.TagID.ToUpper ();
  525. switch (id){
  526. case "APPLICATION":
  527. if (main_directive != null)
  528. throw new ApplicationException (id + " not allowed after " + main_directive);
  529. if (!IsApplication)
  530. throw new ApplicationException ("@Application not allowed.");
  531. string inherits = att ["inherits"] as string;
  532. if (inherits != null)
  533. Options ["Inherits"] = inherits;
  534. main_directive = directive.TagID;
  535. break;
  536. case "PAGE":
  537. case "CONTROL":
  538. if (main_directive != null)
  539. throw new ApplicationException (id + " not allowed after " + main_directive);
  540. if (IsUserControl && id != "CONTROL")
  541. throw new ApplicationException ("@Page not allowed for user controls.");
  542. else if (IsPage && id != "PAGE")
  543. throw new ApplicationException ("@Control not allowed here. This is a page!");
  544. PageDirective (att);
  545. main_directive = directive.TagID;
  546. break;
  547. case "IMPORT":
  548. value = att ["namespace"] as string;
  549. if (value == null || att.Count > 1)
  550. throw new ApplicationException ("Wrong syntax in Import directive.");
  551. string _using = "using " + value + ";";
  552. if (prolog.ToString ().IndexOf (_using) == -1) {
  553. prolog.AppendFormat ("\t{0}\n", _using);
  554. string imports = Options ["Import"] as string;
  555. if (imports == null) {
  556. imports = value;
  557. } else {
  558. imports += "," + value;
  559. }
  560. Options ["Import"] = imports;
  561. }
  562. break;
  563. case "IMPLEMENTS":
  564. if (IsApplication)
  565. throw new ApplicationException ("@ Implements not allowed in an application file.");
  566. string iface = (string) att ["interface"];
  567. interfaces += ", " + iface;
  568. break;
  569. case "REGISTER":
  570. if (IsApplication)
  571. throw new ApplicationException ("@ Register not allowed in an application file.");
  572. RegisterDirective (att);
  573. break;
  574. case "ASSEMBLY":
  575. if (att.Count > 1)
  576. throw new ApplicationException ("Wrong syntax in Assembly directive.");
  577. string name = att ["name"] as string;
  578. string src = att ["src"] as string;
  579. if (name == null && src == null)
  580. throw new ApplicationException ("Wrong syntax in Assembly directive.");
  581. if (IsApplication && src != null)
  582. throw new ApplicationException ("'name' attribute expected.");
  583. value = (name == null) ? src : name;
  584. string assemblies = Options ["Assembly"] as string;
  585. if (assemblies == null) {
  586. assemblies = value;
  587. } else {
  588. assemblies += "," + value;
  589. }
  590. Options ["Assembly"] = assemblies;
  591. break;
  592. }
  593. }
  594. private void ProcessPlainText ()
  595. {
  596. PlainText asis = (PlainText) elements.Current;
  597. string trimmed = asis.Text.Trim ();
  598. if (trimmed == String.Empty && controls.SpaceBetweenTags == true)
  599. return;
  600. if (IsApplication) {
  601. if (trimmed != String.Empty)
  602. throw new ApplicationException (app_file_wrong);
  603. return;
  604. }
  605. if (trimmed != String.Empty && controls.PeekChildKind () != ChildrenKind.CONTROLS){
  606. string tag_id = controls.PeekTagID ();
  607. throw new ApplicationException ("Literal content not allowed for " + tag_id);
  608. }
  609. string escaped_text = Escape (asis.Text);
  610. current_function.AppendFormat ("\t\t\t__parser.AddParsedSubObject (" +
  611. "new System.Web.UI.LiteralControl (\"{0}\"));\n",
  612. escaped_text);
  613. StringBuilder codeRenderFunction = controls.CodeRenderFunction;
  614. codeRenderFunction.AppendFormat ("\t\t\t__output.Write (\"{0}\");\n", escaped_text);
  615. }
  616. private string EnumValueNameToString (Type enum_type, string value_name)
  617. {
  618. if (value_name.EndsWith ("*"))
  619. throw new ApplicationException ("Invalid property value: '" + value_name +
  620. ". It must be a valid " + enum_type.ToString () + " value.");
  621. MemberInfo [] nested_types = enum_type.FindMembers (MemberTypes.Field,
  622. BindingFlags.Public | BindingFlags.Static,
  623. Type.FilterNameIgnoreCase,
  624. value_name);
  625. if (nested_types.Length == 0)
  626. throw new ApplicationException ("Value " + value_name + " not found in enumeration " +
  627. enum_type.ToString ());
  628. if (nested_types.Length > 1)
  629. throw new ApplicationException ("Value " + value_name + " found " +
  630. nested_types.Length + " in enumeration " +
  631. enum_type.ToString ());
  632. return enum_type.ToString () + "." + nested_types [0].Name;
  633. }
  634. private void NewControlFunction (string tag_id,
  635. string control_id,
  636. Type control_type,
  637. ChildrenKind children_kind,
  638. string defaultPropertyName)
  639. {
  640. ChildrenKind prev_children_kind = controls.PeekChildKind ();
  641. if (prev_children_kind == ChildrenKind.NONE ||
  642. prev_children_kind == ChildrenKind.PROPERTIES){
  643. string prev_tag_id = controls.PeekTagID ();
  644. throw new ApplicationException ("Child controls not allowed for " + prev_tag_id);
  645. }
  646. if (prev_children_kind == ChildrenKind.DBCOLUMNS &&
  647. control_type != typeof (System.Web.UI.WebControls.DataGridColumn) &&
  648. !control_type.IsSubclassOf (typeof (System.Web.UI.WebControls.DataGridColumn)))
  649. throw new ApplicationException ("Inside " + controls.PeekTagID () + " only " +
  650. "System.Web.UI.WebControls.DataGridColum " +
  651. "objects are allowed");
  652. else if (prev_children_kind == ChildrenKind.LISTITEM &&
  653. control_type != typeof (System.Web.UI.WebControls.ListItem))
  654. throw new ApplicationException ("Inside " + controls.PeekTagID () + " only " +
  655. "System.Web.UI.WebControls.ListItem " +
  656. "objects are allowed");
  657. else if (prev_children_kind == ChildrenKind.HTMLROW &&
  658. control_type != typeof (System.Web.UI.HtmlControls.HtmlTableRow))
  659. throw new ApplicationException ("Inside " + controls.PeekTagID () + " only " +
  660. "System.Web.UI.HtmlControls.HtmlTableRow " +
  661. "objects are allowed");
  662. else if (prev_children_kind == ChildrenKind.HTMLCELL &&
  663. control_type != typeof (System.Web.UI.HtmlControls.HtmlTableCell))
  664. throw new ApplicationException ("Inside " + controls.PeekTagID () + " only " +
  665. "System.Web.UI.HtmlControls.HtmlTableCell " +
  666. "objects are allowed");
  667. StringBuilder func_code = new StringBuilder ();
  668. current_function = func_code;
  669. if (0 == String.Compare (tag_id, "form", true)){
  670. if (has_form_tag)
  671. throw new ApplicationException ("Only one form server tag allowed.");
  672. has_form_tag = true;
  673. }
  674. controls.Push (control_type, control_id, tag_id, children_kind, defaultPropertyName);
  675. bool is_generic = control_type == typeof (System.Web.UI.HtmlControls.HtmlGenericControl);
  676. functions.Push (current_function);
  677. if (control_type != typeof (System.Web.UI.WebControls.ListItem))
  678. current_function.AppendFormat ("\t\tprivate System.Web.UI.Control __BuildControl_" +
  679. "{0} ()\n\t\t{{\n\t\t\t{1} __ctrl;\n\n\t\t\t__ctrl" +
  680. " = new {1} ({2});\n\t\t\tthis.{0} = __ctrl;\n",
  681. control_id, control_type,
  682. (is_generic? "\"" + tag_id + "\"" : ""));
  683. else
  684. current_function.AppendFormat ("\t\tprivate void __BuildControl_{0} ()\n\t\t{{" +
  685. "\n\t\t\t{1} __ctrl;\n\t\t\t__ctrl = new {1} ();" +
  686. "\n\t\t\tthis.{0} = __ctrl;\n",
  687. control_id, control_type);
  688. if (children_kind == ChildrenKind.CONTROLS || children_kind == ChildrenKind.OPTION)
  689. current_function.Append ("\t\t\tSystem.Web.UI.IParserAccessor __parser = " +
  690. "(System.Web.UI.IParserAccessor) __ctrl;\n");
  691. }
  692. private void DataBoundProperty (Type target, string varName, string value)
  693. {
  694. if (value == "")
  695. throw new ApplicationException ("Empty data binding tag.");
  696. string control_id = controls.PeekControlID ();
  697. string control_type_string = controls.PeekType ().ToString ();
  698. StringBuilder db_function = controls.DataBindFunction;
  699. string container;
  700. if (controls.Container == null)
  701. container = "System.Web.UI.Control";
  702. else
  703. container = controls.Container.ToString ();
  704. if (db_function.Length == 0)
  705. db_function.AppendFormat ("\t\tpublic void __DataBind_{0} (object sender, " +
  706. "System.EventArgs e) {{\n" +
  707. "\t\t\t{1} Container;\n" +
  708. "\t\t\t{2} target;\n" +
  709. "\t\t\ttarget = ({2}) sender;\n" +
  710. "\t\t\tContainer = ({1}) target.BindingContainer;\n",
  711. control_id, container, control_type_string);
  712. /* Removes '<%#' and '%>' */
  713. string real_value = value.Remove (0,3);
  714. real_value = real_value.Remove (real_value.Length - 2, 2);
  715. real_value = real_value.Trim ();
  716. if (target == typeof (string))
  717. db_function.AppendFormat ("\t\t\ttarget.{0} = System.Convert.ToString ({1});\n",
  718. varName, real_value);
  719. else
  720. db_function.AppendFormat ("\t\t\ttarget.{0} = ({1}) ({2});\n",
  721. varName, target, real_value);
  722. }
  723. /*
  724. * Returns true if it generates some code for the specified property
  725. */
  726. private void AddPropertyCode (Type prop_type, string var_name, string att, bool isDataBound)
  727. {
  728. /* FIXME: should i check for this or let the compiler fail?
  729. * if (!prop.CanWrite)
  730. * ....
  731. */
  732. if (isDataBound) {
  733. DataBoundProperty (prop_type, var_name, att);
  734. }
  735. else if (prop_type == typeof (string)){
  736. if (att == null)
  737. throw new ApplicationException ("null value for attribute " + var_name );
  738. current_function.AppendFormat ("\t\t\t__ctrl.{0} = \"{1}\";\n", var_name,
  739. Escape (att)); // FIXME: really Escape this?
  740. }
  741. else if (prop_type.IsEnum){
  742. if (att == null)
  743. throw new ApplicationException ("null value for attribute " + var_name );
  744. string enum_value = EnumValueNameToString (prop_type, att);
  745. current_function.AppendFormat ("\t\t\t__ctrl.{0} = {1};\n", var_name, enum_value);
  746. }
  747. else if (prop_type == typeof (bool)){
  748. string value;
  749. if (att == null)
  750. value = "true"; //FIXME: is this ok for non Style properties?
  751. else if (0 == String.Compare (att, "true", true))
  752. value = "true";
  753. else if (0 == String.Compare (att, "false", true))
  754. value = "false";
  755. else
  756. throw new ApplicationException ("Value '" + att + "' is not a valid boolean.");
  757. current_function.AppendFormat ("\t\t\t__ctrl.{0} = {1};\n", var_name, value);
  758. }
  759. else if (prop_type == typeof (System.Web.UI.WebControls.Unit)){
  760. //FIXME: should use the culture specified in Page
  761. try {
  762. Unit value = Unit.Parse (att, System.Globalization.CultureInfo.InvariantCulture);
  763. } catch (Exception) {
  764. throw new ApplicationException ("'" + att + "' cannot be parsed as a unit.");
  765. }
  766. current_function.AppendFormat ("\t\t\t__ctrl.{0} = " +
  767. "System.Web.UI.WebControls.Unit.Parse (\"{1}\", " +
  768. "System.Globalization.CultureInfo.InvariantCulture);\n",
  769. var_name, att);
  770. }
  771. else if (prop_type == typeof (System.Web.UI.WebControls.FontUnit)){
  772. //FIXME: should use the culture specified in Page
  773. try {
  774. FontUnit value = FontUnit.Parse (att, System.Globalization.CultureInfo.InvariantCulture);
  775. } catch (Exception) {
  776. throw new ApplicationException ("'" + att + "' cannot be parsed as a unit.");
  777. }
  778. current_function.AppendFormat ("\t\t\t__ctrl.{0} = " +
  779. "System.Web.UI.WebControls.FontUnit.Parse (\"{1}\", " +
  780. "System.Globalization.CultureInfo.InvariantCulture);\n",
  781. var_name, att);
  782. }
  783. else if (prop_type == typeof (Int16) ||
  784. prop_type == typeof (Int32) ||
  785. prop_type == typeof (Int64)){
  786. long value;
  787. try {
  788. value = Int64.Parse (att); //FIXME: should use the culture specified in Page
  789. } catch (Exception){
  790. throw new ApplicationException (att + " is not a valid signed number " +
  791. "or is out of range.");
  792. }
  793. current_function.AppendFormat ("\t\t\t__ctrl.{0} = {1};\n", var_name, value);
  794. }
  795. else if (prop_type == typeof (UInt16) ||
  796. prop_type == typeof (UInt32) ||
  797. prop_type == typeof (UInt64)){
  798. ulong value;
  799. try {
  800. value = UInt64.Parse (att); //FIXME: should use the culture specified in Page
  801. } catch (Exception){
  802. throw new ApplicationException (att + " is not a valid unsigned number " +
  803. "or is out of range.");
  804. }
  805. current_function.AppendFormat ("\t\t\t__ctrl.{0} = {1};\n", var_name, value);
  806. }
  807. else if (prop_type == typeof (float)){
  808. float value;
  809. try {
  810. value = Single.Parse (att);
  811. } catch (Exception){
  812. throw new ApplicationException (att + " is not avalid float number or " +
  813. "is out of range.");
  814. }
  815. current_function.AppendFormat ("\t\t\t__ctrl.{0} = {1};\n", var_name, value);
  816. }
  817. else if (prop_type == typeof (double)){
  818. double value;
  819. try {
  820. value = Double.Parse (att);
  821. } catch (Exception){
  822. throw new ApplicationException (att + " is not avalid double number or " +
  823. "is out of range.");
  824. }
  825. current_function.AppendFormat ("\t\t\t__ctrl.{0} = {1};\n", var_name, value);
  826. }
  827. else if (prop_type == typeof (System.Drawing.Color)){
  828. Color c;
  829. try {
  830. c = (Color) TypeDescriptor.GetConverter (typeof (Color)).ConvertFromString (att);
  831. } catch (Exception e){
  832. throw new ApplicationException ("Color " + att + " is not a valid color.", e);
  833. }
  834. // Should i also test for IsSystemColor?
  835. // Are KnownColor members in System.Drawing.Color?
  836. if (c.IsKnownColor){
  837. current_function.AppendFormat ("\t\t\t__ctrl.{0} = System.Drawing.Color." +
  838. "{1};\n", var_name, c.Name);
  839. }
  840. else {
  841. current_function.AppendFormat ("\t\t\t__ctrl.{0} = System.Drawing.Color." +
  842. "FromArgb ({1}, {2}, {3}, {4});\n",
  843. var_name, c.A, c.R, c.G, c.B);
  844. }
  845. }
  846. else {
  847. throw new ApplicationException ("Unsupported type in property: " +
  848. prop_type.ToString ());
  849. }
  850. }
  851. private bool ProcessProperties (PropertyInfo prop, string id, TagAttributes att)
  852. {
  853. int hyphen = id.IndexOf ('-');
  854. if (hyphen == -1 && prop.CanWrite == false)
  855. return false;
  856. bool is_processed = false;
  857. bool isDataBound = att.IsDataBound ((string) att [id]);
  858. Type type = prop.PropertyType;
  859. Type style = typeof (System.Web.UI.WebControls.Style);
  860. Type fontinfo = typeof (System.Web.UI.WebControls.FontInfo);
  861. if (0 == String.Compare (prop.Name, id, true)){
  862. AddPropertyCode (type, prop.Name, (string) att [id], isDataBound);
  863. is_processed = true;
  864. } else if ((type == fontinfo || type == style || type.IsSubclassOf (style)) && hyphen != -1){
  865. string prop_field = id.Replace ("-", ".");
  866. string [] parts = prop_field.Split (new char [] {'.'});
  867. if (parts.Length != 2 || 0 != String.Compare (prop.Name, parts [0], true))
  868. return false;
  869. PropertyInfo [] subprops = type.GetProperties ();
  870. foreach (PropertyInfo subprop in subprops){
  871. if (0 != String.Compare (subprop.Name, parts [1], true))
  872. continue;
  873. if (subprop.CanWrite == false)
  874. return false;
  875. bool is_bool = subprop.PropertyType == typeof (bool);
  876. if (!is_bool && att == null){
  877. att [id] = ""; // Font-Size -> Font-Size="" as html
  878. return false;
  879. }
  880. string value;
  881. if (att == null && is_bool)
  882. value = "true"; // Font-Bold <=> Font-Bold="true"
  883. else
  884. value = (string) att [id];
  885. AddPropertyCode (subprop.PropertyType,
  886. prop.Name + "." + subprop.Name,
  887. value, isDataBound);
  888. is_processed = true;
  889. }
  890. }
  891. return is_processed;
  892. }
  893. private void AddCodeForAttributes (Type type, TagAttributes att)
  894. {
  895. EventInfo [] ev_info = type.GetEvents ();
  896. PropertyInfo [] prop_info = type.GetProperties ();
  897. bool is_processed = false;
  898. ArrayList processed = new ArrayList ();
  899. foreach (string id in att.Keys){
  900. if (0 == String.Compare (id, "runat", true) || 0 == String.Compare (id, "id", true))
  901. continue;
  902. if (id.Length > 2 && id.Substring (0, 2).ToUpper () == "ON"){
  903. string id_as_event = id.Substring (2);
  904. foreach (EventInfo ev in ev_info){
  905. if (0 == String.Compare (ev.Name, id_as_event, true)){
  906. current_function.AppendFormat (
  907. "\t\t\t__ctrl.{0} += " +
  908. "new {1} (this.{2});\n",
  909. ev.Name, ev.EventHandlerType, att [id]);
  910. is_processed = true;
  911. break;
  912. }
  913. }
  914. if (is_processed){
  915. is_processed = false;
  916. continue;
  917. }
  918. }
  919. foreach (PropertyInfo prop in prop_info){
  920. is_processed = ProcessProperties (prop, id, att);
  921. if (is_processed)
  922. break;
  923. }
  924. if (is_processed){
  925. is_processed = false;
  926. continue;
  927. }
  928. current_function.AppendFormat ("\t\t\t((System.Web.UI.IAttributeAccessor) __ctrl)." +
  929. "SetAttribute (\"{0}\", \"{1}\");\n",
  930. id, Escape ((string) att [id]));
  931. }
  932. }
  933. private void AddCodeRenderControl (StringBuilder function, int index)
  934. {
  935. function.AppendFormat ("\t\t\tparameterContainer.Controls [{0}]." +
  936. "RenderControl (__output);\n", index);
  937. }
  938. private void AddRenderMethodDelegate (StringBuilder function, string control_id)
  939. {
  940. function.AppendFormat ("\t\t\t__ctrl.SetRenderMethodDelegate (new System.Web." +
  941. "UI.RenderMethod (this.__Render_{0}));\n", control_id);
  942. }
  943. private void AddCodeRenderFunction (string codeRender, string control_id)
  944. {
  945. StringBuilder codeRenderFunction = new StringBuilder ();
  946. codeRenderFunction.AppendFormat ("\t\tprivate void __Render_{0} " +
  947. "(System.Web.UI.HtmlTextWriter __output, " +
  948. "System.Web.UI.Control parameterContainer)\n" +
  949. "\t\t{{\n", control_id);
  950. codeRenderFunction.Append (codeRender);
  951. codeRenderFunction.Append ("\t\t}\n\n");
  952. init_funcs.Append (codeRenderFunction);
  953. }
  954. private void RemoveLiterals (StringBuilder function)
  955. {
  956. string no_literals = Regex.Replace (function.ToString (),
  957. @"\t\t\t__parser.AddParsedSubObject \(" +
  958. @"new System.Web.UI.LiteralControl \(.+\);\n", "");
  959. function.Length = 0;
  960. function.Append (no_literals);
  961. }
  962. private bool FinishControlFunction (string tag_id)
  963. {
  964. if (functions.Count == 0)
  965. throw new ApplicationException ("Unbalanced open/close tags");
  966. if (controls.Count == 0)
  967. return false;
  968. string saved_id = controls.PeekTagID ();
  969. if (0 != String.Compare (saved_id, tag_id, true))
  970. return false;
  971. StringBuilder old_function = (StringBuilder) functions.Pop ();
  972. current_function = (StringBuilder) functions.Peek ();
  973. string control_id = controls.PeekControlID ();
  974. Type control_type = controls.PeekType ();
  975. ChildrenKind child_kind = controls.PeekChildKind ();
  976. bool hasDataBindFunction = controls.HasDataBindFunction ();
  977. if (hasDataBindFunction)
  978. old_function.AppendFormat ("\t\t\t__ctrl.DataBinding += new System.EventHandler " +
  979. "(this.__DataBind_{0});\n", control_id);
  980. bool useCodeRender = controls.UseCodeRender;
  981. if (useCodeRender)
  982. AddRenderMethodDelegate (old_function, control_id);
  983. if (control_type == typeof (System.Web.UI.ITemplate)){
  984. old_function.Append ("\n\t\t}\n\n");
  985. current_function.AppendFormat ("\t\t\t__ctrl.{0} = new System.Web.UI." +
  986. "CompiledTemplateBuilder (new System.Web.UI." +
  987. "BuildTemplateMethod (this.__BuildControl_{1}));\n",
  988. saved_id, control_id);
  989. }
  990. else if (control_type == typeof (System.Web.UI.WebControls.DataGridColumnCollection)){
  991. old_function.Append ("\n\t\t}\n\n");
  992. current_function.AppendFormat ("\t\t\tthis.__BuildControl_{0} (__ctrl.{1});\n",
  993. control_id, saved_id);
  994. }
  995. else if (control_type == typeof (System.Web.UI.WebControls.DataGridColumn) ||
  996. control_type.IsSubclassOf (typeof (System.Web.UI.WebControls.DataGridColumn)) ||
  997. control_type == typeof (System.Web.UI.WebControls.ListItem)){
  998. old_function.Append ("\n\t\t}\n\n");
  999. string parsed = "";
  1000. string ctrl_name = "ctrl";
  1001. Type cont = controls.Container;
  1002. if (cont == null || cont == typeof (System.Web.UI.HtmlControls.HtmlSelect)){
  1003. parsed = "ParsedSubObject";
  1004. ctrl_name = "parser";
  1005. }
  1006. current_function.AppendFormat ("\t\t\tthis.__BuildControl_{0} ();\n" +
  1007. "\t\t\t__{1}.Add{2} (this.{0});\n\n",
  1008. control_id, ctrl_name, parsed);
  1009. }
  1010. else if (child_kind == ChildrenKind.LISTITEM){
  1011. old_function.Append ("\n\t\t}\n\n");
  1012. init_funcs.Append (old_function); // Closes the BuildList function
  1013. old_function = (StringBuilder) functions.Pop ();
  1014. current_function = (StringBuilder) functions.Peek ();
  1015. old_function.AppendFormat ("\n\t\t\tthis.__BuildControl_{0} (__ctrl.{1});\n\t\t\t" +
  1016. "return __ctrl;\n\t\t}}\n\n",
  1017. control_id, controls.PeekDefaultPropertyName ());
  1018. controls.Pop ();
  1019. control_id = controls.PeekControlID ();
  1020. current_function.AppendFormat ("\t\t\tthis.__BuildControl_{0} ();\n\t\t\t__parser." +
  1021. "AddParsedSubObject (this.{0});\n\n", control_id);
  1022. } else if (control_type == typeof (HtmlTableCell)) {
  1023. old_function.Append ("\n\t\t\treturn __ctrl;\n\t\t}\n\n");
  1024. object top = controls.Pop ();
  1025. Type t = controls.PeekType ();
  1026. controls.Push (top);
  1027. string parsed = "";
  1028. string ctrl_name = "ctrl";
  1029. if (t != typeof (HtmlTableRow)) {
  1030. parsed = "ParsedSubObject";
  1031. ctrl_name = "parser";
  1032. }
  1033. current_function.AppendFormat ("\t\t\tthis.__BuildControl_{0} ();\n" +
  1034. "\t\t\t__{1}.Add{2} (this.{0});\n\n",
  1035. control_id, ctrl_name, parsed);
  1036. } else if (child_kind == ChildrenKind.HTMLROW || child_kind == ChildrenKind.HTMLCELL) {
  1037. old_function.Append ("\n\t\t}\n\n");
  1038. init_funcs.Append (old_function);
  1039. old_function = (StringBuilder) functions.Pop ();
  1040. current_function = (StringBuilder) functions.Peek ();
  1041. old_function.AppendFormat ("\n\t\t\tthis.__BuildControl_{0} (__ctrl.{1});\n\t\t\t" +
  1042. "return __ctrl;\n\t\t}}\n\n",
  1043. control_id, controls.PeekDefaultPropertyName ());
  1044. controls.Pop ();
  1045. control_id = controls.PeekControlID ();
  1046. current_function.AppendFormat ("\t\t\tthis.__BuildControl_{0} ();\n", control_id);
  1047. if (child_kind == ChildrenKind.HTMLROW) {
  1048. current_function.AppendFormat ("\t\t\t__parser.AddParsedSubObject ({0});\n",
  1049. control_id);
  1050. } else {
  1051. current_function.AppendFormat ("\t\t\t__ctrl.Add (this.{0});\n", control_id);
  1052. }
  1053. } else {
  1054. old_function.Append ("\n\t\t\treturn __ctrl;\n\t\t}\n\n");
  1055. current_function.AppendFormat ("\t\t\tthis.__BuildControl_{0} ();\n\t\t\t__parser." +
  1056. "AddParsedSubObject (this.{0});\n\n", control_id);
  1057. }
  1058. if (useCodeRender)
  1059. RemoveLiterals (old_function);
  1060. init_funcs.Append (old_function);
  1061. if (useCodeRender)
  1062. AddCodeRenderFunction (controls.CodeRenderFunction.ToString (), control_id);
  1063. if (hasDataBindFunction){
  1064. StringBuilder db_function = controls.DataBindFunction;
  1065. db_function.Append ("\t\t}\n\n");
  1066. init_funcs.Append (db_function);
  1067. }
  1068. // Avoid getting empty stacks for unbalanced open/close tags
  1069. if (controls.Count > 1){
  1070. controls.Pop ();
  1071. AddCodeRenderControl (controls.CodeRenderFunction, controls.ChildIndex);
  1072. }
  1073. return true;
  1074. }
  1075. private void NewTableElementFunction (HtmlControlTag ctrl)
  1076. {
  1077. string control_id = Tag.GetDefaultID ();
  1078. ChildrenKind child_kind;
  1079. Type t;
  1080. if (ctrl.ControlType == typeof (HtmlTable)) {
  1081. t = typeof (HtmlTableRowCollection);
  1082. child_kind = ChildrenKind.HTMLROW;
  1083. } else {
  1084. t = typeof (HtmlTableCellCollection);
  1085. child_kind = ChildrenKind.HTMLCELL;
  1086. }
  1087. controls.Push (ctrl.ControlType,
  1088. control_id,
  1089. ctrl.TagID,
  1090. child_kind,
  1091. ctrl.ParseChildren);
  1092. current_function = new StringBuilder ();
  1093. functions.Push (current_function);
  1094. current_function.AppendFormat ("\t\tprivate void __BuildControl_{0} ({1} __ctrl)\n" +
  1095. "\t\t{{\n", control_id, t);
  1096. }
  1097. private void ProcessHtmlControlTag ()
  1098. {
  1099. HtmlControlTag html_ctrl = (HtmlControlTag) elements.Current;
  1100. if (html_ctrl.TagID.ToUpper () == "SCRIPT"){
  1101. //FIXME: if the is script is to be read from disk, do it!
  1102. if (html_ctrl.SelfClosing)
  1103. throw new ApplicationException ("Read script from file not supported yet.");
  1104. if (elements.MoveNext () == false)
  1105. throw new ApplicationException ("Error after " + html_ctrl.ToString ());
  1106. if (elements.Current is PlainText){
  1107. script.Append (((PlainText) elements.Current).Text);
  1108. if (!elements.MoveNext ())
  1109. throw new ApplicationException ("Error after " +
  1110. elements.Current.ToString ());
  1111. }
  1112. if (elements.Current is CloseTag)
  1113. elements.MoveNext ();
  1114. return;
  1115. } else if (IsApplication) {
  1116. throw new ApplicationException (app_file_wrong);
  1117. }
  1118. Type controlType = html_ctrl.ControlType;
  1119. declarations.AppendFormat ("\t\tprotected {0} {1};\n", controlType, html_ctrl.ControlID);
  1120. ChildrenKind children_kind;
  1121. if (0 == String.Compare (html_ctrl.TagID, "table", true))
  1122. children_kind = ChildrenKind.HTMLROW;
  1123. else if (0 == String.Compare (html_ctrl.TagID, "tr", true))
  1124. children_kind = ChildrenKind.HTMLCELL;
  1125. else if (0 != String.Compare (html_ctrl.TagID, "select", true))
  1126. children_kind = html_ctrl.IsContainer ? ChildrenKind.CONTROLS :
  1127. ChildrenKind.NONE;
  1128. else
  1129. children_kind = ChildrenKind.OPTION;
  1130. NewControlFunction (html_ctrl.TagID, html_ctrl.ControlID, controlType, children_kind, html_ctrl.ParseChildren);
  1131. current_function.AppendFormat ("\t\t\t__ctrl.ID = \"{0}\";\n", html_ctrl.ControlID);
  1132. AddCodeForAttributes (html_ctrl.ControlType, html_ctrl.Attributes);
  1133. if (children_kind == ChildrenKind.HTMLROW || children_kind == ChildrenKind.HTMLCELL)
  1134. NewTableElementFunction (html_ctrl);
  1135. if (!html_ctrl.SelfClosing)
  1136. JustDoIt ();
  1137. else
  1138. FinishControlFunction (html_ctrl.TagID);
  1139. }
  1140. // Closing is performed in FinishControlFunction ()
  1141. private void NewBuildListFunction (AspComponent component)
  1142. {
  1143. string control_id = Tag.GetDefaultID ();
  1144. controls.Push (component.ComponentType,
  1145. control_id,
  1146. component.TagID,
  1147. ChildrenKind.LISTITEM,
  1148. component.DefaultPropertyName);
  1149. current_function = new StringBuilder ();
  1150. functions.Push (current_function);
  1151. current_function.AppendFormat ("\t\tprivate void __BuildControl_{0} " +
  1152. "(System.Web.UI.WebControls.ListItemCollection __ctrl)\n" +
  1153. "\t\t{{\n", control_id);
  1154. }
  1155. private void ProcessComponent ()
  1156. {
  1157. AspComponent component = (AspComponent) elements.Current;
  1158. Type component_type = component.ComponentType;
  1159. declarations.AppendFormat ("\t\tprotected {0} {1};\n", component_type, component.ControlID);
  1160. NewControlFunction (component.TagID, component.ControlID, component_type,
  1161. component.ChildrenKind, component.DefaultPropertyName);
  1162. if (component_type.IsSubclassOf (typeof (System.Web.UI.UserControl)))
  1163. current_function.Append ("\t\t\t__ctrl.InitializeAsUserControl (Page);\n");
  1164. if (component_type.IsSubclassOf (typeof (System.Web.UI.Control)))
  1165. current_function.AppendFormat ("\t\t\t__ctrl.ID = \"{0}\";\n", component.ControlID);
  1166. AddCodeForAttributes (component.ComponentType, component.Attributes);
  1167. if (component.ChildrenKind == ChildrenKind.LISTITEM)
  1168. NewBuildListFunction (component);
  1169. if (!component.SelfClosing)
  1170. JustDoIt ();
  1171. else
  1172. FinishControlFunction (component.TagID);
  1173. }
  1174. private void ProcessServerObjectTag ()
  1175. {
  1176. ServerObjectTag obj = (ServerObjectTag) elements.Current;
  1177. declarations.AppendFormat ("\t\tprivate {0} cached{1};\n", obj.ObjectClass, obj.ObjectID);
  1178. constructor.AppendFormat ("\n\t\tprivate {0} {1}\n\t\t{{\n\t\t\tget {{\n\t\t\t\t" +
  1179. "if (this.cached{1} == null)\n\t\t\t\t\tthis.cached{1} = " +
  1180. "new {0} ();\n\t\t\t\treturn cached{1};\n\t\t\t}}\n\t\t}}\n\n",
  1181. obj.ObjectClass, obj.ObjectID);
  1182. }
  1183. // Creates a new function that sets the values of subproperties.
  1184. private void NewStyleFunction (PropertyTag tag)
  1185. {
  1186. current_function = new StringBuilder ();
  1187. string prop_id = tag.PropertyID;
  1188. Type prop_type = tag.PropertyType;
  1189. // begin function
  1190. current_function.AppendFormat ("\t\tprivate void __BuildControl_{0} ({1} __ctrl)\n" +
  1191. "\t\t{{\n", prop_id, prop_type);
  1192. // Add property initialization code
  1193. PropertyInfo [] subprop_info = prop_type.GetProperties ();
  1194. TagAttributes att = tag.Attributes;
  1195. string subprop_name = null;
  1196. foreach (string id in att.Keys){
  1197. if (0 == String.Compare (id, "runat", true) || 0 == String.Compare (id, "id", true))
  1198. continue;
  1199. bool is_processed = false;
  1200. foreach (PropertyInfo subprop in subprop_info){
  1201. is_processed = ProcessProperties (subprop, id, att);
  1202. if (is_processed){
  1203. subprop_name = subprop.Name;
  1204. break;
  1205. }
  1206. }
  1207. if (subprop_name == null)
  1208. throw new ApplicationException ("Property " + tag.TagID + " does not have " +
  1209. "a " + id + " subproperty.");
  1210. }
  1211. // Finish function
  1212. current_function.Append ("\n\t\t}\n\n");
  1213. init_funcs.Append (current_function);
  1214. current_function = (StringBuilder) functions.Peek ();
  1215. current_function.AppendFormat ("\t\t\tthis.__BuildControl_{0} (__ctrl.{1});\n",
  1216. prop_id, tag.PropertyName);
  1217. if (!tag.SelfClosing){
  1218. // Next tag should be the closing tag
  1219. controls.Push (null, null, null, ChildrenKind.NONE, null);
  1220. bool closing_tag_found = false;
  1221. Element elem;
  1222. while (!closing_tag_found && elements.MoveNext ()){
  1223. elem = (Element) elements.Current;
  1224. if (elem is PlainText)
  1225. ProcessPlainText ();
  1226. else if (!(elem is CloseTag))
  1227. throw new ApplicationException ("Tag " + tag.TagID +
  1228. " not properly closed.");
  1229. else
  1230. closing_tag_found = true;
  1231. }
  1232. if (!closing_tag_found)
  1233. throw new ApplicationException ("Tag " + tag.TagID + " not properly closed.");
  1234. controls.Pop ();
  1235. }
  1236. }
  1237. // This one just opens the function. Closing is performed in FinishControlFunction ()
  1238. private void NewTemplateFunction (PropertyTag tag)
  1239. {
  1240. /*
  1241. * FIXME
  1242. * This function does almost the same as NewControlFunction.
  1243. * Consider merging.
  1244. */
  1245. string prop_id = tag.PropertyID;
  1246. Type prop_type = tag.PropertyType;
  1247. string tag_id = tag.PropertyName; // Real property name used in FinishControlFunction
  1248. controls.Push (prop_type, prop_id, tag_id, ChildrenKind.CONTROLS, null);
  1249. current_function = new StringBuilder ();
  1250. functions.Push (current_function);
  1251. current_function.AppendFormat ("\t\tprivate void __BuildControl_{0} " +
  1252. "(System.Web.UI.Control __ctrl)\n" +
  1253. "\t\t{{\n" +
  1254. "\t\t\tSystem.Web.UI.IParserAccessor __parser " +
  1255. "= (System.Web.UI.IParserAccessor) __ctrl;\n" , prop_id);
  1256. }
  1257. // Closing is performed in FinishControlFunction ()
  1258. private void NewDBColumnFunction (PropertyTag tag)
  1259. {
  1260. /*
  1261. * FIXME
  1262. * This function also does almost the same as NewControlFunction.
  1263. * Consider merging.
  1264. */
  1265. string prop_id = tag.PropertyID;
  1266. Type prop_type = tag.PropertyType;
  1267. string tag_id = tag.PropertyName; // Real property name used in FinishControlFunction
  1268. controls.Push (prop_type, prop_id, tag_id, ChildrenKind.DBCOLUMNS, null);
  1269. current_function = new StringBuilder ();
  1270. functions.Push (current_function);
  1271. current_function.AppendFormat ("\t\tprivate void __BuildControl_{0} " +
  1272. "(System.Web.UI.WebControl.DataGridColumnCollection __ctrl)\n" +
  1273. "\t\t{{\n", prop_id);
  1274. }
  1275. private void NewPropertyFunction (PropertyTag tag)
  1276. {
  1277. if (tag.PropertyType == typeof (System.Web.UI.WebControls.Style) ||
  1278. tag.PropertyType.IsSubclassOf (typeof (System.Web.UI.WebControls.Style)))
  1279. NewStyleFunction (tag);
  1280. else if (tag.PropertyType == typeof (System.Web.UI.ITemplate))
  1281. NewTemplateFunction (tag);
  1282. else if (tag.PropertyType == typeof (System.Web.UI.WebControls.DataGridColumnCollection))
  1283. NewDBColumnFunction (tag);
  1284. else
  1285. throw new ApplicationException ("Other than Style and ITemplate not supported yet. " +
  1286. tag.PropertyType);
  1287. }
  1288. private void ProcessHtmlTag ()
  1289. {
  1290. Tag tag = (Tag) elements.Current;
  1291. ChildrenKind child_kind = controls.PeekChildKind ();
  1292. if (child_kind == ChildrenKind.NONE){
  1293. string tag_id = controls.PeekTagID ();
  1294. throw new ApplicationException (tag + " not allowed inside " + tag_id);
  1295. }
  1296. if (child_kind == ChildrenKind.OPTION){
  1297. if (0 != String.Compare (tag.TagID, "option", true))
  1298. throw new ApplicationException ("Only <option> tags allowed inside <select>.");
  1299. string default_id = Tag.GetDefaultID ();
  1300. Type type = typeof (System.Web.UI.WebControls.ListItem);
  1301. declarations.AppendFormat ("\t\tprotected {0} {1};\n", type, default_id);
  1302. NewControlFunction (tag.TagID, default_id, type, ChildrenKind.CONTROLS, null);
  1303. return;
  1304. }
  1305. if (child_kind == ChildrenKind.CONTROLS) {
  1306. ArrayList tag_elements = tag.GetElements ();
  1307. foreach (Element e in tag_elements) {
  1308. if (e is PlainText) {
  1309. elements.Current = e;
  1310. ProcessPlainText ();
  1311. } else if (e is CodeRenderTag) {
  1312. elements.Current = e;
  1313. ProcessCodeRenderTag ();
  1314. } else if (e is DataBindingTag) {
  1315. elements.Current = e;
  1316. ProcessDataBindingLiteral ();
  1317. } else {
  1318. throw new ApplicationException (fullPath + ": unexpected tag type " + e.GetType ());
  1319. }
  1320. }
  1321. return;
  1322. }
  1323. if (child_kind == ChildrenKind.HTMLROW) {
  1324. if (0 == String.Compare (tag.TagID, "tr", true)) {
  1325. elements.Current = new HtmlControlTag (tag);
  1326. ProcessHtmlControlTag ();
  1327. return;
  1328. }
  1329. }
  1330. if (child_kind == ChildrenKind.HTMLCELL) {
  1331. if (0 == String.Compare (tag.TagID, "td", true)) {
  1332. elements.Current = new HtmlControlTag (tag);
  1333. ProcessHtmlControlTag ();
  1334. return;
  1335. }
  1336. }
  1337. // Now child_kind should be PROPERTIES, so only allow tag_id == property
  1338. Type control_type = controls.PeekType ();
  1339. PropertyInfo [] prop_info = control_type.GetProperties ();
  1340. bool is_processed = false;
  1341. foreach (PropertyInfo prop in prop_info){
  1342. if (0 == String.Compare (prop.Name, tag.TagID, true)){
  1343. PropertyTag prop_tag = new PropertyTag (tag, prop.PropertyType, prop.Name);
  1344. NewPropertyFunction (prop_tag);
  1345. is_processed = true;
  1346. break;
  1347. }
  1348. }
  1349. if (!is_processed){
  1350. string tag_id = controls.PeekTagID ();
  1351. throw new ApplicationException (tag.TagID + " is not a property of " + control_type);
  1352. }
  1353. }
  1354. private Tag Map (Tag tag)
  1355. {
  1356. int pos = tag.TagID.IndexOf (":");
  1357. if (pos == -1) {
  1358. ChildrenKind child_kind = controls.PeekChildKind ();
  1359. if (child_kind == ChildrenKind.HTMLROW && 0 == String.Compare (tag.TagID, "tr", true)) {
  1360. tag.Attributes.Add ("runat", "server");
  1361. return new HtmlControlTag (tag);
  1362. } else if (child_kind == ChildrenKind.HTMLROW && 0 == String.Compare (tag.TagID, "tr", true)) {
  1363. tag.Attributes.Add ("runat", "server");
  1364. return new HtmlControlTag (tag);
  1365. }
  1366. }
  1367. if (tag is CloseTag ||
  1368. ((tag.Attributes == null ||
  1369. !tag.Attributes.IsRunAtServer ()) && pos == -1))
  1370. return tag;
  1371. if (pos == -1){
  1372. if (0 == String.Compare (tag.TagID, "object", true))
  1373. return new ServerObjectTag (tag);
  1374. return new HtmlControlTag (tag);
  1375. }
  1376. string foundry_name = tag.TagID.Substring (0, pos);
  1377. string component_name = tag.TagID.Substring (pos + 1);
  1378. if (Foundry.LookupFoundry (foundry_name) == false)
  1379. throw new ApplicationException ("Cannot find foundry for alias'" + foundry_name + "'");
  1380. AspComponent component = Foundry.MakeAspComponent (foundry_name, component_name, tag);
  1381. if (component == null)
  1382. throw new ApplicationException ("Cannot find component '" + component_name +
  1383. "' for alias '" + foundry_name + "'");
  1384. return component;
  1385. }
  1386. private void ProcessCloseTag ()
  1387. {
  1388. CloseTag close_tag = (CloseTag) elements.Current;
  1389. if (FinishControlFunction (close_tag.TagID))
  1390. return;
  1391. elements.Current = new PlainText (close_tag.PlainHtml);
  1392. ProcessPlainText ();
  1393. }
  1394. private void ProcessDataBindingLiteral ()
  1395. {
  1396. DataBindingTag dataBinding = (DataBindingTag) elements.Current;
  1397. string actual_value = dataBinding.Data;
  1398. if (actual_value == "")
  1399. throw new ApplicationException ("Empty data binding tag.");
  1400. if (controls.PeekChildKind () != ChildrenKind.CONTROLS)
  1401. throw new ApplicationException ("Data bound content not allowed for " +
  1402. controls.PeekTagID ());
  1403. StringBuilder db_function = new StringBuilder ();
  1404. string control_id = Tag.GetDefaultID ();
  1405. string control_type_string = "System.Web.UI.DataBoundLiteralControl";
  1406. declarations.AppendFormat ("\t\tprotected {0} {1};\n", control_type_string, control_id);
  1407. // Build the control
  1408. db_function.AppendFormat ("\t\tprivate System.Web.UI.Control __BuildControl_{0} ()\n" +
  1409. "\t\t{{\n\t\t\t{1} __ctrl;\n\n" +
  1410. "\t\t\t__ctrl = new {1} (0, 1);\n" +
  1411. "\t\t\tthis.{0} = __ctrl;\n" +
  1412. "\t\t\t__ctrl.DataBinding += new System.EventHandler " +
  1413. "(this.__DataBind_{0});\n" +
  1414. "\t\t\treturn __ctrl;\n"+
  1415. "\t\t}}\n\n",
  1416. control_id, control_type_string);
  1417. // DataBinding handler
  1418. db_function.AppendFormat ("\t\tpublic void __DataBind_{0} (object sender, " +
  1419. "System.EventArgs e) {{\n" +
  1420. "\t\t\t{1} Container;\n" +
  1421. "\t\t\t{2} target;\n" +
  1422. "\t\t\ttarget = ({2}) sender;\n" +
  1423. "\t\t\tContainer = ({1}) target.BindingContainer;\n" +
  1424. "\t\t\ttarget.SetDataBoundString (0, System.Convert." +
  1425. "ToString ({3}));\n" +
  1426. "\t\t}}\n\n",
  1427. control_id, controls.Container, control_type_string,
  1428. actual_value);
  1429. init_funcs.Append (db_function);
  1430. current_function.AppendFormat ("\t\t\tthis.__BuildControl_{0} ();\n\t\t\t__parser." +
  1431. "AddParsedSubObject (this.{0});\n\n", control_id);
  1432. }
  1433. private void ProcessCodeRenderTag ()
  1434. {
  1435. CodeRenderTag code_tag = (CodeRenderTag) elements.Current;
  1436. controls.UseCodeRender = true;
  1437. if (code_tag.IsVarName)
  1438. controls.CodeRenderFunction.AppendFormat ("\t\t\t__output.Write ({0});\n",
  1439. code_tag.Code);
  1440. else
  1441. controls.CodeRenderFunction.AppendFormat ("\t\t\t{0}\n", code_tag.Code);
  1442. }
  1443. public void ProcessElements ()
  1444. {
  1445. JustDoIt ();
  1446. End ();
  1447. parse_ok = true;
  1448. }
  1449. private void JustDoIt ()
  1450. {
  1451. Element element;
  1452. while (elements.MoveNext ()){
  1453. element = (Element) elements.Current;
  1454. if (element is Directive){
  1455. ProcessDirective ();
  1456. } else if (element is PlainText){
  1457. ProcessPlainText ();
  1458. } else if (element is DataBindingTag){
  1459. if (IsApplication)
  1460. throw new ApplicationException (app_file_wrong);
  1461. ProcessDataBindingLiteral ();
  1462. } else if (element is CodeRenderTag){
  1463. if (IsApplication)
  1464. throw new ApplicationException (app_file_wrong);
  1465. ProcessCodeRenderTag ();
  1466. } else {
  1467. elements.Current = Map ((Tag) element);
  1468. if (elements.Current is ServerObjectTag) {
  1469. ProcessServerObjectTag ();
  1470. continue;
  1471. }
  1472. if (elements.Current is HtmlControlTag) {
  1473. ProcessHtmlControlTag ();
  1474. continue;
  1475. }
  1476. if (IsApplication)
  1477. throw new ApplicationException (app_file_wrong);
  1478. else if (elements.Current is AspComponent)
  1479. ProcessComponent ();
  1480. else if (elements.Current is CloseTag)
  1481. ProcessCloseTag ();
  1482. else if (elements.Current is Tag)
  1483. ProcessHtmlTag ();
  1484. else
  1485. throw new ApplicationException ("This place should not be reached.");
  1486. }
  1487. }
  1488. }
  1489. private void End ()
  1490. {
  1491. classDecl = "\tpublic class " + className + " : " + parent + interfaces + " {\n";
  1492. prolog.Append ("\n" + classDecl);
  1493. declarations.Append ("\t\tprivate static bool __intialized = false;\n\n");
  1494. if (IsPage)
  1495. declarations.Append ("\t\tprivate static ArrayList __fileDependencies;\n\n");
  1496. // adds the constructor
  1497. constructor.AppendFormat ("\t\tpublic {0} ()\n\t\t{{\n", className);
  1498. if (!IsApplication)
  1499. constructor.Append ("\t\t\tSystem.Collections.ArrayList dependencies;\n\n");
  1500. constructor.AppendFormat ("\t\t\tif (ASP.{0}.__intialized == false){{\n", className);
  1501. if (IsPage) {
  1502. constructor.AppendFormat ("\t\t\t\tdependencies = new System.Collections.ArrayList ();\n" +
  1503. "\t\t\t\tdependencies.Add (@\"{1}\");\n" +
  1504. "\t\t\t\tASP.{0}.__fileDependencies = dependencies;\n",
  1505. className, fullPath);
  1506. }
  1507. constructor.AppendFormat ("\t\t\t\tASP.{0}.__intialized = true;\n\t\t\t}}\n\t\t}}\n\n",
  1508. className);
  1509. if (!IsApplication) {
  1510. //FIXME: add AutoHandlers: don't know what for...yet!
  1511. constructor.AppendFormat (
  1512. "\t\tprotected override int AutoHandlers\n\t\t{{\n" +
  1513. "\t\t\tget {{ return ASP.{0}.__autoHandlers; }}\n" +
  1514. "\t\t\tset {{ ASP.{0}.__autoHandlers = value; }}\n" +
  1515. "\t\t}}\n\n", className);
  1516. constructor.Append (
  1517. "\t\tprotected System.Web.HttpApplication ApplicationInstance\n\t\t{\n" +
  1518. "\t\t\tget { return (System.Web.HttpApplication) this.Context.ApplicationInstance; }\n" +
  1519. "\t\t}\n\n");
  1520. //FIXME: add TemplateSourceDirectory: don't know what for...yet!
  1521. //FIXME: it should be the path from the root where the file resides
  1522. constructor.AppendFormat (
  1523. "\t\tpublic override string TemplateSourceDirectory\n\t\t{{\n" +
  1524. "\t\t\tget {{ return \"{0}\"; }}\n" +
  1525. "\t\t}}\n\n", Path.GetDirectoryName (fullPath)); // FIXME: should be rooted on .appVPath
  1526. epilog.Append ("\n\t\tprotected override void FrameworkInitialize ()\n\t\t{\n" +
  1527. "\t\t\tthis.__BuildControlTree (this);\n");
  1528. if (IsPage) {
  1529. epilog.AppendFormat ("\t\t\tthis.FileDependencies = ASP.{0}.__fileDependencies;\n" +
  1530. "\t\t\tthis.EnableViewStateMac = true;\n", className);
  1531. }
  1532. epilog.Append ("\t\t}\n\n");
  1533. }
  1534. if (IsPage) {
  1535. Random rnd = new Random ();
  1536. epilog.AppendFormat ("\t\tpublic override int GetTypeHashCode ()\n\t\t{{\n" +
  1537. "\t\t\treturn {0};\n" +
  1538. "\t\t}}\n", rnd.Next ());
  1539. }
  1540. epilog.Append ("\t}\n}\n");
  1541. // Closes the currently opened tags
  1542. StringBuilder old_function = current_function;
  1543. string control_id;
  1544. while (functions.Count > 1){
  1545. old_function.Append ("\n\t\t\treturn __ctrl;\n\t\t}\n\n");
  1546. init_funcs.Append (old_function);
  1547. control_id = controls.PeekControlID ();
  1548. FinishControlFunction (control_id);
  1549. controls.AddChild ();
  1550. old_function = (StringBuilder) functions.Pop ();
  1551. current_function = (StringBuilder) functions.Peek ();
  1552. controls.Pop ();
  1553. }
  1554. bool useCodeRender = controls.UseCodeRender;
  1555. if (useCodeRender){
  1556. RemoveLiterals (current_function);
  1557. AddRenderMethodDelegate (current_function, controls.PeekControlID ());
  1558. }
  1559. current_function.Append ("\t\t}\n\n");
  1560. init_funcs.Append (current_function);
  1561. if (useCodeRender)
  1562. AddCodeRenderFunction (controls.CodeRenderFunction.ToString (), controls.PeekControlID ());
  1563. functions.Pop ();
  1564. }
  1565. //
  1566. // Functions related to compilation of user controls
  1567. //
  1568. private static char dirSeparator = Path.DirectorySeparatorChar;
  1569. struct UserControlData
  1570. {
  1571. public UserControlResult result;
  1572. public string className;
  1573. public string assemblyName;
  1574. }
  1575. private static UserControlData GenerateUserControl (string src)
  1576. {
  1577. UserControlData data = new UserControlData ();
  1578. data.result = UserControlResult.OK;
  1579. if (!File.Exists (src)) {
  1580. data.result = UserControlResult.FileNotFound;
  1581. return data;
  1582. }
  1583. UserControlCompiler compiler = new UserControlCompiler (new UserControlParser (src));
  1584. Type t = compiler.GetCompiledType ();
  1585. if (t == null) {
  1586. data.result = UserControlResult.CompilationFailed;
  1587. return data;
  1588. }
  1589. data.className = t.Name;
  1590. data.assemblyName = compiler.TargetFile;
  1591. return data;
  1592. }
  1593. }
  1594. }