Esempio n. 1
0
        internal CultureInfo DetermineCulture(ScriptManager scriptManager)
        {
            if ((ResourceUICultures == null) || (ResourceUICultures.Length == 0))
            {
                // In this case we want to determine available cultures from assembly info if available
                if (!String.IsNullOrEmpty(EffectiveResourceName))
                {
                    return(ScriptResourceHandler
                           .DetermineNearestAvailableCulture(GetAssembly(scriptManager), EffectiveResourceName, CultureInfo.CurrentUICulture));
                }
                return(CultureInfo.InvariantCulture);
            }
            CultureInfo currentCulture = CultureInfo.CurrentUICulture;

            while (!currentCulture.Equals(CultureInfo.InvariantCulture))
            {
                string cultureName = currentCulture.ToString();
                foreach (string uiCulture in ResourceUICultures)
                {
                    if (String.Equals(cultureName, uiCulture.Trim(), StringComparison.OrdinalIgnoreCase))
                    {
                        return(currentCulture);
                    }
                }
                currentCulture = currentCulture.Parent;
            }
            return(currentCulture);
        }
        protected internal override string GetUrl(ScriptManager scriptManager, bool zip)
        {
            bool   isDebugMode = IsDebugMode(scriptManager);
            string path;
            string url  = String.Empty;
            string name = Name;
            WebResourceAttribute wra;

            // LAMESPEC: Name property takes precedence
            if (!String.IsNullOrEmpty(name))
            {
                Assembly assembly = ResolvedAssembly;
                name = GetScriptName(name, isDebugMode, null, assembly, out wra);
                path = scriptManager.ScriptPath;
                if (IgnoreScriptPath || String.IsNullOrEmpty(path))
                {
                    url = ScriptResourceHandler.GetResourceUrl(assembly, name, NotifyScriptLoaded);
                }
                else
                {
                    AssemblyName an = assembly.GetName();
                    url = scriptManager.ResolveClientUrl(String.Concat(VirtualPathUtility.AppendTrailingSlash(path), an.Name, '/', an.Version, '/', name));
                }
            }
            else if (!String.IsNullOrEmpty((path = Path)))
            {
                url = GetScriptName(path, isDebugMode, scriptManager.EnableScriptLocalization ? ResourceUICultures : null, null, out wra);
            }
            else
            {
                throw new InvalidOperationException("Name and Path cannot both be empty.");
            }

            return(url);
        }
