2 // System.Web.Compilation.AspComponentFoundry
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
33 using System.Reflection;
35 namespace System.Web.Compilation
37 internal class AspComponentFoundry
39 private Hashtable foundries;
41 public AspComponentFoundry ()
44 foundries = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
46 foundries = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
47 CaseInsensitiveComparer.DefaultInvariant);
50 Assembly sw = typeof (AspComponentFoundry).Assembly;
51 RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
52 RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
55 public Type GetComponentType (string foundryName, string tag)
57 Foundry foundry = foundries [foundryName] as Foundry;
61 return foundry.GetType (tag);
64 public void RegisterFoundry (string foundryName,
68 AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
69 InternalRegister (foundryName, foundry);
72 public void RegisterFoundry (string foundryName,
76 TagNameFoundry foundry = new TagNameFoundry (tagName, type);
77 InternalRegister (foundryName, foundry);
80 void InternalRegister (string foundryName, Foundry foundry)
82 object f = foundries [foundryName];
83 if (f is CompoundFoundry) {
84 ((CompoundFoundry) f).Add (foundry);
85 } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
86 // If more than 1 namespace/assembly specified, the last one is used.
87 foundries [foundryName] = foundry;
88 } else if (f != null) {
89 CompoundFoundry compound = new CompoundFoundry (foundryName);
90 compound.Add ((Foundry) f);
91 compound.Add (foundry);
92 foundries [foundryName] = compound;
96 public bool LookupFoundry (string foundryName)
98 return foundries.Contains (foundryName);
101 abstract class Foundry
103 public abstract Type GetType (string componentName);
107 class TagNameFoundry : Foundry
112 public TagNameFoundry (string tagName, Type type)
114 this.tagName = tagName;
118 public override Type GetType (string componentName)
120 if (0 != String.Compare (componentName, tagName, true))
126 public string TagName {
127 get { return tagName; }
131 class AssemblyFoundry : Foundry
136 public AssemblyFoundry (Assembly assembly, string nameSpace)
138 this.assembly = assembly;
139 this.nameSpace = nameSpace;
142 public override Type GetType (string componentName)
144 return assembly.GetType (nameSpace + "." + componentName, true, true);
148 class CompoundFoundry : Foundry
150 AssemblyFoundry assemblyFoundry;
154 public CompoundFoundry (string tagPrefix)
156 this.tagPrefix = tagPrefix;
158 tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
160 tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
161 CaseInsensitiveComparer.DefaultInvariant);
165 public void Add (Foundry foundry)
167 if (foundry is AssemblyFoundry) {
168 assemblyFoundry = (AssemblyFoundry) foundry;
172 TagNameFoundry tn = (TagNameFoundry) foundry;
173 string tagName = tn.TagName;
174 if (tagnames.Contains (tagName)) {
175 string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
176 throw new ApplicationException (msg);
178 tagnames.Add (tagName, foundry);
181 public override Type GetType (string componentName)
184 Foundry foundry = tagnames [componentName] as Foundry;
186 return foundry.GetType (componentName);
188 if (assemblyFoundry != null) {
190 type = assemblyFoundry.GetType (componentName);
195 string msg = String.Format ("Type {0} not registered for prefix {1}",
196 componentName, tagPrefix);
197 throw new ApplicationException (msg);