AspGenerator.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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.CodeDom.Compiler;
  12. using System.IO;
  13. using System.Text;
  14. using System.Web.UI;
  15. using System.Web.Util;
  16. namespace System.Web.Compilation
  17. {
  18. class BuilderLocation
  19. {
  20. public ControlBuilder Builder;
  21. public ILocation Location;
  22. public BuilderLocation (ControlBuilder builder, ILocation location)
  23. {
  24. this.Builder = builder;
  25. this.Location = location;
  26. }
  27. }
  28. class BuilderLocationStack : Stack
  29. {
  30. public override void Push (object o)
  31. {
  32. if (!(o is BuilderLocation))
  33. throw new InvalidOperationException ();
  34. base.Push (o);
  35. }
  36. public virtual void Push (ControlBuilder builder, ILocation location)
  37. {
  38. BuilderLocation bl = new BuilderLocation (builder, location);
  39. Push (bl);
  40. }
  41. public new BuilderLocation Peek ()
  42. {
  43. return (BuilderLocation) base.Peek ();
  44. }
  45. public new BuilderLocation Pop ()
  46. {
  47. return (BuilderLocation) base.Pop ();
  48. }
  49. public ControlBuilder Builder {
  50. get { return Peek ().Builder; }
  51. }
  52. }
  53. class ParserStack
  54. {
  55. Hashtable files;
  56. Stack parsers;
  57. AspParser current;
  58. public ParserStack ()
  59. {
  60. files = new Hashtable (); // may be this should be case sensitive for windows
  61. parsers = new Stack ();
  62. }
  63. public bool Push (AspParser parser)
  64. {
  65. if (files.Contains (parser.Filename))
  66. return false;
  67. files [parser.Filename] = true;
  68. parsers.Push (parser);
  69. current = parser;
  70. return true;
  71. }
  72. public AspParser Pop ()
  73. {
  74. if (parsers.Count == 0)
  75. return null;
  76. files.Remove (current.Filename);
  77. AspParser result = (AspParser) parsers.Pop ();
  78. if (parsers.Count > 0)
  79. current = (AspParser) parsers.Peek ();
  80. else
  81. current = null;
  82. return result;
  83. }
  84. public AspParser Parser {
  85. get { return current; }
  86. }
  87. public string Filename {
  88. get { return current.Filename; }
  89. }
  90. }
  91. class AspGenerator
  92. {
  93. ParserStack pstack;
  94. BuilderLocationStack stack;
  95. TemplateParser tparser;
  96. StringBuilder text;
  97. RootBuilder rootBuilder;
  98. bool inScript, javascript;
  99. ILocation location;
  100. static Hashtable emptyHash = new Hashtable ();
  101. public AspGenerator (TemplateParser tparser)
  102. {
  103. this.tparser = tparser;
  104. tparser.AddDependency (tparser.InputFile);
  105. text = new StringBuilder ();
  106. stack = new BuilderLocationStack ();
  107. rootBuilder = new RootBuilder (tparser);
  108. stack.Push (rootBuilder, null);
  109. tparser.RootBuilder = rootBuilder;
  110. pstack = new ParserStack ();
  111. }
  112. public AspParser Parser {
  113. get { return pstack.Parser; }
  114. }
  115. public string Filename {
  116. get { return pstack.Filename; }
  117. }
  118. BaseCompiler GetCompilerFromType ()
  119. {
  120. Type type = tparser.GetType ();
  121. if (type == typeof (PageParser))
  122. return new PageCompiler ((PageParser) tparser);
  123. if (type == typeof (ApplicationFileParser))
  124. return new GlobalAsaxCompiler ((ApplicationFileParser) tparser);
  125. if (type == typeof (UserControlParser))
  126. return new UserControlCompiler ((UserControlParser) tparser);
  127. throw new Exception ("Got type: " + type);
  128. }
  129. void InitParser (string filename)
  130. {
  131. StreamReader reader = new StreamReader (filename, WebEncoding.FileEncoding);
  132. AspParser parser = new AspParser (filename, reader);
  133. parser.Error += new ParseErrorHandler (ParseError);
  134. parser.TagParsed += new TagParsedHandler (TagParsed);
  135. parser.TextParsed += new TextParsedHandler (TextParsed);
  136. if (!pstack.Push (parser))
  137. throw new ParseException (Location, "Infinite recursion detected including file: " + filename);
  138. tparser.AddDependency (filename);
  139. }
  140. void DoParse ()
  141. {
  142. pstack.Parser.Parse ();
  143. if (text.Length > 0)
  144. FlushText ();
  145. pstack.Pop ();
  146. }
  147. public Type GetCompiledType ()
  148. {
  149. InitParser (Path.GetFullPath (tparser.InputFile));
  150. DoParse ();
  151. #if DEBUG
  152. PrintTree (rootBuilder, 0);
  153. #endif
  154. if (stack.Count > 1)
  155. throw new ParseException (stack.Builder.location,
  156. "Expecting </" + stack.Builder.TagName + ">" + stack.Builder);
  157. BaseCompiler compiler = GetCompilerFromType ();
  158. return compiler.GetCompiledType ();
  159. }
  160. #if DEBUG
  161. static void PrintTree (ControlBuilder builder, int indent)
  162. {
  163. if (builder == null)
  164. return;
  165. string i = new string ('\t', indent);
  166. Console.Write (i);
  167. Console.WriteLine ("b: {0} id: {1} type: {2} parent: {3}",
  168. builder, builder.ID, builder.ControlType, builder.parentBuilder);
  169. if (builder.Children != null)
  170. foreach (object o in builder.Children) {
  171. if (o is ControlBuilder)
  172. PrintTree ((ControlBuilder) o, indent++);
  173. }
  174. }
  175. #endif
  176. static void PrintLocation (ILocation loc)
  177. {
  178. Console.WriteLine ("\tFile name: " + loc.Filename);
  179. Console.WriteLine ("\tBegin line: " + loc.BeginLine);
  180. Console.WriteLine ("\tEnd line: " + loc.EndLine);
  181. Console.WriteLine ("\tBegin column: " + loc.BeginColumn);
  182. Console.WriteLine ("\tEnd column: " + loc.EndColumn);
  183. Console.WriteLine ("\tPlainText: " + loc.PlainText);
  184. Console.WriteLine ();
  185. }
  186. void ParseError (ILocation location, string message)
  187. {
  188. throw new ParseException (location, message);
  189. }
  190. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  191. {
  192. this.location = new Location (location);
  193. if (tparser != null)
  194. tparser.Location = location;
  195. if (text.Length != 0)
  196. FlushText ();
  197. if (0 == String.Compare (tagid, "script", true)) {
  198. if (ProcessScript (tagtype, attributes))
  199. return;
  200. }
  201. switch (tagtype) {
  202. case TagType.Directive:
  203. if (tagid == "")
  204. tagid = tparser.DefaultDirectiveName;
  205. tparser.AddDirective (tagid, attributes.GetDictionary (null));
  206. break;
  207. case TagType.Tag:
  208. if (!ProcessTag (tagid, attributes))
  209. TextParsed (location, location.PlainText);
  210. break;
  211. case TagType.Close:
  212. if (!CloseControl (tagid))
  213. TextParsed (location, location.PlainText);
  214. break;
  215. case TagType.SelfClosing:
  216. int count = stack.Count;
  217. if (!ProcessTag (tagid, attributes)) {
  218. TextParsed (location, location.PlainText);
  219. } else if (stack.Count != count) {
  220. CloseControl (tagid);
  221. }
  222. break;
  223. case TagType.DataBinding:
  224. goto case TagType.CodeRender;
  225. case TagType.CodeRenderExpression:
  226. goto case TagType.CodeRender;
  227. case TagType.CodeRender:
  228. ProcessCode (tagtype, tagid, location);
  229. break;
  230. case TagType.Include:
  231. string file = attributes ["virtual"] as string;
  232. bool isvirtual = (file != null);
  233. if (!isvirtual)
  234. file = attributes ["file"] as string;
  235. if (isvirtual) {
  236. file = tparser.MapPath (file);
  237. } else if (!Path.IsPathRooted (file)) {
  238. file = UrlUtils.Combine (tparser.BaseVirtualDir, file);
  239. }
  240. InitParser (file);
  241. DoParse ();
  242. break;
  243. default:
  244. break;
  245. }
  246. //PrintLocation (location);
  247. }
  248. void TextParsed (ILocation location, string text)
  249. {
  250. if (text.IndexOf ("<%") != -1 && !inScript && !javascript) {
  251. if (this.text.Length > 0)
  252. FlushText ();
  253. CodeRenderParser r = new CodeRenderParser (text, stack.Builder);
  254. r.AddChildren ();
  255. return;
  256. }
  257. this.text.Append (text);
  258. //PrintLocation (location);
  259. }
  260. void FlushText ()
  261. {
  262. string t = text.ToString ();
  263. text.Length = 0;
  264. if (inScript) {
  265. // TODO: store location
  266. tparser.Scripts.Add (t);
  267. return;
  268. }
  269. if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
  270. throw new ParseException (location, "Content not valid for application file.");
  271. stack.Builder.AppendLiteralString (t);
  272. }
  273. bool ProcessTag (string tagid, TagAttributes atts)
  274. {
  275. if ((atts == null || !atts.IsRunAtServer ()) && String.Compare (tagid, "tbody", true) == 0) {
  276. // MS completely ignores tbody or, if runat="server", fails when compiling
  277. if (stack.Count > 0)
  278. return stack.Builder.ChildrenAsProperties;
  279. return false;
  280. }
  281. ControlBuilder parent = stack.Builder;
  282. ControlBuilder builder = null;
  283. BuilderLocation bl = null;
  284. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  285. if (stack.Count > 1) {
  286. try {
  287. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  288. } catch (TypeLoadException e) {
  289. throw new ParseException (Location, "Type not found.", e);
  290. } catch (Exception e) {
  291. throw new ParseException (Location, e.Message, e);
  292. }
  293. }
  294. if (builder == null && atts != null && atts.IsRunAtServer ()) {
  295. string id = htable ["id"] as string;
  296. if (id != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (id))
  297. throw new ParseException (Location, "'" + id + "' is not a valid identifier");
  298. try {
  299. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  300. } catch (TypeLoadException e) {
  301. throw new ParseException (Location, "Type not found.", e);
  302. } catch (Exception e) {
  303. throw new ParseException (Location, e.Message, e);
  304. }
  305. }
  306. if (builder == null)
  307. return false;
  308. builder.location = location;
  309. builder.ID = htable ["id"] as string;
  310. if (builder.HasBody ()) {
  311. if (builder is TemplateBuilder) {
  312. // push the id list
  313. }
  314. stack.Push (builder, location);
  315. } else {
  316. // FIXME:ObjectTags...
  317. parent.AppendSubBuilder (builder);
  318. builder.CloseControl ();
  319. }
  320. return true;
  321. }
  322. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  323. {
  324. if (tagtype != TagType.Close) {
  325. if (attributes != null && attributes.IsRunAtServer ()) {
  326. CheckLanguage ((string) attributes ["language"]);
  327. if (tagtype == TagType.Tag) {
  328. Parser.VerbatimID = "script";
  329. inScript = true;
  330. } //else if (tagtype == TagType.SelfClosing)
  331. // load script file here
  332. return true;
  333. } else {
  334. Parser.VerbatimID = "script";
  335. javascript = true;
  336. TextParsed (location, location.PlainText);
  337. return true;
  338. }
  339. }
  340. bool result;
  341. if (inScript) {
  342. result = inScript;
  343. inScript = false;
  344. } else {
  345. result = javascript;
  346. javascript = false;
  347. TextParsed (location, location.PlainText);
  348. }
  349. return result;
  350. }
  351. bool CloseControl (string tagid)
  352. {
  353. ControlBuilder current = stack.Builder;
  354. if (String.Compare (tagid, "tbody", true) == 0) {
  355. if (!current.ChildrenAsProperties) {
  356. try {
  357. TextParsed (location, location.PlainText);
  358. FlushText ();
  359. } catch {}
  360. }
  361. return true;
  362. }
  363. string btag = current.TagName;
  364. if (0 != String.Compare (tagid, btag, true))
  365. return false;
  366. // if (current is TemplateBuilder)
  367. // pop from the id list
  368. current.CloseControl ();
  369. stack.Pop ();
  370. stack.Builder.AppendSubBuilder (current);
  371. return true;
  372. }
  373. bool ProcessCode (TagType tagtype, string code, ILocation location)
  374. {
  375. ControlBuilder b = null;
  376. if (tagtype == TagType.CodeRender)
  377. b = new CodeRenderBuilder (code, false, location);
  378. else if (tagtype == TagType.CodeRenderExpression)
  379. b = new CodeRenderBuilder (code, true, location);
  380. else if (tagtype == TagType.DataBinding)
  381. b = new DataBindingBuilder (code, location);
  382. else
  383. throw new HttpException ("Should never happen");
  384. stack.Builder.AppendSubBuilder (b);
  385. return true;
  386. }
  387. public ILocation Location {
  388. get { return location; }
  389. }
  390. void CheckLanguage (string lang)
  391. {
  392. if (lang == null || lang == "")
  393. return;
  394. if (String.Compare (lang, tparser.Language, true) != 0) {
  395. throw new ParseException (Location,
  396. String.Format ("Trying to mix language '{0}' and '{1}'.",
  397. tparser.Language, lang));
  398. }
  399. }
  400. // Used to get CodeRender tags in attribute values
  401. class CodeRenderParser
  402. {
  403. string str;
  404. ControlBuilder builder;
  405. public CodeRenderParser (string str, ControlBuilder builder)
  406. {
  407. this.str = str;
  408. this.builder = builder;
  409. }
  410. public void AddChildren ()
  411. {
  412. int index = str.IndexOf ("<%");
  413. if (index > 0) {
  414. TextParsed (null, str.Substring (0, index));
  415. str = str.Substring (index);
  416. }
  417. AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
  418. parser.Error += new ParseErrorHandler (ParseError);
  419. parser.TagParsed += new TagParsedHandler (TagParsed);
  420. parser.TextParsed += new TextParsedHandler (TextParsed);
  421. parser.Parse ();
  422. }
  423. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  424. {
  425. if (tagtype == TagType.CodeRender)
  426. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
  427. else if (tagtype == TagType.CodeRenderExpression)
  428. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
  429. else if (tagtype == TagType.DataBinding)
  430. builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
  431. else
  432. builder.AppendLiteralString (location.PlainText);
  433. }
  434. void TextParsed (ILocation location, string text)
  435. {
  436. builder.AppendLiteralString (text);
  437. }
  438. void ParseError (ILocation location, string message)
  439. {
  440. throw new ParseException (location, message);
  441. }
  442. }
  443. }
  444. }