CompositeScriptReference.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // Authors:
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2011 Novell, Inc (http://novell.com/)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. #if NET_3_5
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Concurrent;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. using System.IO;
  34. using System.Reflection;
  35. using System.Text;
  36. using System.Web;
  37. using System.Web.Handlers;
  38. using System.Web.Hosting;
  39. namespace System.Web.UI
  40. {
  41. [DefaultProperty ("Path")]
  42. public class CompositeScriptReference : ScriptReferenceBase
  43. {
  44. public const string COMPOSITE_SCRIPT_REFERENCE_PREFIX = "CSR:";
  45. static SplitOrderedList <string, List <CompositeEntry>> entriesCache;
  46. ScriptReferenceCollection scripts;
  47. [PersistenceMode (PersistenceMode.InnerProperty)]
  48. [Editor ("System.Web.UI.Design.CollectionEditorBase, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Design)]
  49. [MergableProperty (false)]
  50. [Category ("Behavior")]
  51. [DefaultValue (null)]
  52. [NotifyParentProperty (true)]
  53. public ScriptReferenceCollection Scripts {
  54. get {
  55. if (scripts == null)
  56. scripts = new ScriptReferenceCollection ();
  57. return scripts;
  58. }
  59. }
  60. static CompositeScriptReference ()
  61. {
  62. entriesCache = new SplitOrderedList <string, List <CompositeEntry>> (StringComparer.Ordinal);
  63. }
  64. internal static List <CompositeEntry> GetCompositeScriptEntries (string url)
  65. {
  66. if (String.IsNullOrEmpty (url) || entriesCache.Count == 0)
  67. return null;
  68. List <CompositeEntry> ret;
  69. if (!entriesCache.Find ((uint)url.GetHashCode (), url, out ret))
  70. return null;
  71. return ret;
  72. }
  73. protected internal override string GetUrl (ScriptManager scriptManager, bool zip)
  74. {
  75. if (scriptManager == null)
  76. // .NET emulation...
  77. throw new NullReferenceException (".NET emulation");
  78. var url = new StringBuilder (COMPOSITE_SCRIPT_REFERENCE_PREFIX);
  79. string path;
  80. string name;
  81. CompositeEntry entry;
  82. List <CompositeEntry> entries = null;
  83. WebResourceAttribute wra;
  84. foreach (ScriptReference sr in Scripts) {
  85. if (sr == null)
  86. continue;
  87. name = sr.Name;
  88. if (!String.IsNullOrEmpty (name)) {
  89. Assembly assembly = sr.ResolvedAssembly;
  90. name = GetScriptName (name, sr.IsDebugMode (scriptManager), null, assembly, out wra);
  91. path = scriptManager.ScriptPath;
  92. if (sr.IgnoreScriptPath || String.IsNullOrEmpty (path)) {
  93. entry = new CompositeEntry {
  94. Assembly = assembly,
  95. NameOrPath = name,
  96. Attribute = wra
  97. };
  98. } else {
  99. AssemblyName an = assembly.GetName ();
  100. entry = new CompositeEntry {
  101. NameOrPath = String.Concat (VirtualPathUtility.AppendTrailingSlash (path), an.Name, '/', an.Version, '/', name),
  102. Attribute = wra
  103. };
  104. }
  105. } else if (!String.IsNullOrEmpty ((path = sr.Path))) {
  106. bool notFound = false;
  107. name = GetScriptName (path, sr.IsDebugMode (scriptManager), scriptManager.EnableScriptLocalization ? ResourceUICultures : null, null, out wra);
  108. if (!HostingEnvironment.HaveCustomVPP)
  109. notFound = !File.Exists (HostingEnvironment.MapPath (name));
  110. else
  111. notFound = !HostingEnvironment.VirtualPathProvider.FileExists (name);
  112. if (notFound)
  113. throw new HttpException ("Web resource '" + name + "' was not found.");
  114. entry = new CompositeEntry {
  115. NameOrPath = name
  116. };
  117. } else
  118. entry = null;
  119. if (entry != null) {
  120. if (entries == null)
  121. entries = new List <CompositeEntry> ();
  122. entries.Add (entry);
  123. url.Append (entry.GetHashCode ().ToString ("x"));
  124. entry = null;
  125. }
  126. }
  127. if (entries == null || entries.Count == 0)
  128. return String.Empty;
  129. string ret = ScriptResourceHandler.GetResourceUrl (ThisAssembly, url.ToString (), NotifyScriptLoaded);
  130. entriesCache.InsertOrUpdate ((uint)ret.GetHashCode (), ret, entries, entries);
  131. return ret;
  132. }
  133. #if NET_4_0
  134. protected internal override bool IsAjaxFrameworkScript (ScriptManager scriptManager)
  135. {
  136. return false;
  137. }
  138. [Obsolete ("Use IsAjaxFrameworkScript(ScriptManager)")]
  139. #endif
  140. protected internal override bool IsFromSystemWebExtensions ()
  141. {
  142. if (scripts == null || scripts.Count == 0)
  143. return false;
  144. Assembly myAssembly = ThisAssembly;
  145. foreach (ScriptReference sr in scripts)
  146. if (sr.ResolvedAssembly == myAssembly)
  147. return true;
  148. return false;
  149. }
  150. internal bool HaveScripts ()
  151. {
  152. return (scripts != null && scripts.Count > 0);
  153. }
  154. }
  155. }
  156. #endif