Merge pull request #1218 from AndreyAkinshin/master
[mono.git] / mcs / class / System.Web.Extensions / System.Web.UI / ScriptReference.cs
1 //
2 // ScriptReference.cs
3 //
4 // Authors:
5 //   Igor Zelmanovich <igorz@mainsoft.com>
6 //   Marek Habersack <grendel@twistedcode.net>
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
31 using System;
32 using System.Collections.Generic;
33 using System.ComponentModel;
34 using System.IO;
35 using System.Reflection;
36 using System.Resources;
37 using System.Text;
38 using System.Threading;
39 using System.Web.Handlers;
40 using System.Web.UI.WebControls;
41
42 namespace System.Web.UI
43 {
44         [DefaultProperty ("Path")]
45         public class ScriptReference : ScriptReferenceBase
46         {
47                 string _name;
48                 string _assembly;
49                 bool _ignoreScriptPath;
50                 Assembly _resolvedAssembly;
51                 
52                 public ScriptReference ()
53                 {
54                 }
55
56                 public ScriptReference (string path)
57                 {
58                         this.Path = path;
59                 }
60
61                 public ScriptReference (string name, string assembly)
62                 {
63                         _name = name;
64                         _assembly = assembly;
65                 }
66
67                 public string Assembly {
68                         get {
69                                 return _assembly;
70                         }
71                         set {
72                                 _assembly = value;
73                                 _resolvedAssembly = null;
74                         }
75                 }
76
77                 internal Assembly ResolvedAssembly {
78                         get {
79                                 if (_resolvedAssembly == null) {
80                                         string assemblyName = this.Assembly;
81                                 
82                                         if (String.IsNullOrEmpty (assemblyName))
83                                                 _resolvedAssembly = typeof (ScriptManager).Assembly;
84                                         else
85                                                 _resolvedAssembly = global::System.Reflection.Assembly.Load (assemblyName);
86                                 }
87                                 return _resolvedAssembly;
88                         }
89                 }
90
91                 ScriptMode ScriptModeInternal {
92                         get {
93                                 if (ScriptMode == ScriptMode.Auto) {
94                                         if (!String.IsNullOrEmpty (Name))
95                                                 return ScriptMode.Inherit;
96                                         else
97                                                 return ScriptMode.Release;
98                                 }
99                                 else
100                                         return ScriptMode;
101                         }
102                 }
103                 
104                 public bool IgnoreScriptPath {
105                         get {
106                                 return _ignoreScriptPath;
107                         }
108                         set {
109                                 _ignoreScriptPath = value;
110                         }
111                 }
112
113                 public string Name {
114                         get {
115                                 return _name != null ? _name : String.Empty;
116                         }
117                         set {
118                                 _name = value;
119                         }
120                 }
121
122                 internal bool IsDebugMode (ScriptManager scriptManager)
123                 {
124                         if (scriptManager == null)
125                                 return ScriptModeInternal == ScriptMode.Debug;
126                         
127                         if (scriptManager.IsDeploymentRetail)
128                                 return false;
129
130                         switch (ScriptModeInternal) {
131                                 case ScriptMode.Inherit:
132                                         return scriptManager.IsDebuggingEnabled;
133
134                                 case ScriptMode.Debug:
135                                         return true;
136
137                                 default:
138                                         return false;
139                         }
140                 }
141                 
142                 [MonoTODO ("Compression not supported yet.")]
143                 protected internal override string GetUrl (ScriptManager scriptManager, bool zip)
144                 {
145                         bool isDebugMode = IsDebugMode (scriptManager);
146                         string path;
147                         string url = String.Empty;
148                         string name = Name;
149                         WebResourceAttribute wra;
150                         
151                         // LAMESPEC: Name property takes precedence
152                         if (!String.IsNullOrEmpty (name)) {
153                                 Assembly assembly = ResolvedAssembly;
154                                 name = GetScriptName (name, isDebugMode, null, assembly, out wra);
155                                 path = scriptManager.ScriptPath;
156                                 if (IgnoreScriptPath || String.IsNullOrEmpty (path))
157                                         url = ScriptResourceHandler.GetResourceUrl (assembly, name, NotifyScriptLoaded);
158                                 else {
159                                         AssemblyName an = assembly.GetName ();
160                                         url = scriptManager.ResolveClientUrl (String.Concat (VirtualPathUtility.AppendTrailingSlash (path), an.Name, '/', an.Version, '/', name));
161                                 }
162                         } else if (!String.IsNullOrEmpty ((path = Path))) {
163                                 url = GetScriptName (path, isDebugMode, scriptManager.EnableScriptLocalization ? ResourceUICultures : null, null, out wra);
164                         } else {
165                                 throw new InvalidOperationException ("Name and Path cannot both be empty.");
166                         }
167
168                         return url;
169                 }
170                 protected internal override bool IsAjaxFrameworkScript (ScriptManager scriptManager)
171                 {
172                         return false;
173                 }
174                 
175                 [Obsolete ("Use IsAjaxFrameworkScript(ScriptManager)")]
176                 protected internal override bool IsFromSystemWebExtensions ()
177                 {
178                         return ResolvedAssembly == ThisAssembly;
179                 }
180                 
181                 public override string ToString ()
182                 {
183                         return Name.Length > 0 ? Name : Path;
184                 }
185         }
186 }