TemplateParser.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. //
  2. // System.Web.UI.TemplateParser
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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.CodeDom.Compiler;
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.Globalization;
  34. using System.IO;
  35. using System.Reflection;
  36. using System.Security.Permissions;
  37. using System.Web.Compilation;
  38. using System.Web.Configuration;
  39. using System.Web.Util;
  40. #if NET_2_0
  41. using System.Collections.Generic;
  42. #endif
  43. namespace System.Web.UI {
  44. // CAS
  45. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  46. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  47. public abstract class TemplateParser : BaseParser
  48. {
  49. string inputFile;
  50. string text;
  51. string privateBinPath;
  52. Hashtable mainAttributes;
  53. ArrayList dependencies;
  54. ArrayList assemblies;
  55. Hashtable anames;
  56. ArrayList imports;
  57. ArrayList interfaces;
  58. ArrayList scripts;
  59. Type baseType;
  60. bool baseTypeIsGlobal;
  61. string className;
  62. RootBuilder rootBuilder;
  63. bool debug;
  64. string compilerOptions;
  65. string language;
  66. bool implicitLanguage;
  67. bool strictOn = false;
  68. bool explicitOn = false;
  69. bool linePragmasOn = false;
  70. bool output_cache;
  71. int oc_duration;
  72. string oc_header, oc_custom, oc_param, oc_controls;
  73. #if NET_2_0
  74. string oc_content_encodings;
  75. #endif
  76. bool oc_shared;
  77. OutputCacheLocation oc_location;
  78. CultureInfo invariantCulture = CultureInfo.InvariantCulture;
  79. #if NET_2_0
  80. string src;
  81. string partialClassName;
  82. string codeFileBaseClass;
  83. string metaResourceKey;
  84. Type codeFileBaseClassType;
  85. List <UnknownAttributeDescriptor> unknownMainAttributes;
  86. #endif
  87. Assembly srcAssembly;
  88. int appAssemblyIndex = -1;
  89. internal TemplateParser ()
  90. {
  91. LoadConfigDefaults ();
  92. imports = new ArrayList ();
  93. #if NET_2_0
  94. AddNamespaces (imports);
  95. #else
  96. imports.Add ("System");
  97. imports.Add ("System.Collections");
  98. imports.Add ("System.Collections.Specialized");
  99. imports.Add ("System.Configuration");
  100. imports.Add ("System.Text");
  101. imports.Add ("System.Text.RegularExpressions");
  102. imports.Add ("System.Web");
  103. imports.Add ("System.Web.Caching");
  104. imports.Add ("System.Web.Security");
  105. imports.Add ("System.Web.SessionState");
  106. imports.Add ("System.Web.UI");
  107. imports.Add ("System.Web.UI.WebControls");
  108. imports.Add ("System.Web.UI.HtmlControls");
  109. #endif
  110. assemblies = new ArrayList ();
  111. #if NET_2_0
  112. CompilationSection compConfig = CompilationConfig;
  113. bool addAssembliesInBin = false;
  114. foreach (AssemblyInfo info in compConfig.Assemblies) {
  115. if (info.Assembly == "*")
  116. addAssembliesInBin = true;
  117. else
  118. AddAssemblyByName (info.Assembly);
  119. }
  120. if (addAssembliesInBin)
  121. AddAssembliesInBin ();
  122. foreach (NamespaceInfo info in PagesConfig.Namespaces) {
  123. imports.Add (info.Namespace);
  124. }
  125. #else
  126. CompilationConfiguration compConfig = CompilationConfig;
  127. foreach (string a in compConfig.Assemblies)
  128. AddAssemblyByName (a);
  129. if (compConfig.AssembliesInBin)
  130. AddAssembliesInBin ();
  131. #endif
  132. language = compConfig.DefaultLanguage;
  133. implicitLanguage = true;
  134. }
  135. internal virtual void LoadConfigDefaults ()
  136. {
  137. debug = CompilationConfig.Debug;
  138. }
  139. internal void AddApplicationAssembly ()
  140. {
  141. if (Context.ApplicationInstance == null)
  142. return; // this may happen if we have Global.asax and have
  143. // controls registered from Web.Config
  144. string location = Context.ApplicationInstance.AssemblyLocation;
  145. if (location != typeof (TemplateParser).Assembly.Location) {
  146. appAssemblyIndex = assemblies.Add (location);
  147. }
  148. }
  149. protected abstract Type CompileIntoType ();
  150. #if NET_2_0
  151. void AddNamespaces (ArrayList imports)
  152. {
  153. if (BuildManager.HaveResources)
  154. imports.Add ("System.Resources");
  155. PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  156. if (pages == null)
  157. return;
  158. NamespaceCollection namespaces = pages.Namespaces;
  159. if (namespaces == null || namespaces.Count == 0)
  160. return;
  161. foreach (NamespaceInfo nsi in namespaces)
  162. imports.Add (nsi.Namespace);
  163. }
  164. #endif
  165. internal void RegisterCustomControl (string tagPrefix, string tagName, string src)
  166. {
  167. string realpath = MapPath (src);
  168. if (String.Compare (realpath, inputFile, false, invariantCulture) == 0)
  169. return;
  170. if (!File.Exists (realpath))
  171. throw new ParseException (Location, "Could not find file \"" + realpath + "\".");
  172. string vpath = VirtualPathUtility.Combine (BaseVirtualDir, src);
  173. Type type = null;
  174. AddDependency (realpath);
  175. try {
  176. ArrayList other_deps = new ArrayList ();
  177. type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, Context);
  178. foreach (string s in other_deps) {
  179. AddDependency (s);
  180. }
  181. } catch (ParseException pe) {
  182. if (this is UserControlParser)
  183. throw new ParseException (Location, pe.Message, pe);
  184. throw;
  185. }
  186. AddAssembly (type.Assembly, true);
  187. RootBuilder.Foundry.RegisterFoundry (tagPrefix, tagName, type);
  188. }
  189. internal void RegisterNamespace (string tagPrefix, string ns, string assembly)
  190. {
  191. AddImport (ns);
  192. Assembly ass = null;
  193. if (assembly != null && assembly.Length > 0) {
  194. ass = AddAssemblyByName (assembly);
  195. AddDependency (ass.Location);
  196. }
  197. RootBuilder.Foundry.RegisterFoundry (tagPrefix, ass, ns);
  198. }
  199. internal virtual void HandleOptions (object obj)
  200. {
  201. }
  202. internal static string GetOneKey (Hashtable tbl)
  203. {
  204. foreach (object key in tbl.Keys)
  205. return key.ToString ();
  206. return null;
  207. }
  208. internal virtual void AddDirective (string directive, Hashtable atts)
  209. {
  210. if (String.Compare (directive, DefaultDirectiveName, true) == 0) {
  211. if (mainAttributes != null)
  212. ThrowParseException ("Only 1 " + DefaultDirectiveName + " is allowed");
  213. mainAttributes = atts;
  214. ProcessMainAttributes (mainAttributes);
  215. return;
  216. }
  217. int cmp = String.Compare ("Assembly", directive, true);
  218. if (cmp == 0) {
  219. string name = GetString (atts, "Name", null);
  220. string src = GetString (atts, "Src", null);
  221. if (atts.Count > 0)
  222. ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
  223. if (name == null && src == null)
  224. ThrowParseException ("You gotta specify Src or Name");
  225. if (name != null && src != null)
  226. ThrowParseException ("Src and Name cannot be used together");
  227. if (name != null) {
  228. AddAssemblyByName (name);
  229. } else {
  230. GetAssemblyFromSource (src);
  231. }
  232. return;
  233. }
  234. cmp = String.Compare ("Import", directive, true);
  235. if (cmp == 0) {
  236. string namesp = GetString (atts, "Namespace", null);
  237. if (atts.Count > 0)
  238. ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
  239. if (namesp != null && namesp != "")
  240. AddImport (namesp);
  241. return;
  242. }
  243. cmp = String.Compare ("Implements", directive, true);
  244. if (cmp == 0) {
  245. string ifacename = GetString (atts, "Interface", "");
  246. if (atts.Count > 0)
  247. ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
  248. Type iface = LoadType (ifacename);
  249. if (iface == null)
  250. ThrowParseException ("Cannot find type " + ifacename);
  251. if (!iface.IsInterface)
  252. ThrowParseException (iface + " is not an interface");
  253. AddInterface (iface.FullName);
  254. return;
  255. }
  256. cmp = String.Compare ("OutputCache", directive, true);
  257. if (cmp == 0) {
  258. HttpResponse response = HttpContext.Current.Response;
  259. if (response != null)
  260. response.Cache.SetValidUntilExpires (true);
  261. output_cache = true;
  262. if (atts ["Duration"] == null)
  263. ThrowParseException ("The directive is missing a 'duration' attribute.");
  264. if (atts ["VaryByParam"] == null && atts ["VaryByControl"] == null)
  265. ThrowParseException ("This directive is missing 'VaryByParam' " +
  266. "or 'VaryByControl' attribute, which should be set to \"none\", \"*\", " +
  267. "or a list of name/value pairs.");
  268. foreach (DictionaryEntry entry in atts) {
  269. string key = (string) entry.Key;
  270. switch (key.ToLower ()) {
  271. case "duration":
  272. oc_duration = Int32.Parse ((string) entry.Value);
  273. if (oc_duration < 1)
  274. ThrowParseException ("The 'duration' attribute must be set " +
  275. "to a positive integer value");
  276. break;
  277. #if NET_2_0
  278. case "varybycontentencodings":
  279. oc_content_encodings = (string) entry.Value;
  280. break;
  281. #endif
  282. case "varybyparam":
  283. oc_param = (string) entry.Value;
  284. if (String.Compare (oc_param, "none") == 0)
  285. oc_param = null;
  286. break;
  287. case "varybyheader":
  288. oc_header = (string) entry.Value;
  289. break;
  290. case "varybycustom":
  291. oc_custom = (string) entry.Value;
  292. break;
  293. case "location":
  294. if (!(this is PageParser))
  295. goto default;
  296. try {
  297. oc_location = (OutputCacheLocation) Enum.Parse (
  298. typeof (OutputCacheLocation), (string) entry.Value, true);
  299. } catch {
  300. ThrowParseException ("The 'location' attribute is case sensitive and " +
  301. "must be one of the following values: Any, Client, " +
  302. "Downstream, Server, None, ServerAndClient.");
  303. }
  304. break;
  305. case "varybycontrol":
  306. #if ONLY_1_1
  307. if (this is PageParser)
  308. goto default;
  309. #endif
  310. oc_controls = (string) entry.Value;
  311. break;
  312. case "shared":
  313. if (this is PageParser)
  314. goto default;
  315. try {
  316. oc_shared = Boolean.Parse ((string) entry.Value);
  317. } catch {
  318. ThrowParseException ("The 'shared' attribute is case sensitive" +
  319. " and must be set to 'true' or 'false'.");
  320. }
  321. break;
  322. default:
  323. ThrowParseException ("The '" + key + "' attribute is not " +
  324. "supported by the 'Outputcache' directive.");
  325. break;
  326. }
  327. }
  328. return;
  329. }
  330. ThrowParseException ("Unknown directive: " + directive);
  331. }
  332. internal Type LoadType (string typeName)
  333. {
  334. Type type = HttpApplication.LoadType (typeName);
  335. if (type == null)
  336. return null;
  337. Assembly asm = type.Assembly;
  338. string location = asm.Location;
  339. AddDependency (location);
  340. string dirname = Path.GetDirectoryName (location);
  341. bool doAddAssembly = true;
  342. foreach (string dir in HttpApplication.BinDirectories) {
  343. if (dirname == dir) {
  344. doAddAssembly = false;
  345. break;
  346. }
  347. }
  348. if (doAddAssembly)
  349. AddAssembly (asm, true);
  350. return type;
  351. }
  352. void AddAssembliesInBin ()
  353. {
  354. foreach (string s in HttpApplication.BinDirectoryAssemblies)
  355. assemblies.Add (s);
  356. }
  357. internal virtual void AddInterface (string iface)
  358. {
  359. if (interfaces == null)
  360. interfaces = new ArrayList ();
  361. if (!interfaces.Contains (iface))
  362. interfaces.Add (iface);
  363. }
  364. internal virtual void AddImport (string namesp)
  365. {
  366. if (imports == null)
  367. imports = new ArrayList ();
  368. if (!imports.Contains (namesp))
  369. imports.Add (namesp);
  370. }
  371. internal virtual void AddSourceDependency (string filename)
  372. {
  373. if (dependencies != null && dependencies.Contains (filename)) {
  374. ThrowParseException ("Circular file references are not allowed. File: " + filename);
  375. }
  376. AddDependency (filename);
  377. }
  378. internal virtual void AddDependency (string filename)
  379. {
  380. if (filename == "")
  381. return;
  382. if (dependencies == null)
  383. dependencies = new ArrayList ();
  384. if (!dependencies.Contains (filename))
  385. dependencies.Add (filename);
  386. }
  387. internal virtual void AddAssembly (Assembly assembly, bool fullPath)
  388. {
  389. if (assembly.Location == "")
  390. return;
  391. if (anames == null)
  392. anames = new Hashtable ();
  393. string name = assembly.GetName ().Name;
  394. string loc = assembly.Location;
  395. if (fullPath) {
  396. if (!assemblies.Contains (loc)) {
  397. assemblies.Add (loc);
  398. }
  399. anames [name] = loc;
  400. anames [loc] = assembly;
  401. } else {
  402. if (!assemblies.Contains (name)) {
  403. assemblies.Add (name);
  404. }
  405. anames [name] = assembly;
  406. }
  407. }
  408. internal virtual Assembly AddAssemblyByFileName (string filename)
  409. {
  410. Assembly assembly = null;
  411. Exception error = null;
  412. try {
  413. assembly = Assembly.LoadFrom (filename);
  414. } catch (Exception e) { error = e; }
  415. if (assembly == null)
  416. ThrowParseException ("Assembly " + filename + " not found", error);
  417. AddAssembly (assembly, true);
  418. return assembly;
  419. }
  420. internal virtual Assembly AddAssemblyByName (string name)
  421. {
  422. if (anames == null)
  423. anames = new Hashtable ();
  424. if (anames.Contains (name)) {
  425. object o = anames [name];
  426. if (o is string)
  427. o = anames [o];
  428. return (Assembly) o;
  429. }
  430. Assembly assembly = null;
  431. Exception error = null;
  432. try {
  433. assembly = Assembly.Load (name);
  434. } catch (Exception e) { error = e; }
  435. if (assembly == null) {
  436. try {
  437. assembly = Assembly.LoadWithPartialName (name);
  438. } catch (Exception e) { error = e; }
  439. }
  440. if (assembly == null)
  441. ThrowParseException ("Assembly " + name + " not found", error);
  442. AddAssembly (assembly, true);
  443. return assembly;
  444. }
  445. internal virtual void ProcessMainAttributes (Hashtable atts)
  446. {
  447. #if NET_2_0
  448. CompilationSection compConfig;
  449. #else
  450. CompilationConfiguration compConfig;
  451. #endif
  452. compConfig = CompilationConfig;
  453. atts.Remove ("Description"); // ignored
  454. #if NET_1_1
  455. atts.Remove ("CodeBehind"); // ignored
  456. #endif
  457. atts.Remove ("AspCompat"); // ignored
  458. debug = GetBool (atts, "Debug", compConfig.Debug);
  459. compilerOptions = GetString (atts, "CompilerOptions", "");
  460. language = GetString (atts, "Language", "");
  461. if (language.Length != 0)
  462. implicitLanguage = false;
  463. else
  464. language = compConfig.DefaultLanguage;
  465. strictOn = GetBool (atts, "Strict", compConfig.Strict);
  466. explicitOn = GetBool (atts, "Explicit", compConfig.Explicit);
  467. linePragmasOn = GetBool (atts, "LinePragmas", false);
  468. string inherits = GetString (atts, "Inherits", null);
  469. #if NET_2_0
  470. // In ASP 2, the source file is actually integrated with
  471. // the generated file via the use of partial classes. This
  472. // means that the code file has to be confirmed, but not
  473. // used at this point.
  474. src = GetString (atts, "CodeFile", null);
  475. codeFileBaseClass = GetString (atts, "CodeFileBaseClass", null);
  476. if (src == null && codeFileBaseClass != null)
  477. ThrowParseException ("The 'CodeFileBaseClass' attribute cannot be used without a 'CodeFile' attribute");
  478. string legacySrc = GetString (atts, "Src", null);
  479. if (legacySrc != null) {
  480. if (src == null)
  481. src = legacySrc;
  482. else
  483. // We need to compile it even though CodeFile is present, to
  484. // report errors.
  485. GetAssemblyFromSource (legacySrc);
  486. AddDependency (MapPath (legacySrc, false));
  487. }
  488. if (src != null && inherits != null) {
  489. // Make sure the source exists
  490. src = UrlUtils.Combine (BaseVirtualDir, src);
  491. string realPath = MapPath (src, false);
  492. if (!File.Exists (realPath))
  493. ThrowParseException ("File " + src + " not found");
  494. // We are going to create a partial class that shares
  495. // the same name as the inherits tag, so reset the
  496. // name. The base type is changed because it is the
  497. // code file's responsibilty to extend the classes
  498. // needed.
  499. partialClassName = inherits;
  500. // Add the code file as an option to the
  501. // compiler. This lets both files be compiled at once.
  502. compilerOptions += " \"" + realPath + "\"";
  503. if (codeFileBaseClass != null) {
  504. try {
  505. codeFileBaseClassType = LoadType (codeFileBaseClass);
  506. } catch (Exception) {
  507. }
  508. if (codeFileBaseClassType == null)
  509. ThrowParseException ("Could not load type '{0}'", codeFileBaseClass);
  510. }
  511. } else if (inherits != null) {
  512. // We just set the inherits directly because this is a
  513. // Single-Page model.
  514. SetBaseType (inherits);
  515. }
  516. #else
  517. string src = GetString (atts, "Src", null);
  518. if (src != null)
  519. srcAssembly = GetAssemblyFromSource (src);
  520. if (inherits != null)
  521. SetBaseType (inherits);
  522. #endif
  523. if (src != null)
  524. AddDependency (MapPath (src, false));
  525. className = GetString (atts, "ClassName", null);
  526. if (className != null) {
  527. #if NET_2_0
  528. string [] identifiers = className.Split ('.');
  529. for (int i = 0; i < identifiers.Length; i++)
  530. if (!CodeGenerator.IsValidLanguageIndependentIdentifier (identifiers [i]))
  531. ThrowParseException (String.Format ("'{0}' is not a valid "
  532. + "value for attribute 'classname'.", className));
  533. #else
  534. if (!CodeGenerator.IsValidLanguageIndependentIdentifier (className))
  535. ThrowParseException (String.Format ("'{0}' is not a valid "
  536. + "value for attribute 'classname'.", className));
  537. #endif
  538. }
  539. #if NET_2_0
  540. if (this is TemplateControlParser)
  541. metaResourceKey = GetString (atts, "meta:resourcekey", null);
  542. if (inherits != null && (this is PageParser || this is UserControlParser) && atts.Count > 0) {
  543. if (unknownMainAttributes == null)
  544. unknownMainAttributes = new List <UnknownAttributeDescriptor> ();
  545. string key, val;
  546. foreach (DictionaryEntry de in atts) {
  547. key = de.Key as string;
  548. val = de.Value as string;
  549. if (String.IsNullOrEmpty (key) || String.IsNullOrEmpty (val))
  550. continue;
  551. CheckUnknownAttribute (key, val, inherits);
  552. }
  553. return;
  554. }
  555. #endif
  556. if (atts.Count > 0)
  557. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  558. }
  559. #if NET_2_0
  560. void CheckUnknownAttribute (string name, string val, string inherits)
  561. {
  562. MemberInfo mi = null;
  563. bool missing = false;
  564. string memberName = name.Trim ().ToLower (CultureInfo.InvariantCulture);
  565. Type parent = codeFileBaseClassType;
  566. if (parent == null)
  567. parent = baseType;
  568. try {
  569. MemberInfo[] infos = parent.GetMember (memberName,
  570. MemberTypes.Field | MemberTypes.Property,
  571. BindingFlags.Public | BindingFlags.Instance |
  572. BindingFlags.IgnoreCase | BindingFlags.Static);
  573. if (infos.Length != 0) {
  574. // prefer public properties to public methods (it's what MS.NET does)
  575. foreach (MemberInfo tmp in infos) {
  576. if (tmp is PropertyInfo) {
  577. mi = tmp;
  578. break;
  579. }
  580. }
  581. if (mi == null)
  582. mi = infos [0];
  583. } else
  584. missing = true;
  585. } catch (Exception) {
  586. missing = true;
  587. }
  588. if (missing)
  589. ThrowParseException (
  590. "Error parsing attribute '{0}': Type '{1}' does not have a public property named '{0}'",
  591. memberName, inherits);
  592. Type memberType = null;
  593. if (mi is PropertyInfo) {
  594. PropertyInfo pi = mi as PropertyInfo;
  595. if (!pi.CanWrite)
  596. ThrowParseException (
  597. "Error parsing attribute '{0}': The '{0}' property is read-only and cannot be set.",
  598. memberName);
  599. memberType = pi.PropertyType;
  600. } else if (mi is FieldInfo) {
  601. memberType = ((FieldInfo)mi).FieldType;
  602. } else
  603. ThrowParseException ("Could not determine member the kind of '{0}' in base type '{1}",
  604. memberName, inherits);
  605. TypeConverter converter = TypeDescriptor.GetConverter (memberType);
  606. bool convertible = true;
  607. object value = null;
  608. if (converter == null || !converter.CanConvertFrom (typeof (string)))
  609. convertible = false;
  610. if (convertible) {
  611. try {
  612. value = converter.ConvertFromInvariantString (val);
  613. } catch (Exception) {
  614. convertible = false;
  615. }
  616. }
  617. if (!convertible)
  618. ThrowParseException ("Error parsing attribute '{0}': Cannot create an object of type '{1}' from its string representation '{2}' for the '{3}' property.",
  619. memberName, memberType, val, mi.Name);
  620. UnknownAttributeDescriptor desc = new UnknownAttributeDescriptor (mi, value);
  621. unknownMainAttributes.Add (desc);
  622. }
  623. #endif
  624. void CheckIfBaseTypeIsGlobal ()
  625. {
  626. if (baseType == null)
  627. return;
  628. // If we have a fully-qualified type name, then we don't need to use the
  629. // global:: prefix
  630. if (baseType.FullName.IndexOf ('.') == -1)
  631. baseTypeIsGlobal = true;
  632. else
  633. baseTypeIsGlobal = false;
  634. }
  635. internal void SetBaseType (string type)
  636. {
  637. if (type == DefaultBaseTypeName) {
  638. baseType = DefaultBaseType;
  639. CheckIfBaseTypeIsGlobal ();
  640. return;
  641. }
  642. Type parent = null;
  643. if (srcAssembly != null)
  644. parent = srcAssembly.GetType (type);
  645. if (parent == null)
  646. parent = LoadType (type);
  647. if (parent == null)
  648. ThrowParseException ("Cannot find type " + type);
  649. if (!DefaultBaseType.IsAssignableFrom (parent))
  650. ThrowParseException ("The parent type does not derive from " + DefaultBaseType);
  651. baseType = parent;
  652. CheckIfBaseTypeIsGlobal ();
  653. }
  654. internal void SetLanguage (string language)
  655. {
  656. this.language = language;
  657. implicitLanguage = false;
  658. }
  659. Assembly GetAssemblyFromSource (string vpath)
  660. {
  661. vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
  662. string realPath = MapPath (vpath, false);
  663. if (!File.Exists (realPath))
  664. ThrowParseException ("File " + vpath + " not found");
  665. AddSourceDependency (realPath);
  666. CompilerResults result = CachingCompiler.Compile (language, realPath, realPath, assemblies);
  667. if (result.NativeCompilerReturnValue != 0) {
  668. StreamReader reader = new StreamReader (realPath);
  669. throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
  670. }
  671. AddAssembly (result.CompiledAssembly, true);
  672. return result.CompiledAssembly;
  673. }
  674. internal abstract Type DefaultBaseType { get; }
  675. internal abstract string DefaultBaseTypeName { get; }
  676. internal abstract string DefaultDirectiveName { get; }
  677. internal string InputFile
  678. {
  679. get { return inputFile; }
  680. set { inputFile = value; }
  681. }
  682. #if NET_2_0
  683. internal bool IsPartial {
  684. get { return src != null; }
  685. }
  686. internal string PartialClassName {
  687. get { return partialClassName; }
  688. }
  689. internal string CodeFileBaseClass {
  690. get { return codeFileBaseClass; }
  691. }
  692. internal string MetaResourceKey {
  693. get { return metaResourceKey; }
  694. }
  695. internal Type CodeFileBaseClassType
  696. {
  697. get { return codeFileBaseClassType; }
  698. }
  699. internal List <UnknownAttributeDescriptor> UnknownMainAttributes
  700. {
  701. get { return unknownMainAttributes; }
  702. }
  703. #endif
  704. internal string Text {
  705. get { return text; }
  706. set { text = value; }
  707. }
  708. internal Type BaseType {
  709. get {
  710. if (baseType == null)
  711. SetBaseType (DefaultBaseTypeName);
  712. return baseType;
  713. }
  714. }
  715. internal bool BaseTypeIsGlobal {
  716. get { return baseTypeIsGlobal; }
  717. }
  718. internal string ClassName {
  719. get {
  720. if (className != null)
  721. return className;
  722. #if NET_2_0
  723. string physPath = HttpContext.Current.Request.PhysicalApplicationPath;
  724. if (StrUtils.StartsWith (inputFile, physPath)) {
  725. className = inputFile.Substring (physPath.Length).ToLower (CultureInfo.InvariantCulture);
  726. className = className.Replace ('.', '_');
  727. className = className.Replace ('/', '_').Replace ('\\', '_');
  728. } else
  729. #endif
  730. className = Path.GetFileName (inputFile).Replace ('.', '_');
  731. className = className.Replace ('-', '_');
  732. className = className.Replace (' ', '_');
  733. if (Char.IsDigit(className[0])) {
  734. className = "_" + className;
  735. }
  736. return className;
  737. }
  738. }
  739. internal ArrayList Scripts {
  740. get {
  741. if (scripts == null)
  742. scripts = new ArrayList ();
  743. return scripts;
  744. }
  745. }
  746. internal ArrayList Imports {
  747. get { return imports; }
  748. }
  749. internal ArrayList Assemblies {
  750. get {
  751. if (appAssemblyIndex != -1) {
  752. object o = assemblies [appAssemblyIndex];
  753. assemblies.RemoveAt (appAssemblyIndex);
  754. assemblies.Add (o);
  755. appAssemblyIndex = -1;
  756. }
  757. return assemblies;
  758. }
  759. }
  760. internal ArrayList Interfaces {
  761. get { return interfaces; }
  762. }
  763. internal RootBuilder RootBuilder {
  764. get { return rootBuilder; }
  765. set { rootBuilder = value; }
  766. }
  767. internal ArrayList Dependencies {
  768. get { return dependencies; }
  769. set { dependencies = value; }
  770. }
  771. internal string CompilerOptions {
  772. get { return compilerOptions; }
  773. }
  774. internal string Language {
  775. get { return language; }
  776. }
  777. internal bool ImplicitLanguage {
  778. get { return implicitLanguage; }
  779. }
  780. internal bool StrictOn {
  781. get { return strictOn; }
  782. }
  783. internal bool ExplicitOn {
  784. get { return explicitOn; }
  785. }
  786. internal bool Debug {
  787. get { return debug; }
  788. }
  789. internal bool OutputCache {
  790. get { return output_cache; }
  791. }
  792. internal int OutputCacheDuration {
  793. get { return oc_duration; }
  794. }
  795. #if NET_2_0
  796. internal string OutputCacheVaryByContentEncodings {
  797. get { return oc_content_encodings; }
  798. }
  799. #endif
  800. internal string OutputCacheVaryByHeader {
  801. get { return oc_header; }
  802. }
  803. internal string OutputCacheVaryByCustom {
  804. get { return oc_custom; }
  805. }
  806. internal string OutputCacheVaryByControls {
  807. get { return oc_controls; }
  808. }
  809. internal bool OutputCacheShared {
  810. get { return oc_shared; }
  811. }
  812. internal OutputCacheLocation OutputCacheLocation {
  813. get { return oc_location; }
  814. }
  815. internal string OutputCacheVaryByParam {
  816. get { return oc_param; }
  817. }
  818. #if NET_2_0
  819. internal PagesSection PagesConfig {
  820. get {
  821. return WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  822. }
  823. }
  824. #else
  825. internal PagesConfiguration PagesConfig {
  826. get { return PagesConfiguration.GetInstance (Context); }
  827. }
  828. #endif
  829. }
  830. }