TemplateParser.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. //
  10. using System;
  11. using System.CodeDom.Compiler;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Web;
  16. using System.Web.Compilation;
  17. using System.Web.Configuration;
  18. using System.Web.Util;
  19. namespace System.Web.UI
  20. {
  21. public abstract class TemplateParser : BaseParser
  22. {
  23. string inputFile;
  24. string text;
  25. string privateBinPath;
  26. Hashtable mainAttributes;
  27. ArrayList dependencies;
  28. ArrayList assemblies;
  29. Hashtable anames;
  30. ArrayList imports;
  31. ArrayList interfaces;
  32. ArrayList scripts;
  33. Type baseType;
  34. string className;
  35. RootBuilder rootBuilder;
  36. bool debug;
  37. string compilerOptions;
  38. string language;
  39. bool output_cache;
  40. int oc_duration;
  41. string oc_header, oc_custom, oc_param, oc_controls;
  42. bool oc_shared;
  43. OutputCacheLocation oc_location;
  44. Assembly srcAssembly;
  45. internal TemplateParser ()
  46. {
  47. imports = new ArrayList ();
  48. imports.Add ("System");
  49. imports.Add ("System.Collections");
  50. imports.Add ("System.Collections.Specialized");
  51. imports.Add ("System.Configuration");
  52. imports.Add ("System.Text");
  53. imports.Add ("System.Text.RegularExpressions");
  54. imports.Add ("System.Web");
  55. imports.Add ("System.Web.Caching");
  56. imports.Add ("System.Web.Security");
  57. imports.Add ("System.Web.SessionState");
  58. imports.Add ("System.Web.UI");
  59. imports.Add ("System.Web.UI.WebControls");
  60. imports.Add ("System.Web.UI.HtmlControls");
  61. assemblies = new ArrayList ();
  62. assemblies.AddRange (CompilationConfig.Assemblies);
  63. if (CompilationConfig.AssembliesInBin)
  64. AddAssembliesInBin ();
  65. language = CompilationConfig.DefaultLanguage;
  66. if (GlobalAsaxCompiler.Assemblies != null) {
  67. foreach (string assembly in GlobalAsaxCompiler.Assemblies) {
  68. if (!assemblies.Contains (assembly)) {
  69. AddAssemblyByName (assembly);
  70. }
  71. }
  72. }
  73. if (GlobalAsaxCompiler.Imports != null) {
  74. foreach (string import in GlobalAsaxCompiler.Imports) {
  75. if (!imports.Contains (import)) {
  76. imports.Add (import);
  77. }
  78. }
  79. }
  80. }
  81. internal void AddApplicationAssembly ()
  82. {
  83. string location = Context.ApplicationInstance.AssemblyLocation;
  84. if (location != typeof (TemplateParser).Assembly.Location)
  85. assemblies.Add (location);
  86. }
  87. protected abstract Type CompileIntoType ();
  88. internal virtual void HandleOptions (object obj)
  89. {
  90. }
  91. internal static string GetOneKey (Hashtable tbl)
  92. {
  93. foreach (object key in tbl.Keys)
  94. return key.ToString ();
  95. return null;
  96. }
  97. internal virtual void AddDirective (string directive, Hashtable atts)
  98. {
  99. if (String.Compare (directive, DefaultDirectiveName, true) == 0) {
  100. if (mainAttributes != null)
  101. ThrowParseException ("Only 1 " + DefaultDirectiveName + " is allowed");
  102. mainAttributes = atts;
  103. ProcessMainAttributes (mainAttributes);
  104. return;
  105. }
  106. int cmp = String.Compare ("Assembly", directive, true);
  107. if (cmp == 0) {
  108. string name = GetString (atts, "Name", null);
  109. string src = GetString (atts, "Src", null);
  110. if (atts.Count > 0)
  111. ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
  112. if (name == null && src == null)
  113. ThrowParseException ("You gotta specify Src or Name");
  114. if (name != null && src != null)
  115. ThrowParseException ("Src and Name cannot be used together");
  116. if (name != null) {
  117. AddAssemblyByName (name);
  118. } else {
  119. GetAssemblyFromSource (src);
  120. }
  121. return;
  122. }
  123. cmp = String.Compare ("Import", directive, true);
  124. if (cmp == 0) {
  125. string namesp = GetString (atts, "Namespace", null);
  126. if (atts.Count > 0)
  127. ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
  128. if (namesp != null && namesp != "")
  129. AddImport (namesp);
  130. return;
  131. }
  132. cmp = String.Compare ("Implements", directive, true);
  133. if (cmp == 0) {
  134. string ifacename = GetString (atts, "Interface", "");
  135. if (atts.Count > 0)
  136. ThrowParseException ("Attribute " + GetOneKey (atts) + " unknown.");
  137. Type iface = LoadType (ifacename);
  138. if (iface == null)
  139. ThrowParseException ("Cannot find type " + ifacename);
  140. if (!iface.IsInterface)
  141. ThrowParseException (iface + " is not an interface");
  142. AddInterface (iface.FullName);
  143. return;
  144. }
  145. cmp = String.Compare ("OutputCache", directive, true);
  146. if (cmp == 0) {
  147. output_cache = true;
  148. if (atts ["Duration"] == null)
  149. ThrowParseException ("The directive is missing a 'duration' attribute.");
  150. if (atts ["VaryByParam"] == null)
  151. ThrowParseException ("This directive is missing a 'VaryByParam' " +
  152. "attribute, which should be set to \"none\", \"*\", " +
  153. "or a list of name/value pairs.");
  154. foreach (DictionaryEntry entry in atts) {
  155. string key = (string) entry.Key;
  156. switch (key.ToLower ()) {
  157. case "duration":
  158. oc_duration = Int32.Parse ((string) entry.Value);
  159. if (oc_duration < 1)
  160. ThrowParseException ("The 'duration' attribute must be set " +
  161. "to a positive integer value");
  162. break;
  163. case "varybyparam":
  164. oc_param = (string) entry.Value;
  165. if (String.Compare (oc_param, "none") == 0)
  166. oc_param = null;
  167. break;
  168. case "varybyheader":
  169. oc_header = (string) entry.Value;
  170. break;
  171. case "varybycustom":
  172. oc_custom = (string) entry.Value;
  173. break;
  174. case "location":
  175. if (!(this is PageParser))
  176. goto default;
  177. try {
  178. oc_location = (OutputCacheLocation) Enum.Parse (
  179. typeof (OutputCacheLocation), (string) entry.Value, true);
  180. } catch {
  181. ThrowParseException ("The 'location' attribute is case sensitive and " +
  182. "must be one of the following values: Any, Client, " +
  183. "Downstream, Server, None, ServerAndClient.");
  184. }
  185. break;
  186. case "varybycontrol":
  187. if (this is PageParser)
  188. goto default;
  189. oc_controls = (string) entry.Value;
  190. break;
  191. case "shared":
  192. if (this is PageParser)
  193. goto default;
  194. try {
  195. oc_shared = Boolean.Parse ((string) entry.Value);
  196. } catch {
  197. ThrowParseException ("The 'shared' attribute is case sensitive" +
  198. " and must be set to 'true' or 'false'.");
  199. }
  200. break;
  201. default:
  202. ThrowParseException ("The '" + key + "' attribute is not " +
  203. "supported by the 'Outputcache' directive.");
  204. break;
  205. }
  206. }
  207. return;
  208. }
  209. ThrowParseException ("Unknown directive: " + directive);
  210. }
  211. internal Type LoadType (string typeName)
  212. {
  213. // First try loaded assemblies, then try assemblies in Bin directory.
  214. // By now i do this 'by hand' but may be this is a runtime/gac task.
  215. Type type = null;
  216. Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies ();
  217. foreach (Assembly ass in assemblies) {
  218. type = ass.GetType (typeName);
  219. if (type != null) {
  220. AddAssembly (ass, (Path.GetDirectoryName (ass.Location) == PrivateBinPath));
  221. AddDependency (ass.Location);
  222. return type;
  223. }
  224. }
  225. return null;
  226. }
  227. void AddAssembliesInBin ()
  228. {
  229. if (!Directory.Exists (PrivateBinPath))
  230. return;
  231. string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
  232. foreach (string dll in binDlls) {
  233. Assembly assembly = Assembly.LoadFrom (dll);
  234. AddAssembly (assembly, true);
  235. }
  236. }
  237. Assembly LoadAssemblyFromBin (string name)
  238. {
  239. Assembly assembly;
  240. if (!Directory.Exists (PrivateBinPath))
  241. return null;
  242. string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
  243. foreach (string dll in binDlls) {
  244. string fn = Path.GetFileName (dll);
  245. fn = Path.ChangeExtension (fn, null);
  246. if (fn != name)
  247. continue;
  248. assembly = Assembly.LoadFrom (dll);
  249. return assembly;
  250. }
  251. return null;
  252. }
  253. internal virtual void AddInterface (string iface)
  254. {
  255. if (interfaces == null)
  256. interfaces = new ArrayList ();
  257. if (!interfaces.Contains (iface))
  258. interfaces.Add (iface);
  259. }
  260. internal virtual void AddImport (string namesp)
  261. {
  262. if (imports == null)
  263. imports = new ArrayList ();
  264. if (!imports.Contains (namesp))
  265. imports.Add (namesp);
  266. }
  267. internal virtual void AddDependency (string filename)
  268. {
  269. if (dependencies == null)
  270. dependencies = new ArrayList ();
  271. if (!dependencies.Contains (filename))
  272. dependencies.Add (filename);
  273. }
  274. internal virtual void AddAssembly (Assembly assembly, bool fullPath)
  275. {
  276. if (anames == null)
  277. anames = new Hashtable ();
  278. string name = assembly.GetName ().Name;
  279. string loc = assembly.Location;
  280. if (fullPath) {
  281. if (!assemblies.Contains (loc)) {
  282. assemblies.Add (loc);
  283. }
  284. anames [name] = loc;
  285. anames [loc] = assembly;
  286. } else {
  287. if (!assemblies.Contains (name)) {
  288. assemblies.Add (name);
  289. }
  290. anames [name] = assembly;
  291. }
  292. }
  293. internal virtual Assembly AddAssemblyByName (string name)
  294. {
  295. if (anames == null)
  296. anames = new Hashtable ();
  297. if (anames.Contains (name)) {
  298. object o = anames [name];
  299. if (o is string)
  300. o = anames [o];
  301. return (Assembly) o;
  302. }
  303. bool fullpath = true;
  304. Assembly assembly = LoadAssemblyFromBin (name);
  305. if (assembly != null) {
  306. AddAssembly (assembly, fullpath);
  307. return assembly;
  308. }
  309. try {
  310. assembly = Assembly.Load (name);
  311. string loc = assembly.Location;
  312. fullpath = (Path.GetDirectoryName (loc) == PrivateBinPath);
  313. } catch (Exception e) {
  314. ThrowParseException ("Assembly " + name + " not found", e);
  315. }
  316. AddAssembly (assembly, fullpath);
  317. return assembly;
  318. }
  319. internal virtual void ProcessMainAttributes (Hashtable atts)
  320. {
  321. atts.Remove ("Description"); // ignored
  322. atts.Remove ("CodeBehind"); // ignored
  323. atts.Remove ("AspCompat"); // ignored
  324. debug = GetBool (atts, "Debug", true);
  325. compilerOptions = GetString (atts, "CompilerOptions", null);
  326. language = GetString (atts, "Language", CompilationConfig.DefaultLanguage);
  327. string src = GetString (atts, "Src", null);
  328. if (src != null)
  329. srcAssembly = GetAssemblyFromSource (src);
  330. string inherits = GetString (atts, "Inherits", null);
  331. if (inherits != null)
  332. SetBaseType (inherits);
  333. className = GetString (atts, "ClassName", null);
  334. if (className != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (className))
  335. ThrowParseException (String.Format ("'{0}' is not valid for 'className'", className));
  336. if (atts.Count > 0)
  337. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  338. }
  339. internal void SetBaseType (string type)
  340. {
  341. if (type == DefaultBaseTypeName)
  342. return;
  343. Type parent = null;
  344. if (srcAssembly != null)
  345. parent = srcAssembly.GetType (type);
  346. if (parent == null)
  347. parent = LoadType (type);
  348. if (parent == null)
  349. ThrowParseException ("Cannot find type " + type);
  350. if (!DefaultBaseType.IsAssignableFrom (parent))
  351. ThrowParseException ("The parent type does not derive from " + DefaultBaseType);
  352. baseType = parent;
  353. }
  354. Assembly GetAssemblyFromSource (string vpath)
  355. {
  356. vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
  357. string realPath = MapPath (vpath, false);
  358. if (!File.Exists (realPath))
  359. ThrowParseException ("File " + vpath + " not found");
  360. AddDependency (realPath);
  361. CompilerResults result = CachingCompiler.Compile (realPath, realPath, assemblies);
  362. if (result.NativeCompilerReturnValue != 0) {
  363. StringWriter writer = new StringWriter();
  364. StreamReader reader = new StreamReader (realPath);
  365. throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
  366. }
  367. AddAssembly (result.CompiledAssembly, true);
  368. return result.CompiledAssembly;
  369. }
  370. internal abstract Type DefaultBaseType { get; }
  371. internal abstract string DefaultBaseTypeName { get; }
  372. internal abstract string DefaultDirectiveName { get; }
  373. internal string InputFile
  374. {
  375. get { return inputFile; }
  376. set { inputFile = value; }
  377. }
  378. internal string Text
  379. {
  380. get { return text; }
  381. set { text = value; }
  382. }
  383. internal Type BaseType
  384. {
  385. get {
  386. if (baseType == null)
  387. baseType = DefaultBaseType;
  388. return baseType;
  389. }
  390. }
  391. internal string ClassName {
  392. get {
  393. if (className != null)
  394. return className;
  395. className = Path.GetFileName (inputFile).Replace ('.', '_');
  396. className = className.Replace ('-', '_');
  397. className = className.Replace (' ', '_');
  398. if (Char.IsDigit(className[0])) {
  399. className = "_" + className;
  400. }
  401. return className;
  402. }
  403. }
  404. internal string PrivateBinPath {
  405. get {
  406. if (privateBinPath != null)
  407. return privateBinPath;
  408. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  409. privateBinPath = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
  410. return privateBinPath;
  411. }
  412. }
  413. internal ArrayList Scripts {
  414. get {
  415. if (scripts == null)
  416. scripts = new ArrayList ();
  417. return scripts;
  418. }
  419. }
  420. internal ArrayList Imports {
  421. get { return imports; }
  422. }
  423. internal ArrayList Assemblies {
  424. get { return assemblies; }
  425. }
  426. internal ArrayList Interfaces {
  427. get { return interfaces; }
  428. }
  429. internal RootBuilder RootBuilder {
  430. get { return rootBuilder; }
  431. set { rootBuilder = value; }
  432. }
  433. internal ArrayList Dependencies {
  434. get { return dependencies; }
  435. }
  436. internal string CompilerOptions {
  437. get { return compilerOptions; }
  438. }
  439. internal string Language {
  440. get { return language; }
  441. }
  442. internal bool Debug {
  443. get { return debug; }
  444. }
  445. internal bool OutputCache {
  446. get { return output_cache; }
  447. }
  448. internal int OutputCacheDuration {
  449. get { return oc_duration; }
  450. }
  451. internal string OutputCacheVaryByHeader {
  452. get { return oc_header; }
  453. }
  454. internal string OutputCacheVaryByCustom {
  455. get { return oc_custom; }
  456. }
  457. internal string OutputCacheVaryByControls {
  458. get { return oc_controls; }
  459. }
  460. internal bool OutputCacheShared {
  461. get { return oc_shared; }
  462. }
  463. internal OutputCacheLocation OutputCacheLocation {
  464. get { return oc_location; }
  465. }
  466. internal string OutputCacheVaryByParam {
  467. get { return oc_param; }
  468. }
  469. internal PagesConfiguration PagesConfig {
  470. get { return PagesConfiguration.GetInstance (Context); }
  471. }
  472. }
  473. }