AspGenerator.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. public AspGenerator (TemplateParser tparser)
  165. {
  166. this.tparser = tparser;
  167. text = new StringBuilder ();
  168. stack = new BuilderLocationStack ();
  169. rootBuilder = new RootBuilder (tparser);
  170. stack.Push (rootBuilder, null);
  171. tparser.RootBuilder = rootBuilder;
  172. pstack = new ParserStack ();
  173. }
  174. public RootBuilder RootBuilder {
  175. get { return tparser.RootBuilder; }
  176. }
  177. public AspParser Parser {
  178. get { return pstack.Parser; }
  179. }
  180. public string Filename {
  181. get { return pstack.Filename; }
  182. }
  183. BaseCompiler GetCompilerFromType ()
  184. {
  185. Type type = tparser.GetType ();
  186. if (type == typeof (PageParser))
  187. return new PageCompiler ((PageParser) tparser);
  188. if (type == typeof (ApplicationFileParser))
  189. return new GlobalAsaxCompiler ((ApplicationFileParser) tparser);
  190. if (type == typeof (UserControlParser))
  191. return new UserControlCompiler ((UserControlParser) tparser);
  192. #if NET_2_0
  193. if (type == typeof(MasterPageParser))
  194. return new MasterPageCompiler ((MasterPageParser) tparser);
  195. #endif
  196. throw new Exception ("Got type: " + type);
  197. }
  198. void InitParser (TextReader reader, string filename)
  199. {
  200. AspParser parser = new AspParser (filename, reader);
  201. parser.Error += new ParseErrorHandler (ParseError);
  202. parser.TagParsed += new TagParsedHandler (TagParsed);
  203. parser.TextParsed += new TextParsedHandler (TextParsed);
  204. if (!pstack.Push (parser))
  205. throw new ParseException (Location, "Infinite recursion detected including file: " + filename);
  206. if (filename != "@@inner_string@@") {
  207. string arvp = Path.Combine (tparser.BaseVirtualDir, Path.GetFileName (filename));
  208. if (VirtualPathUtility.IsAbsolute (arvp))
  209. arvp = VirtualPathUtility.ToAppRelative (arvp);
  210. tparser.AddDependency (arvp);
  211. }
  212. }
  213. void InitParser (string filename)
  214. {
  215. StreamReader reader = new StreamReader (filename, WebEncoding.FileEncoding);
  216. InitParser (reader, filename);
  217. }
  218. public void Parse (string file)
  219. {
  220. #if ONLY_1_1
  221. Parse (file, true);
  222. #else
  223. Parse (file, false);
  224. #endif
  225. }
  226. public void Parse (TextReader reader, string filename, bool doInitParser)
  227. {
  228. try {
  229. isApplication = tparser.DefaultDirectiveName == "application";
  230. if (doInitParser)
  231. InitParser (reader, filename);
  232. pstack.Parser.Parse ();
  233. if (text.Length > 0)
  234. FlushText ();
  235. pstack.Pop ();
  236. #if DEBUG
  237. PrintTree (rootBuilder, 0);
  238. #endif
  239. if (stack.Count > 1 && pstack.Count == 0)
  240. throw new ParseException (stack.Builder.location,
  241. "Expecting </" + stack.Builder.TagName + "> " + stack.Builder);
  242. } finally {
  243. if (reader != null)
  244. reader.Close ();
  245. }
  246. }
  247. public void Parse (Stream stream, string filename, bool doInitParser)
  248. {
  249. Parse (new StreamReader (stream, WebEncoding.FileEncoding), filename, doInitParser);
  250. }
  251. public void Parse (string filename, bool doInitParser)
  252. {
  253. StreamReader reader = new StreamReader (filename, WebEncoding.FileEncoding);
  254. Parse (reader, filename, doInitParser);
  255. }
  256. public void Parse ()
  257. {
  258. #if NET_2_0
  259. string inputFile = tparser.InputFile;
  260. TextReader inputReader = tparser.Reader;
  261. try {
  262. if (String.IsNullOrEmpty (inputFile)) {
  263. StreamReader sr = inputReader as StreamReader;
  264. if (sr != null) {
  265. FileStream fr = sr.BaseStream as FileStream;
  266. if (fr != null)
  267. inputFile = fr.Name;
  268. }
  269. if (String.IsNullOrEmpty (inputFile))
  270. inputFile = "@@inner_string@@";
  271. }
  272. if (inputReader != null) {
  273. Parse (inputReader, inputFile, true);
  274. } else {
  275. if (String.IsNullOrEmpty (inputFile))
  276. throw new HttpException ("Parser input file is empty, cannot continue.");
  277. inputFile = Path.GetFullPath (inputFile);
  278. InitParser (inputFile);
  279. Parse (inputFile);
  280. }
  281. } finally {
  282. if (inputReader != null)
  283. inputReader.Close ();
  284. }
  285. #else
  286. Parse (Path.GetFullPath (tparser.InputFile));
  287. #endif
  288. }
  289. internal static void AddTypeToCache (ArrayList dependencies, string inputFile, Type type)
  290. {
  291. string [] deps = (string []) dependencies.ToArray (typeof (string));
  292. HttpContext ctx = HttpContext.Current;
  293. HttpRequest req = ctx != null ? ctx.Request : null;
  294. if (req == null)
  295. throw new HttpException ("No current context, cannot compile.");
  296. int depLength = deps.Length;
  297. for (int i = 0; i < deps.Length; i++)
  298. deps [i] = req.MapPath (deps [i]);
  299. HttpRuntime.InternalCache.Insert ("@@Type" + inputFile, type, new CacheDependency (deps));
  300. }
  301. public Type GetCompiledType ()
  302. {
  303. Type type = (Type) HttpRuntime.InternalCache.Get ("@@Type" + tparser.InputFile);
  304. if (type != null) {
  305. return type;
  306. }
  307. Parse ();
  308. BaseCompiler compiler = GetCompilerFromType ();
  309. type = compiler.GetCompiledType ();
  310. AddTypeToCache (tparser.Dependencies, tparser.InputFile, type);
  311. return type;
  312. }
  313. #if DEBUG
  314. static void PrintTree (ControlBuilder builder, int indent)
  315. {
  316. if (builder == null)
  317. return;
  318. string i = new string ('\t', indent);
  319. Console.Write (i);
  320. Console.WriteLine ("b: {0} id: {1} type: {2} parent: {3}",
  321. builder, builder.ID, builder.ControlType, builder.parentBuilder);
  322. if (builder.Children != null)
  323. foreach (object o in builder.Children) {
  324. if (o is ControlBuilder)
  325. PrintTree ((ControlBuilder) o, indent++);
  326. }
  327. }
  328. static void PrintLocation (ILocation loc)
  329. {
  330. Console.WriteLine ("\tFile name: " + loc.Filename);
  331. Console.WriteLine ("\tBegin line: " + loc.BeginLine);
  332. Console.WriteLine ("\tEnd line: " + loc.EndLine);
  333. Console.WriteLine ("\tBegin column: " + loc.BeginColumn);
  334. Console.WriteLine ("\tEnd column: " + loc.EndColumn);
  335. Console.WriteLine ("\tPlainText: " + loc.PlainText);
  336. Console.WriteLine ();
  337. }
  338. #endif
  339. void ParseError (ILocation location, string message)
  340. {
  341. throw new ParseException (location, message);
  342. }
  343. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  344. {
  345. this.location = new Location (location);
  346. if (tparser != null)
  347. tparser.Location = location;
  348. if (text.Length != 0)
  349. FlushText ();
  350. if (0 == String.Compare (tagid, "script", true, CultureInfo.InvariantCulture)) {
  351. bool in_script = (inScript || ignore_text);
  352. if (in_script || (tagtype != TagType.Close && attributes != null)) {
  353. if ((in_script || attributes.IsRunAtServer ()) && ProcessScript (tagtype, attributes))
  354. return;
  355. }
  356. }
  357. switch (tagtype) {
  358. case TagType.Directive:
  359. if (tagid == "")
  360. tagid = tparser.DefaultDirectiveName;
  361. tparser.AddDirective (tagid, attributes.GetDictionary (null));
  362. break;
  363. case TagType.Tag:
  364. if (ProcessTag (tagid, attributes, tagtype)) {
  365. useOtherTags = true;
  366. break;
  367. }
  368. if (useOtherTags) {
  369. stack.Builder.EnsureOtherTags ();
  370. stack.Builder.OtherTags.Add (tagid);
  371. }
  372. TextParsed (location, location.PlainText);
  373. break;
  374. case TagType.Close:
  375. bool notServer = (useOtherTags && TryRemoveTag (tagid, stack.Builder.OtherTags));
  376. if (!notServer && CloseControl (tagid))
  377. break;
  378. TextParsed (location, location.PlainText);
  379. break;
  380. case TagType.SelfClosing:
  381. int count = stack.Count;
  382. if (!ProcessTag (tagid, attributes, tagtype)) {
  383. TextParsed (location, location.PlainText);
  384. } else if (stack.Count != count) {
  385. CloseControl (tagid);
  386. }
  387. break;
  388. case TagType.DataBinding:
  389. goto case TagType.CodeRender;
  390. case TagType.CodeRenderExpression:
  391. goto case TagType.CodeRender;
  392. case TagType.CodeRender:
  393. if (isApplication)
  394. throw new ParseException (location, "Invalid content for application file.");
  395. ProcessCode (tagtype, tagid, location);
  396. break;
  397. case TagType.Include:
  398. if (isApplication)
  399. throw new ParseException (location, "Invalid content for application file.");
  400. string file = attributes ["virtual"] as string;
  401. bool isvirtual = (file != null);
  402. if (!isvirtual)
  403. file = attributes ["file"] as string;
  404. if (isvirtual) {
  405. bool parsed = false;
  406. #if NET_2_0
  407. VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
  408. if (vpp.FileExists (file)) {
  409. VirtualFile vf = vpp.GetFile (file);
  410. if (vf != null) {
  411. Parse (vf.Open (), file, true);
  412. parsed = true;
  413. }
  414. }
  415. #endif
  416. if (!parsed)
  417. Parse (tparser.MapPath (file), true);
  418. } else {
  419. string includeFilePath = GetIncludeFilePath (tparser.ParserDir, file);
  420. tparser.PushIncludeDir (Path.GetDirectoryName (includeFilePath));
  421. try {
  422. Parse (includeFilePath, true);
  423. } finally {
  424. tparser.PopIncludeDir ();
  425. }
  426. }
  427. break;
  428. default:
  429. break;
  430. }
  431. //PrintLocation (location);
  432. }
  433. static bool TryRemoveTag (string tagid, ArrayList otags)
  434. {
  435. if (otags == null || otags.Count == 0)
  436. return false;
  437. for (int idx = otags.Count - 1; idx >= 0; idx--) {
  438. string otagid = (string) otags [idx];
  439. if (0 == String.Compare (tagid, otagid, true, CultureInfo.InvariantCulture)) {
  440. do {
  441. otags.RemoveAt (idx);
  442. } while (otags.Count - 1 >= idx);
  443. return true;
  444. }
  445. }
  446. return false;
  447. }
  448. static string GetIncludeFilePath (string basedir, string filename)
  449. {
  450. if (Path.DirectorySeparatorChar == '/')
  451. filename = filename.Replace ("\\", "/");
  452. return Path.GetFullPath (Path.Combine (basedir, filename));
  453. }
  454. void TextParsed (ILocation location, string text)
  455. {
  456. if (ignore_text)
  457. return;
  458. if (text.IndexOf ("<%") != -1 && !inScript) {
  459. if (this.text.Length > 0)
  460. FlushText ();
  461. CodeRenderParser r = new CodeRenderParser (text, stack.Builder);
  462. r.AddChildren ();
  463. return;
  464. }
  465. this.text.Append (text);
  466. //PrintLocation (location);
  467. }
  468. void FlushText ()
  469. {
  470. string t = text.ToString ();
  471. text.Length = 0;
  472. if (inScript) {
  473. tparser.Scripts.Add (new ServerSideScript (t, new System.Web.Compilation.Location (tparser.Location)));
  474. return;
  475. }
  476. if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
  477. throw new ParseException (location, "Content not valid for application file.");
  478. ControlBuilder current = stack.Builder;
  479. current.AppendLiteralString (t);
  480. if (current.NeedsTagInnerText ()) {
  481. tagInnerText.Append (t);
  482. }
  483. }
  484. #if NET_2_0
  485. bool BuilderHasOtherThan (Type type, ControlBuilder cb)
  486. {
  487. ArrayList al = cb.OtherTags;
  488. if (al != null && al.Count > 0)
  489. return true;
  490. al = cb.Children;
  491. if (al != null) {
  492. ControlBuilder tmp;
  493. foreach (object o in al) {
  494. if (o == null)
  495. continue;
  496. tmp = o as ControlBuilder;
  497. if (tmp == null) {
  498. string s = o as string;
  499. if (s != null && String.IsNullOrEmpty (s.Trim ()))
  500. continue;
  501. return true;
  502. }
  503. if (tmp is System.Web.UI.WebControls.ContentBuilderInternal)
  504. continue;
  505. if (!(tmp.ControlType is System.Web.UI.WebControls.Content))
  506. return true;
  507. }
  508. }
  509. return false;
  510. }
  511. bool OtherControlsAllowed (ControlBuilder cb)
  512. {
  513. if (cb == null)
  514. return true;
  515. if (!typeof (System.Web.UI.WebControls.Content).IsAssignableFrom (cb.ControlType))
  516. return true;
  517. if (BuilderHasOtherThan (typeof (System.Web.UI.WebControls.Content), rootBuilder))
  518. return false;
  519. return true;
  520. }
  521. #endif
  522. bool ProcessTag (string tagid, TagAttributes atts, TagType tagtype)
  523. {
  524. if (isApplication) {
  525. if (String.Compare (tagid, "object", true, CultureInfo.InvariantCulture) != 0)
  526. throw new ParseException (location, "Invalid tag for application file.");
  527. }
  528. ControlBuilder parent = stack.Builder;
  529. ControlBuilder builder = null;
  530. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  531. if (stack.Count > 1) {
  532. try {
  533. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  534. } catch (TypeLoadException e) {
  535. throw new ParseException (Location, "Type not found.", e);
  536. } catch (Exception e) {
  537. throw new ParseException (Location, e.Message, e);
  538. }
  539. }
  540. if (builder == null && atts != null && atts.IsRunAtServer ()) {
  541. string id = htable ["id"] as string;
  542. if (id != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (id))
  543. throw new ParseException (Location, "'" + id + "' is not a valid identifier");
  544. try {
  545. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  546. } catch (TypeLoadException e) {
  547. throw new ParseException (Location, "Type not found.", e);
  548. } catch (Exception e) {
  549. throw new ParseException (Location, e.Message, e);
  550. }
  551. }
  552. if (builder == null)
  553. return false;
  554. #if NET_2_0
  555. if (!OtherControlsAllowed (builder))
  556. throw new ParseException (Location, "Only Content controls are allowed directly in a content page that contains Content controls.");
  557. #endif
  558. builder.location = location;
  559. builder.ID = htable ["id"] as string;
  560. if (typeof (HtmlForm).IsAssignableFrom (builder.ControlType)) {
  561. if (inForm)
  562. throw new ParseException (location, "Only one <form> allowed.");
  563. inForm = true;
  564. }
  565. if (builder.HasBody () && !(builder is ObjectTagBuilder)) {
  566. if (builder is TemplateBuilder) {
  567. // push the id list
  568. }
  569. stack.Push (builder, location);
  570. } else {
  571. if (!isApplication && builder is ObjectTagBuilder) {
  572. ObjectTagBuilder ot = (ObjectTagBuilder) builder;
  573. if (ot.Scope != null && ot.Scope != "")
  574. throw new ParseException (location, "Scope not allowed here");
  575. if (tagtype == TagType.Tag) {
  576. stack.Push (builder, location);
  577. return true;
  578. }
  579. }
  580. parent.AppendSubBuilder (builder);
  581. builder.CloseControl ();
  582. }
  583. return true;
  584. }
  585. string ReadFile (string filename)
  586. {
  587. string realpath = tparser.MapPath (filename);
  588. using (StreamReader sr = new StreamReader (realpath, WebEncoding.FileEncoding)) {
  589. string content = sr.ReadToEnd ();
  590. return content;
  591. }
  592. }
  593. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  594. {
  595. if (tagtype != TagType.Close) {
  596. if (attributes != null && attributes.IsRunAtServer ()) {
  597. string language = (string) attributes ["language"];
  598. if (language != null && language.Length > 0 && tparser.ImplicitLanguage)
  599. tparser.SetLanguage (language);
  600. CheckLanguage (language);
  601. string src = (string) attributes ["src"];
  602. if (src != null) {
  603. if (src == "")
  604. throw new ParseException (Parser,
  605. "src cannot be an empty string");
  606. string content = ReadFile (src);
  607. inScript = true;
  608. TextParsed (Parser, content);
  609. FlushText ();
  610. inScript = false;
  611. if (tagtype != TagType.SelfClosing) {
  612. ignore_text = true;
  613. Parser.VerbatimID = "script";
  614. }
  615. } else if (tagtype == TagType.Tag) {
  616. Parser.VerbatimID = "script";
  617. inScript = true;
  618. }
  619. return true;
  620. } else {
  621. if (tagtype != TagType.SelfClosing) {
  622. Parser.VerbatimID = "script";
  623. javascript = true;
  624. }
  625. TextParsed (location, location.PlainText);
  626. return true;
  627. }
  628. }
  629. bool result;
  630. if (inScript) {
  631. result = inScript;
  632. inScript = false;
  633. } else if (!ignore_text) {
  634. result = javascript;
  635. javascript = false;
  636. TextParsed (location, location.PlainText);
  637. } else {
  638. ignore_text = false;
  639. result = true;
  640. }
  641. return result;
  642. }
  643. bool CloseControl (string tagid)
  644. {
  645. ControlBuilder current = stack.Builder;
  646. string btag = current.TagName;
  647. if (String.Compare (btag, "tbody", true, CultureInfo.InvariantCulture) != 0 &&
  648. String.Compare (tagid, "tbody", true, CultureInfo.InvariantCulture) == 0) {
  649. if (!current.ChildrenAsProperties) {
  650. try {
  651. TextParsed (location, location.PlainText);
  652. FlushText ();
  653. } catch {}
  654. }
  655. return true;
  656. }
  657. if (0 != String.Compare (tagid, btag, true, CultureInfo.InvariantCulture))
  658. return false;
  659. // if (current is TemplateBuilder)
  660. // pop from the id list
  661. if (current.NeedsTagInnerText ()) {
  662. try {
  663. current.SetTagInnerText (tagInnerText.ToString ());
  664. } catch (Exception e) {
  665. throw new ParseException (current.location, e.Message, e);
  666. }
  667. tagInnerText.Length = 0;
  668. }
  669. if (typeof (HtmlForm).IsAssignableFrom (current.ControlType)) {
  670. inForm = false;
  671. }
  672. current.CloseControl ();
  673. stack.Pop ();
  674. stack.Builder.AppendSubBuilder (current);
  675. return true;
  676. }
  677. bool ProcessCode (TagType tagtype, string code, ILocation location)
  678. {
  679. ControlBuilder b = null;
  680. if (tagtype == TagType.CodeRender)
  681. b = new CodeRenderBuilder (code, false, location);
  682. else if (tagtype == TagType.CodeRenderExpression)
  683. b = new CodeRenderBuilder (code, true, location);
  684. else if (tagtype == TagType.DataBinding)
  685. b = new DataBindingBuilder (code, location);
  686. else
  687. throw new HttpException ("Should never happen");
  688. stack.Builder.AppendSubBuilder (b);
  689. return true;
  690. }
  691. public ILocation Location {
  692. get { return location; }
  693. }
  694. void CheckLanguage (string lang)
  695. {
  696. if (lang == null || lang == "")
  697. return;
  698. if (String.Compare (lang, tparser.Language, true, CultureInfo.InvariantCulture) == 0)
  699. return;
  700. #if NET_2_0
  701. CompilationSection section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  702. if (section.Compilers[tparser.Language] != section.Compilers[lang])
  703. #else
  704. CompilationConfiguration cfg = CompilationConfiguration.GetInstance (HttpContext.Current);
  705. if (!cfg.Compilers.CompareLanguages (tparser.Language, lang))
  706. #endif
  707. throw new ParseException (Location,
  708. String.Format ("Trying to mix language '{0}' and '{1}'.",
  709. tparser.Language, lang));
  710. }
  711. // Used to get CodeRender tags in attribute values
  712. class CodeRenderParser
  713. {
  714. string str;
  715. ControlBuilder builder;
  716. public CodeRenderParser (string str, ControlBuilder builder)
  717. {
  718. this.str = str;
  719. this.builder = builder;
  720. }
  721. public void AddChildren ()
  722. {
  723. int index = str.IndexOf ("<%");
  724. if (index > 0) {
  725. TextParsed (null, str.Substring (0, index));
  726. str = str.Substring (index);
  727. }
  728. AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
  729. parser.Error += new ParseErrorHandler (ParseError);
  730. parser.TagParsed += new TagParsedHandler (TagParsed);
  731. parser.TextParsed += new TextParsedHandler (TextParsed);
  732. parser.Parse ();
  733. }
  734. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  735. {
  736. if (tagtype == TagType.CodeRender)
  737. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
  738. else if (tagtype == TagType.CodeRenderExpression)
  739. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
  740. else if (tagtype == TagType.DataBinding)
  741. builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
  742. else
  743. builder.AppendLiteralString (location.PlainText);
  744. }
  745. void TextParsed (ILocation location, string text)
  746. {
  747. builder.AppendLiteralString (text);
  748. }
  749. void ParseError (ILocation location, string message)
  750. {
  751. throw new ParseException (location, message);
  752. }
  753. }
  754. }
  755. }