AspGenerator.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 = 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. default:
  175. break;
  176. }
  177. //PrintLocation (location);
  178. }
  179. void TextParsed (ILocation location, string text)
  180. {
  181. if (text.IndexOf ("<%") != -1) {
  182. if (this.text.Length > 0)
  183. FlushText ();
  184. CodeRenderParser r = new CodeRenderParser (text, stack.Builder);
  185. r.AddChildren ();
  186. return;
  187. }
  188. this.text.Append (text);
  189. //PrintLocation (location);
  190. }
  191. void FlushText ()
  192. {
  193. string t = text.ToString ();
  194. text.Length = 0;
  195. if (inScript) {
  196. // TODO: store location
  197. tparser.Scripts.Add (t);
  198. return;
  199. }
  200. if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
  201. throw new ParseException (location, "Content not valid for application file.");
  202. stack.Builder.AppendLiteralString (t);
  203. }
  204. bool ProcessTag (string tagid, TagAttributes atts)
  205. {
  206. ControlBuilder parent = stack.Builder;
  207. ControlBuilder builder = null;
  208. BuilderLocation bl = null;
  209. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  210. if (stack.Count > 1)
  211. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  212. if (builder == null && atts != null && atts.IsRunAtServer ())
  213. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  214. if (builder == null)
  215. return false;
  216. builder.location = location;
  217. builder.ID = htable ["id"] as string;
  218. if (builder.HasBody ()) {
  219. if (builder is TemplateBuilder) {
  220. // push the id list
  221. }
  222. stack.Push (builder, location);
  223. } else {
  224. // FIXME:ObjectTags...
  225. parent.AppendSubBuilder (builder);
  226. builder.CloseControl ();
  227. }
  228. return true;
  229. }
  230. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  231. {
  232. if (tagtype != TagType.Close && attributes != null && attributes.IsRunAtServer ()) {
  233. if (tagtype == TagType.Tag) {
  234. parser.VerbatimID = "script";
  235. inScript = true;
  236. } //else if (tagtype == TagType.SelfClosing)
  237. // load script file here
  238. return true;
  239. }
  240. bool result = inScript;
  241. inScript = false;
  242. return result;
  243. }
  244. bool CloseControl (string tagid)
  245. {
  246. ControlBuilder current = stack.Builder;
  247. string btag = current.TagName;
  248. if (0 != String.Compare (tagid, btag, true))
  249. return false;
  250. // if (current is TemplateBuilder)
  251. // pop from the id list
  252. current.CloseControl ();
  253. stack.Pop ();
  254. stack.Builder.AppendSubBuilder (current);
  255. return true;
  256. }
  257. bool ProcessCode (TagType tagtype, string code, ILocation location)
  258. {
  259. ControlBuilder b = null;
  260. if (tagtype == TagType.CodeRender)
  261. b = new CodeRenderBuilder (code, false, location);
  262. else if (tagtype == TagType.CodeRenderExpression)
  263. b = new CodeRenderBuilder (code, true, location);
  264. else if (tagtype == TagType.DataBinding)
  265. b = new DataBindingBuilder (code, location);
  266. else
  267. throw new HttpException ("Should never happen");
  268. stack.Builder.AppendSubBuilder (b);
  269. return true;
  270. }
  271. public ILocation Location {
  272. get { return location; }
  273. }
  274. // Used to get CodeRender tags in attribute values
  275. class CodeRenderParser
  276. {
  277. string str;
  278. ControlBuilder builder;
  279. public CodeRenderParser (string str, ControlBuilder builder)
  280. {
  281. this.str = str;
  282. this.builder = builder;
  283. }
  284. public void AddChildren ()
  285. {
  286. int index = str.IndexOf ("<%");
  287. if (index > 0) {
  288. TextParsed (null, str.Substring (0, index));
  289. str = str.Substring (index);
  290. }
  291. AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
  292. parser.Error += new ParseErrorHandler (ParseError);
  293. parser.TagParsed += new TagParsedHandler (TagParsed);
  294. parser.TextParsed += new TextParsedHandler (TextParsed);
  295. parser.Parse ();
  296. }
  297. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  298. {
  299. if (tagtype == TagType.CodeRender)
  300. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
  301. else if (tagtype == TagType.CodeRenderExpression)
  302. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
  303. else if (tagtype == TagType.DataBinding)
  304. builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
  305. else
  306. builder.AppendLiteralString (location.PlainText);
  307. }
  308. void TextParsed (ILocation location, string text)
  309. {
  310. builder.AppendLiteralString (text);
  311. }
  312. void ParseError (ILocation location, string message)
  313. {
  314. throw new ParseException (location, message);
  315. }
  316. }
  317. }
  318. }