AspComponentFoundry.cs 5.4 KB

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