AspComponentFoundry.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. 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. Assembly sw = typeof (AspComponentFoundry).Assembly;
  23. RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
  24. RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
  25. }
  26. public Type GetComponentType (string foundryName, string tag)
  27. {
  28. Foundry foundry = foundries [foundryName] as Foundry;
  29. if (foundry == null)
  30. return null;
  31. return foundry.GetType (tag);
  32. }
  33. public void RegisterFoundry (string foundryName,
  34. Assembly assembly,
  35. string nameSpace)
  36. {
  37. AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
  38. InternalRegister (foundryName, foundry);
  39. }
  40. public void RegisterFoundry (string foundryName,
  41. string tagName,
  42. Type type)
  43. {
  44. TagNameFoundry foundry = new TagNameFoundry (tagName, type);
  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. }
  70. class TagNameFoundry : Foundry
  71. {
  72. string tagName;
  73. Type type;
  74. public TagNameFoundry (string tagName, Type type)
  75. {
  76. this.tagName = tagName;
  77. this.type = type;
  78. }
  79. public override Type GetType (string componentName)
  80. {
  81. if (0 != String.Compare (componentName, tagName, true))
  82. return null;
  83. return type;
  84. }
  85. public string TagName {
  86. get { return tagName; }
  87. }
  88. }
  89. class AssemblyFoundry : Foundry
  90. {
  91. string nameSpace;
  92. Assembly assembly;
  93. public AssemblyFoundry (Assembly assembly, string nameSpace)
  94. {
  95. this.assembly = assembly;
  96. this.nameSpace = nameSpace;
  97. }
  98. public override Type GetType (string componentName)
  99. {
  100. return assembly.GetType (nameSpace + "." + componentName, true, true);
  101. }
  102. }
  103. class CompoundFoundry : Foundry
  104. {
  105. AssemblyFoundry assemblyFoundry;
  106. Hashtable tagnames;
  107. string tagPrefix;
  108. public CompoundFoundry (string tagPrefix)
  109. {
  110. this.tagPrefix = tagPrefix;
  111. tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  112. CaseInsensitiveComparer.Default);
  113. }
  114. public void Add (Foundry foundry)
  115. {
  116. if (foundry is AssemblyFoundry) {
  117. assemblyFoundry = (AssemblyFoundry) foundry;
  118. return;
  119. }
  120. TagNameFoundry tn = (TagNameFoundry) foundry;
  121. string tagName = tn.TagName;
  122. if (tagnames.Contains (tagName)) {
  123. string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
  124. throw new ApplicationException (msg);
  125. }
  126. tagnames.Add (tagName, foundry);
  127. }
  128. public override Type GetType (string componentName)
  129. {
  130. Type type = null;
  131. if (assemblyFoundry != null) {
  132. try {
  133. type = assemblyFoundry.GetType (componentName);
  134. return type;
  135. } catch { }
  136. }
  137. Foundry foundry = tagnames [componentName] as Foundry;
  138. if (foundry == null) {
  139. string msg = String.Format ("Type {0} not registered for prefix {1}",
  140. componentName, tagPrefix);
  141. throw new ApplicationException (msg);
  142. }
  143. return foundry.GetType (componentName);
  144. }
  145. }
  146. }
  147. }