AspGenerator.cs 12 KB

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