AspGenerator.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. // Copyright (c) 2004,2006 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.CodeDom.Compiler;
  33. using System.Globalization;
  34. using System.IO;
  35. using System.Text;
  36. using System.Web.Caching;
  37. using System.Web.Configuration;
  38. using System.Web.Hosting;
  39. using System.Web.UI;
  40. using System.Web.UI.HtmlControls;
  41. using System.Web.Util;
  42. namespace System.Web.Compilation
  43. {
  44. class BuilderLocation
  45. {
  46. public ControlBuilder Builder;
  47. public ILocation Location;
  48. public BuilderLocation (ControlBuilder builder, ILocation location)
  49. {
  50. this.Builder = builder;
  51. this.Location = location;
  52. }
  53. }
  54. class BuilderLocationStack : Stack
  55. {
  56. public override void Push (object o)
  57. {
  58. if (!(o is BuilderLocation))
  59. throw new InvalidOperationException ();
  60. base.Push (o);
  61. }
  62. public virtual void Push (ControlBuilder builder, ILocation location)
  63. {
  64. BuilderLocation bl = new BuilderLocation (builder, location);
  65. Push (bl);
  66. }
  67. public new BuilderLocation Peek ()
  68. {
  69. return (BuilderLocation) base.Peek ();
  70. }
  71. public new BuilderLocation Pop ()
  72. {
  73. return (BuilderLocation) base.Pop ();
  74. }
  75. public ControlBuilder Builder {
  76. get { return Peek ().Builder; }
  77. }
  78. }
  79. class ParserStack
  80. {
  81. Hashtable files;
  82. Stack parsers;
  83. AspParser current;
  84. public ParserStack ()
  85. {
  86. files = new Hashtable (); // may be this should be case sensitive for windows
  87. parsers = new Stack ();
  88. }
  89. public bool Push (AspParser parser)
  90. {
  91. if (files.Contains (parser.Filename))
  92. return false;
  93. files [parser.Filename] = true;
  94. parsers.Push (parser);
  95. current = parser;
  96. return true;
  97. }
  98. public AspParser Pop ()
  99. {
  100. if (parsers.Count == 0)
  101. return null;
  102. files.Remove (current.Filename);
  103. AspParser result = (AspParser) parsers.Pop ();
  104. if (parsers.Count > 0)
  105. current = (AspParser) parsers.Peek ();
  106. else
  107. current = null;
  108. return result;
  109. }
  110. public int Count {
  111. get { return parsers.Count; }
  112. }
  113. public AspParser Parser {
  114. get { return current; }
  115. }
  116. public string Filename {
  117. get { return current.Filename; }
  118. }
  119. }
  120. class TagStack
  121. {
  122. Stack tags;
  123. public TagStack ()
  124. {
  125. tags = new Stack ();
  126. }
  127. public void Push (string tagid)
  128. {
  129. tags.Push (tagid);
  130. }
  131. public string Pop ()
  132. {
  133. if (tags.Count == 0)
  134. return null;
  135. return (string) tags.Pop ();
  136. }
  137. public bool CompareTo (string tagid)
  138. {
  139. if (tags.Count == 0)
  140. return false;
  141. return 0 == String.Compare (tagid, (string) tags.Peek (), true, CultureInfo.InvariantCulture);
  142. }
  143. public int Count {
  144. get { return tags.Count; }
  145. }
  146. public string Current {
  147. get { return (string) tags.Peek (); }
  148. }
  149. }
  150. class AspGenerator
  151. {
  152. ParserStack pstack;
  153. BuilderLocationStack stack;
  154. TemplateParser tparser;
  155. StringBuilder text;
  156. RootBuilder rootBuilder;
  157. bool inScript, javascript, ignore_text;
  158. ILocation location;
  159. bool isApplication;
  160. StringBuilder tagInnerText = new StringBuilder ();
  161. static Hashtable emptyHash = new Hashtable ();
  162. bool inForm;
  163. bool useOtherTags;
  164. TagType lastTag;
  165. public AspGenerator (TemplateParser tparser)
  166. {
  167. this.tparser = tparser;
  168. text = new StringBuilder ();
  169. stack = new BuilderLocationStack ();
  170. rootBuilder = new RootBuilder (tparser);
  171. stack.Push (rootBuilder, null);
  172. tparser.RootBuilder = rootBuilder;
  173. pstack = new ParserStack ();
  174. }
  175. public RootBuilder RootBuilder {
  176. get { return tparser.RootBuilder; }
  177. }
  178. public AspParser Parser {
  179. get { return pstack.Parser; }
  180. }
  181. public string Filename {
  182. get { return pstack.Filename; }
  183. }
  184. BaseCompiler GetCompilerFromType ()
  185. {
  186. Type type = tparser.GetType ();
  187. if (type == typeof (PageParser))
  188. return new PageCompiler ((PageParser) tparser);
  189. if (type == typeof (ApplicationFileParser))
  190. return new GlobalAsaxCompiler ((ApplicationFileParser) tparser);
  191. if (type == typeof (UserControlParser))
  192. return new UserControlCompiler ((UserControlParser) tparser);
  193. #if NET_2_0
  194. if (type == typeof(MasterPageParser))
  195. return new MasterPageCompiler ((MasterPageParser) tparser);
  196. #endif
  197. throw new Exception ("Got type: " + type);
  198. }
  199. void InitParser (TextReader reader, string filename)
  200. {
  201. AspParser parser = new AspParser (filename, reader);
  202. parser.Error += new ParseErrorHandler (ParseError);
  203. parser.TagParsed += new TagParsedHandler (TagParsed);
  204. parser.TextParsed += new TextParsedHandler (TextParsed);
  205. if (!pstack.Push (parser))
  206. throw new ParseException (Location, "Infinite recursion detected including file: " + filename);
  207. if (filename != "@@inner_string@@") {
  208. string arvp = Path.Combine (tparser.BaseVirtualDir, Path.GetFileName (filename));
  209. if (VirtualPathUtility.IsAbsolute (arvp))
  210. arvp = VirtualPathUtility.ToAppRelative (arvp);
  211. tparser.AddDependency (arvp);
  212. }
  213. tparser.MD5Checksum = parser.MD5Checksum;
  214. }
  215. void InitParser (string filename)
  216. {
  217. StreamReader reader = new StreamReader (filename, WebEncoding.FileEncoding);
  218. InitParser (reader, filename);
  219. }
  220. public void Parse (string file)
  221. {
  222. #if ONLY_1_1
  223. Parse (file, true);
  224. #else
  225. Parse (file, false);
  226. #endif
  227. }
  228. public void Parse (TextReader reader, string filename, bool doInitParser)
  229. {
  230. try {
  231. isApplication = tparser.DefaultDirectiveName == "application";
  232. if (doInitParser)
  233. InitParser (reader, filename);
  234. pstack.Parser.Parse ();
  235. if (text.Length > 0)
  236. FlushText ();
  237. pstack.Pop ();
  238. #if DEBUG
  239. PrintTree (rootBuilder, 0);
  240. #endif
  241. if (stack.Count > 1 && pstack.Count == 0)
  242. throw new ParseException (stack.Builder.location,
  243. "Expecting </" + stack.Builder.TagName + "> " + stack.Builder);
  244. } finally {
  245. if (reader != null)
  246. reader.Close ();
  247. }
  248. }
  249. public void Parse (Stream stream, string filename, bool doInitParser)
  250. {
  251. Parse (new StreamReader (stream, WebEncoding.FileEncoding), filename, doInitParser);
  252. }
  253. public void Parse (string filename, bool doInitParser)
  254. {
  255. StreamReader reader = new StreamReader (filename, WebEncoding.FileEncoding);
  256. Parse (reader, filename, doInitParser);
  257. }
  258. public void Parse ()
  259. {
  260. #if NET_2_0
  261. string inputFile = tparser.InputFile;
  262. TextReader inputReader = tparser.Reader;
  263. try {
  264. if (String.IsNullOrEmpty (inputFile)) {
  265. StreamReader sr = inputReader as StreamReader;
  266. if (sr != null) {
  267. FileStream fr = sr.BaseStream as FileStream;
  268. if (fr != null)
  269. inputFile = fr.Name;
  270. }
  271. if (String.IsNullOrEmpty (inputFile))
  272. inputFile = "@@inner_string@@";
  273. }
  274. if (inputReader != null) {
  275. Parse (inputReader, inputFile, true);
  276. } else {
  277. if (String.IsNullOrEmpty (inputFile))
  278. throw new HttpException ("Parser input file is empty, cannot continue.");
  279. inputFile = Path.GetFullPath (inputFile);
  280. InitParser (inputFile);
  281. Parse (inputFile);
  282. }
  283. } finally {
  284. if (inputReader != null)
  285. inputReader.Close ();
  286. }
  287. #else
  288. Parse (Path.GetFullPath (tparser.InputFile));
  289. #endif
  290. }
  291. internal static void AddTypeToCache (ArrayList dependencies, string inputFile, Type type)
  292. {
  293. string [] deps = (string []) dependencies.ToArray (typeof (string));
  294. HttpContext ctx = HttpContext.Current;
  295. HttpRequest req = ctx != null ? ctx.Request : null;
  296. if (req == null)
  297. throw new HttpException ("No current context, cannot compile.");
  298. int depLength = deps.Length;
  299. for (int i = 0; i < deps.Length; i++)
  300. deps [i] = req.MapPath (deps [i]);
  301. HttpRuntime.InternalCache.Insert ("@@Type" + inputFile, type, new CacheDependency (deps));
  302. }
  303. public Type GetCompiledType ()
  304. {
  305. Type type = (Type) HttpRuntime.InternalCache.Get ("@@Type" + tparser.InputFile);
  306. if (type != null) {
  307. return type;
  308. }
  309. Parse ();
  310. BaseCompiler compiler = GetCompilerFromType ();
  311. type = compiler.GetCompiledType ();
  312. AddTypeToCache (tparser.Dependencies, tparser.InputFile, type);
  313. return type;
  314. }
  315. #if DEBUG
  316. static void PrintTree (ControlBuilder builder, int indent)
  317. {
  318. if (builder == null)
  319. return;
  320. string i = new string ('\t', indent);
  321. Console.Write (i);
  322. Console.WriteLine ("b: {0} id: {1} type: {2} parent: {3}",
  323. builder, builder.ID, builder.ControlType, builder.parentBuilder);
  324. if (builder.Children != null)
  325. foreach (object o in builder.Children) {
  326. if (o is ControlBuilder)
  327. PrintTree ((ControlBuilder) o, indent++);
  328. }
  329. }
  330. static void PrintLocation (ILocation loc)
  331. {
  332. Console.WriteLine ("\tFile name: " + loc.Filename);
  333. Console.WriteLine ("\tBegin line: " + loc.BeginLine);
  334. Console.WriteLine ("\tEnd line: " + loc.EndLine);
  335. Console.WriteLine ("\tBegin column: " + loc.BeginColumn);
  336. Console.WriteLine ("\tEnd column: " + loc.EndColumn);
  337. Console.WriteLine ("\tPlainText: " + loc.PlainText);
  338. Console.WriteLine ();
  339. }
  340. #endif
  341. void ParseError (ILocation location, string message)
  342. {
  343. throw new ParseException (location, message);
  344. }
  345. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  346. {
  347. this.location = new Location (location);
  348. if (tparser != null)
  349. tparser.Location = location;
  350. if (text.Length != 0)
  351. FlushText (lastTag == TagType.CodeRender);
  352. if (0 == String.Compare (tagid, "script", true, CultureInfo.InvariantCulture)) {
  353. bool in_script = (inScript || ignore_text);
  354. if (in_script || (tagtype != TagType.Close && attributes != null)) {
  355. if ((in_script || attributes.IsRunAtServer ()) && ProcessScript (tagtype, attributes))
  356. return;
  357. }
  358. }
  359. lastTag = tagtype;
  360. switch (tagtype) {
  361. case TagType.Directive:
  362. if (tagid == "")
  363. tagid = tparser.DefaultDirectiveName;
  364. tparser.AddDirective (tagid, attributes.GetDictionary (null));
  365. break;
  366. case TagType.Tag:
  367. if (ProcessTag (tagid, attributes, tagtype)) {
  368. useOtherTags = true;
  369. break;
  370. }
  371. if (useOtherTags) {
  372. stack.Builder.EnsureOtherTags ();
  373. stack.Builder.OtherTags.Add (tagid);
  374. }
  375. TextParsed (location, location.PlainText);
  376. break;
  377. case TagType.Close:
  378. bool notServer = (useOtherTags && TryRemoveTag (tagid, stack.Builder.OtherTags));
  379. if (!notServer && CloseControl (tagid))
  380. break;
  381. TextParsed (location, location.PlainText);
  382. break;
  383. case TagType.SelfClosing:
  384. int count = stack.Count;
  385. if (!ProcessTag (tagid, attributes, tagtype)) {
  386. TextParsed (location, location.PlainText);
  387. } else if (stack.Count != count) {
  388. CloseControl (tagid);
  389. }
  390. break;
  391. case TagType.DataBinding:
  392. goto case TagType.CodeRender;
  393. case TagType.CodeRenderExpression:
  394. goto case TagType.CodeRender;
  395. case TagType.CodeRender:
  396. lastTag = TagType.CodeRender;
  397. if (isApplication)
  398. throw new ParseException (location, "Invalid content for application file.");
  399. ProcessCode (tagtype, tagid, location);
  400. break;
  401. case TagType.Include:
  402. if (isApplication)
  403. throw new ParseException (location, "Invalid content for application file.");
  404. string file = attributes ["virtual"] as string;
  405. bool isvirtual = (file != null);
  406. if (!isvirtual)
  407. file = attributes ["file"] as string;
  408. if (isvirtual) {
  409. bool parsed = false;
  410. #if NET_2_0
  411. VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
  412. if (vpp.FileExists (file)) {
  413. VirtualFile vf = vpp.GetFile (file);
  414. if (vf != null) {
  415. Parse (vf.Open (), file, true);
  416. parsed = true;
  417. }
  418. }
  419. #endif
  420. if (!parsed)
  421. Parse (tparser.MapPath (file), true);
  422. } else {
  423. string includeFilePath = GetIncludeFilePath (tparser.ParserDir, file);
  424. tparser.PushIncludeDir (Path.GetDirectoryName (includeFilePath));
  425. try {
  426. Parse (includeFilePath, true);
  427. } finally {
  428. tparser.PopIncludeDir ();
  429. }
  430. }
  431. break;
  432. default:
  433. break;
  434. }
  435. //PrintLocation (location);
  436. }
  437. static bool TryRemoveTag (string tagid, ArrayList otags)
  438. {
  439. if (otags == null || otags.Count == 0)
  440. return false;
  441. for (int idx = otags.Count - 1; idx >= 0; idx--) {
  442. string otagid = (string) otags [idx];
  443. if (0 == String.Compare (tagid, otagid, true, CultureInfo.InvariantCulture)) {
  444. do {
  445. otags.RemoveAt (idx);
  446. } while (otags.Count - 1 >= idx);
  447. return true;
  448. }
  449. }
  450. return false;
  451. }
  452. static string GetIncludeFilePath (string basedir, string filename)
  453. {
  454. if (Path.DirectorySeparatorChar == '/')
  455. filename = filename.Replace ("\\", "/");
  456. return Path.GetFullPath (Path.Combine (basedir, filename));
  457. }
  458. void TextParsed (ILocation location, string text)
  459. {
  460. if (ignore_text)
  461. return;
  462. bool isComment = text.StartsWith ("<!--"
  463. #if NET_2_0
  464. , StringComparison.OrdinalIgnoreCase
  465. #endif
  466. );
  467. if (!isComment && text.IndexOf ("<%") != -1 && !inScript) {
  468. if (this.text.Length > 0)
  469. FlushText (true);
  470. CodeRenderParser r = new CodeRenderParser (text, stack.Builder);
  471. r.AddChildren ();
  472. return;
  473. }
  474. this.text.Append (text);
  475. //PrintLocation (location);
  476. }
  477. void FlushText ()
  478. {
  479. FlushText (false);
  480. }
  481. void FlushText (bool ignoreEmptyString)
  482. {
  483. string t = text.ToString ();
  484. text.Length = 0;
  485. if (ignoreEmptyString && t.Trim ().Length == 0)
  486. return;
  487. if (inScript) {
  488. tparser.Scripts.Add (new ServerSideScript (t, new System.Web.Compilation.Location (tparser.Location)));
  489. return;
  490. }
  491. if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
  492. throw new ParseException (location, "Content not valid for application file.");
  493. ControlBuilder current = stack.Builder;
  494. current.AppendLiteralString (t);
  495. if (current.NeedsTagInnerText ()) {
  496. tagInnerText.Append (t);
  497. }
  498. }
  499. #if NET_2_0
  500. bool BuilderHasOtherThan (Type type, ControlBuilder cb)
  501. {
  502. ArrayList al = cb.OtherTags;
  503. if (al != null && al.Count > 0)
  504. return true;
  505. al = cb.Children;
  506. if (al != null) {
  507. ControlBuilder tmp;
  508. foreach (object o in al) {
  509. if (o == null)
  510. continue;
  511. tmp = o as ControlBuilder;
  512. if (tmp == null) {
  513. string s = o as string;
  514. if (s != null && String.IsNullOrEmpty (s.Trim ()))
  515. continue;
  516. return true;
  517. }
  518. if (tmp is System.Web.UI.WebControls.ContentBuilderInternal)
  519. continue;
  520. if (!(tmp.ControlType is System.Web.UI.WebControls.Content))
  521. return true;
  522. }
  523. }
  524. return false;
  525. }
  526. bool OtherControlsAllowed (ControlBuilder cb)
  527. {
  528. if (cb == null)
  529. return true;
  530. if (!typeof (System.Web.UI.WebControls.Content).IsAssignableFrom (cb.ControlType))
  531. return true;
  532. if (BuilderHasOtherThan (typeof (System.Web.UI.WebControls.Content), rootBuilder))
  533. return false;
  534. return true;
  535. }
  536. #endif
  537. bool ProcessTag (string tagid, TagAttributes atts, TagType tagtype)
  538. {
  539. if (isApplication) {
  540. if (String.Compare (tagid, "object", true, CultureInfo.InvariantCulture) != 0)
  541. throw new ParseException (location, "Invalid tag for application file.");
  542. }
  543. ControlBuilder parent = stack.Builder;
  544. ControlBuilder builder = null;
  545. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  546. if (stack.Count > 1) {
  547. try {
  548. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  549. } catch (TypeLoadException e) {
  550. throw new ParseException (Location, "Type not found.", e);
  551. } catch (Exception e) {
  552. throw new ParseException (Location, e.Message, e);
  553. }
  554. }
  555. if (builder == null && atts != null && atts.IsRunAtServer ()) {
  556. string id = htable ["id"] as string;
  557. if (id != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (id))
  558. throw new ParseException (Location, "'" + id + "' is not a valid identifier");
  559. try {
  560. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  561. } catch (TypeLoadException e) {
  562. throw new ParseException (Location, "Type not found.", e);
  563. } catch (Exception e) {
  564. throw new ParseException (Location, e.Message, e);
  565. }
  566. }
  567. if (builder == null)
  568. return false;
  569. #if NET_2_0
  570. if (!OtherControlsAllowed (builder))
  571. throw new ParseException (Location, "Only Content controls are allowed directly in a content page that contains Content controls.");
  572. #endif
  573. builder.location = location;
  574. builder.ID = htable ["id"] as string;
  575. if (typeof (HtmlForm).IsAssignableFrom (builder.ControlType)) {
  576. if (inForm)
  577. throw new ParseException (location, "Only one <form> allowed.");
  578. inForm = true;
  579. }
  580. if (builder.HasBody () && !(builder is ObjectTagBuilder)) {
  581. if (builder is TemplateBuilder) {
  582. // push the id list
  583. }
  584. stack.Push (builder, location);
  585. } else {
  586. if (!isApplication && builder is ObjectTagBuilder) {
  587. ObjectTagBuilder ot = (ObjectTagBuilder) builder;
  588. if (ot.Scope != null && ot.Scope != "")
  589. throw new ParseException (location, "Scope not allowed here");
  590. if (tagtype == TagType.Tag) {
  591. stack.Push (builder, location);
  592. return true;
  593. }
  594. }
  595. parent.AppendSubBuilder (builder);
  596. builder.CloseControl ();
  597. }
  598. return true;
  599. }
  600. string ReadFile (string filename)
  601. {
  602. string realpath = tparser.MapPath (filename);
  603. using (StreamReader sr = new StreamReader (realpath, WebEncoding.FileEncoding)) {
  604. string content = sr.ReadToEnd ();
  605. return content;
  606. }
  607. }
  608. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  609. {
  610. if (tagtype != TagType.Close) {
  611. if (attributes != null && attributes.IsRunAtServer ()) {
  612. string language = (string) attributes ["language"];
  613. if (language != null && language.Length > 0 && tparser.ImplicitLanguage)
  614. tparser.SetLanguage (language);
  615. CheckLanguage (language);
  616. string src = (string) attributes ["src"];
  617. if (src != null) {
  618. if (src == "")
  619. throw new ParseException (Parser,
  620. "src cannot be an empty string");
  621. string content = ReadFile (src);
  622. inScript = true;
  623. TextParsed (Parser, content);
  624. FlushText ();
  625. inScript = false;
  626. if (tagtype != TagType.SelfClosing) {
  627. ignore_text = true;
  628. Parser.VerbatimID = "script";
  629. }
  630. } else if (tagtype == TagType.Tag) {
  631. Parser.VerbatimID = "script";
  632. inScript = true;
  633. }
  634. return true;
  635. } else {
  636. if (tagtype != TagType.SelfClosing) {
  637. Parser.VerbatimID = "script";
  638. javascript = true;
  639. }
  640. TextParsed (location, location.PlainText);
  641. return true;
  642. }
  643. }
  644. bool result;
  645. if (inScript) {
  646. result = inScript;
  647. inScript = false;
  648. } else if (!ignore_text) {
  649. result = javascript;
  650. javascript = false;
  651. TextParsed (location, location.PlainText);
  652. } else {
  653. ignore_text = false;
  654. result = true;
  655. }
  656. return result;
  657. }
  658. bool CloseControl (string tagid)
  659. {
  660. ControlBuilder current = stack.Builder;
  661. string btag = current.TagName;
  662. if (String.Compare (btag, "tbody", true, CultureInfo.InvariantCulture) != 0 &&
  663. String.Compare (tagid, "tbody", true, CultureInfo.InvariantCulture) == 0) {
  664. if (!current.ChildrenAsProperties) {
  665. try {
  666. TextParsed (location, location.PlainText);
  667. FlushText ();
  668. } catch {}
  669. }
  670. return true;
  671. }
  672. if (0 != String.Compare (tagid, btag, true, CultureInfo.InvariantCulture))
  673. return false;
  674. // if (current is TemplateBuilder)
  675. // pop from the id list
  676. if (current.NeedsTagInnerText ()) {
  677. try {
  678. current.SetTagInnerText (tagInnerText.ToString ());
  679. } catch (Exception e) {
  680. throw new ParseException (current.location, e.Message, e);
  681. }
  682. tagInnerText.Length = 0;
  683. }
  684. if (typeof (HtmlForm).IsAssignableFrom (current.ControlType)) {
  685. inForm = false;
  686. }
  687. current.CloseControl ();
  688. stack.Pop ();
  689. stack.Builder.AppendSubBuilder (current);
  690. return true;
  691. }
  692. bool ProcessCode (TagType tagtype, string code, ILocation location)
  693. {
  694. ControlBuilder b = null;
  695. if (tagtype == TagType.CodeRender)
  696. b = new CodeRenderBuilder (code, false, location);
  697. else if (tagtype == TagType.CodeRenderExpression)
  698. b = new CodeRenderBuilder (code, true, location);
  699. else if (tagtype == TagType.DataBinding)
  700. b = new DataBindingBuilder (code, location);
  701. else
  702. throw new HttpException ("Should never happen");
  703. stack.Builder.AppendSubBuilder (b);
  704. return true;
  705. }
  706. public ILocation Location {
  707. get { return location; }
  708. }
  709. void CheckLanguage (string lang)
  710. {
  711. if (lang == null || lang == "")
  712. return;
  713. if (String.Compare (lang, tparser.Language, true, CultureInfo.InvariantCulture) == 0)
  714. return;
  715. #if NET_2_0
  716. CompilationSection section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  717. if (section.Compilers[tparser.Language] != section.Compilers[lang])
  718. #else
  719. CompilationConfiguration cfg = CompilationConfiguration.GetInstance (HttpContext.Current);
  720. if (!cfg.Compilers.CompareLanguages (tparser.Language, lang))
  721. #endif
  722. throw new ParseException (Location,
  723. String.Format ("Trying to mix language '{0}' and '{1}'.",
  724. tparser.Language, lang));
  725. }
  726. // Used to get CodeRender tags in attribute values
  727. class CodeRenderParser
  728. {
  729. string str;
  730. ControlBuilder builder;
  731. public CodeRenderParser (string str, ControlBuilder builder)
  732. {
  733. this.str = str;
  734. this.builder = builder;
  735. }
  736. public void AddChildren ()
  737. {
  738. int index = str.IndexOf ("<%");
  739. if (index > 0) {
  740. TextParsed (null, str.Substring (0, index));
  741. str = str.Substring (index);
  742. }
  743. AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
  744. parser.Error += new ParseErrorHandler (ParseError);
  745. parser.TagParsed += new TagParsedHandler (TagParsed);
  746. parser.TextParsed += new TextParsedHandler (TextParsed);
  747. parser.Parse ();
  748. }
  749. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  750. {
  751. if (tagtype == TagType.CodeRender)
  752. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
  753. else if (tagtype == TagType.CodeRenderExpression)
  754. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
  755. else if (tagtype == TagType.DataBinding)
  756. builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
  757. else
  758. builder.AppendLiteralString (location.PlainText);
  759. }
  760. void TextParsed (ILocation location, string text)
  761. {
  762. builder.AppendLiteralString (text);
  763. }
  764. void ParseError (ILocation location, string message)
  765. {
  766. throw new ParseException (location, message);
  767. }
  768. }
  769. }
  770. }