CompositeScriptReference.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Concurrent;
  30. using System.Collections.Generic;
  31. using System.ComponentModel;
  32. using System.IO;
  33. using System.Reflection;
  34. using System.Text;
  35. using System.Web;
  36. using System.Web.Handlers;
  37. using System.Web.Hosting;
  38. namespace System.Web.UI
  39. {
  40. [DefaultProperty ("Path")]
  41. public class CompositeScriptReference : ScriptReferenceBase
  42. {
  43. public const string COMPOSITE_SCRIPT_REFERENCE_PREFIX = "CSR:";
  44. static SplitOrderedList <string, List <CompositeEntry>> entriesCache;
  45. ScriptReferenceCollection scripts;
  46. [PersistenceMode (PersistenceMode.InnerProperty)]
  47. [Editor ("System.Web.UI.Design.CollectionEditorBase, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Design)]
  48. [MergableProperty (false)]
  49. [Category ("Behavior")]
  50. [DefaultValue (null)]
  51. [NotifyParentProperty (true)]
  52. public ScriptReferenceCollection Scripts {
  53. get {
  54. if (scripts == null)
  55. scripts = new ScriptReferenceCollection ();
  56. return scripts;
  57. }
  58. }
  59. static CompositeScriptReference ()
  60. {
  61. entriesCache = new SplitOrderedList <string, List <CompositeEntry>> (StringComparer.Ordinal);
  62. }
  63. internal static List <CompositeEntry> GetCompositeScriptEntries (string url)
  64. {
  65. if (String.IsNullOrEmpty (url) || entriesCache.Count == 0)
  66. return null;
  67. List <CompositeEntry> ret;
  68. if (!entriesCache.Find ((uint)url.GetHashCode (), url, out ret))
  69. return null;
  70. return ret;
  71. }
  72. protected internal override string GetUrl (ScriptManager scriptManager, bool zip)
  73. {
  74. if (scriptManager == null)
  75. // .NET emulation...
  76. throw new NullReferenceException (".NET emulation");
  77. var url = new StringBuilder (COMPOSITE_SCRIPT_REFERENCE_PREFIX);
  78. string path;
  79. string name;
  80. CompositeEntry entry;
  81. List <CompositeEntry> entries = null;
  82. WebResourceAttribute wra;
  83. foreach (ScriptReference sr in Scripts) {
  84. if (sr == null)
  85. continue;
  86. name = sr.Name;
  87. if (!String.IsNullOrEmpty (name)) {
  88. Assembly assembly = sr.ResolvedAssembly;
  89. name = GetScriptName (name, sr.IsDebugMode (scriptManager), null, assembly, out wra);
  90. path = scriptManager.ScriptPath;
  91. if (sr.IgnoreScriptPath || String.IsNullOrEmpty (path)) {
  92. entry = new CompositeEntry {
  93. Assembly = assembly,
  94. NameOrPath = name,
  95. Attribute = wra
  96. };
  97. } else {
  98. AssemblyName an = assembly.GetName ();
  99. entry = new CompositeEntry {
  100. NameOrPath = String.Concat (VirtualPathUtility.AppendTrailingSlash (path), an.Name, '/', an.Version, '/', name),
  101. Attribute = wra
  102. };
  103. }
  104. } else if (!String.IsNullOrEmpty ((path = sr.Path))) {
  105. bool notFound = false;
  106. name = GetScriptName (path, sr.IsDebugMode (scriptManager), scriptManager.EnableScriptLocalization ? ResourceUICultures : null, null, out wra);
  107. if (!HostingEnvironment.HaveCustomVPP)
  108. notFound = !File.Exists (HostingEnvironment.MapPath (name));
  109. else
  110. notFound = !HostingEnvironment.VirtualPathProvider.FileExists (name);
  111. if (notFound)
  112. throw new HttpException ("Web resource '" + name + "' was not found.");
  113. entry = new CompositeEntry {
  114. NameOrPath = name
  115. };
  116. } else
  117. entry = null;
  118. if (entry != null) {
  119. if (entries == null)
  120. entries = new List <CompositeEntry> ();
  121. entries.Add (entry);
  122. url.Append (entry.GetHashCode ().ToString ("x"));
  123. entry = null;
  124. }
  125. }
  126. if (entries == null || entries.Count == 0)
  127. return String.Empty;
  128. string ret = ScriptResourceHandler.GetResourceUrl (ThisAssembly, url.ToString (), NotifyScriptLoaded);
  129. entriesCache.InsertOrUpdate ((uint)ret.GetHashCode (), ret, entries, entries);
  130. return ret;
  131. }
  132. protected internal override bool IsAjaxFrameworkScript (ScriptManager scriptManager)
  133. {
  134. return false;
  135. }
  136. [Obsolete ("Use IsAjaxFrameworkScript(ScriptManager)")]
  137. protected internal override bool IsFromSystemWebExtensions ()
  138. {
  139. if (scripts == null || scripts.Count == 0)
  140. return false;
  141. Assembly myAssembly = ThisAssembly;
  142. foreach (ScriptReference sr in scripts)
  143. if (sr.ResolvedAssembly == myAssembly)
  144. return true;
  145. return false;
  146. }
  147. internal bool HaveScripts ()
  148. {
  149. return (scripts != null && scripts.Count > 0);
  150. }
  151. }
  152. }