AspComponentFoundry.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // System.Web.Compilation.AspComponentFoundry
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Reflection;
  13. namespace System.Web.Compilation
  14. {
  15. internal class AspComponentFoundry
  16. {
  17. private Hashtable foundries;
  18. public AspComponentFoundry ()
  19. {
  20. foundries = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  21. CaseInsensitiveComparer.Default);
  22. RegisterFoundry ("asp", "System.Web", "System.Web.UI.WebControls");
  23. }
  24. public AspComponent MakeAspComponent (string foundryName, string componentName, Tag tag)
  25. {
  26. Foundry foundry = foundries [foundryName] as Foundry;
  27. if (foundry == null)
  28. throw new ApplicationException ("Foundry not found: " + foundryName);
  29. return new AspComponent (tag, foundry.GetType (componentName));
  30. }
  31. public void RegisterFoundry (string foundryName,
  32. string assemblyName,
  33. string nameSpace)
  34. {
  35. AssemblyFoundry foundry = new AssemblyFoundry (assemblyName, nameSpace);
  36. InternalRegister (foundryName, foundry);
  37. }
  38. public void RegisterFoundry (string foundryName,
  39. string tagName,
  40. string assemblyName,
  41. string nameSpace,
  42. string typeName)
  43. {
  44. TagNameFoundry foundry = new TagNameFoundry (assemblyName, tagName, nameSpace, typeName);
  45. InternalRegister (foundryName, foundry);
  46. }
  47. void InternalRegister (string foundryName, Foundry foundry)
  48. {
  49. object f = foundries [foundryName];
  50. if (f is CompoundFoundry) {
  51. ((CompoundFoundry) f).Add (foundry);
  52. } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
  53. // If more than 1 namespace/assembly specified, the last one is used.
  54. foundries [foundryName] = foundry;
  55. } else if (f != null) {
  56. CompoundFoundry compound = new CompoundFoundry (foundryName);
  57. compound.Add ((Foundry) f);
  58. compound.Add (foundry);
  59. foundries [foundryName] = compound;
  60. }
  61. }
  62. public bool LookupFoundry (string foundryName)
  63. {
  64. return foundries.Contains (foundryName);
  65. }
  66. abstract class Foundry
  67. {
  68. public abstract Type GetType (string componentName);
  69. public Assembly LoadAssembly (string assemblyName)
  70. {
  71. Assembly assembly = null;
  72. try {
  73. assembly = Assembly.LoadFrom (Path.GetFullPath (assemblyName));
  74. } catch {
  75. string partialName = assemblyName;
  76. if (String.Compare (Path.GetExtension (partialName), ".dll", true) == 0)
  77. partialName = Path.GetFileNameWithoutExtension (assemblyName);
  78. assembly = Assembly.LoadWithPartialName (partialName);
  79. }
  80. if (assembly == null)
  81. throw new ApplicationException ("Assembly not found:" + assemblyName);
  82. return assembly;
  83. }
  84. }
  85. class TagNameFoundry : Foundry
  86. {
  87. string assemblyName;
  88. string tagName;
  89. string nameSpace;
  90. string typeName;
  91. Type type;
  92. public TagNameFoundry (string assemblyName, string tagName, string nameSpace, string typeName)
  93. {
  94. this.assemblyName = assemblyName;
  95. this.tagName = tagName;
  96. this.nameSpace = nameSpace;
  97. this.typeName = typeName;
  98. }
  99. public override Type GetType (string componentName)
  100. {
  101. if (0 != String.Compare (componentName, tagName, true))
  102. throw new ArgumentException (componentName + " != " + tagName);
  103. if (type != null)
  104. return type;
  105. Assembly assembly = LoadAssembly (assemblyName);
  106. type = assembly.GetType (nameSpace + "." + typeName, true, true);
  107. return type;
  108. }
  109. public string TagName {
  110. get { return tagName; }
  111. }
  112. }
  113. class AssemblyFoundry : Foundry
  114. {
  115. string nameSpace;
  116. string assemblyName;
  117. Assembly assembly;
  118. public AssemblyFoundry (string assemblyName, string nameSpace)
  119. {
  120. this.assemblyName = assemblyName;
  121. this.nameSpace = nameSpace;
  122. assembly = null;
  123. }
  124. public override Type GetType (string componentName)
  125. {
  126. Assembly ass = EnsureAssembly (componentName);
  127. return ass.GetType (nameSpace + "." + componentName, true, true);
  128. }
  129. Assembly EnsureAssembly (string componentName)
  130. {
  131. if (assembly != null)
  132. return assembly;
  133. assembly = LoadAssembly (assemblyName);
  134. return assembly;
  135. }
  136. }
  137. class CompoundFoundry : Foundry
  138. {
  139. AssemblyFoundry assemblyFoundry;
  140. Hashtable tagnames;
  141. string tagPrefix;
  142. public CompoundFoundry (string tagPrefix)
  143. {
  144. this.tagPrefix = tagPrefix;
  145. tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  146. CaseInsensitiveComparer.Default);
  147. }
  148. public void Add (Foundry foundry)
  149. {
  150. if (foundry is AssemblyFoundry) {
  151. assemblyFoundry = (AssemblyFoundry) foundry;
  152. return;
  153. }
  154. TagNameFoundry tn = (TagNameFoundry) foundry;
  155. string tagName = tn.TagName;
  156. if (tagnames.Contains (tagName)) {
  157. string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
  158. throw new ApplicationException (msg);
  159. }
  160. tagnames.Add (tagName, foundry);
  161. }
  162. public override Type GetType (string componentName)
  163. {
  164. Type type = null;
  165. if (assemblyFoundry != null) {
  166. try {
  167. type = assemblyFoundry.GetType (componentName);
  168. return type;
  169. } catch { }
  170. }
  171. Foundry foundry = tagnames [componentName] as Foundry;
  172. if (foundry == null) {
  173. string msg = String.Format ("Type {0} not registered for prefix {1}",
  174. componentName, tagPrefix);
  175. throw new ApplicationException (msg);
  176. }
  177. return foundry.GetType (componentName);
  178. }
  179. }
  180. }
  181. }