AspComponentFoundry.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // System.Web.Compilation.AspComponentFoundry
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Reflection;
  33. #if NET_2_0
  34. using System.Collections.Generic;
  35. using System.Web;
  36. using System.Web.Configuration;
  37. using System.Web.UI;
  38. #endif
  39. namespace System.Web.Compilation
  40. {
  41. internal class AspComponentFoundry
  42. {
  43. private Hashtable foundries;
  44. public AspComponentFoundry ()
  45. {
  46. #if NET_2_0
  47. foundries = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
  48. #else
  49. foundries = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
  50. CaseInsensitiveComparer.DefaultInvariant);
  51. #endif
  52. Assembly sw = typeof (AspComponentFoundry).Assembly;
  53. RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
  54. RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
  55. #if NET_2_0
  56. RegisterConfigControls ();
  57. #endif
  58. }
  59. public Type GetComponentType (string foundryName, string tag)
  60. {
  61. Foundry foundry = foundries [foundryName] as Foundry;
  62. if (foundry == null)
  63. return null;
  64. return foundry.GetType (tag);
  65. }
  66. public void RegisterFoundry (string foundryName,
  67. Assembly assembly,
  68. string nameSpace)
  69. {
  70. AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
  71. InternalRegister (foundryName, foundry);
  72. }
  73. public void RegisterFoundry (string foundryName,
  74. string tagName,
  75. Type type)
  76. {
  77. TagNameFoundry foundry = new TagNameFoundry (tagName, type);
  78. InternalRegister (foundryName, foundry);
  79. }
  80. #if NET_2_0
  81. public void RegisterFoundry (string foundryName,
  82. string tagName,
  83. string source)
  84. {
  85. TagNameFoundry foundry = new TagNameFoundry (tagName, source);
  86. InternalRegister (foundryName, foundry);
  87. }
  88. // Look up the controls/namespaces defined in the config
  89. // file(s), resolve the assemblies but do not compile the types.
  90. void RegisterConfigControls ()
  91. {
  92. PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  93. if (pages == null)
  94. return;
  95. TagPrefixCollection controls = pages.Controls;
  96. if (controls == null || controls.Count == 0)
  97. return;
  98. Dictionary <string, Assembly> assemblyCache = new Dictionary <string, Assembly> ();
  99. foreach (TagPrefixInfo tpi in controls) {
  100. if (!String.IsNullOrEmpty (tpi.TagName))
  101. RegisterFoundry (tpi.TagPrefix, tpi.TagName, tpi.Source);
  102. else if (!String.IsNullOrEmpty (tpi.Namespace))
  103. RegisterFoundry (tpi.TagPrefix, GetAssemblyByName (assemblyCache, tpi.Assembly), tpi.Namespace);
  104. }
  105. }
  106. Assembly GetAssemblyByName (Dictionary <string, Assembly> cache, string name)
  107. {
  108. if (cache.ContainsKey (name))
  109. return cache [name];
  110. Assembly assembly = null;
  111. Exception error = null;
  112. if (name.IndexOf (',') != -1) {
  113. try {
  114. assembly = Assembly.Load (name);
  115. } catch (Exception e) { error = e; }
  116. }
  117. if (assembly == null) {
  118. try {
  119. assembly = Assembly.LoadWithPartialName (name);
  120. } catch (Exception e) { error = e; }
  121. }
  122. if (assembly == null)
  123. throw new HttpException ("Assembly " + name + " not found", error);
  124. return assembly;
  125. }
  126. #endif
  127. void InternalRegister (string foundryName, Foundry foundry)
  128. {
  129. object f = foundries [foundryName];
  130. if (f is CompoundFoundry) {
  131. ((CompoundFoundry) f).Add (foundry);
  132. } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
  133. // If more than 1 namespace/assembly specified, the last one is used.
  134. foundries [foundryName] = foundry;
  135. } else if (f != null) {
  136. CompoundFoundry compound = new CompoundFoundry (foundryName);
  137. compound.Add ((Foundry) f);
  138. compound.Add (foundry);
  139. foundries [foundryName] = compound;
  140. }
  141. }
  142. public bool LookupFoundry (string foundryName)
  143. {
  144. return foundries.Contains (foundryName);
  145. }
  146. abstract class Foundry
  147. {
  148. public abstract Type GetType (string componentName);
  149. }
  150. class TagNameFoundry : Foundry
  151. {
  152. string tagName;
  153. Type type;
  154. #if NET_2_0
  155. string source;
  156. public bool FromWebConfig {
  157. get { return source != null; }
  158. }
  159. public TagNameFoundry (string tagName, string source)
  160. {
  161. this.tagName = tagName;
  162. this.source = source;
  163. }
  164. #endif
  165. public TagNameFoundry (string tagName, Type type)
  166. {
  167. this.tagName = tagName;
  168. this.type = type;
  169. }
  170. public override Type GetType (string componentName)
  171. {
  172. if (0 != String.Compare (componentName, tagName, true))
  173. return null;
  174. return LoadType ();
  175. }
  176. Type LoadType ()
  177. {
  178. #if NET_2_0
  179. if (type != null)
  180. return type;
  181. HttpContext context = HttpContext.Current;
  182. string vpath;
  183. string realpath;
  184. if (VirtualPathUtility.IsAppRelative (source)) {
  185. vpath = source;
  186. realpath = context.Request.MapPath (source);
  187. } else {
  188. vpath = VirtualPathUtility.ToAppRelative (source);
  189. realpath = source;
  190. }
  191. if ((type = CachingCompiler.GetTypeFromCache (realpath)) != null)
  192. return type;
  193. ArrayList other_deps = new ArrayList ();
  194. type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, context);
  195. if (type != null) {
  196. AspGenerator.AddTypeToCache (other_deps, realpath, type);
  197. WebConfigurationManager.ExtraAssemblies.Add (type.Assembly.Location);
  198. }
  199. return type;
  200. #else
  201. return type;
  202. #endif
  203. }
  204. public string TagName {
  205. get { return tagName; }
  206. }
  207. }
  208. class AssemblyFoundry : Foundry
  209. {
  210. string nameSpace;
  211. Assembly assembly;
  212. public AssemblyFoundry (Assembly assembly, string nameSpace)
  213. {
  214. this.assembly = assembly;
  215. this.nameSpace = nameSpace;
  216. }
  217. public override Type GetType (string componentName)
  218. {
  219. return assembly.GetType (nameSpace + "." + componentName, true, true);
  220. }
  221. }
  222. class CompoundFoundry : Foundry
  223. {
  224. AssemblyFoundry assemblyFoundry;
  225. Hashtable tagnames;
  226. string tagPrefix;
  227. public CompoundFoundry (string tagPrefix)
  228. {
  229. this.tagPrefix = tagPrefix;
  230. #if NET_2_0
  231. tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
  232. #else
  233. tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
  234. CaseInsensitiveComparer.DefaultInvariant);
  235. #endif
  236. }
  237. public void Add (Foundry foundry)
  238. {
  239. if (foundry is AssemblyFoundry) {
  240. assemblyFoundry = (AssemblyFoundry) foundry;
  241. return;
  242. }
  243. TagNameFoundry tn = (TagNameFoundry) foundry;
  244. string tagName = tn.TagName;
  245. if (tagnames.Contains (tagName)) {
  246. #if NET_2_0
  247. if (tn.FromWebConfig)
  248. return;
  249. #endif
  250. string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
  251. throw new ApplicationException (msg);
  252. }
  253. tagnames.Add (tagName, foundry);
  254. }
  255. public override Type GetType (string componentName)
  256. {
  257. Type type = null;
  258. Foundry foundry = tagnames [componentName] as Foundry;
  259. if (foundry != null)
  260. return foundry.GetType (componentName);
  261. if (assemblyFoundry != null) {
  262. try {
  263. type = assemblyFoundry.GetType (componentName);
  264. return type;
  265. } catch { }
  266. }
  267. string msg = String.Format ("Type {0} not registered for prefix {1}",
  268. componentName, tagPrefix);
  269. throw new ApplicationException (msg);
  270. }
  271. }
  272. }
  273. }