AspGenerator.cs 21 KB

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