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 ()
43 foundries = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
44 CaseInsensitiveComparer.DefaultInvariant);
46 Assembly sw = typeof (AspComponentFoundry).Assembly;
47 RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
48 RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
51 public Type GetComponentType (string foundryName, string tag)
53 Foundry foundry = foundries [foundryName] as Foundry;
57 return foundry.GetType (tag);
60 public void RegisterFoundry (string foundryName,
64 AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
65 InternalRegister (foundryName, foundry);
68 public void RegisterFoundry (string foundryName,
72 TagNameFoundry foundry = new TagNameFoundry (tagName, type);
73 InternalRegister (foundryName, foundry);
76 void InternalRegister (string foundryName, Foundry foundry)
78 object f = foundries [foundryName];
79 if (f is CompoundFoundry) {
80 ((CompoundFoundry) f).Add (foundry);
81 } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
82 // If more than 1 namespace/assembly specified, the last one is used.
83 foundries [foundryName] = foundry;
84 } else if (f != null) {
85 CompoundFoundry compound = new CompoundFoundry (foundryName);
86 compound.Add ((Foundry) f);
87 compound.Add (foundry);
88 foundries [foundryName] = compound;
92 public bool LookupFoundry (string foundryName)
94 return foundries.Contains (foundryName);
97 abstract class Foundry
99 public abstract Type GetType (string componentName);
103 class TagNameFoundry : Foundry
108 public TagNameFoundry (string tagName, Type type)
110 this.tagName = tagName;
114 public override Type GetType (string componentName)
116 if (0 != String.Compare (componentName, tagName, true))
122 public string TagName {
123 get { return tagName; }
127 class AssemblyFoundry : Foundry
132 public AssemblyFoundry (Assembly assembly, string nameSpace)
134 this.assembly = assembly;
135 this.nameSpace = nameSpace;
138 public override Type GetType (string componentName)
140 return assembly.GetType (nameSpace + "." + componentName, true, true);
144 class CompoundFoundry : Foundry
146 AssemblyFoundry assemblyFoundry;
150 public CompoundFoundry (string tagPrefix)
152 this.tagPrefix = tagPrefix;
153 tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
154 CaseInsensitiveComparer.DefaultInvariant);
157 public void Add (Foundry foundry)
159 if (foundry is AssemblyFoundry) {
160 assemblyFoundry = (AssemblyFoundry) foundry;
164 TagNameFoundry tn = (TagNameFoundry) foundry;
165 string tagName = tn.TagName;
166 if (tagnames.Contains (tagName)) {
167 string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
168 throw new ApplicationException (msg);
170 tagnames.Add (tagName, foundry);
173 public override Type GetType (string componentName)
176 Foundry foundry = tagnames [componentName] as Foundry;
178 return foundry.GetType (componentName);
180 if (assemblyFoundry != null) {
182 type = assemblyFoundry.GetType (componentName);
187 string msg = String.Format ("Type {0} not registered for prefix {1}",
188 componentName, tagPrefix);
189 throw new ApplicationException (msg);