Esempio n. 3
0
        public string GetCompositeScript(string[] urls)
        {
            var          script             = new StringBuilder();
            IHttpHandler handler            = new ScriptResourceHandler();
            var          absoluteUri        = Page.Request.Url.AbsoluteUri;
            var          fileNameIndex      = absoluteUri.LastIndexOf(Page.Request.Url.Segments[2]);
            var          scriptResourcePath = absoluteUri.Substring(0, fileNameIndex) + "ScriptResource.axd";

            foreach (var url in urls)
            {
                // Is script resource-based?
                if (url.IndexOf("/scriptresource.axd", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    var queryStringIndex = url.IndexOf('?');
                    var queryString      = url.Substring(queryStringIndex + 1);
                    var request          = new HttpRequest("scriptresource.axd", scriptResourcePath, queryString);
                    using (var textWriter = new StringWriter(script)) {
                        HttpResponse response = new HttpResponse(textWriter);
                        HttpContext  context  = new HttpContext(request, response);
                        handler.ProcessRequest(context);
                    }
                    script.AppendLine();
                }
                else
                {
                    using (var textReader = new StreamReader(Page.Server.MapPath(url), true)) {
                        script.AppendLine(textReader.ReadToEnd());
                    }
                }
            }
            script.AppendLine("if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();");
            return(script.ToString());
        }
Esempio n. 4
0
        protected internal override string GetUrl(ScriptManager scriptManager, bool zip)
        {
            bool isDebugMode = scriptManager.IsDeploymentRetail ? false :
                               (ScriptModeInternal == ScriptMode.Inherit ? scriptManager.IsDebuggingEnabled : (ScriptModeInternal == ScriptMode.Debug));
            string path = Path;
            string url  = String.Empty;

            if (!String.IsNullOrEmpty(path))
            {
                url = GetScriptName(path, isDebugMode, scriptManager.EnableScriptLocalization ? ResourceUICultures : null);
            }
            else if (!String.IsNullOrEmpty(Name))
            {
                Assembly assembly;
                string   assemblyName = this.Assembly;

                if (String.IsNullOrEmpty(assemblyName))
                {
                    assembly = typeof(ScriptManager).Assembly;
                }
                else
                {
                    assembly = global::System.Reflection.Assembly.Load(assemblyName);
                }
                string name       = GetScriptName(Name, isDebugMode, null);
                string scriptPath = scriptManager.ScriptPath;
                if (IgnoreScriptPath || String.IsNullOrEmpty(scriptPath))
                {
                    url = ScriptResourceHandler.GetResourceUrl(assembly, name, NotifyScriptLoaded);
                }
                else
                {
                    AssemblyName an = assembly.GetName();
                    url = scriptManager.ResolveClientUrl(String.Concat(VirtualPathUtility.AppendTrailingSlash(scriptPath), an.Name, '/', an.Version, '/', name));
                }
            }
            else
            {
                throw new InvalidOperationException("Name and Path cannot both be empty.");
            }

            return(url);
        }
Esempio n. 5
0
        private string GetUrlFromName(ScriptManager scriptManager, IControl scriptManagerControl, bool zip, bool useCdnPath)
        {
            string   resourceName     = EffectiveResourceName;
            Assembly assembly         = GetAssembly(scriptManager);
            bool     hasDebugResource = DetermineResourceNameAndAssembly(scriptManager, IsDebuggingEnabled(scriptManager),
                                                                         ref resourceName, ref assembly);

            if (useCdnPath)
            {
                string cdnPath = GetUrlForCdn(scriptManager, resourceName, assembly, hasDebugResource);
                if (!String.IsNullOrEmpty(cdnPath))
                {
                    return(cdnPath);
                }
            }

            CultureInfo culture = (scriptManager.EnableScriptLocalization ?
                                   DetermineCulture(scriptManager) : CultureInfo.InvariantCulture);

#pragma warning disable 618
            // ScriptPath is obsolete but still functional
            if (IgnoreScriptPath || String.IsNullOrEmpty(scriptManager.ScriptPath))
            {
                return(ScriptResourceHandler.GetScriptResourceUrl(assembly, resourceName, culture, zip));
            }
            else
            {
                string path = GetScriptPath(resourceName, assembly, culture, scriptManager.ScriptPath);

                if (IsBundleReference)
                {
                    return(scriptManager.BundleReflectionHelper.GetBundleUrl(path));
                }

                // Always want to resolve ScriptPath urls against the ScriptManager itself,
                // regardless of whether the ScriptReference was declared on the ScriptManager
                // or a ScriptManagerProxy.
                return(scriptManagerControl.ResolveClientUrl(path));
            }
#pragma warning restore 618
        }
Esempio n. 6
0
        private static void CopyScriptToStringBuilderWithSubstitution(
            string content, Assembly assembly, bool zip, StringBuilder output)
        {
            // Looking for something of the form: WebResource("resourcename")
            MatchCollection matches    = _webResourceRegEx.Matches(content);
            int             startIndex = 0;

            foreach (Match match in matches)
            {
                output.Append(content.Substring(startIndex, match.Index - startIndex));

                Group  group = match.Groups["resourceName"];
                string embeddedResourceName = group.Value;
                bool   isScriptResource     = String.Equals(
                    match.Groups["resourceType"].Value, "ScriptResource", StringComparison.Ordinal);
                try {
                    if (isScriptResource)
                    {
                        output.Append(ScriptResourceHandler.GetScriptResourceUrl(
                                          assembly, embeddedResourceName, CultureInfo.CurrentUICulture, zip));
                    }
                    else
                    {
                        output.Append(AssemblyResourceLoader.GetWebResourceUrlInternal(
                                          assembly, embeddedResourceName, htmlEncoded: false, forSubstitution: true, scriptManager: null));
                    }
                }
                catch (HttpException e) {
                    throw new HttpException(String.Format(CultureInfo.CurrentCulture,
                                                          AtlasWeb.ScriptResourceHandler_UnknownResource,
                                                          embeddedResourceName), e);
                }

                startIndex = match.Index + match.Length;
            }

            output.Append(content.Substring(startIndex, content.Length - startIndex));
        }
        protected internal override string GetUrl(ScriptManager scriptManager, bool zip)
        {
            bool isDebuggingEnabled = !scriptManager.DeploymentSectionRetail &&
                                      ((ScriptMode == ScriptMode.Debug) ||
                                       (((ScriptMode == ScriptMode.Inherit) || (ScriptMode == ScriptMode.Auto)) &&
                                        (scriptManager.IsDebuggingEnabled)));

            if (!String.IsNullOrEmpty(Path))
            {
                string path = Path;
                if (isDebuggingEnabled)
                {
                    path = GetDebugPath(path);
                }
                if (scriptManager.EnableScriptLocalization &&
                    (ResourceUICultures != null) && (ResourceUICultures.Length != 0))
                {
                    CultureInfo currentCulture = CultureInfo.CurrentUICulture;
                    string      cultureName    = null;
                    bool        found          = false;
                    while (!currentCulture.Equals(CultureInfo.InvariantCulture))
                    {
                        cultureName = currentCulture.ToString();
                        foreach (string uiCulture in ResourceUICultures)
                        {
                            if (String.Equals(cultureName, uiCulture.Trim(), StringComparison.OrdinalIgnoreCase))
                            {
                                found = true;
                                break;
                            }
                        }
                        if (found)
                        {
                            break;
                        }
                        currentCulture = currentCulture.Parent;
                    }
                    if (found)
                    {
                        path = (path.Substring(0, path.Length - 2) + cultureName + ".js");
                    }
                }

                // ResolveClientUrl is appropriate here because the path is consumed by the page it was declared within
                return(ClientUrlResolver.ResolveClientUrl(path));
            }
            List <Tuple <Assembly, List <Tuple <string, CultureInfo> > > > resources =
                new List <Tuple <Assembly, List <Tuple <string, CultureInfo> > > >();
            Tuple <Assembly, List <Tuple <string, CultureInfo> > > resourceList = null;

            foreach (ScriptReference reference in Scripts)
            {
                if ((scriptManager.AjaxFrameworkMode == AjaxFrameworkMode.Explicit) &&
                    reference.IsAjaxFrameworkScript(scriptManager) &&
                    reference.EffectiveResourceName.StartsWith("MicrosoftAjax.", StringComparison.Ordinal))
                {
                    continue;
                }
                bool hasPath = !String.IsNullOrEmpty(reference.EffectivePath);
#pragma warning disable 618
                // ScriptPath is obsolete but still functional
                bool hasScriptPath = (!String.IsNullOrEmpty(scriptManager.ScriptPath) && !reference.IgnoreScriptPath);
#pragma warning restore 618
                // cacheAssembly will be null if ScriptPath is set, but we still need the resource assembly in that case
                Assembly   resourceAssembly = null;
                string     resourceName     = null;
                Assembly   cacheAssembly    = null;
                ScriptMode effectiveScriptModeForReference = reference.EffectiveScriptMode;
                bool       isDebuggingEnabledForReference  =
                    (effectiveScriptModeForReference == ScriptMode.Inherit) ?
                    isDebuggingEnabled :
                    (effectiveScriptModeForReference == ScriptMode.Debug);
                if (!hasPath)
                {
                    resourceAssembly = reference.GetAssembly(scriptManager);
                    resourceName     = reference.EffectiveResourceName;
                    reference.DetermineResourceNameAndAssembly(scriptManager, isDebuggingEnabledForReference,
                                                               ref resourceName, ref resourceAssembly);
                    if ((resourceAssembly != scriptManager.AjaxFrameworkAssembly) &&
                        (resourceAssembly != AssemblyCache.SystemWebExtensions) &&
                        AssemblyCache.IsAjaxFrameworkAssembly(resourceAssembly))
                    {
                        // if it is coming from an assembly that is not the current ajax script assembly, make sure the assembly
                        // is not meant to be an ajax script assembly.
                        // it isnt an AjaxFrameworkScript but it might be from an assembly that is meant to
                        // be an ajax script assembly, in which case we should throw an error.
                        throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture,
                                                                          AtlasWeb.ScriptReference_ResourceRequiresAjaxAssembly, resourceName, resourceAssembly));
                    }
                    if (!hasScriptPath)
                    {
                        // The resource requested in the composite url will only contain the assembly name if it
                        // will ultimately come from the assembly -- if ScriptPath is set, it doesn't.
                        // We do still need to know the resource assembly in that case though, hence the separate
                        // assembly variables.
                        cacheAssembly = resourceAssembly;
                    }
                }

                CultureInfo culture = reference.DetermineCulture(scriptManager);
                if ((resourceList == null) || (resourceList.Item1 != cacheAssembly))
                {
                    resourceList = new Tuple <Assembly, List <Tuple <string, CultureInfo> > >(
                        cacheAssembly, new List <Tuple <string, CultureInfo> >());
                    resources.Add(resourceList);
                }
                if (hasPath || hasScriptPath)
                {
                    if (hasPath)
                    {
                        if (String.IsNullOrEmpty(reference.Path))
                        {
                            // the Path is coming from a script mapping, so its debug path applies
                            resourceName = reference.GetPath(scriptManager, reference.EffectivePath, reference.ScriptInfo.DebugPath,
                                                             isDebuggingEnabledForReference);
                        }
                        else
                        {
                            // path explicitly set, even if a mapping has a DebugPath it does not apply
                            resourceName = reference.GetPath(scriptManager, reference.Path, null,
                                                             isDebuggingEnabledForReference);
                        }
                    }
                    else
                    {
#pragma warning disable 618
                        // ScriptPath is obsolete but still functional
                        resourceName = ScriptReference.GetScriptPath(resourceName, resourceAssembly,
                                                                     culture, scriptManager.ScriptPath);
#pragma warning restore 618
                    }

                    // ResolveClientUrl not appropriate here because the handler that will serve the response is not
                    // in the same directory as the page that is generating the url. Instead, an absolute url is needed
                    // as with ResolveUrl(). However, ResolveUrl() would prepend the entire application root name. For
                    // example, ~/foo.js would be /TheApplicationRoot/foo.js. If there are many path based scripts the
                    // app root would be repeated many times, which for deep apps or long named apps could cause the url
                    // to reach the maximum 2048 characters very quickly. So, the path is combined with the control's
                    // AppRelativeTemplateSourceDirectory manually, so that ~/foo.js remains ~/foo.js, and foo/bar.js
                    // becomes ~/templatesource/foo/bar.js. Absolute paths can remain as is. The ScriptResourceHandler will
                    // resolve the ~/ with the app root using VirtualPathUtility.ToAbsolute().
                    if (UrlPath.IsRelativeUrl(resourceName) && !UrlPath.IsAppRelativePath(resourceName))
                    {
                        resourceName = UrlPath.Combine(ClientUrlResolver.AppRelativeTemplateSourceDirectory, resourceName);
                    }
                }
                resourceList.Item2.Add(new Tuple <string, CultureInfo>(resourceName, culture));
            }
            return(ScriptResourceHandler.GetScriptResourceUrl(resources, zip));
        }
        protected internal override string GetUrl(ScriptManager scriptManager, bool zip)
        {
            if (scriptManager == null)
            {
                // .NET emulation...
                throw new NullReferenceException(".NET emulation");
            }

            var                   url = new StringBuilder(COMPOSITE_SCRIPT_REFERENCE_PREFIX);
            string                path;
            string                name;
            CompositeEntry        entry;
            List <CompositeEntry> entries = null;
            WebResourceAttribute  wra;

            foreach (ScriptReference sr in Scripts)
            {
                if (sr == null)
                {
                    continue;
                }

                name = sr.Name;
                if (!String.IsNullOrEmpty(name))
                {
                    Assembly assembly = sr.ResolvedAssembly;
                    name = GetScriptName(name, sr.IsDebugMode(scriptManager), null, assembly, out wra);
                    path = scriptManager.ScriptPath;
                    if (sr.IgnoreScriptPath || String.IsNullOrEmpty(path))
                    {
                        entry = new CompositeEntry {
                            Assembly   = assembly,
                            NameOrPath = name,
                            Attribute  = wra
                        };
                    }
                    else
                    {
                        AssemblyName an = assembly.GetName();
                        entry = new CompositeEntry {
                            NameOrPath = String.Concat(VirtualPathUtility.AppendTrailingSlash(path), an.Name, '/', an.Version, '/', name),
                            Attribute  = wra
                        };
                    }
                }
                else if (!String.IsNullOrEmpty((path = sr.Path)))
                {
                    bool notFound = false;
                    name = GetScriptName(path, sr.IsDebugMode(scriptManager), scriptManager.EnableScriptLocalization ? ResourceUICultures : null, null, out wra);
                    if (!HostingEnvironment.HaveCustomVPP)
                    {
                        notFound = !File.Exists(HostingEnvironment.MapPath(name));
                    }
                    else
                    {
                        notFound = !HostingEnvironment.VirtualPathProvider.FileExists(name);
                    }

                    if (notFound)
                    {
                        throw new HttpException("Web resource '" + name + "' was not found.");
                    }

                    entry = new CompositeEntry {
                        NameOrPath = name
                    };
                }
                else
                {
                    entry = null;
                }

                if (entry != null)
                {
                    if (entries == null)
                    {
                        entries = new List <CompositeEntry> ();
                    }
                    entries.Add(entry);
                    url.Append(entry.GetHashCode().ToString("x"));
                    entry = null;
                }
            }

            if (entries == null || entries.Count == 0)
            {
                return(String.Empty);
            }

            string ret = ScriptResourceHandler.GetResourceUrl(ThisAssembly, url.ToString(), NotifyScriptLoaded);

            entriesCache.InsertOrUpdate((uint)ret.GetHashCode(), ret, entries, entries);
            return(ret);
        }