AspComponentFoundry.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. namespace System.Web.Compilation
  34. {
  35. internal class AspComponentFoundry
  36. {
  37. private Hashtable foundries;
  38. public AspComponentFoundry ()
  39. {
  40. foundries = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  41. CaseInsensitiveComparer.Default);
  42. Assembly sw = typeof (AspComponentFoundry).Assembly;
  43. RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
  44. RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
  45. }
  46. public Type GetComponentType (string foundryName, string tag)
  47. {
  48. Foundry foundry = foundries [foundryName] as Foundry;
  49. if (foundry == null)
  50. return null;
  51. return foundry.GetType (tag);
  52. }
  53. public void RegisterFoundry (string foundryName,
  54. Assembly assembly,
  55. string nameSpace)
  56. {
  57. AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
  58. InternalRegister (foundryName, foundry);
  59. }
  60. public void RegisterFoundry (string foundryName,
  61. string tagName,
  62. Type type)
  63. {
  64. TagNameFoundry foundry = new TagNameFoundry (tagName, type);
  65. InternalRegister (foundryName, foundry);
  66. }
  67. void InternalRegister (string foundryName, Foundry foundry)
  68. {
  69. object f = foundries [foundryName];
  70. if (f is CompoundFoundry) {
  71. ((CompoundFoundry) f).Add (foundry);
  72. } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
  73. // If more than 1 namespace/assembly specified, the last one is used.
  74. foundries [foundryName] = foundry;
  75. } else if (f != null) {
  76. CompoundFoundry compound = new CompoundFoundry (foundryName);
  77. compound.Add ((Foundry) f);
  78. compound.Add (foundry);
  79. foundries [foundryName] = compound;
  80. }
  81. }
  82. public bool LookupFoundry (string foundryName)
  83. {
  84. return foundries.Contains (foundryName);
  85. }
  86. abstract class Foundry
  87. {
  88. public abstract Type GetType (string componentName);
  89. }
  90. class TagNameFoundry : Foundry
  91. {
  92. string tagName;
  93. Type type;
  94. public TagNameFoundry (string tagName, Type type)
  95. {
  96. this.tagName = tagName;
  97. this.type = type;
  98. }
  99. public override Type GetType (string componentName)
  100. {
  101. if (0 != String.Compare (componentName, tagName, true))
  102. return null;
  103. return type;
  104. }
  105. public string TagName {
  106. get { return tagName; }
  107. }
  108. }
  109. class AssemblyFoundry : Foundry
  110. {
  111. string nameSpace;
  112. Assembly assembly;
  113. public AssemblyFoundry (Assembly assembly, string nameSpace)
  114. {
  115. this.assembly = assembly;
  116. this.nameSpace = nameSpace;
  117. }
  118. public override Type GetType (string componentName)
  119. {
  120. return assembly.GetType (nameSpace + "." + componentName, true, true);
  121. }
  122. }
  123. class CompoundFoundry : Foundry
  124. {
  125. AssemblyFoundry assemblyFoundry;
  126. Hashtable tagnames;
  127. string tagPrefix;
  128. public CompoundFoundry (string tagPrefix)
  129. {
  130. this.tagPrefix = tagPrefix;
  131. tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  132. CaseInsensitiveComparer.Default);
  133. }
  134. public void Add (Foundry foundry)
  135. {
  136. if (foundry is AssemblyFoundry) {
  137. assemblyFoundry = (AssemblyFoundry) foundry;
  138. return;
  139. }
  140. TagNameFoundry tn = (TagNameFoundry) foundry;
  141. string tagName = tn.TagName;
  142. if (tagnames.Contains (tagName)) {
  143. string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
  144. throw new ApplicationException (msg);
  145. }
  146. tagnames.Add (tagName, foundry);
  147. }
  148. public override Type GetType (string componentName)
  149. {
  150. Type type = null;
  151. if (assemblyFoundry != null) {
  152. try {
  153. type = assemblyFoundry.GetType (componentName);
  154. return type;
  155. } catch { }
  156. }
  157. Foundry foundry = tagnames [componentName] as Foundry;
  158. if (foundry == null) {
  159. string msg = String.Format ("Type {0} not registered for prefix {1}",
  160. componentName, tagPrefix);
  161. throw new ApplicationException (msg);
  162. }
  163. return foundry.GetType (componentName);
  164. }
  165. }
  166. }
  167. }