[bcl] Remove NET_3_5 defines from class libs
[mono.git] / mcs / class / System.Web.Extensions / System.Web.UI / CompositeScriptReference.cs
1 //
2 // Authors:
3 //   Marek Habersack <grendel@twistedcode.net>
4 //
5 // (C) 2011 Novell, Inc (http://novell.com/)
6 //
7
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
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
40 namespace System.Web.UI
41 {
42         [DefaultProperty ("Path")]
43         public class CompositeScriptReference : ScriptReferenceBase
44         {
45                 public const string COMPOSITE_SCRIPT_REFERENCE_PREFIX = "CSR:";
46
47                 static SplitOrderedList <string, List <CompositeEntry>> entriesCache;
48                 
49                 ScriptReferenceCollection scripts;
50                 
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")]
55                 [DefaultValue (null)]
56                 [NotifyParentProperty (true)]   
57                 public ScriptReferenceCollection Scripts {
58                         get {
59                                 if (scripts == null)
60                                         scripts = new ScriptReferenceCollection ();
61                                 return scripts;
62                         }
63                 }
64
65                 static CompositeScriptReference ()
66                 {
67                         entriesCache = new SplitOrderedList <string, List <CompositeEntry>> (StringComparer.Ordinal);
68                 }
69
70                 internal static List <CompositeEntry> GetCompositeScriptEntries (string url)
71                 {
72                         if (String.IsNullOrEmpty (url) || entriesCache.Count == 0)
73                                 return null;
74                         
75                         List <CompositeEntry> ret;
76                         if (!entriesCache.Find ((uint)url.GetHashCode (), url, out ret))
77                                 return null;
78
79                         return ret;
80                 }
81                 
82                 protected internal override string GetUrl (ScriptManager scriptManager, bool zip)
83                 {
84                         if (scriptManager == null)
85                                 // .NET emulation...
86                                 throw new NullReferenceException (".NET emulation");
87                         
88                         var url = new StringBuilder (COMPOSITE_SCRIPT_REFERENCE_PREFIX);
89                         string path;
90                         string name;
91                         CompositeEntry entry;
92                         List <CompositeEntry> entries = null;
93                         WebResourceAttribute wra;
94                         
95                         foreach (ScriptReference sr in Scripts) {
96                                 if (sr == null)
97                                         continue;
98
99                                 name = sr.Name;
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 {
106                                                         Assembly = assembly,
107                                                         NameOrPath = name,
108                                                         Attribute = wra
109                                                 };
110                                         } else {
111                                                 AssemblyName an = assembly.GetName ();
112                                                 entry = new CompositeEntry {
113                                                         NameOrPath = String.Concat (VirtualPathUtility.AppendTrailingSlash (path), an.Name, '/', an.Version, '/', name),
114                                                         Attribute = wra
115                                                 };
116                                         }
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));
122                                         else 
123                                                 notFound = !HostingEnvironment.VirtualPathProvider.FileExists (name);
124
125                                         if (notFound)
126                                                 throw new HttpException ("Web resource '" + name + "' was not found.");
127                                         
128                                         entry = new CompositeEntry {
129                                                 NameOrPath = name
130                                         };
131                                 } else
132                                         entry = null;
133
134                                 if (entry != null) {
135                                         if (entries == null)
136                                                 entries = new List <CompositeEntry> ();
137                                         entries.Add (entry);
138                                         url.Append (entry.GetHashCode ().ToString ("x"));
139                                         entry = null;
140                                 }
141                         }
142                         
143                         if (entries == null || entries.Count == 0)
144                                 return String.Empty;
145
146                         string ret = ScriptResourceHandler.GetResourceUrl (ThisAssembly, url.ToString (), NotifyScriptLoaded);
147                         entriesCache.InsertOrUpdate ((uint)ret.GetHashCode (), ret, entries, entries);
148                         return ret;
149                 }
150                 protected internal override bool IsAjaxFrameworkScript (ScriptManager scriptManager)
151                 {
152                         return false;
153                 }
154                 
155                 [Obsolete ("Use IsAjaxFrameworkScript(ScriptManager)")]
156                 protected internal override bool IsFromSystemWebExtensions ()
157                 {
158                         if (scripts == null || scripts.Count == 0)
159                                 return false;
160
161                         Assembly myAssembly = ThisAssembly;
162                         foreach (ScriptReference sr in scripts)
163                                 if (sr.ResolvedAssembly == myAssembly)
164                                         return true;
165
166                         return false;
167                 }
168
169                 internal bool HaveScripts ()
170                 {
171                         return (scripts != null && scripts.Count > 0);
172                 }
173         }
174 }