AspGenerator.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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.UI;
  39. using System.Web.UI.HtmlControls;
  40. using System.Web.Util;
  41. namespace System.Web.Compilation
  42. {
  43. class BuilderLocation
  44. {
  45. public ControlBuilder Builder;
  46. public ILocation Location;
  47. public BuilderLocation (ControlBuilder builder, ILocation location)
  48. {
  49. this.Builder = builder;
  50. this.Location = location;
  51. }
  52. }
  53. class BuilderLocationStack : Stack
  54. {
  55. public override void Push (object o)
  56. {
  57. if (!(o is BuilderLocation))
  58. throw new InvalidOperationException ();
  59. base.Push (o);
  60. }
  61. public virtual void Push (ControlBuilder builder, ILocation location)
  62. {
  63. BuilderLocation bl = new BuilderLocation (builder, location);
  64. Push (bl);
  65. }
  66. public new BuilderLocation Peek ()
  67. {
  68. return (BuilderLocation) base.Peek ();
  69. }
  70. public new BuilderLocation Pop ()
  71. {
  72. return (BuilderLocation) base.Pop ();
  73. }
  74. public ControlBuilder Builder {
  75. get { return Peek ().Builder; }
  76. }
  77. }
  78. class ParserStack
  79. {
  80. Hashtable files;
  81. Stack parsers;
  82. AspParser current;
  83. public ParserStack ()
  84. {
  85. files = new Hashtable (); // may be this should be case sensitive for windows
  86. parsers = new Stack ();
  87. }
  88. public bool Push (AspParser parser)
  89. {
  90. if (files.Contains (parser.Filename))
  91. return false;
  92. files [parser.Filename] = true;
  93. parsers.Push (parser);
  94. current = parser;
  95. return true;
  96. }
  97. public AspParser Pop ()
  98. {
  99. if (parsers.Count == 0)
  100. return null;
  101. files.Remove (current.Filename);
  102. AspParser result = (AspParser) parsers.Pop ();
  103. if (parsers.Count > 0)
  104. current = (AspParser) parsers.Peek ();
  105. else
  106. current = null;
  107. return result;
  108. }
  109. public int Count {
  110. get { return parsers.Count; }
  111. }
  112. public AspParser Parser {
  113. get { return current; }
  114. }
  115. public string Filename {
  116. get { return current.Filename; }
  117. }
  118. }
  119. class TagStack
  120. {
  121. Stack tags;
  122. public TagStack ()
  123. {
  124. tags = new Stack ();
  125. }
  126. public void Push (string tagid)
  127. {
  128. tags.Push (tagid);
  129. }
  130. public string Pop ()
  131. {
  132. if (tags.Count == 0)
  133. return null;
  134. return (string) tags.Pop ();
  135. }
  136. public bool CompareTo (string tagid)
  137. {
  138. if (tags.Count == 0)
  139. return false;
  140. return 0 == String.Compare (tagid, (string) tags.Peek (), true, CultureInfo.InvariantCulture);
  141. }
  142. public int Count {
  143. get { return tags.Count; }
  144. }
  145. public string Current {
  146. get { return (string) tags.Peek (); }
  147. }
  148. }
  149. class AspGenerator
  150. {
  151. ParserStack pstack;
  152. BuilderLocationStack stack;
  153. TemplateParser tparser;
  154. StringBuilder text;
  155. RootBuilder rootBuilder;
  156. bool inScript, javascript, ignore_text;
  157. ILocation location;
  158. bool isApplication;
  159. StringBuilder tagInnerText = new StringBuilder ();
  160. static Hashtable emptyHash = new Hashtable ();
  161. bool inForm;
  162. bool useOtherTags;
  163. public AspGenerator (TemplateParser tparser)
  164. {
  165. this.tparser = tparser;
  166. text = new StringBuilder ();
  167. stack = new BuilderLocationStack ();
  168. rootBuilder = new RootBuilder (tparser);
  169. stack.Push (rootBuilder, null);
  170. tparser.RootBuilder = rootBuilder;
  171. pstack = new ParserStack ();
  172. }
  173. public RootBuilder RootBuilder {
  174. get { return tparser.RootBuilder; }
  175. }
  176. public AspParser Parser {
  177. get { return pstack.Parser; }
  178. }
  179. public string Filename {
  180. get { return pstack.Filename; }
  181. }
  182. BaseCompiler GetCompilerFromType ()
  183. {
  184. Type type = tparser.GetType ();
  185. if (type == typeof (PageParser))
  186. return new PageCompiler ((PageParser) tparser);
  187. if (type == typeof (ApplicationFileParser))
  188. return new GlobalAsaxCompiler ((ApplicationFileParser) tparser);
  189. if (type == typeof (UserControlParser))
  190. return new UserControlCompiler ((UserControlParser) tparser);
  191. #if NET_2_0
  192. if (type == typeof(MasterPageParser))
  193. return new MasterPageCompiler ((MasterPageParser) tparser);
  194. #endif
  195. throw new Exception ("Got type: " + type);
  196. }
  197. void InitParser (TextReader reader, string filename)
  198. {
  199. AspParser parser = new AspParser (filename, reader);
  200. reader.Close ();
  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. isApplication = tparser.DefaultDirectiveName == "application";
  221. #if ONLY_1_1
  222. InitParser (file);
  223. #endif
  224. pstack.Parser.Parse ();
  225. if (text.Length > 0)
  226. FlushText ();
  227. pstack.Pop ();
  228. #if DEBUG
  229. PrintTree (rootBuilder, 0);
  230. #endif
  231. if (stack.Count > 1 && pstack.Count == 0)
  232. throw new ParseException (stack.Builder.location,
  233. "Expecting </" + stack.Builder.TagName + "> " + stack.Builder);
  234. }
  235. public void Parse ()
  236. {
  237. #if NET_2_0
  238. string inputFile = tparser.InputFile;
  239. TextReader inputReader = tparser.Reader;
  240. if (String.IsNullOrEmpty (inputFile)) {
  241. StreamReader sr = inputReader as StreamReader;
  242. if (sr != null) {
  243. FileStream fr = sr.BaseStream as FileStream;
  244. if (fr != null)
  245. inputFile = fr.Name;
  246. }
  247. if (String.IsNullOrEmpty (inputFile))
  248. inputFile = "@@inner_string@@";
  249. }
  250. if (inputReader != null)
  251. InitParser (inputReader, inputFile);
  252. else {
  253. if (String.IsNullOrEmpty (inputFile))
  254. throw new HttpException ("Parser input file is empty, cannot continue.");
  255. inputFile = Path.GetFullPath (inputFile);
  256. InitParser (inputFile);
  257. }
  258. Parse (inputFile);
  259. #else
  260. Parse (Path.GetFullPath (tparser.InputFile));
  261. #endif
  262. }
  263. internal static void AddTypeToCache (ArrayList dependencies, string inputFile, Type type)
  264. {
  265. string [] deps = (string []) dependencies.ToArray (typeof (string));
  266. HttpContext ctx = HttpContext.Current;
  267. HttpRequest req = ctx != null ? ctx.Request : null;
  268. if (req == null)
  269. throw new HttpException ("No current context, cannot compile.");
  270. int depLength = deps.Length;
  271. for (int i = 0; i < deps.Length; i++)
  272. deps [i] = req.MapPath (deps [i]);
  273. HttpRuntime.InternalCache.Insert ("@@Type" + inputFile, type, new CacheDependency (deps));
  274. }
  275. public Type GetCompiledType ()
  276. {
  277. Type type = (Type) HttpRuntime.InternalCache.Get ("@@Type" + tparser.InputFile);
  278. if (type != null) {
  279. return type;
  280. }
  281. Parse ();
  282. BaseCompiler compiler = GetCompilerFromType ();
  283. type = compiler.GetCompiledType ();
  284. AddTypeToCache (tparser.Dependencies, tparser.InputFile, type);
  285. return type;
  286. }
  287. #if DEBUG
  288. static void PrintTree (ControlBuilder builder, int indent)
  289. {
  290. if (builder == null)
  291. return;
  292. string i = new string ('\t', indent);
  293. Console.Write (i);
  294. Console.WriteLine ("b: {0} id: {1} type: {2} parent: {3}",
  295. builder, builder.ID, builder.ControlType, builder.parentBuilder);
  296. if (builder.Children != null)
  297. foreach (object o in builder.Children) {
  298. if (o is ControlBuilder)
  299. PrintTree ((ControlBuilder) o, indent++);
  300. }
  301. }
  302. static void PrintLocation (ILocation loc)
  303. {
  304. Console.WriteLine ("\tFile name: " + loc.Filename);
  305. Console.WriteLine ("\tBegin line: " + loc.BeginLine);
  306. Console.WriteLine ("\tEnd line: " + loc.EndLine);
  307. Console.WriteLine ("\tBegin column: " + loc.BeginColumn);
  308. Console.WriteLine ("\tEnd column: " + loc.EndColumn);
  309. Console.WriteLine ("\tPlainText: " + loc.PlainText);
  310. Console.WriteLine ();
  311. }
  312. #endif
  313. void ParseError (ILocation location, string message)
  314. {
  315. throw new ParseException (location, message);
  316. }
  317. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  318. {
  319. this.location = new Location (location);
  320. if (tparser != null)
  321. tparser.Location = location;
  322. if (text.Length != 0)
  323. FlushText ();
  324. if (0 == String.Compare (tagid, "script", true, CultureInfo.InvariantCulture)) {
  325. bool in_script = (inScript || ignore_text);
  326. if (in_script || (tagtype != TagType.Close && attributes != null)) {
  327. if ((in_script || attributes.IsRunAtServer ()) && ProcessScript (tagtype, attributes))
  328. return;
  329. }
  330. }
  331. switch (tagtype) {
  332. case TagType.Directive:
  333. if (tagid == "")
  334. tagid = tparser.DefaultDirectiveName;
  335. tparser.AddDirective (tagid, attributes.GetDictionary (null));
  336. break;
  337. case TagType.Tag:
  338. if (ProcessTag (tagid, attributes, tagtype)) {
  339. useOtherTags = true;
  340. break;
  341. }
  342. if (useOtherTags) {
  343. stack.Builder.EnsureOtherTags ();
  344. stack.Builder.OtherTags.Add (tagid);
  345. }
  346. TextParsed (location, location.PlainText);
  347. break;
  348. case TagType.Close:
  349. bool notServer = (useOtherTags && TryRemoveTag (tagid, stack.Builder.OtherTags));
  350. if (!notServer && CloseControl (tagid))
  351. break;
  352. TextParsed (location, location.PlainText);
  353. break;
  354. case TagType.SelfClosing:
  355. int count = stack.Count;
  356. if (!ProcessTag (tagid, attributes, tagtype)) {
  357. TextParsed (location, location.PlainText);
  358. } else if (stack.Count != count) {
  359. CloseControl (tagid);
  360. }
  361. break;
  362. case TagType.DataBinding:
  363. goto case TagType.CodeRender;
  364. case TagType.CodeRenderExpression:
  365. goto case TagType.CodeRender;
  366. case TagType.CodeRender:
  367. if (isApplication)
  368. throw new ParseException (location, "Invalid content for application file.");
  369. ProcessCode (tagtype, tagid, location);
  370. break;
  371. case TagType.Include:
  372. if (isApplication)
  373. throw new ParseException (location, "Invalid content for application file.");
  374. string file = attributes ["virtual"] as string;
  375. bool isvirtual = (file != null);
  376. if (!isvirtual)
  377. file = attributes ["file"] as string;
  378. if (isvirtual) {
  379. file = tparser.MapPath (file);
  380. } else {
  381. file = GetIncludeFilePath (tparser.BaseDir, file);
  382. }
  383. Parse (file);
  384. break;
  385. default:
  386. break;
  387. }
  388. //PrintLocation (location);
  389. }
  390. static bool TryRemoveTag (string tagid, ArrayList otags)
  391. {
  392. if (otags == null || otags.Count == 0)
  393. return false;
  394. for (int idx = otags.Count - 1; idx >= 0; idx--) {
  395. string otagid = (string) otags [idx];
  396. if (0 == String.Compare (tagid, otagid, true, CultureInfo.InvariantCulture)) {
  397. do {
  398. otags.RemoveAt (idx);
  399. } while (otags.Count - 1 >= idx);
  400. return true;
  401. }
  402. }
  403. return false;
  404. }
  405. static string GetIncludeFilePath (string basedir, string filename)
  406. {
  407. if (Path.DirectorySeparatorChar == '/')
  408. filename = filename.Replace ("\\", "/");
  409. return Path.GetFullPath (Path.Combine (basedir, filename));
  410. }
  411. void TextParsed (ILocation location, string text)
  412. {
  413. if (ignore_text)
  414. return;
  415. if (text.IndexOf ("<%") != -1 && !inScript) {
  416. if (this.text.Length > 0)
  417. FlushText ();
  418. CodeRenderParser r = new CodeRenderParser (text, stack.Builder);
  419. r.AddChildren ();
  420. return;
  421. }
  422. this.text.Append (text);
  423. //PrintLocation (location);
  424. }
  425. void FlushText ()
  426. {
  427. string t = text.ToString ();
  428. text.Length = 0;
  429. if (inScript) {
  430. tparser.Scripts.Add (new ServerSideScript (t, new System.Web.Compilation.Location (tparser.Location)));
  431. return;
  432. }
  433. if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
  434. throw new ParseException (location, "Content not valid for application file.");
  435. ControlBuilder current = stack.Builder;
  436. current.AppendLiteralString (t);
  437. if (current.NeedsTagInnerText ()) {
  438. tagInnerText.Append (t);
  439. }
  440. }
  441. bool ProcessTag (string tagid, TagAttributes atts, TagType tagtype)
  442. {
  443. if (isApplication) {
  444. if (String.Compare (tagid, "object", true, CultureInfo.InvariantCulture) != 0)
  445. throw new ParseException (location, "Invalid tag for application file.");
  446. }
  447. ControlBuilder parent = stack.Builder;
  448. ControlBuilder builder = null;
  449. Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
  450. if (stack.Count > 1) {
  451. try {
  452. builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
  453. } catch (TypeLoadException e) {
  454. throw new ParseException (Location, "Type not found.", e);
  455. } catch (Exception e) {
  456. throw new ParseException (Location, e.Message, e);
  457. }
  458. }
  459. if (builder == null && atts != null && atts.IsRunAtServer ()) {
  460. string id = htable ["id"] as string;
  461. if (id != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (id))
  462. throw new ParseException (Location, "'" + id + "' is not a valid identifier");
  463. try {
  464. builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
  465. } catch (TypeLoadException e) {
  466. throw new ParseException (Location, "Type not found.", e);
  467. } catch (Exception e) {
  468. throw new ParseException (Location, e.Message, e);
  469. }
  470. }
  471. if (builder == null)
  472. return false;
  473. builder.location = location;
  474. builder.ID = htable ["id"] as string;
  475. if (typeof (HtmlForm).IsAssignableFrom (builder.ControlType)) {
  476. if (inForm)
  477. throw new ParseException (location, "Only one <form> allowed.");
  478. inForm = true;
  479. }
  480. if (builder.HasBody () && !(builder is ObjectTagBuilder)) {
  481. if (builder is TemplateBuilder) {
  482. // push the id list
  483. }
  484. stack.Push (builder, location);
  485. } else {
  486. if (!isApplication && builder is ObjectTagBuilder) {
  487. ObjectTagBuilder ot = (ObjectTagBuilder) builder;
  488. if (ot.Scope != null && ot.Scope != "")
  489. throw new ParseException (location, "Scope not allowed here");
  490. if (tagtype == TagType.Tag) {
  491. stack.Push (builder, location);
  492. return true;
  493. }
  494. }
  495. parent.AppendSubBuilder (builder);
  496. builder.CloseControl ();
  497. }
  498. return true;
  499. }
  500. string ReadFile (string filename)
  501. {
  502. string realpath = tparser.MapPath (filename);
  503. using (StreamReader sr = new StreamReader (realpath, WebEncoding.FileEncoding)) {
  504. string content = sr.ReadToEnd ();
  505. return content;
  506. }
  507. }
  508. bool ProcessScript (TagType tagtype, TagAttributes attributes)
  509. {
  510. if (tagtype != TagType.Close) {
  511. if (attributes != null && attributes.IsRunAtServer ()) {
  512. string language = (string) attributes ["language"];
  513. if (language != null && language.Length > 0 && tparser.ImplicitLanguage)
  514. tparser.SetLanguage (language);
  515. CheckLanguage (language);
  516. string src = (string) attributes ["src"];
  517. if (src != null) {
  518. if (src == "")
  519. throw new ParseException (Parser,
  520. "src cannot be an empty string");
  521. string content = ReadFile (src);
  522. inScript = true;
  523. TextParsed (Parser, content);
  524. FlushText ();
  525. inScript = false;
  526. if (tagtype != TagType.SelfClosing) {
  527. ignore_text = true;
  528. Parser.VerbatimID = "script";
  529. }
  530. } else if (tagtype == TagType.Tag) {
  531. Parser.VerbatimID = "script";
  532. inScript = true;
  533. }
  534. return true;
  535. } else {
  536. if (tagtype != TagType.SelfClosing) {
  537. Parser.VerbatimID = "script";
  538. javascript = true;
  539. }
  540. TextParsed (location, location.PlainText);
  541. return true;
  542. }
  543. }
  544. bool result;
  545. if (inScript) {
  546. result = inScript;
  547. inScript = false;
  548. } else if (!ignore_text) {
  549. result = javascript;
  550. javascript = false;
  551. TextParsed (location, location.PlainText);
  552. } else {
  553. ignore_text = false;
  554. result = true;
  555. }
  556. return result;
  557. }
  558. bool CloseControl (string tagid)
  559. {
  560. ControlBuilder current = stack.Builder;
  561. string btag = current.TagName;
  562. if (String.Compare (btag, "tbody", true, CultureInfo.InvariantCulture) != 0 &&
  563. String.Compare (tagid, "tbody", true, CultureInfo.InvariantCulture) == 0) {
  564. if (!current.ChildrenAsProperties) {
  565. try {
  566. TextParsed (location, location.PlainText);
  567. FlushText ();
  568. } catch {}
  569. }
  570. return true;
  571. }
  572. if (0 != String.Compare (tagid, btag, true, CultureInfo.InvariantCulture))
  573. return false;
  574. // if (current is TemplateBuilder)
  575. // pop from the id list
  576. if (current.NeedsTagInnerText ()) {
  577. try {
  578. current.SetTagInnerText (tagInnerText.ToString ());
  579. } catch (Exception e) {
  580. throw new ParseException (current.location, e.Message, e);
  581. }
  582. tagInnerText.Length = 0;
  583. }
  584. if (typeof (HtmlForm).IsAssignableFrom (current.ControlType)) {
  585. inForm = false;
  586. }
  587. current.CloseControl ();
  588. stack.Pop ();
  589. stack.Builder.AppendSubBuilder (current);
  590. return true;
  591. }
  592. bool ProcessCode (TagType tagtype, string code, ILocation location)
  593. {
  594. ControlBuilder b = null;
  595. if (tagtype == TagType.CodeRender)
  596. b = new CodeRenderBuilder (code, false, location);
  597. else if (tagtype == TagType.CodeRenderExpression)
  598. b = new CodeRenderBuilder (code, true, location);
  599. else if (tagtype == TagType.DataBinding)
  600. b = new DataBindingBuilder (code, location);
  601. else
  602. throw new HttpException ("Should never happen");
  603. stack.Builder.AppendSubBuilder (b);
  604. return true;
  605. }
  606. public ILocation Location {
  607. get { return location; }
  608. }
  609. void CheckLanguage (string lang)
  610. {
  611. if (lang == null || lang == "")
  612. return;
  613. if (String.Compare (lang, tparser.Language, true, CultureInfo.InvariantCulture) == 0)
  614. return;
  615. #if NET_2_0
  616. CompilationSection section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  617. if (section.Compilers[tparser.Language] != section.Compilers[lang])
  618. #else
  619. CompilationConfiguration cfg = CompilationConfiguration.GetInstance (HttpContext.Current);
  620. if (!cfg.Compilers.CompareLanguages (tparser.Language, lang))
  621. #endif
  622. throw new ParseException (Location,
  623. String.Format ("Trying to mix language '{0}' and '{1}'.",
  624. tparser.Language, lang));
  625. }
  626. // Used to get CodeRender tags in attribute values
  627. class CodeRenderParser
  628. {
  629. string str;
  630. ControlBuilder builder;
  631. public CodeRenderParser (string str, ControlBuilder builder)
  632. {
  633. this.str = str;
  634. this.builder = builder;
  635. }
  636. public void AddChildren ()
  637. {
  638. int index = str.IndexOf ("<%");
  639. if (index > 0) {
  640. TextParsed (null, str.Substring (0, index));
  641. str = str.Substring (index);
  642. }
  643. AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
  644. parser.Error += new ParseErrorHandler (ParseError);
  645. parser.TagParsed += new TagParsedHandler (TagParsed);
  646. parser.TextParsed += new TextParsedHandler (TextParsed);
  647. parser.Parse ();
  648. }
  649. void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
  650. {
  651. if (tagtype == TagType.CodeRender)
  652. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
  653. else if (tagtype == TagType.CodeRenderExpression)
  654. builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
  655. else if (tagtype == TagType.DataBinding)
  656. builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
  657. else
  658. builder.AppendLiteralString (location.PlainText);
  659. }
  660. void TextParsed (ILocation location, string text)
  661. {
  662. builder.AppendLiteralString (text);
  663. }
  664. void ParseError (ILocation location, string message)
  665. {
  666. throw new ParseException (location, message);
  667. }
  668. }
  669. }
  670. }