2
0

AspGenerator.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. namespace System.Web.Compilation
  15. {
  16. class BuilderLocation
  17. {
  18. public ControlBuilder Builder;
  19. public ILocation Location;
  20. public BuilderLocation (ControlBuilder builder, ILocation location)
  21. {
  22. this.Builder = builder;
  23. this.Location = location;
  24. }
  25. }
  26. class BuilderLocationStack : Stack
  27. {
  28. public override void Push (object o)
  29. {
  30. if (!(o is BuilderLocation))
  31. throw new InvalidOperationException ();
  32. base.Push (o);
  33. }
  34. public virtual void Push (ControlBuilder builder, ILocation location)
  35. {
  36. BuilderLocation bl = new BuilderLocation (builder, location);
  37. Push (bl);
  38. }
  39. public new BuilderLocation Peek ()
  40. {
  41. return (BuilderLocation) base.Peek ();
  42. }
  43. public new BuilderLocation Pop ()
  44. {
  45. return (BuilderLocation) base.Pop ();
  46. }
  47. public ControlBuilder Builder {
  48. get { return Peek ().Builder; }
  49. }
  50. }
  51. class AspGenerator
  52. {
  53. string filename;
  54. AspParser parser;
  55. BuilderLocationStack stack;
  56. TemplateParser tparser;
  57. StringBuilder text;
  58. RootBuilder rootBuilder;
  59. bool inScript;
  60. ILocation location;
  61. static Hashtable emptyHash = new Hashtable ();
  62. public AspGenerator (TemplateParser tparser)
  63. {
  64. this.tparser = tparser;
  65. this.filename = Path.GetFullPath (tparser.InputFile);
  66. tparser.AddDependency (tparser.InputFile);
  67. text = new StringBuilder ();
  68. stack = new BuilderLocationStack ();
  69. rootBuilder = new RootBuilder (tparser);
  70. stack.Push (rootBuilder, null);
  71. tparser.RootBuilder = rootBuilder;
  72. }
  73. BaseCompiler GetCompilerFromType ()
  74. {
  75. Type type = tparser.GetType ();
  76. if (type == typeof (PageParser))
  77. return new PageCompiler ((PageParser) tparser);
  78. if (type == typeof (ApplicationFileParser))
  79. return new GlobalAsaxCompiler ((ApplicationFileParser) tparser);
  80. if (type == typeof (UserControlParser))
  81. return new UserControlCompiler ((UserControlParser) tparser);
  82. throw new Exception ("Got type: " + type);
  83. }
  84. public Type GetCompiledType ()
  85. {
  86. //FIXME: use the encoding of the file or the one specified in the machine.config/web.config file.
  87. StreamReader reader = new StreamReader (filename, Encoding.Default);
  88. parser = new AspParser (filename, reader);
  89. parser.Error += new ParseErrorHandler (ParseError);
  90. parser.TagParsed += new TagParsedHandler (TagParsed);
  91. parser.TextParsed += new TextParsedHandler (TextParsed);
  92. parser.Parse ();
  93. if (text.Length > 0)
  94. FlushText ();
  95. #if DEBUG
  96. PrintTree (rootBuilder, 0);
  97. #endif
  98. if (stack.Count > 1)
  99. throw new ParseException (stack.Builder.location,
  100. "Expecting </" + stack.Builder.TagName + ">" + stack.Builder);
  101. BaseCompiler compiler = GetCompilerFromType ();
  102. return compiler.GetCompiledType ();
  103. }
  104. #if DEBUG
  105. static void PrintTree (ControlBuilder builder, int indent)
  106. {
  107. if (builder == null)
  108. return;
  109. string i = new string ('\t', indent);
  110. Console.Write (i);
  111. Console.WriteLine ("b: {0} id: {1} type: {2} parent: {3}",
  112. builder, builder.ID, builder.ControlType, builder.parentBuilder);
  113. if (builder.Children != null)
  114. foreach (object o in builder.Children) {
  115. if (o is ControlBuilder)
  116. PrintTree ((ControlBuilder) o, indent++);
  117. }
  118. }
  119. #endif
  120. static void PrintLocation (ILocation loc)
  121. {
  122. Console.WriteLine ("\tFile name: " + loc.Filename);
  123. Console.WriteLine ("\tBegin line: " + loc.BeginLine);
  124. Console.WriteLine ("\tEnd line: " + loc.EndLine);
  125. Console.WriteLine ("\tBegin column: " + loc.BeginColumn);
  126. Console.WriteLine ("\tEnd column: " + loc.EndColumn);
  127. Console.WriteLine ("\tPlainText: " + loc.PlainText);
  128. Console.WriteLine ();
  129. }
  130. void ParseError (ILocation location, string message)
  131. {
  132. throw new ParseException (location, message);
  133. }
  134. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  135. {
  136. this.location = new Location (location);
  137. if (tparser != null)
  138. tparser.Location = location;
  139. if (text.Length != 0)
  140. FlushText ();
  141. if (0 == String.Compare (tagid, "script", true)) {
  142. if (ProcessScript (tagtype, attributes))
  143. return;
  144. }
  145. switch (tagtype) {
  146. case TagType.Directive:
  147. if (tagid == "")
  148. tagid = tparser.DefaultDirectiveName;
  149. tparser.AddDirective (tagid, attributes.GetDictionary (null));
  150. break;
  151. case TagType.Tag:
  152. if (!ProcessTag (tagid, attributes))
  153. TextParsed (location, location.PlainText);
  154. break;
  155. case TagType.Close:
  156. if (!CloseControl (tagid))
  157. TextParsed (location, location.PlainText);
  158. break;
  159. case TagType.SelfClosing:
  160. int count = stack.Count;
  161. if (!ProcessTag (tagid, attributes)) {
  162. TextParsed (location, location.PlainText);
  163. } else if (stack.Count != count) {
  164. CloseControl (tagid);
  165. }
  166. break;
  167. case TagType.DataBinding:
  168. goto case TagType.CodeRender;
  169. case TagType.CodeRenderExpression:
  170. goto case TagType.CodeRender;
  171. case TagType.CodeRender:
  172. ProcessCode (tagtype, tagid, location);
  173. break;
  174. case TagType.Include:
  175. string file = attributes ["virtual"] as string;
  176. bool isvirtual = (file != null);
  177. if (!isvirtual)
  178. file = attributes ["file"] as string;
  179. TextParsed (location, tparser.ProcessInclude (isvirtual, file));
  180. break;
  181. default:
  182. break;
  183. }
  184. //PrintLocation (location);
  185. }
  186. void TextParsed (ILocation location, string text)
  187. {
  188. if (text.IndexOf ("<%") != -1) {
  189. if (this.text.Length > 0)
  190. FlushText ();
  191. CodeRenderParser r = new CodeRenderParser (text, stack.Builder);
  192. r.AddChildren ();
  193. return;
  194. }
  195. this.text.Append (text);
  196. //PrintLocation (location);
  197. }
  198. void FlushText ()
  199. {
  200. string t = text.ToString ();
  201. text.Length = 0;
  202. if (inScript) {
  203. // TODO: store location
  204. tparser.Scripts.Add (t);
  205. return;
  206. }
  207. if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
  208. throw new ParseException (location, "Content not valid for application file.");
  209. stack.Builder.AppendLiteralString (t);
  210. }
  211. bool ProcessTag (string tagid, TagAttributes atts)
  212. {
  213. ControlBuilder parent = stack.Builder;
  214. ControlBuilder builder = null;
  215. BuilderLocation bl = null;
  216. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  217. if (stack.Count > 1)
  218. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  219. if (builder == null && atts != null && atts.IsRunAtServer ())
  220. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  221. if (builder == null)
  222. return false;
  223. builder.location = location;
  224. builder.ID = htable ["id"] as string;
  225. if (builder.HasBody ()) {
  226. if (builder is TemplateBuilder) {
  227. // push the id list
  228. }
  229. stack.Push (builder, location);
  230. } else {
  231. // FIXME:ObjectTags...
  232. parent.AppendSubBuilder (builder);
  233. builder.CloseControl ();
  234. }
  235. return true;
  236. }
  237. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  238. {
  239. if (tagtype != TagType.Close && attributes != null && attributes.IsRunAtServer ()) {
  240. if (tagtype == TagType.Tag) {
  241. parser.VerbatimID = "script";
  242. inScript = true;
  243. } //else if (tagtype == TagType.SelfClosing)
  244. // load script file here
  245. return true;
  246. }
  247. bool result = inScript;
  248. inScript = false;
  249. return result;
  250. }
  251. bool CloseControl (string tagid)
  252. {
  253. ControlBuilder current = stack.Builder;
  254. string btag = current.TagName;
  255. if (0 != String.Compare (tagid, btag, true))
  256. return false;
  257. // if (current is TemplateBuilder)
  258. // pop from the id list
  259. current.CloseControl ();
  260. stack.Pop ();
  261. stack.Builder.AppendSubBuilder (current);
  262. return true;
  263. }
  264. bool ProcessCode (TagType tagtype, string code, ILocation location)
  265. {
  266. ControlBuilder b = null;
  267. if (tagtype == TagType.CodeRender)
  268. b = new CodeRenderBuilder (code, false, location);
  269. else if (tagtype == TagType.CodeRenderExpression)
  270. b = new CodeRenderBuilder (code, true, location);
  271. else if (tagtype == TagType.DataBinding)
  272. b = new DataBindingBuilder (code, location);
  273. else
  274. throw new HttpException ("Should never happen");
  275. stack.Builder.AppendSubBuilder (b);
  276. return true;
  277. }
  278. public ILocation Location {
  279. get { return location; }
  280. }
  281. // Used to get CodeRender tags in attribute values
  282. class CodeRenderParser
  283. {
  284. string str;
  285. ControlBuilder builder;
  286. public CodeRenderParser (string str, ControlBuilder builder)
  287. {
  288. this.str = str;
  289. this.builder = builder;
  290. }
  291. public void AddChildren ()
  292. {
  293. int index = str.IndexOf ("<%");
  294. if (index > 0) {
  295. TextParsed (null, str.Substring (0, index));
  296. str = str.Substring (index);
  297. }
  298. AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
  299. parser.Error += new ParseErrorHandler (ParseError);
  300. parser.TagParsed += new TagParsedHandler (TagParsed);
  301. parser.TextParsed += new TextParsedHandler (TextParsed);
  302. parser.Parse ();
  303. }
  304. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  305. {
  306. if (tagtype == TagType.CodeRender)
  307. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
  308. else if (tagtype == TagType.CodeRenderExpression)
  309. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
  310. else if (tagtype == TagType.DataBinding)
  311. builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
  312. else
  313. builder.AppendLiteralString (location.PlainText);
  314. }
  315. void TextParsed (ILocation location, string text)
  316. {
  317. builder.AppendLiteralString (text);
  318. }
  319. void ParseError (ILocation location, string message)
  320. {
  321. throw new ParseException (location, message);
  322. }
  323. }
  324. }
  325. }