AspGenerator.cs 12 KB

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