AspGenerator.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. ControlBuilder parent = stack.Builder;
  276. ControlBuilder builder = null;
  277. BuilderLocation bl = null;
  278. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  279. if (stack.Count > 1) {
  280. try {
  281. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  282. } catch (TypeLoadException e) {
  283. throw new ParseException (Location, "Type not found.", e);
  284. } catch (Exception e) {
  285. throw new ParseException (Location, e.Message, e);
  286. }
  287. }
  288. if (builder == null && atts != null && atts.IsRunAtServer ()) {
  289. string id = htable ["id"] as string;
  290. if (id != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (id))
  291. throw new ParseException (Location, "'" + id + "' is not a valid identifier");
  292. try {
  293. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  294. } catch (TypeLoadException e) {
  295. throw new ParseException (Location, "Type not found.", e);
  296. } catch (Exception e) {
  297. throw new ParseException (Location, e.Message, e);
  298. }
  299. }
  300. if (builder == null)
  301. return false;
  302. builder.location = location;
  303. builder.ID = htable ["id"] as string;
  304. if (builder.HasBody ()) {
  305. if (builder is TemplateBuilder) {
  306. // push the id list
  307. }
  308. stack.Push (builder, location);
  309. } else {
  310. // FIXME:ObjectTags...
  311. parent.AppendSubBuilder (builder);
  312. builder.CloseControl ();
  313. }
  314. return true;
  315. }
  316. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  317. {
  318. if (tagtype != TagType.Close) {
  319. if (attributes != null && attributes.IsRunAtServer ()) {
  320. CheckLanguage ((string) attributes ["language"]);
  321. if (tagtype == TagType.Tag) {
  322. Parser.VerbatimID = "script";
  323. inScript = true;
  324. } //else if (tagtype == TagType.SelfClosing)
  325. // load script file here
  326. return true;
  327. } else {
  328. Parser.VerbatimID = "script";
  329. javascript = true;
  330. TextParsed (location, location.PlainText);
  331. return true;
  332. }
  333. }
  334. bool result;
  335. if (inScript) {
  336. result = inScript;
  337. inScript = false;
  338. } else {
  339. result = javascript;
  340. javascript = false;
  341. TextParsed (location, location.PlainText);
  342. }
  343. return result;
  344. }
  345. bool CloseControl (string tagid)
  346. {
  347. ControlBuilder current = stack.Builder;
  348. string btag = current.TagName;
  349. if (0 != String.Compare (tagid, btag, true))
  350. return false;
  351. // if (current is TemplateBuilder)
  352. // pop from the id list
  353. current.CloseControl ();
  354. stack.Pop ();
  355. stack.Builder.AppendSubBuilder (current);
  356. return true;
  357. }
  358. bool ProcessCode (TagType tagtype, string code, ILocation location)
  359. {
  360. ControlBuilder b = null;
  361. if (tagtype == TagType.CodeRender)
  362. b = new CodeRenderBuilder (code, false, location);
  363. else if (tagtype == TagType.CodeRenderExpression)
  364. b = new CodeRenderBuilder (code, true, location);
  365. else if (tagtype == TagType.DataBinding)
  366. b = new DataBindingBuilder (code, location);
  367. else
  368. throw new HttpException ("Should never happen");
  369. stack.Builder.AppendSubBuilder (b);
  370. return true;
  371. }
  372. public ILocation Location {
  373. get { return location; }
  374. }
  375. void CheckLanguage (string lang)
  376. {
  377. if (lang == null || lang == "")
  378. return;
  379. if (String.Compare (lang, tparser.Language, true) != 0) {
  380. throw new ParseException (Location,
  381. String.Format ("Trying to mix language '{0}' and '{1}'.",
  382. tparser.Language, lang));
  383. }
  384. }
  385. // Used to get CodeRender tags in attribute values
  386. class CodeRenderParser
  387. {
  388. string str;
  389. ControlBuilder builder;
  390. public CodeRenderParser (string str, ControlBuilder builder)
  391. {
  392. this.str = str;
  393. this.builder = builder;
  394. }
  395. public void AddChildren ()
  396. {
  397. int index = str.IndexOf ("<%");
  398. if (index > 0) {
  399. TextParsed (null, str.Substring (0, index));
  400. str = str.Substring (index);
  401. }
  402. AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
  403. parser.Error += new ParseErrorHandler (ParseError);
  404. parser.TagParsed += new TagParsedHandler (TagParsed);
  405. parser.TextParsed += new TextParsedHandler (TextParsed);
  406. parser.Parse ();
  407. }
  408. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  409. {
  410. if (tagtype == TagType.CodeRender)
  411. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
  412. else if (tagtype == TagType.CodeRenderExpression)
  413. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
  414. else if (tagtype == TagType.DataBinding)
  415. builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
  416. else
  417. builder.AppendLiteralString (location.PlainText);
  418. }
  419. void TextParsed (ILocation location, string text)
  420. {
  421. builder.AppendLiteralString (text);
  422. }
  423. void ParseError (ILocation location, string message)
  424. {
  425. throw new ParseException (location, message);
  426. }
  427. }
  428. }
  429. }