AspGenerator.cs 62 KB

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