1 namespace System.Web.UI { 2 using System; 3 using System.Collections.Concurrent; 4 using System.Globalization; 5 using System.Reflection; 6 using System.Web.Resources; 7 using System.Web.Util; 8 9 public class ScriptResourceMapping : IScriptResourceMapping { 10 private readonly ConcurrentDictionary<Tuple<String, Assembly>, ScriptResourceDefinition> _definitions = 11 new ConcurrentDictionary<Tuple<String, Assembly>, ScriptResourceDefinition>(); 12 AddDefinition(string name, ScriptResourceDefinition definition)13 public void AddDefinition(string name, ScriptResourceDefinition definition) { 14 AddDefinition(name, assembly: AssemblyCache.SystemWebExtensions, definition: definition); 15 } 16 AddDefinition(string name, Assembly assembly, ScriptResourceDefinition definition)17 public void AddDefinition(string name, Assembly assembly, ScriptResourceDefinition definition) { 18 // dictionary indexer will update the value if it already exists 19 if (String.IsNullOrEmpty(name)) { 20 throw new ArgumentException(AtlasWeb.Common_NullOrEmpty, "name"); 21 } 22 if (definition == null) { 23 throw new ArgumentNullException("definition"); 24 } 25 if (String.IsNullOrEmpty(definition.ResourceName) && String.IsNullOrEmpty(definition.Path)) { 26 throw new ArgumentException(AtlasWeb.ScriptResourceDefinition_NameAndPathCannotBeEmpty, "definition"); 27 } 28 EnsureAbsoluteOrAppRelative(definition.Path); 29 EnsureAbsoluteOrAppRelative(definition.DebugPath); 30 EnsureAbsoluteOrAppRelative(definition.CdnPath); 31 EnsureAbsoluteOrAppRelative(definition.CdnDebugPath); 32 assembly = NormalizeAssembly(assembly); 33 _definitions[new Tuple<String, Assembly>(name, assembly)] = definition; 34 } 35 Clear()36 public void Clear() { 37 _definitions.Clear(); 38 } 39 EnsureAbsoluteOrAppRelative(string path)40 private void EnsureAbsoluteOrAppRelative(string path) { 41 if (!String.IsNullOrEmpty(path) && 42 !UrlPath.IsAppRelativePath(path) && // ~/foo.. 43 !UrlPath.IsRooted(path) && // /foo 44 !Uri.IsWellFormedUriString(path, UriKind.Absolute)) { // http://... 45 throw new InvalidOperationException( 46 String.Format(CultureInfo.InvariantCulture, AtlasWeb.ScriptResourceDefinition_InvalidPath, path)); 47 } 48 } 49 GetDefinition(string name)50 public ScriptResourceDefinition GetDefinition(string name) { 51 return GetDefinition(name, AssemblyCache.SystemWebExtensions); 52 } 53 GetDefinition(string name, Assembly assembly)54 public ScriptResourceDefinition GetDefinition(string name, Assembly assembly) { 55 if (String.IsNullOrEmpty(name)) { 56 throw new ArgumentException(AtlasWeb.Common_NullOrEmpty, "name"); 57 } 58 ScriptResourceDefinition definition; 59 assembly = NormalizeAssembly(assembly); 60 _definitions.TryGetValue(new Tuple<string, Assembly>(name, assembly), out definition); 61 return definition; 62 } 63 GetDefinition(ScriptReference scriptReference)64 public ScriptResourceDefinition GetDefinition(ScriptReference scriptReference) { 65 if (scriptReference == null) { 66 throw new ArgumentNullException("scriptReference"); 67 } 68 string name = scriptReference.Name; 69 Assembly assembly = null; 70 ScriptResourceDefinition definition = null; 71 if (!String.IsNullOrEmpty(name)) { 72 assembly = scriptReference.GetAssembly(); 73 definition = ScriptManager.ScriptResourceMapping.GetDefinition(name, assembly); 74 } 75 return definition; 76 } 77 RemoveDefinition(string name)78 public ScriptResourceDefinition RemoveDefinition(string name) { 79 return RemoveDefinition(name, AssemblyCache.SystemWebExtensions); 80 } 81 RemoveDefinition(string name, Assembly assembly)82 public ScriptResourceDefinition RemoveDefinition(string name, Assembly assembly) { 83 if (String.IsNullOrEmpty(name)) { 84 throw new ArgumentException(AtlasWeb.Common_NullOrEmpty, "name"); 85 } 86 ScriptResourceDefinition definition; 87 assembly = NormalizeAssembly(assembly); 88 _definitions.TryRemove(new Tuple<String, Assembly>(name, assembly), out definition); 89 return definition; 90 } 91 92 #region IScriptResourceMapping Members IScriptResourceMapping.GetDefinition(string name)93 IScriptResourceDefinition IScriptResourceMapping.GetDefinition(string name) { 94 return GetDefinition(name); 95 } 96 IScriptResourceMapping.GetDefinition(string name, Assembly assembly)97 IScriptResourceDefinition IScriptResourceMapping.GetDefinition(string name, Assembly assembly) { 98 return GetDefinition(name, assembly); 99 } 100 #endregion 101 NormalizeAssembly(Assembly assembly)102 private static Assembly NormalizeAssembly(Assembly assembly) { 103 if ((assembly != null) && AssemblyCache.IsAjaxFrameworkAssembly(assembly)) { 104 assembly = null; 105 } 106 return assembly; 107 } 108 } 109 } 110