2 // System.Web.Compilation.AspComponentFoundry
\r
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
\r
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
\r
10 using System.Collections;
\r
12 using System.Reflection;
\r
14 namespace System.Web.Compilation
\r
16 internal class AspComponentFoundry
\r
18 private Hashtable foundries;
\r
20 public AspComponentFoundry ()
\r
22 foundries = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
\r
23 CaseInsensitiveComparer.Default);
\r
25 Assembly sw = typeof (AspComponentFoundry).Assembly;
\r
26 RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
\r
27 RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
\r
30 public Type GetComponentType (string foundryName, string tag)
\r
32 Foundry foundry = foundries [foundryName] as Foundry;
\r
33 if (foundry == null)
\r
36 return foundry.GetType (tag);
\r
39 public void RegisterFoundry (string foundryName,
\r
43 AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
\r
44 InternalRegister (foundryName, foundry);
\r
47 public void RegisterFoundry (string foundryName,
\r
51 TagNameFoundry foundry = new TagNameFoundry (tagName, type);
\r
52 InternalRegister (foundryName, foundry);
\r
55 void InternalRegister (string foundryName, Foundry foundry)
\r
57 object f = foundries [foundryName];
\r
58 if (f is CompoundFoundry) {
\r
59 ((CompoundFoundry) f).Add (foundry);
\r
60 } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
\r
61 // If more than 1 namespace/assembly specified, the last one is used.
\r
62 foundries [foundryName] = foundry;
\r
63 } else if (f != null) {
\r
64 CompoundFoundry compound = new CompoundFoundry (foundryName);
\r
65 compound.Add ((Foundry) f);
\r
66 compound.Add (foundry);
\r
67 foundries [foundryName] = compound;
\r
71 public bool LookupFoundry (string foundryName)
\r
73 return foundries.Contains (foundryName);
\r
76 abstract class Foundry
\r
78 public abstract Type GetType (string componentName);
\r
82 class TagNameFoundry : Foundry
\r
87 public TagNameFoundry (string tagName, Type type)
\r
89 this.tagName = tagName;
\r
93 public override Type GetType (string componentName)
\r
95 if (0 != String.Compare (componentName, tagName, true))
\r
101 public string TagName {
\r
102 get { return tagName; }
\r
106 class AssemblyFoundry : Foundry
\r
111 public AssemblyFoundry (Assembly assembly, string nameSpace)
\r
113 this.assembly = assembly;
\r
114 this.nameSpace = nameSpace;
\r
117 public override Type GetType (string componentName)
\r
119 return assembly.GetType (nameSpace + "." + componentName, true, true);
\r
123 class CompoundFoundry : Foundry
\r
125 AssemblyFoundry assemblyFoundry;
\r
126 Hashtable tagnames;
\r
129 public CompoundFoundry (string tagPrefix)
\r
131 this.tagPrefix = tagPrefix;
\r
132 tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
\r
133 CaseInsensitiveComparer.Default);
\r
136 public void Add (Foundry foundry)
\r
138 if (foundry is AssemblyFoundry) {
\r
139 assemblyFoundry = (AssemblyFoundry) foundry;
\r
143 TagNameFoundry tn = (TagNameFoundry) foundry;
\r
144 string tagName = tn.TagName;
\r
145 if (tagnames.Contains (tagName)) {
\r
146 string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
\r
147 throw new ApplicationException (msg);
\r
149 tagnames.Add (tagName, foundry);
\r
152 public override Type GetType (string componentName)
\r
155 if (assemblyFoundry != null) {
\r
157 type = assemblyFoundry.GetType (componentName);
\r
162 Foundry foundry = tagnames [componentName] as Foundry;
\r
163 if (foundry == null) {
\r
164 string msg = String.Format ("Type {0} not registered for prefix {1}",
\r
165 componentName, tagPrefix);
\r
166 throw new ApplicationException (msg);
\r
169 return foundry.GetType (componentName);
\r