AspGenerator.cs 57 KB

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