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
.MonoRail
.Framework
.Services
18 using System
.Collections
.Generic
;
19 using System
.Globalization
;
20 using Castle
.Core
.Resource
;
25 public class DefaultStaticResourceRegistry
: IStaticResourceRegistry
27 private static readonly Dictionary
<ResourceKey
, ResourceHolder
> keyToResource
= new Dictionary
<ResourceKey
, ResourceHolder
>();
30 /// Initializes a new instance of the <see cref="DefaultStaticResourceRegistry"/> class.
32 public DefaultStaticResourceRegistry()
34 RegisterAssemblyResource("BehaviourScripts", null, null, "Castle.MonoRail.Framework",
35 "Castle.MonoRail.Framework.JSResources.Behaviour", "jsfunctions", "text/javascript");
37 RegisterAssemblyResource("AjaxScripts", null, null, "Castle.MonoRail.Framework",
38 "Castle.MonoRail.Framework.JSResources.Ajax", "jsfunctions", "text/javascript");
40 RegisterAssemblyResource("FormHelperScript", null, null, "Castle.MonoRail.Framework",
41 "Castle.MonoRail.Framework.JSResources.FormHelper", "jsfunctions", "text/javascript");
43 RegisterAssemblyResource("ZebdaScripts", null, null, "Castle.MonoRail.Framework",
44 "Castle.MonoRail.Framework.JSResources.ZebdaValidation", "jsfunctions", "text/javascript");
46 RegisterAssemblyResource("ValidateCore", null, null, "Castle.MonoRail.Framework",
47 "Castle.MonoRail.Framework.JSResources.Validation", "fValidateCore", "text/javascript");
49 RegisterAssemblyResource("ValidateLang", null, null, "Castle.MonoRail.Framework",
50 "Castle.MonoRail.Framework.JSResources.ValidationLang", "fValidateLang", "text/javascript");
52 RegisterAssemblyResource("ValidateValidators", null, null, "Castle.MonoRail.Framework",
53 "Castle.MonoRail.Framework.JSResources.Validation", "fValidateValidators", "text/javascript");
55 RegisterAssemblyResource("ValidateConfig", null, null, "Castle.MonoRail.Framework",
56 "Castle.MonoRail.Framework.JSResources.Validation", "fValidateConfig", "text/javascript");
58 RegisterAssemblyResource("Effects2", null, null, "Castle.MonoRail.Framework",
59 "Castle.MonoRail.Framework.JSResources.Effects2", "functions", "text/javascript");
61 RegisterAssemblyResource("EffectsFatScripts", null, null, "Castle.MonoRail.Framework",
62 "Castle.MonoRail.Framework.JSResources.EffectsFat", "fatfunctions", "text/javascript");
66 /// Registers an assembly resource.
68 /// <param name="name">The name.</param>
69 /// <param name="location">The location.</param>
70 /// <param name="version">The version.</param>
71 /// <param name="mimeType">Mime-type.</param>
72 /// <param name="assemblyName"></param>
73 /// <param name="resourceName">Resource name.</param>
74 /// <param name="resourceEntry">The resource entry name/key.</param>
75 public void RegisterAssemblyResource(string name
, string location
, string version
,
76 string assemblyName
, string resourceName
, string resourceEntry
, string mimeType
)
78 AssertParams(name
, assemblyName
, resourceName
, resourceEntry
, mimeType
);
80 CultureInfo ci
= CultureInfo
.InvariantCulture
;
82 if (location
!= null && location
!= "neutral")
84 ci
= CultureInfo
.CreateSpecificCulture(location
);
87 IResource resource
= new AssemblyBundleResource(
88 new CustomUri("assembly://" + assemblyName
+ "/" + resourceName
+ "/" + resourceEntry
), ci
);
90 keyToResource
[new ResourceKey(name
, location
, version
)] = new ResourceHolder(resource
, mimeType
);
94 /// Registers a custom resource.
96 /// <param name="name">The name.</param>
97 /// <param name="location">The location.</param>
98 /// <param name="version">The version.</param>
99 /// <param name="resource">The resource.</param>
100 /// <param name="mimeType">Mime-type.</param>
101 public void RegisterCustomResource(string name
, string location
, string version
, IResource resource
, string mimeType
)
103 AssertParams(name
, resource
, mimeType
);
105 keyToResource
[new ResourceKey(name
, location
, version
)] = new ResourceHolder(resource
, mimeType
);
109 /// Checks whether the resource exists for name, location and version
111 /// <param name="name">The name.</param>
112 /// <param name="location">The location.</param>
113 /// <param name="version">The version.</param>
114 /// <returns></returns>
115 public bool Exists(string name
, string location
, string version
)
117 return keyToResource
.ContainsKey(new ResourceKey(name
, location
, version
));
121 /// Gets the resource content identified by the name, location and version.
123 /// <param name="name">The name.</param>
124 /// <param name="location">The location.</param>
125 /// <param name="version">The version.</param>
126 /// <param name="mimeType">Mime-type.</param>
127 /// <returns></returns>
128 public string GetResource(string name
, string location
, string version
, out string mimeType
)
130 ResourceHolder resource
;
132 if (!keyToResource
.TryGetValue(new ResourceKey(name
, location
, version
), out resource
))
134 throw new ResourceException("Could not load resource: " + name
+ " location: " + location
+ " version: " + version
);
137 mimeType
= resource
.MimeType
;
138 return resource
.Content
;
143 private void AssertParams(string name
, IResource resource
, string mimeType
)
145 if (string.IsNullOrEmpty(name
))
147 throw new ArgumentNullException("name");
149 if (string.IsNullOrEmpty(mimeType
))
151 throw new ArgumentNullException("mimeType");
153 if (resource
== null)
155 throw new ArgumentNullException("resource");
159 private void AssertParams(string name
, string assemblyName
, string resourceName
, string resourceEntry
, string mimeType
)
161 if (string.IsNullOrEmpty(name
))
163 throw new ArgumentNullException("name");
165 if (string.IsNullOrEmpty(mimeType
))
167 throw new ArgumentNullException("mimeType");
169 if (string.IsNullOrEmpty(assemblyName
))
171 throw new ArgumentNullException("assemblyName");
173 if (string.IsNullOrEmpty(resourceName
))
175 throw new ArgumentNullException("resourceName");
177 if (string.IsNullOrEmpty(resourceEntry
))
179 throw new ArgumentNullException("resourceEntry");
185 private class ResourceHolder
187 private IResource resource
;
188 private string mimeType
;
190 public ResourceHolder(IResource resource
, string mimeType
)
192 this.resource
= resource
;
193 this.mimeType
= mimeType
;
196 public string MimeType
198 get { return mimeType; }
201 public string Content
203 get { return resource.GetStreamReader().ReadToEnd(); }
207 private class ResourceKey
: IEquatable
<ResourceKey
>
209 private string name
, location
, version
;
211 public ResourceKey(string name
, string location
, string version
)
214 this.location
= location
;
215 this.version
= version
;
218 public bool Equals(ResourceKey resourceKey
)
220 if (resourceKey
== null) return false;
221 if (String
.Compare(name
, resourceKey
.name
, StringComparison
.InvariantCultureIgnoreCase
) != 0)
225 if (String
.Compare(location
, resourceKey
.location
, StringComparison
.InvariantCultureIgnoreCase
) != 0)
229 if (String
.Compare(version
, resourceKey
.version
, StringComparison
.InvariantCultureIgnoreCase
) != 0)
236 public override int GetHashCode()
238 int result
= name
.ToLowerInvariant().GetHashCode();
239 result
= 29 * result
+ (location
!= null ? location
.ToLowerInvariant().GetHashCode() : 0);
240 result
= 29 * result
+ (version
!= null ? version
.ToLowerInvariant().GetHashCode() : 0);