1 // Copyright 2004-2007 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
.Core
.Resource
19 using System
.Reflection
;
21 public class AssemblyResource
: AbstractStreamResource
23 private readonly Stream stream
;
24 private string assemblyName
;
25 private string resourcePath
;
26 private String basePath
;
28 public AssemblyResource(CustomUri resource
)
30 stream
= CreateResourceFromUri(resource
, null);
33 public AssemblyResource(CustomUri resource
, String basePath
)
35 stream
= CreateResourceFromUri(resource
, basePath
);
38 public AssemblyResource(String resource
)
40 stream
= CreateResourceFromPath(resource
, basePath
);
43 protected override Stream Stream
45 get { return stream; }
48 public override IResource
CreateRelative(String resourceName
)
50 throw new NotImplementedException();
53 public override string ToString()
55 return String
.Format("AssemblyResource: [{0}] [{1}]", assemblyName
, resourcePath
);
58 private Stream
CreateResourceFromPath(String resource
, String path
)
60 if (!resource
.StartsWith("assembly" + CustomUri
.SchemeDelimiter
))
62 resource
= "assembly" + CustomUri
.SchemeDelimiter
+ resource
;
65 return CreateResourceFromUri(new CustomUri(resource
), path
);
68 private Stream
CreateResourceFromUri(CustomUri resourcex
, String basePath
)
70 if (resourcex
== null) throw new ArgumentNullException("resourcex");
72 assemblyName
= resourcex
.Host
;
73 resourcePath
= ConvertToResourceName(assemblyName
, resourcex
.Path
, basePath
);
75 Assembly assembly
= ObtainAssembly(assemblyName
);
77 String
[] names
= assembly
.GetManifestResourceNames();
79 String nameFound
= GetNameFound(names
);
81 if (nameFound
== null)
83 resourcePath
= resourcex
.Path
.Replace('/', '.').Substring(1);
84 nameFound
= GetNameFound(names
);
87 if (nameFound
== null)
89 String message
= String
.Format("The assembly resource {0} could not be located", resourcePath
);
90 throw new ResourceException(message
);
93 this.basePath
= ConvertToPath(resourcePath
);
95 return assembly
.GetManifestResourceStream(nameFound
);
98 private string GetNameFound(string[] names
)
100 string nameFound
= null;
101 foreach(String name
in names
)
103 if (String
.Compare(resourcePath
, name
, true) == 0)
112 private string ConvertToResourceName(String assemblyName
, String resource
, String basePath
)
114 // TODO: use basePath for relative name construction
115 return String
.Format("{0}{1}", assemblyName
, resource
.Replace('/', '.'));
118 private string ConvertToPath(String resource
)
120 string path
= resource
.Replace('.', '/');
123 path
= string.Format("/{0}", path
);
128 private Assembly
ObtainAssembly(String assemblyName
)
132 return Assembly
.Load(assemblyName
);
136 String message
= String
.Format("The assembly {0} could not be loaded", assemblyName
);
137 throw new ResourceException(message
, ex
);