AspGenerator.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. reader.Close ();
  134. parser.Error += new ParseErrorHandler (ParseError);
  135. parser.TagParsed += new TagParsedHandler (TagParsed);
  136. parser.TextParsed += new TextParsedHandler (TextParsed);
  137. if (!pstack.Push (parser))
  138. throw new ParseException (Location, "Infinite recursion detected including file: " + filename);
  139. tparser.AddDependency (filename);
  140. }
  141. void DoParse ()
  142. {
  143. pstack.Parser.Parse ();
  144. if (text.Length > 0)
  145. FlushText ();
  146. pstack.Pop ();
  147. }
  148. public Type GetCompiledType ()
  149. {
  150. InitParser (Path.GetFullPath (tparser.InputFile));
  151. DoParse ();
  152. #if DEBUG
  153. PrintTree (rootBuilder, 0);
  154. #endif
  155. if (stack.Count > 1)
  156. throw new ParseException (stack.Builder.location,
  157. "Expecting </" + stack.Builder.TagName + ">" + stack.Builder);
  158. BaseCompiler compiler = GetCompilerFromType ();
  159. return compiler.GetCompiledType ();
  160. }
  161. #if DEBUG
  162. static void PrintTree (ControlBuilder builder, int indent)
  163. {
  164. if (builder == null)
  165. return;
  166. string i = new string ('\t', indent);
  167. Console.Write (i);
  168. Console.WriteLine ("b: {0} id: {1} type: {2} parent: {3}",
  169. builder, builder.ID, builder.ControlType, builder.parentBuilder);
  170. if (builder.Children != null)
  171. foreach (object o in builder.Children) {
  172. if (o is ControlBuilder)
  173. PrintTree ((ControlBuilder) o, indent++);
  174. }
  175. }
  176. #endif
  177. static void PrintLocation (ILocation loc)
  178. {
  179. Console.WriteLine ("\tFile name: " + loc.Filename);
  180. Console.WriteLine ("\tBegin line: " + loc.BeginLine);
  181. Console.WriteLine ("\tEnd line: " + loc.EndLine);
  182. Console.WriteLine ("\tBegin column: " + loc.BeginColumn);
  183. Console.WriteLine ("\tEnd column: " + loc.EndColumn);
  184. Console.WriteLine ("\tPlainText: " + loc.PlainText);
  185. Console.WriteLine ();
  186. }
  187. void ParseError (ILocation location, string message)
  188. {
  189. throw new ParseException (location, message);
  190. }
  191. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  192. {
  193. this.location = new Location (location);
  194. if (tparser != null)
  195. tparser.Location = location;
  196. if (text.Length != 0)
  197. FlushText ();
  198. if (0 == String.Compare (tagid, "script", true)) {
  199. if (ProcessScript (tagtype, attributes))
  200. return;
  201. }
  202. switch (tagtype) {
  203. case TagType.Directive:
  204. if (tagid == "")
  205. tagid = tparser.DefaultDirectiveName;
  206. tparser.AddDirective (tagid, attributes.GetDictionary (null));
  207. break;
  208. case TagType.Tag:
  209. if (!ProcessTag (tagid, attributes))
  210. TextParsed (location, location.PlainText);
  211. break;
  212. case TagType.Close:
  213. if (!CloseControl (tagid))
  214. TextParsed (location, location.PlainText);
  215. break;
  216. case TagType.SelfClosing:
  217. int count = stack.Count;
  218. if (!ProcessTag (tagid, attributes)) {
  219. TextParsed (location, location.PlainText);
  220. } else if (stack.Count != count) {
  221. CloseControl (tagid);
  222. }
  223. break;
  224. case TagType.DataBinding:
  225. goto case TagType.CodeRender;
  226. case TagType.CodeRenderExpression:
  227. goto case TagType.CodeRender;
  228. case TagType.CodeRender:
  229. ProcessCode (tagtype, tagid, location);
  230. break;
  231. case TagType.Include:
  232. string file = attributes ["virtual"] as string;
  233. bool isvirtual = (file != null);
  234. if (!isvirtual)
  235. file = attributes ["file"] as string;
  236. if (isvirtual) {
  237. file = tparser.MapPath (file);
  238. } else if (!Path.IsPathRooted (file)) {
  239. file = UrlUtils.Combine (tparser.BaseVirtualDir, file);
  240. }
  241. InitParser (file);
  242. DoParse ();
  243. break;
  244. default:
  245. break;
  246. }
  247. //PrintLocation (location);
  248. }
  249. void TextParsed (ILocation location, string text)
  250. {
  251. if (text.IndexOf ("<%") != -1 && !inScript) {
  252. if (this.text.Length > 0)
  253. FlushText ();
  254. CodeRenderParser r = new CodeRenderParser (text, stack.Builder);
  255. r.AddChildren ();
  256. return;
  257. }
  258. this.text.Append (text);
  259. //PrintLocation (location);
  260. }
  261. void FlushText ()
  262. {
  263. string t = text.ToString ();
  264. text.Length = 0;
  265. if (inScript) {
  266. // TODO: store location
  267. tparser.Scripts.Add (t);
  268. return;
  269. }
  270. if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
  271. throw new ParseException (location, "Content not valid for application file.");
  272. stack.Builder.AppendLiteralString (t);
  273. }
  274. bool ProcessTag (string tagid, TagAttributes atts)
  275. {
  276. if ((atts == null || !atts.IsRunAtServer ()) && String.Compare (tagid, "tbody", true) == 0) {
  277. // MS completely ignores tbody or, if runat="server", fails when compiling
  278. if (stack.Count > 0)
  279. return stack.Builder.ChildrenAsProperties;
  280. return false;
  281. }
  282. ControlBuilder parent = stack.Builder;
  283. ControlBuilder builder = null;
  284. BuilderLocation bl = null;
  285. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  286. if (stack.Count > 1) {
  287. try {
  288. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  289. } catch (TypeLoadException e) {
  290. throw new ParseException (Location, "Type not found.", e);
  291. } catch (Exception e) {
  292. throw new ParseException (Location, e.Message, e);
  293. }
  294. }
  295. if (builder == null && atts != null && atts.IsRunAtServer ()) {
  296. string id = htable ["id"] as string;
  297. if (id != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (id))
  298. throw new ParseException (Location, "'" + id + "' is not a valid identifier");
  299. try {
  300. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  301. } catch (TypeLoadException e) {
  302. throw new ParseException (Location, "Type not found.", e);
  303. } catch (Exception e) {
  304. throw new ParseException (Location, e.Message, e);
  305. }
  306. }
  307. if (builder == null)
  308. return false;
  309. builder.location = location;
  310. builder.ID = htable ["id"] as string;
  311. if (builder.HasBody ()) {
  312. if (builder is TemplateBuilder) {
  313. // push the id list
  314. }
  315. stack.Push (builder, location);
  316. } else {
  317. // FIXME:ObjectTags...
  318. parent.AppendSubBuilder (builder);
  319. builder.CloseControl ();
  320. }
  321. return true;
  322. }
  323. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  324. {
  325. if (tagtype != TagType.Close) {
  326. if (attributes != null && attributes.IsRunAtServer ()) {
  327. CheckLanguage ((string) attributes ["language"]);
  328. if (tagtype == TagType.Tag) {
  329. Parser.VerbatimID = "script";
  330. inScript = true;
  331. } //else if (tagtype == TagType.SelfClosing)
  332. // load script file here
  333. return true;
  334. } else {
  335. Parser.VerbatimID = "script";
  336. javascript = true;
  337. TextParsed (location, location.PlainText);
  338. return true;
  339. }
  340. }
  341. bool result;
  342. if (inScript) {
  343. result = inScript;
  344. inScript = false;
  345. } else {
  346. result = javascript;
  347. javascript = false;
  348. TextParsed (location, location.PlainText);
  349. }
  350. return result;
  351. }
  352. bool CloseControl (string tagid)
  353. {
  354. ControlBuilder current = stack.Builder;
  355. if (String.Compare (tagid, "tbody", true) == 0) {
  356. if (!current.ChildrenAsProperties) {
  357. try {
  358. TextParsed (location, location.PlainText);
  359. FlushText ();
  360. } catch {}
  361. }
  362. return true;
  363. }
  364. string btag = current.TagName;
  365. if (0 != String.Compare (tagid, btag, true))
  366. return false;
  367. // if (current is TemplateBuilder)
  368. // pop from the id list
  369. current.CloseControl ();
  370. stack.Pop ();
  371. stack.Builder.AppendSubBuilder (current);
  372. return true;
  373. }
  374. bool ProcessCode (TagType tagtype, string code, ILocation location)
  375. {
  376. ControlBuilder b = null;
  377. if (tagtype == TagType.CodeRender)
  378. b = new CodeRenderBuilder (code, false, location);
  379. else if (tagtype == TagType.CodeRenderExpression)
  380. b = new CodeRenderBuilder (code, true, location);
  381. else if (tagtype == TagType.DataBinding)
  382. b = new DataBindingBuilder (code, location);
  383. else
  384. throw new HttpException ("Should never happen");
  385. stack.Builder.AppendSubBuilder (b);
  386. return true;
  387. }
  388. public ILocation Location {
  389. get { return location; }
  390. }
  391. void CheckLanguage (string lang)
  392. {
  393. if (lang == null || lang == "")
  394. return;
  395. if (String.Compare (lang, tparser.Language, true) != 0) {
  396. throw new ParseException (Location,
  397. String.Format ("Trying to mix language '{0}' and '{1}'.",
  398. tparser.Language, lang));
  399. }
  400. }
  401. // Used to get CodeRender tags in attribute values
  402. class CodeRenderParser
  403. {
  404. string str;
  405. ControlBuilder builder;
  406. public CodeRenderParser (string str, ControlBuilder builder)
  407. {
  408. this.str = str;
  409. this.builder = builder;
  410. }
  411. public void AddChildren ()
  412. {
  413. int index = str.IndexOf ("<%");
  414. if (index > 0) {
  415. TextParsed (null, str.Substring (0, index));
  416. str = str.Substring (index);
  417. }
  418. AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
  419. parser.Error += new ParseErrorHandler (ParseError);
  420. parser.TagParsed += new TagParsedHandler (TagParsed);
  421. parser.TextParsed += new TextParsedHandler (TextParsed);
  422. parser.Parse ();
  423. }
  424. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  425. {
  426. if (tagtype == TagType.CodeRender)
  427. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
  428. else if (tagtype == TagType.CodeRenderExpression)
  429. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
  430. else if (tagtype == TagType.DataBinding)
  431. builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
  432. else
  433. builder.AppendLiteralString (location.PlainText);
  434. }
  435. void TextParsed (ILocation location, string text)
  436. {
  437. builder.AppendLiteralString (text);
  438. }
  439. void ParseError (ILocation location, string message)
  440. {
  441. throw new ParseException (location, message);
  442. }
  443. }
  444. }
  445. }