**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.Compilation / AspComponentFoundry.cs
blobaf0b8b6e92d2d7ce3dde558bc3cf12975d45457b
1 //
2 // System.Web.Compilation.AspComponentFoundry
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
30 using System;
31 using System.Collections;
32 using System.IO;
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.Default,
44 CaseInsensitiveComparer.Default);
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;
54 if (foundry == null)
55 return null;
57 return foundry.GetType (tag);
60 public void RegisterFoundry (string foundryName,
61 Assembly assembly,
62 string nameSpace)
64 AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
65 InternalRegister (foundryName, foundry);
68 public void RegisterFoundry (string foundryName,
69 string tagName,
70 Type type)
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
105 string tagName;
106 Type type;
108 public TagNameFoundry (string tagName, Type type)
110 this.tagName = tagName;
111 this.type = type;
114 public override Type GetType (string componentName)
116 if (0 != String.Compare (componentName, tagName, true))
117 return null;
119 return type;
122 public string TagName {
123 get { return tagName; }
127 class AssemblyFoundry : Foundry
129 string nameSpace;
130 Assembly assembly;
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;
147 Hashtable tagnames;
148 string tagPrefix;
150 public CompoundFoundry (string tagPrefix)
152 this.tagPrefix = tagPrefix;
153 tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
154 CaseInsensitiveComparer.Default);
157 public void Add (Foundry foundry)
159 if (foundry is AssemblyFoundry) {
160 assemblyFoundry = (AssemblyFoundry) foundry;
161 return;
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)
175 Type type = null;
176 if (assemblyFoundry != null) {
177 try {
178 type = assemblyFoundry.GetType (componentName);
179 return type;
180 } catch { }
183 Foundry foundry = tagnames [componentName] as Foundry;
184 if (foundry == null) {
185 string msg = String.Format ("Type {0} not registered for prefix {1}",
186 componentName, tagPrefix);
187 throw new ApplicationException (msg);
190 return foundry.GetType (componentName);