TemplateParser.cs 12 KB

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