TemplateParser.cs 25 KB

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