1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
21 using System
.Reflection
;
24 /// Represents a source of views in an assembly resource.
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
;
34 /// Initializes a new instance of the <see cref="AssemblySourceInfo"/> class.
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
);
49 /// Gets the name of the assembly.
51 /// <value>The name of the assembly.</value>
52 public string AssemblyName
54 get { return assemblyName; }
58 /// Gets the namespace.
60 /// <value>The namespace.</value>
61 public string Namespace
63 get { return _namespace; }
67 /// Determines whether the assembly has the specified template.
69 /// <param name="templateName">Name of the template.</param>
71 /// <c>true</c> if the specified exists on the assembly; otherwise, <c>false</c>.
73 public bool HasTemplate(String templateName
)
75 return entries
.Contains(NormalizeTemplateName(templateName
));
79 /// Gets the template stream.
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
);
91 /// Collects the views on the assembly resource.
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
)))
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);
128 return name
.Substring(toStripLength
);
135 /// Normalizes the name of the template.
137 /// <param name="templateName">Name of the template.</param>
138 /// <returns></returns>
139 private static String
NormalizeTemplateName(string templateName
)
141 return templateName
.Replace('/', '.').Replace('\\', '.');
145 /// Registers the entries.
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);
165 name
= name
.Substring(toStripLength
);
169 // Registers the lookup name
170 // associated to the original name
171 entries
.Add(name
, names
[i
]);