3 // Marek Habersack <grendel@twistedcode.net>
5 // (C) 2011 Novell, Inc (http://novell.com/)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.Collections.Concurrent;
31 using System.Collections.Generic;
32 using System.ComponentModel;
34 using System.Reflection;
37 using System.Web.Handlers;
38 using System.Web.Hosting;
40 namespace System.Web.UI
42 [DefaultProperty ("Path")]
43 public class CompositeScriptReference : ScriptReferenceBase
45 public const string COMPOSITE_SCRIPT_REFERENCE_PREFIX = "CSR:";
47 static SplitOrderedList <string, List <CompositeEntry>> entriesCache;
49 ScriptReferenceCollection scripts;
51 [PersistenceMode (PersistenceMode.InnerProperty)]
52 [Editor ("System.Web.UI.Design.CollectionEditorBase, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Design)]
53 [MergableProperty (false)]
54 [Category ("Behavior")]
56 [NotifyParentProperty (true)]
57 public ScriptReferenceCollection Scripts {
60 scripts = new ScriptReferenceCollection ();
65 static CompositeScriptReference ()
67 entriesCache = new SplitOrderedList <string, List <CompositeEntry>> (StringComparer.Ordinal);
70 internal static List <CompositeEntry> GetCompositeScriptEntries (string url)
72 if (String.IsNullOrEmpty (url) || entriesCache.Count == 0)
75 List <CompositeEntry> ret;
76 if (!entriesCache.Find ((uint)url.GetHashCode (), url, out ret))
82 protected internal override string GetUrl (ScriptManager scriptManager, bool zip)
84 if (scriptManager == null)
86 throw new NullReferenceException (".NET emulation");
88 var url = new StringBuilder (COMPOSITE_SCRIPT_REFERENCE_PREFIX);
92 List <CompositeEntry> entries = null;
93 WebResourceAttribute wra;
95 foreach (ScriptReference sr in Scripts) {
100 if (!String.IsNullOrEmpty (name)) {
101 Assembly assembly = sr.ResolvedAssembly;
102 name = GetScriptName (name, sr.IsDebugMode (scriptManager), null, assembly, out wra);
103 path = scriptManager.ScriptPath;
104 if (sr.IgnoreScriptPath || String.IsNullOrEmpty (path)) {
105 entry = new CompositeEntry {
111 AssemblyName an = assembly.GetName ();
112 entry = new CompositeEntry {
113 NameOrPath = String.Concat (VirtualPathUtility.AppendTrailingSlash (path), an.Name, '/', an.Version, '/', name),
117 } else if (!String.IsNullOrEmpty ((path = sr.Path))) {
118 bool notFound = false;
119 name = GetScriptName (path, sr.IsDebugMode (scriptManager), scriptManager.EnableScriptLocalization ? ResourceUICultures : null, null, out wra);
120 if (!HostingEnvironment.HaveCustomVPP)
121 notFound = !File.Exists (HostingEnvironment.MapPath (name));
123 notFound = !HostingEnvironment.VirtualPathProvider.FileExists (name);
126 throw new HttpException ("Web resource '" + name + "' was not found.");
128 entry = new CompositeEntry {
136 entries = new List <CompositeEntry> ();
138 url.Append (entry.GetHashCode ().ToString ("x"));
143 if (entries == null || entries.Count == 0)
146 string ret = ScriptResourceHandler.GetResourceUrl (ThisAssembly, url.ToString (), NotifyScriptLoaded);
147 entriesCache.InsertOrUpdate ((uint)ret.GetHashCode (), ret, entries, entries);
150 protected internal override bool IsAjaxFrameworkScript (ScriptManager scriptManager)
155 [Obsolete ("Use IsAjaxFrameworkScript(ScriptManager)")]
156 protected internal override bool IsFromSystemWebExtensions ()
158 if (scripts == null || scripts.Count == 0)
161 Assembly myAssembly = ThisAssembly;
162 foreach (ScriptReference sr in scripts)
163 if (sr.ResolvedAssembly == myAssembly)
169 internal bool HaveScripts ()
171 return (scripts != null && scripts.Count > 0);