TemplateParser.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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 = UrlUtils.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)
  255. ThrowParseException ("This directive is missing a 'VaryByParam' " +
  256. "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 (this is PageParser)
  292. goto default;
  293. oc_controls = (string) entry.Value;
  294. break;
  295. case "shared":
  296. if (this is PageParser)
  297. goto default;
  298. try {
  299. oc_shared = Boolean.Parse ((string) entry.Value);
  300. } catch {
  301. ThrowParseException ("The 'shared' attribute is case sensitive" +
  302. " and must be set to 'true' or 'false'.");
  303. }
  304. break;
  305. default:
  306. ThrowParseException ("The '" + key + "' attribute is not " +
  307. "supported by the 'Outputcache' directive.");
  308. break;
  309. }
  310. }
  311. return;
  312. }
  313. ThrowParseException ("Unknown directive: " + directive);
  314. }
  315. internal Type LoadType (string typeName)
  316. {
  317. Type type = HttpApplication.LoadType (typeName);
  318. if (type == null)
  319. return null;
  320. Assembly asm = type.Assembly;
  321. string location = asm.Location;
  322. AddDependency (location);
  323. if (Path.GetDirectoryName (location) != PrivateBinPath)
  324. AddAssembly (asm, true);
  325. return type;
  326. }
  327. void AddAssembliesInBin ()
  328. {
  329. if (!Directory.Exists (PrivateBinPath))
  330. return;
  331. string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
  332. foreach (string s in binDlls)
  333. assemblies.Add (s);
  334. }
  335. internal virtual void AddInterface (string iface)
  336. {
  337. if (interfaces == null)
  338. interfaces = new ArrayList ();
  339. if (!interfaces.Contains (iface))
  340. interfaces.Add (iface);
  341. }
  342. internal virtual void AddImport (string namesp)
  343. {
  344. if (imports == null)
  345. imports = new ArrayList ();
  346. if (!imports.Contains (namesp))
  347. imports.Add (namesp);
  348. }
  349. internal virtual void AddSourceDependency (string filename)
  350. {
  351. if (dependencies != null && dependencies.Contains (filename)) {
  352. ThrowParseException ("Circular file references are not allowed. File: " + filename);
  353. }
  354. AddDependency (filename);
  355. }
  356. internal virtual void AddDependency (string filename)
  357. {
  358. if (filename == "")
  359. return;
  360. if (dependencies == null)
  361. dependencies = new ArrayList ();
  362. if (!dependencies.Contains (filename))
  363. dependencies.Add (filename);
  364. }
  365. internal virtual void AddAssembly (Assembly assembly, bool fullPath)
  366. {
  367. if (assembly.Location == "")
  368. return;
  369. if (anames == null)
  370. anames = new Hashtable ();
  371. string name = assembly.GetName ().Name;
  372. string loc = assembly.Location;
  373. if (fullPath) {
  374. if (!assemblies.Contains (loc)) {
  375. assemblies.Add (loc);
  376. }
  377. anames [name] = loc;
  378. anames [loc] = assembly;
  379. } else {
  380. if (!assemblies.Contains (name)) {
  381. assemblies.Add (name);
  382. }
  383. anames [name] = assembly;
  384. }
  385. }
  386. internal virtual Assembly AddAssemblyByFileName (string filename)
  387. {
  388. Assembly assembly = null;
  389. Exception error = null;
  390. try {
  391. assembly = Assembly.LoadFrom (filename);
  392. } catch (Exception e) { error = e; }
  393. if (assembly == null)
  394. ThrowParseException ("Assembly " + filename + " not found", error);
  395. AddAssembly (assembly, true);
  396. return assembly;
  397. }
  398. internal virtual Assembly AddAssemblyByName (string name)
  399. {
  400. if (anames == null)
  401. anames = new Hashtable ();
  402. if (anames.Contains (name)) {
  403. object o = anames [name];
  404. if (o is string)
  405. o = anames [o];
  406. return (Assembly) o;
  407. }
  408. Assembly assembly = null;
  409. Exception error = null;
  410. if (name.IndexOf (',') != -1) {
  411. try {
  412. assembly = Assembly.Load (name);
  413. } catch (Exception e) { error = e; }
  414. }
  415. if (assembly == null) {
  416. try {
  417. assembly = Assembly.LoadWithPartialName (name);
  418. } catch (Exception e) { error = e; }
  419. }
  420. if (assembly == null)
  421. ThrowParseException ("Assembly " + name + " not found", error);
  422. AddAssembly (assembly, true);
  423. return assembly;
  424. }
  425. internal virtual void ProcessMainAttributes (Hashtable atts)
  426. {
  427. atts.Remove ("Description"); // ignored
  428. #if NET_1_1
  429. atts.Remove ("CodeBehind"); // ignored
  430. #endif
  431. atts.Remove ("AspCompat"); // ignored
  432. #if NET_2_0
  433. // these two are ignored for the moment
  434. atts.Remove ("Async");
  435. atts.Remove ("AsyncTimeOut");
  436. #endif
  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. className = GetString (atts, "ClassName", null);
  493. if (className != null) {
  494. #if NET_2_0
  495. string [] identifiers = className.Split ('.');
  496. for (int i = 0; i < identifiers.Length; i++)
  497. if (!CodeGenerator.IsValidLanguageIndependentIdentifier (identifiers [i]))
  498. ThrowParseException (String.Format ("'{0}' is not a valid "
  499. + "value for attribute 'classname'.", className));
  500. #else
  501. if (!CodeGenerator.IsValidLanguageIndependentIdentifier (className))
  502. ThrowParseException (String.Format ("'{0}' is not a valid "
  503. + "value for attribute 'classname'.", className));
  504. #endif
  505. }
  506. #if NET_2_0
  507. if (this is TemplateControlParser)
  508. metaResourceKey = GetString (atts, "meta:resourcekey", null);
  509. if (inherits != null && (this is PageParser || this is UserControlParser) && atts.Count > 0) {
  510. if (unknownMainAttributes == null)
  511. unknownMainAttributes = new List <UnknownAttributeDescriptor> ();
  512. string key, val;
  513. foreach (DictionaryEntry de in atts) {
  514. key = de.Key as string;
  515. val = de.Value as string;
  516. if (String.IsNullOrEmpty (key) || String.IsNullOrEmpty (val))
  517. continue;
  518. CheckUnknownAttribute (key, val, inherits);
  519. }
  520. return;
  521. }
  522. #endif
  523. if (atts.Count > 0)
  524. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  525. }
  526. #if NET_2_0
  527. void CheckUnknownAttribute (string name, string val, string inherits)
  528. {
  529. MemberInfo mi = null;
  530. bool missing = false;
  531. string memberName = name.Trim ().ToLower (CultureInfo.InvariantCulture);
  532. Type parent = codeFileBaseClassType;
  533. if (parent == null)
  534. parent = baseType;
  535. try {
  536. MemberInfo[] infos = parent.GetMember (memberName,
  537. MemberTypes.Field | MemberTypes.Property,
  538. BindingFlags.Public | BindingFlags.Instance |
  539. BindingFlags.IgnoreCase | BindingFlags.Static);
  540. if (infos.Length != 0) {
  541. // prefer public properties to public methods (it's what MS.NET does)
  542. foreach (MemberInfo tmp in infos) {
  543. if (tmp is PropertyInfo) {
  544. mi = tmp;
  545. break;
  546. }
  547. }
  548. if (mi == null)
  549. mi = infos [0];
  550. } else
  551. missing = true;
  552. } catch (Exception) {
  553. missing = true;
  554. }
  555. if (missing)
  556. ThrowParseException (
  557. "Error parsing attribute '{0}': Type '{1}' does not have a public property named '{0}'",
  558. memberName, inherits);
  559. Type memberType = null;
  560. if (mi is PropertyInfo) {
  561. PropertyInfo pi = mi as PropertyInfo;
  562. if (!pi.CanWrite)
  563. ThrowParseException (
  564. "Error parsing attribute '{0}': The '{0}' property is read-only and cannot be set.",
  565. memberName);
  566. memberType = pi.PropertyType;
  567. } else if (mi is FieldInfo) {
  568. memberType = ((FieldInfo)mi).FieldType;
  569. } else
  570. ThrowParseException ("Could not determine member the kind of '{0}' in base type '{1}",
  571. memberName, inherits);
  572. TypeConverter converter = TypeDescriptor.GetConverter (memberType);
  573. bool convertible = true;
  574. object value = null;
  575. if (converter == null || !converter.CanConvertFrom (typeof (string)))
  576. convertible = false;
  577. if (convertible) {
  578. try {
  579. value = converter.ConvertFromInvariantString (val);
  580. } catch (Exception) {
  581. convertible = false;
  582. }
  583. }
  584. if (!convertible)
  585. ThrowParseException ("Error parsing attribute '{0}': Cannot create an object of type '{1}' from its string representation '{2}' for the '{3}' property.",
  586. memberName, memberType, val, mi.Name);
  587. UnknownAttributeDescriptor desc = new UnknownAttributeDescriptor (mi, value);
  588. unknownMainAttributes.Add (desc);
  589. }
  590. #endif
  591. internal void SetBaseType (string type)
  592. {
  593. if (type == DefaultBaseTypeName)
  594. return;
  595. Type parent = null;
  596. if (srcAssembly != null)
  597. parent = srcAssembly.GetType (type);
  598. if (parent == null)
  599. parent = LoadType (type);
  600. if (parent == null)
  601. ThrowParseException ("Cannot find type " + type);
  602. if (!DefaultBaseType.IsAssignableFrom (parent))
  603. ThrowParseException ("The parent type does not derive from " + DefaultBaseType);
  604. baseType = parent;
  605. if (parent.FullName.IndexOf ('.') == -1)
  606. baseTypeIsGlobal = true;
  607. }
  608. internal void SetLanguage (string language)
  609. {
  610. this.language = language;
  611. implicitLanguage = false;
  612. }
  613. Assembly GetAssemblyFromSource (string vpath)
  614. {
  615. vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
  616. string realPath = MapPath (vpath, false);
  617. if (!File.Exists (realPath))
  618. ThrowParseException ("File " + vpath + " not found");
  619. AddSourceDependency (realPath);
  620. CompilerResults result = CachingCompiler.Compile (language, realPath, realPath, assemblies);
  621. if (result.NativeCompilerReturnValue != 0) {
  622. StreamReader reader = new StreamReader (realPath);
  623. throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
  624. }
  625. AddAssembly (result.CompiledAssembly, true);
  626. return result.CompiledAssembly;
  627. }
  628. internal abstract Type DefaultBaseType { get; }
  629. internal abstract string DefaultBaseTypeName { get; }
  630. internal abstract string DefaultDirectiveName { get; }
  631. internal string InputFile
  632. {
  633. get { return inputFile; }
  634. set { inputFile = value; }
  635. }
  636. #if NET_2_0
  637. internal bool IsPartial {
  638. get { return src != null; }
  639. }
  640. internal string PartialClassName {
  641. get { return partialClassName; }
  642. }
  643. internal string CodeFileBaseClass {
  644. get { return codeFileBaseClass; }
  645. }
  646. internal string MetaResourceKey {
  647. get { return metaResourceKey; }
  648. }
  649. internal Type CodeFileBaseClassType
  650. {
  651. get { return codeFileBaseClassType; }
  652. }
  653. internal List <UnknownAttributeDescriptor> UnknownMainAttributes
  654. {
  655. get { return unknownMainAttributes; }
  656. }
  657. #endif
  658. internal string Text
  659. {
  660. get { return text; }
  661. set { text = value; }
  662. }
  663. internal Type BaseType
  664. {
  665. get {
  666. if (baseType == null)
  667. baseType = DefaultBaseType;
  668. return baseType;
  669. }
  670. }
  671. internal bool BaseTypeIsGlobal {
  672. get { return baseTypeIsGlobal; }
  673. }
  674. internal string ClassName {
  675. get {
  676. if (className != null)
  677. return className;
  678. #if NET_2_0
  679. string physPath = HttpContext.Current.Request.PhysicalApplicationPath;
  680. if (StrUtils.StartsWith (inputFile, physPath)) {
  681. className = inputFile.Substring (physPath.Length).ToLower (CultureInfo.InvariantCulture);
  682. className = className.Replace ('.', '_');
  683. className = className.Replace ('/', '_').Replace ('\\', '_');
  684. } else
  685. #endif
  686. className = Path.GetFileName (inputFile).Replace ('.', '_');
  687. className = className.Replace ('-', '_');
  688. className = className.Replace (' ', '_');
  689. if (Char.IsDigit(className[0])) {
  690. className = "_" + className;
  691. }
  692. return className;
  693. }
  694. }
  695. internal string PrivateBinPath {
  696. get {
  697. if (privateBinPath != null)
  698. return privateBinPath;
  699. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  700. privateBinPath = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
  701. return privateBinPath;
  702. }
  703. }
  704. internal ArrayList Scripts {
  705. get {
  706. if (scripts == null)
  707. scripts = new ArrayList ();
  708. return scripts;
  709. }
  710. }
  711. internal ArrayList Imports {
  712. get { return imports; }
  713. }
  714. internal ArrayList Assemblies {
  715. get {
  716. if (appAssemblyIndex != -1) {
  717. object o = assemblies [appAssemblyIndex];
  718. assemblies.RemoveAt (appAssemblyIndex);
  719. assemblies.Add (o);
  720. appAssemblyIndex = -1;
  721. }
  722. return assemblies;
  723. }
  724. }
  725. internal ArrayList Interfaces {
  726. get { return interfaces; }
  727. }
  728. internal RootBuilder RootBuilder {
  729. get { return rootBuilder; }
  730. set { rootBuilder = value; }
  731. }
  732. internal ArrayList Dependencies {
  733. get { return dependencies; }
  734. set { dependencies = value; }
  735. }
  736. internal string CompilerOptions {
  737. get { return compilerOptions; }
  738. }
  739. internal string Language {
  740. get { return language; }
  741. }
  742. internal bool ImplicitLanguage {
  743. get { return implicitLanguage; }
  744. }
  745. internal bool StrictOn {
  746. get { return strictOn; }
  747. }
  748. internal bool ExplicitOn {
  749. get { return explicitOn; }
  750. }
  751. internal bool Debug {
  752. get { return debug; }
  753. }
  754. internal bool OutputCache {
  755. get { return output_cache; }
  756. }
  757. internal int OutputCacheDuration {
  758. get { return oc_duration; }
  759. }
  760. internal string OutputCacheVaryByHeader {
  761. get { return oc_header; }
  762. }
  763. internal string OutputCacheVaryByCustom {
  764. get { return oc_custom; }
  765. }
  766. internal string OutputCacheVaryByControls {
  767. get { return oc_controls; }
  768. }
  769. internal bool OutputCacheShared {
  770. get { return oc_shared; }
  771. }
  772. internal OutputCacheLocation OutputCacheLocation {
  773. get { return oc_location; }
  774. }
  775. internal string OutputCacheVaryByParam {
  776. get { return oc_param; }
  777. }
  778. #if NET_2_0
  779. internal PagesSection PagesConfig {
  780. get {
  781. return WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  782. }
  783. }
  784. #else
  785. internal PagesConfiguration PagesConfig {
  786. get { return PagesConfiguration.GetInstance (Context); }
  787. }
  788. #endif
  789. }
  790. }