protected static string QuoteString(string value) { return(ToolkitScriptManagerHelper.QuoteString(value)); }
/// <summary> /// Writes scripts (including localized script resources) to the specified stream /// </summary> /// <param name="scriptEntries">list of scripts to write</param> /// <param name="outputWriter">writer for output stream</param> private void WriteScripts(List <ScriptEntry> scriptEntries, TextWriter outputWriter, bool enableCdn) { foreach (ScriptEntry scriptEntry in scriptEntries) { if (!scriptEntry.Loaded) { var combineStatus = IsScriptCombinable(scriptEntry, enableCdn); if (combineStatus == ScriptCombineStatus.NotCorrespondingWebResourceAttribute) { throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Combined script request includes uncombinable script \"{0}\".", scriptEntry.Name)); } if (combineStatus == ScriptCombineStatus.Combineable) { // This script hasn't been loaded by the browser, so add it to the combined script file string script = scriptEntry.GetScript(); if (WebResourceRegex.IsMatch(script)) { // This script uses script substitution which isn't supported yet, so throw an exception since it's too late to fix throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "ToolkitScriptManager does not support <%= WebResource/ScriptResource(...) %> substitution as used by script file \"{0}\".", scriptEntry.Name)); } outputWriter.WriteLine(script); // Save current culture and set the specified culture CultureInfo currentUiCulture = Thread.CurrentThread.CurrentUICulture; try { try { Thread.CurrentThread.CurrentUICulture = new CultureInfo(scriptEntry.Culture); } catch (ArgumentException) { // Invalid culture; proceed with default culture (just as for unsupported cultures) } // Write out the associated script resources (if any) in the proper culture Assembly scriptAssembly = scriptEntry.LoadAssembly(); foreach ( ScriptResourceAttribute scriptResourceAttribute in GetScriptResourceAttributes(scriptAssembly)) { if (scriptResourceAttribute.ScriptName == scriptEntry.Name) { #pragma warning disable 0618 // obsolete members of ScriptResourceAttribute are used but necessary in the 3.5 build // Found a matching script resource; write it out outputWriter.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}={{", scriptResourceAttribute.TypeName)); // Get the script resource name (without the trailing ".resources") string scriptResourceName = scriptResourceAttribute.ScriptResourceName; if (scriptResourceName.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)) { scriptResourceName = scriptResourceName.Substring(0, scriptResourceName.Length - 10); } #pragma warning restore 0618 // Load a ResourceManager/ResourceSet and walk through the list to output them all System.Resources.ResourceManager resourceManager = new System.Resources.ResourceManager(scriptResourceName, scriptAssembly); using ( System.Resources.ResourceSet resourceSet = resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true)) { bool first = true; foreach (System.Collections.DictionaryEntry de in resourceSet) { if (!first) { // Need a comma between all entries outputWriter.Write(","); } // Output the entry string name = (string)de.Key; string value = resourceManager.GetString(name); outputWriter.Write(string.Format(CultureInfo.InvariantCulture, "\"{0}\":\"{1}\"", ToolkitScriptManagerHelper.QuoteString(name), ToolkitScriptManagerHelper.QuoteString(value))); first = false; } } outputWriter.WriteLine("};"); } } } finally { // Restore culture Thread.CurrentThread.CurrentUICulture = currentUiCulture; } // Done with this script // This script is now (or will be soon) loaded by the browser scriptEntry.Loaded = true; } } } }