Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Internal / AssemblySourceInfo.cs
blobf3e7ae5728d4ebfe5c5c1b77d71cbfef80a731c7
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.IO;
21 using System.Reflection;
23 /// <summary>
24 /// Represents a source of views in an assembly resource.
25 /// </summary>
26 public class AssemblySourceInfo
28 private readonly String assemblyName;
29 private readonly String _namespace;
30 private readonly IDictionary entries = new HybridDictionary(true);
31 private readonly Assembly loadedAssembly;
33 /// <summary>
34 /// Initializes a new instance of the <see cref="AssemblySourceInfo"/> class.
35 /// </summary>
36 /// <param name="assemblyName">Name of the assembly.</param>
37 /// <param name="_namespace">The _namespace.</param>
38 public AssemblySourceInfo(string assemblyName, string _namespace)
40 this.assemblyName = assemblyName;
41 this._namespace = _namespace;
43 loadedAssembly = Assembly.Load(assemblyName);
45 RegisterEntries();
48 /// <summary>
49 /// Gets the name of the assembly.
50 /// </summary>
51 /// <value>The name of the assembly.</value>
52 public string AssemblyName
54 get { return assemblyName; }
57 /// <summary>
58 /// Gets the namespace.
59 /// </summary>
60 /// <value>The namespace.</value>
61 public string Namespace
63 get { return _namespace; }
66 /// <summary>
67 /// Determines whether the assembly has the specified template.
68 /// </summary>
69 /// <param name="templateName">Name of the template.</param>
70 /// <returns>
71 /// <c>true</c> if the specified exists on the assembly; otherwise, <c>false</c>.
72 /// </returns>
73 public bool HasTemplate(String templateName)
75 return entries.Contains(NormalizeTemplateName(templateName));
78 /// <summary>
79 /// Gets the template stream.
80 /// </summary>
81 /// <param name="templateName">Name of the template.</param>
82 /// <returns></returns>
83 public Stream GetTemplateStream(String templateName)
85 String resourcePath = (String) entries[NormalizeTemplateName(templateName)];
87 return loadedAssembly.GetManifestResourceStream(resourcePath);
90 /// <summary>
91 /// Collects the views on the assembly resource.
92 /// </summary>
93 /// <param name="dirName">Name of the dir.</param>
94 /// <param name="views">The views.</param>
95 public void CollectViews(string dirName, ArrayList views)
98 dirName = NormalizeTemplateName(dirName);
100 String[] names = loadedAssembly.GetManifestResourceNames();
102 for(int i=0; i < names.Length; i++)
104 String name = names[i].ToLower(System.Globalization.CultureInfo.InvariantCulture);
106 name = RemovePrefix(_namespace, name);
108 if (name.StartsWith(dirName.ToLower(System.Globalization.CultureInfo.InvariantCulture)))
110 views.Add(
111 Path.Combine(dirName, RemovePrefix(dirName, name))
117 private string RemovePrefix(string prefix, string name)
119 int toStripLength = prefix.Length;
120 if (name.StartsWith(prefix.ToLower(System.Globalization.CultureInfo.InvariantCulture)))
122 if (name[toStripLength] == '.')
124 return name.Substring(toStripLength + 1);
126 else
128 return name.Substring(toStripLength);
131 return name;
134 /// <summary>
135 /// Normalizes the name of the template.
136 /// </summary>
137 /// <param name="templateName">Name of the template.</param>
138 /// <returns></returns>
139 private static String NormalizeTemplateName(string templateName)
141 return templateName.Replace('/', '.').Replace('\\', '.');
144 /// <summary>
145 /// Registers the entries.
146 /// </summary>
147 private void RegisterEntries()
149 int toStripLength = _namespace.Length;
151 String[] names = loadedAssembly.GetManifestResourceNames();
153 for(int i=0; i < names.Length; i++)
155 String name = names[i].ToLower(System.Globalization.CultureInfo.InvariantCulture);
157 if (name.StartsWith(_namespace.ToLower(System.Globalization.CultureInfo.InvariantCulture)))
159 if (name[toStripLength] == '.')
161 name = name.Substring(toStripLength + 1);
163 else
165 name = name.Substring(toStripLength);
169 // Registers the lookup name
170 // associated to the original name
171 entries.Add(name, names[i]);