ScriptReference.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // ScriptReference.cs
  3. //
  4. // Authors:
  5. // Igor Zelmanovich <[email protected]>
  6. // Marek Habersack <[email protected]>
  7. //
  8. // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
  9. // (C) 2011 Novell, Inc. http://novell.com/
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. using System.IO;
  34. using System.Reflection;
  35. using System.Resources;
  36. using System.Text;
  37. using System.Threading;
  38. using System.Web.Handlers;
  39. using System.Web.UI.WebControls;
  40. namespace System.Web.UI
  41. {
  42. [DefaultProperty ("Path")]
  43. public class ScriptReference : ScriptReferenceBase
  44. {
  45. string _name;
  46. string _assembly;
  47. bool _ignoreScriptPath;
  48. Assembly _resolvedAssembly;
  49. public ScriptReference ()
  50. {
  51. }
  52. public ScriptReference (string path)
  53. {
  54. this.Path = path;
  55. }
  56. public ScriptReference (string name, string assembly)
  57. {
  58. _name = name;
  59. _assembly = assembly;
  60. }
  61. public string Assembly {
  62. get {
  63. return _assembly;
  64. }
  65. set {
  66. _assembly = value;
  67. _resolvedAssembly = null;
  68. }
  69. }
  70. internal Assembly ResolvedAssembly {
  71. get {
  72. if (_resolvedAssembly == null) {
  73. string assemblyName = this.Assembly;
  74. if (String.IsNullOrEmpty (assemblyName))
  75. _resolvedAssembly = typeof (ScriptManager).Assembly;
  76. else
  77. _resolvedAssembly = global::System.Reflection.Assembly.Load (assemblyName);
  78. }
  79. return _resolvedAssembly;
  80. }
  81. }
  82. ScriptMode ScriptModeInternal {
  83. get {
  84. if (ScriptMode == ScriptMode.Auto) {
  85. if (!String.IsNullOrEmpty (Name))
  86. return ScriptMode.Inherit;
  87. else
  88. return ScriptMode.Release;
  89. }
  90. else
  91. return ScriptMode;
  92. }
  93. }
  94. public bool IgnoreScriptPath {
  95. get {
  96. return _ignoreScriptPath;
  97. }
  98. set {
  99. _ignoreScriptPath = value;
  100. }
  101. }
  102. public string Name {
  103. get {
  104. return _name != null ? _name : String.Empty;
  105. }
  106. set {
  107. _name = value;
  108. }
  109. }
  110. internal bool IsDebugMode (ScriptManager scriptManager)
  111. {
  112. if (scriptManager == null)
  113. return ScriptModeInternal == ScriptMode.Debug;
  114. if (scriptManager.IsDeploymentRetail)
  115. return false;
  116. switch (ScriptModeInternal) {
  117. case ScriptMode.Inherit:
  118. return scriptManager.IsDebuggingEnabled;
  119. case ScriptMode.Debug:
  120. return true;
  121. default:
  122. return false;
  123. }
  124. }
  125. [MonoTODO ("Compression not supported yet.")]
  126. protected internal override string GetUrl (ScriptManager scriptManager, bool zip)
  127. {
  128. bool isDebugMode = IsDebugMode (scriptManager);
  129. string path;
  130. string url = String.Empty;
  131. string name = Name;
  132. WebResourceAttribute wra;
  133. // LAMESPEC: Name property takes precedence
  134. if (!String.IsNullOrEmpty (name)) {
  135. Assembly assembly = ResolvedAssembly;
  136. name = GetScriptName (name, isDebugMode, null, assembly, out wra);
  137. path = scriptManager.ScriptPath;
  138. if (IgnoreScriptPath || String.IsNullOrEmpty (path))
  139. url = ScriptResourceHandler.GetResourceUrl (assembly, name, NotifyScriptLoaded);
  140. else {
  141. AssemblyName an = assembly.GetName ();
  142. url = scriptManager.ResolveClientUrl (String.Concat (VirtualPathUtility.AppendTrailingSlash (path), an.Name, '/', an.Version, '/', name));
  143. }
  144. } else if (!String.IsNullOrEmpty ((path = Path))) {
  145. url = GetScriptName (path, isDebugMode, scriptManager.EnableScriptLocalization ? ResourceUICultures : null, null, out wra);
  146. } else {
  147. throw new InvalidOperationException ("Name and Path cannot both be empty.");
  148. }
  149. return url;
  150. }
  151. #if NET_4_0
  152. protected internal override bool IsAjaxFrameworkScript (ScriptManager scriptManager)
  153. {
  154. return false;
  155. }
  156. [Obsolete ("Use IsAjaxFrameworkScript(ScriptManager)")]
  157. #endif
  158. protected internal override bool IsFromSystemWebExtensions ()
  159. {
  160. return ResolvedAssembly == ThisAssembly;
  161. }
  162. public override string ToString ()
  163. {
  164. return Name.Length > 0 ? Name : Path;
  165. }
  166. }
  167. }