AspComponentFoundry.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 TagNameFoundry (string tagName, string source)
  157. {
  158. this.tagName = tagName;
  159. this.source = source;
  160. }
  161. #endif
  162. public TagNameFoundry (string tagName, Type type)
  163. {
  164. this.tagName = tagName;
  165. this.type = type;
  166. }
  167. public override Type GetType (string componentName)
  168. {
  169. if (0 != String.Compare (componentName, tagName, true))
  170. return null;
  171. return LoadType ();
  172. }
  173. Type LoadType ()
  174. {
  175. #if NET_2_0
  176. if (type != null)
  177. return type;
  178. HttpContext context = HttpContext.Current;
  179. string vpath;
  180. string realpath;
  181. if (VirtualPathUtility.IsAppRelative (source)) {
  182. vpath = source;
  183. realpath = context.Request.MapPath (source);
  184. } else {
  185. vpath = VirtualPathUtility.ToAppRelative (source);
  186. realpath = source;
  187. }
  188. if ((type = CachingCompiler.GetTypeFromCache (realpath)) != null)
  189. return type;
  190. ArrayList other_deps = new ArrayList ();
  191. type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, context);
  192. if (type != null) {
  193. AspGenerator.AddTypeToCache (other_deps, realpath, type);
  194. WebConfigurationManager.ExtraAssemblies.Add (type.Assembly.Location);
  195. }
  196. return type;
  197. #else
  198. return type;
  199. #endif
  200. }
  201. public string TagName {
  202. get { return tagName; }
  203. }
  204. }
  205. class AssemblyFoundry : Foundry
  206. {
  207. string nameSpace;
  208. Assembly assembly;
  209. public AssemblyFoundry (Assembly assembly, string nameSpace)
  210. {
  211. this.assembly = assembly;
  212. this.nameSpace = nameSpace;
  213. }
  214. public override Type GetType (string componentName)
  215. {
  216. return assembly.GetType (nameSpace + "." + componentName, true, true);
  217. }
  218. }
  219. class CompoundFoundry : Foundry
  220. {
  221. AssemblyFoundry assemblyFoundry;
  222. Hashtable tagnames;
  223. string tagPrefix;
  224. public CompoundFoundry (string tagPrefix)
  225. {
  226. this.tagPrefix = tagPrefix;
  227. #if NET_2_0
  228. tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
  229. #else
  230. tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
  231. CaseInsensitiveComparer.DefaultInvariant);
  232. #endif
  233. }
  234. public void Add (Foundry foundry)
  235. {
  236. if (foundry is AssemblyFoundry) {
  237. assemblyFoundry = (AssemblyFoundry) foundry;
  238. return;
  239. }
  240. TagNameFoundry tn = (TagNameFoundry) foundry;
  241. string tagName = tn.TagName;
  242. if (tagnames.Contains (tagName)) {
  243. string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
  244. throw new ApplicationException (msg);
  245. }
  246. tagnames.Add (tagName, foundry);
  247. }
  248. public override Type GetType (string componentName)
  249. {
  250. Type type = null;
  251. Foundry foundry = tagnames [componentName] as Foundry;
  252. if (foundry != null)
  253. return foundry.GetType (componentName);
  254. if (assemblyFoundry != null) {
  255. try {
  256. type = assemblyFoundry.GetType (componentName);
  257. return type;
  258. } catch { }
  259. }
  260. string msg = String.Format ("Type {0} not registered for prefix {1}",
  261. componentName, tagPrefix);
  262. throw new ApplicationException (msg);
  263. }
  264. }
  265. }
  266. }