AspGenerator.cs 11 KB

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