1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
//
// Author:
// Marek Habersack <grendel@twistedcode.net>
//
// (C) 2009-2011 Novell, Inc. http://novell.com/
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Security.Permissions;
using System.Threading;
using System.Web.UI.WebControls;
namespace System.Web.UI
{
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class ScriptReferenceBase
{
static SplitOrderedList <ResourceCacheEntry, bool> resourceCache;
string _path;
public bool NotifyScriptLoaded {
get; set;
}
public string Path {
get { return _path != null ? _path : String.Empty; }
set { _path = value; }
}
[TypeConverterAttribute(typeof(StringArrayConverter))]
public string[] ResourceUICultures {
get; set;
}
public ScriptMode ScriptMode {
get; set;
}
internal static Assembly ThisAssembly {
get; private set;
}
static ScriptReferenceBase ()
{
ThisAssembly = typeof (ScriptReferenceBase).Assembly;
resourceCache = new SplitOrderedList <ResourceCacheEntry, bool> (EqualityComparer <ResourceCacheEntry>.Default);
}
protected ScriptReferenceBase ()
{
this.NotifyScriptLoaded = true;
this.ScriptMode = ScriptMode.Auto;
}
protected internal virtual bool IsAjaxFrameworkScript (ScriptManager scriptManager)
{
return false;
}
[Obsolete ("Use IsAjaxFrameworkScript(ScriptManager)")]
protected internal abstract bool IsFromSystemWebExtensions ();
protected internal abstract string GetUrl (ScriptManager scriptManager, bool zip);
// This method is an example of particularily bad coding - .NET performs NO checks
// on pathOrName!
protected static string ReplaceExtension (string pathOrName)
{
// emulate .NET behavior
if (pathOrName == null)
throw new NullReferenceException ();
// We should check the length, but since .NET doesn't do that, we won't
// either. Ugh.
return pathOrName.Substring (0, pathOrName.Length - 2) + "debug.js";
}
internal static string GetScriptName (string releaseName, bool isDebugMode, string [] supportedUICultures, Assembly assembly, out WebResourceAttribute wra)
{
if (assembly != null)
VerifyAssemblyContainsResource (assembly, releaseName, out wra);
else
wra = null;
if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0))
return releaseName;
if (releaseName.Length < 3 || !releaseName.EndsWith (".js", StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException (String.Format ("'{0}' is not a valid script path. The path must end in '.js'.", releaseName));
StringBuilder sb = new StringBuilder (releaseName);
sb.Length -= 3;
if (isDebugMode)
sb.Append (".debug");
string culture = Thread.CurrentThread.CurrentUICulture.Name;
if (supportedUICultures != null && Array.IndexOf<string> (supportedUICultures, culture) >= 0)
sb.AppendFormat (".{0}", culture);
sb.Append (".js");
string ret = sb.ToString ();
WebResourceAttribute debugWra;
if (!CheckIfAssemblyContainsResource (assembly, ret, out debugWra))
return releaseName;
wra = debugWra;
return ret;
}
static void VerifyAssemblyContainsResource (Assembly assembly, string resourceName, out WebResourceAttribute wra)
{
var rce = new ResourceCacheEntry {
Assembly = assembly,
ResourceName = resourceName
};
WebResourceAttribute attr = null;
if (!resourceCache.InsertOrGet ((uint)rce.GetHashCode (), rce, false, () => CheckIfAssemblyContainsResource (assembly, resourceName, out attr)))
throw new InvalidOperationException (String.Format ("Assembly '{0}' does not contain a Web resource with name '{1}'.",
assembly.FullName, resourceName));
wra = attr;
}
static bool CheckIfAssemblyContainsResource (Assembly assembly, string resourceName, out WebResourceAttribute wra)
{
foreach (WebResourceAttribute attr in assembly.GetCustomAttributes (typeof (WebResourceAttribute), false)) {
if (String.Compare (resourceName, attr.WebResource, StringComparison.Ordinal) == 0) {
using (Stream rs = assembly.GetManifestResourceStream (resourceName)) {
if (rs == null)
throw new InvalidOperationException (
String.Format ("Assembly '{0}' contains a Web resource with name '{1}' but does not contain an embedded resource with name '{1}'.",
assembly.FullName, resourceName)
);
}
wra = attr;
return true;
}
}
wra = null;
return false;
}
sealed class ResourceCacheEntry
{
public Assembly Assembly;
public string ResourceName;
public override int GetHashCode ()
{
int ret = 0;
if (Assembly != null)
ret ^= Assembly.GetHashCode ();
if (ResourceName != null)
ret ^= ResourceName.GetHashCode ();
return ret;
}
}
}
}
|