2
0

AspGenerator.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 (stack.Count > 1)
  96. throw new ParseException (stack.Builder.location,
  97. "Expecting </" + stack.Builder.TagName + ">" + stack.Builder);
  98. BaseCompiler compiler = GetCompilerFromType ();
  99. /*
  100. IndentedTextWriter tw = new IndentedTextWriter (Console.Out, " ");
  101. generator.GenerateCodeFromCompileUnit (unit, tw, new CodeGeneratorOptions());
  102. tw.Close ();
  103. */
  104. return compiler.GetCompiledType ();
  105. }
  106. static void PrintLocation (ILocation loc)
  107. {
  108. Console.WriteLine ("\tFile name: " + loc.Filename);
  109. Console.WriteLine ("\tBegin line: " + loc.BeginLine);
  110. Console.WriteLine ("\tEnd line: " + loc.EndLine);
  111. Console.WriteLine ("\tBegin column: " + loc.BeginColumn);
  112. Console.WriteLine ("\tEnd column: " + loc.EndColumn);
  113. Console.WriteLine ("\tPlainText: " + loc.PlainText);
  114. Console.WriteLine ();
  115. }
  116. void ParseError (ILocation location, string message)
  117. {
  118. throw new ParseException (location, message);
  119. }
  120. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  121. {
  122. this.location = location;
  123. if (tparser != null)
  124. tparser.Location = location;
  125. if (text.Length != 0)
  126. FlushText ();
  127. if (0 == String.Compare (tagid, "script", true)) {
  128. if (ProcessScript (tagtype, attributes))
  129. return;
  130. }
  131. switch (tagtype) {
  132. case TagType.Directive:
  133. if (tagid == "")
  134. tagid = tparser.DefaultDirectiveName;
  135. tparser.AddDirective (tagid, attributes.GetDictionary (null));
  136. break;
  137. case TagType.Tag:
  138. if (!ProcessTag (tagid, attributes))
  139. TextParsed (location, location.PlainText);
  140. break;
  141. case TagType.Close:
  142. if (!CloseControl (tagid))
  143. TextParsed (location, location.PlainText);
  144. break;
  145. case TagType.SelfClosing:
  146. int count = stack.Count;
  147. if (!ProcessTag (tagid, attributes)) {
  148. TextParsed (location, location.PlainText);
  149. } else if (stack.Count != count) {
  150. CloseControl (tagid);
  151. }
  152. break;
  153. case TagType.DataBinding:
  154. goto case TagType.CodeRender;
  155. case TagType.CodeRenderExpression:
  156. goto case TagType.CodeRender;
  157. case TagType.CodeRender:
  158. ProcessCode (tagtype, tagid, location);
  159. break;
  160. default:
  161. break;
  162. }
  163. //PrintLocation (location);
  164. }
  165. void TextParsed (ILocation location, string text)
  166. {
  167. this.text.Append (text);
  168. //PrintLocation (location);
  169. }
  170. void FlushText ()
  171. {
  172. string t = text.ToString ();
  173. text.Length = 0;
  174. if (inScript) {
  175. // TODO: store location
  176. tparser.Scripts.Add (t);
  177. return;
  178. }
  179. if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
  180. throw new ParseException (location, "Content not valid for application file.");
  181. stack.Builder.AppendLiteralString (t);
  182. }
  183. bool ProcessTag (string tagid, TagAttributes atts)
  184. {
  185. ControlBuilder parent = stack.Builder;
  186. ControlBuilder builder = null;
  187. BuilderLocation bl = null;
  188. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  189. if (stack.Count > 1)
  190. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  191. if (builder == null && atts != null && atts.IsRunAtServer ())
  192. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  193. if (builder == null)
  194. return false;
  195. builder.location = location;
  196. builder.ID = htable ["id"] as string;
  197. if (builder.HasBody ()) {
  198. if (builder is TemplateBuilder) {
  199. // push the id list
  200. }
  201. stack.Push (builder, location);
  202. } else {
  203. // FIXME:ObjectTags...
  204. parent.AppendSubBuilder (builder);
  205. builder.CloseControl ();
  206. }
  207. return true;
  208. }
  209. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  210. {
  211. if (tagtype != TagType.Close && attributes != null && attributes.IsRunAtServer ()) {
  212. if (tagtype == TagType.Tag) {
  213. parser.VerbatimID = "script";
  214. inScript = true;
  215. } //else if (tagtype == TagType.SelfClosing)
  216. // load script file here
  217. return true;
  218. }
  219. bool result = inScript;
  220. inScript = false;
  221. return result;
  222. }
  223. bool CloseControl (string tagid)
  224. {
  225. ControlBuilder current = stack.Builder;
  226. string btag = current.TagName;
  227. if (0 != String.Compare (tagid, btag, true))
  228. return false;
  229. // if (current is TemplateBuilder)
  230. // pop from the id list
  231. current.CloseControl ();
  232. stack.Pop ();
  233. stack.Builder.AppendSubBuilder (current);
  234. return true;
  235. }
  236. bool ProcessCode (TagType tagtype, string code, ILocation location)
  237. {
  238. ControlBuilder b = null;
  239. if (tagtype == TagType.CodeRender)
  240. b = new CodeRenderBuilder (code, false, location);
  241. else if (tagtype == TagType.CodeRenderExpression)
  242. b = new CodeRenderBuilder (code, true, location);
  243. else if (tagtype == TagType.DataBinding)
  244. b = new DataBindingBuilder (code, location);
  245. else
  246. throw new HttpException ("Should never happen");
  247. stack.Builder.AppendSubBuilder (b);
  248. return true;
  249. }
  250. public ILocation Location {
  251. get { return location; }
  252. }
  253. }
  254. }