AspGenerator.cs 23 KB

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