Added RedirectUsingNamedRoute
[castle.git] / Core / Castle.Core / Resource / AssemblyResource.cs
blobcd33f42191b2c816fde5e34a0a07c3616e52af9e
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.Core.Resource
17 using System;
18 using System.IO;
19 using System.Reflection;
21 public class AssemblyResource : AbstractStreamResource
23 private string assemblyName;
24 private string resourcePath;
25 private String basePath;
27 public AssemblyResource(CustomUri resource)
29 CreateStream = delegate
31 return CreateResourceFromUri(resource, null);
35 public AssemblyResource(CustomUri resource, String basePath)
37 CreateStream = delegate
39 return CreateResourceFromUri(resource, basePath);
43 public AssemblyResource(String resource)
45 CreateStream = delegate
47 return CreateResourceFromPath(resource, basePath);
51 public override IResource CreateRelative(String resourceName)
53 throw new NotImplementedException();
56 public override string ToString()
58 return String.Format("AssemblyResource: [{0}] [{1}]", assemblyName, resourcePath);
61 private Stream CreateResourceFromPath(String resource, String path)
63 if (!resource.StartsWith("assembly" + CustomUri.SchemeDelimiter))
65 resource = "assembly" + CustomUri.SchemeDelimiter + resource;
68 return CreateResourceFromUri(new CustomUri(resource), path);
71 private Stream CreateResourceFromUri(CustomUri resourcex, String basePath)
73 if (resourcex == null) throw new ArgumentNullException("resourcex");
75 assemblyName = resourcex.Host;
76 resourcePath = ConvertToResourceName(assemblyName, resourcex.Path, basePath);
78 Assembly assembly = ObtainAssembly(assemblyName);
80 String[] names = assembly.GetManifestResourceNames();
82 String nameFound = GetNameFound(names);
84 if (nameFound == null)
86 resourcePath = resourcex.Path.Replace('/', '.').Substring(1);
87 nameFound = GetNameFound(names);
90 if (nameFound == null)
92 String message = String.Format("The assembly resource {0} could not be located", resourcePath);
93 throw new ResourceException(message);
96 this.basePath = ConvertToPath(resourcePath);
98 return assembly.GetManifestResourceStream(nameFound);
101 private string GetNameFound(string[] names)
103 string nameFound = null;
104 foreach(String name in names)
106 if (String.Compare(resourcePath, name, true) == 0)
108 nameFound = name;
109 break;
112 return nameFound;
115 private string ConvertToResourceName(String assemblyName, String resource, String basePath)
117 // TODO: use basePath for relative name construction
118 return String.Format("{0}{1}", assemblyName, resource.Replace('/', '.'));
121 private string ConvertToPath(String resource)
123 string path = resource.Replace('.', '/');
124 if (path[0] != '/')
126 path = string.Format("/{0}", path);
128 return path;
131 private static Assembly ObtainAssembly(String assemblyName)
135 return Assembly.Load(assemblyName);
137 catch(Exception ex)
139 String message = String.Format("The assembly {0} could not be loaded", assemblyName);
140 throw new ResourceException(message, ex);