示例#1
0
        public string GetScript(ScriptReference scriptReference)
        {
            if (_storageCookie != null)
            {
                object value = _storageCookie[scriptReference.Name];
                if ((value != null) && (value is string) &&
                    (String.CompareOrdinal(scriptReference.Version, (string)value) == 0))
                {
                    return(null);
                }
            }

            string cacheKey      = "ScriptContent:" + scriptReference.Name;
            string scriptContent = _httpContext.Cache[cacheKey] as string;

            if (scriptContent == null)
            {
                string filePath = _httpContext.Server.MapPath(scriptReference.Path);

                using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    scriptContent = (new StreamReader(stream)).ReadToEnd();
                }

                if (String.IsNullOrEmpty(scriptContent))
                {
                    throw new InvalidOperationException("Unable to read script of script named '" + scriptReference.Name + "'.");
                }

                _httpContext.Cache.Insert(cacheKey, scriptContent, new CacheDependency(filePath));
            }

            return(scriptContent);
        }
示例#2
0
        public string GetScript(ScriptReference scriptReference)
        {
            if (_storageCookie != null) {
                object value = _storageCookie[scriptReference.Name];
                if ((value != null) && (value is string) &&
                    (String.CompareOrdinal(scriptReference.Version, (string)value) == 0)) {
                    return null;
                }
            }

            string cacheKey = "ScriptContent:" + scriptReference.Name;
            string scriptContent = _httpContext.Cache[cacheKey] as string;

            if (scriptContent == null) {
                string filePath = _httpContext.Server.MapPath(scriptReference.Path);

                using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    scriptContent = (new StreamReader(stream)).ReadToEnd();
                }

                if (String.IsNullOrEmpty(scriptContent)) {
                    throw new InvalidOperationException("Unable to read script of script named '" + scriptReference.Name + "'.");
                }

                _httpContext.Cache.Insert(cacheKey, scriptContent, new CacheDependency(filePath));
            }

            return scriptContent;
        }
示例#3
0
        public void AddScriptReference(ScriptReference scriptReference)
        {
            Debug.Assert(scriptReference != null);

            ScriptReference existingReference;
            if (_referenceMap.TryGetValue(scriptReference.Name, out existingReference)) {
                if ((int)existingReference.Mode > (int)scriptReference.Mode) {
                    existingReference.Mode = scriptReference.Mode;
                }
            }
            else {
                _references.Add(scriptReference);
                _referenceMap[scriptReference.Name] = scriptReference;
            }
        }
示例#4
0
        public void AddScriptReference(ScriptReference scriptReference)
        {
            Debug.Assert(scriptReference != null);

            ScriptReference existingReference;

            if (_referenceMap.TryGetValue(scriptReference.Name, out existingReference))
            {
                if ((int)existingReference.Mode > (int)scriptReference.Mode)
                {
                    existingReference.Mode = scriptReference.Mode;
                }
            }
            else
            {
                _references.Add(scriptReference);
                _referenceMap[scriptReference.Name] = scriptReference;
            }
        }
示例#5
0
        public static void AddScriptReference(this AjaxHelper ajaxHelper, string scriptName, ScriptMode scriptMode)
        {
            ScriptModel scriptModel = GetScriptModel(ajaxHelper);

            if (String.IsNullOrEmpty(scriptName)) {
                throw new ArgumentNullException("scriptName");
            }

            SharpenSection configSection = SharpenSection.GetSettings();
            ScriptElement scriptElement = configSection.Scripts.GetElement(scriptName, scriptModel.ScriptFlavor);
            string actualFlavor = String.Empty;

            int flavorIndex = scriptElement.Name.IndexOf('.');
            if (flavorIndex > 0) {
                actualFlavor = scriptElement.Name.Substring(flavorIndex + 1);
            }

            ScriptReference scriptReference =
                new ScriptReference(scriptName, scriptElement.Url, scriptMode,
                                    scriptElement.GetDependencyList(), scriptElement.Version + actualFlavor);
            scriptModel.AddScriptReference(scriptReference);
        }
示例#6
0
        public void IncludeDependencies(ScriptCollection configuredScripts)
        {
            HashSet<string> referenceSet = new HashSet<string>();
            Queue<string> dependencies = new Queue<string>();

            foreach (ScriptReference reference in _references) {
                referenceSet.Add(reference.Name);
                if (reference.Dependencies != null) {
                    foreach (string dependency in reference.Dependencies) {
                        dependencies.Enqueue(dependency);
                    }
                }
            }

            if (_scriptBlocks != null) {
                foreach (ScriptBlock scriptBlock in _scriptBlocks) {
                    if (scriptBlock.Dependencies != null) {
                        foreach (string dependency in scriptBlock.Dependencies) {
                            dependencies.Enqueue(dependency);
                        }
                    }
                }
            }

            while (dependencies.Count != 0) {
                string name = dependencies.Dequeue();
                if (referenceSet.Contains(name)) {
                    continue;
                }

                ScriptElement scriptElement = configuredScripts.GetElement(name, _scriptFlavor);
                string[] implicitDependencies = scriptElement.GetDependencyList();

                ScriptReference scriptReference =
                    new ScriptReference(name, scriptElement.Url, ScriptMode.OnDemand, implicitDependencies,
                                        scriptElement.Version);
                AddScriptReference(scriptReference);

                referenceSet.Add(name);

                if (implicitDependencies != null) {
                    foreach (string implicitDependency in implicitDependencies) {
                        if (referenceSet.Contains(implicitDependency) == false) {
                            dependencies.Enqueue(implicitDependency);
                        }
                    }
                }
            }
        }
示例#7
0
 public bool CanInlineScript(ScriptReference scriptReference)
 {
     return (String.IsNullOrEmpty(scriptReference.Version) == false) &&
            (Uri.IsWellFormedUriString(scriptReference.Path, UriKind.Absolute) == false);
 }
示例#8
0
 public bool CanInlineScript(ScriptReference scriptReference)
 {
     return((String.IsNullOrEmpty(scriptReference.Version) == false) &&
            (Uri.IsWellFormedUriString(scriptReference.Path, UriKind.Absolute) == false));
 }
示例#9
0
        public void IncludeDependencies(ScriptCollection configuredScripts)
        {
            HashSet <string> referenceSet = new HashSet <string>();
            Queue <string>   dependencies = new Queue <string>();

            foreach (ScriptReference reference in _references)
            {
                referenceSet.Add(reference.Name);
                if (reference.Dependencies != null)
                {
                    foreach (string dependency in reference.Dependencies)
                    {
                        dependencies.Enqueue(dependency);
                    }
                }
            }

            if (_scriptBlocks != null)
            {
                foreach (ScriptBlock scriptBlock in _scriptBlocks)
                {
                    if (scriptBlock.Dependencies != null)
                    {
                        foreach (string dependency in scriptBlock.Dependencies)
                        {
                            dependencies.Enqueue(dependency);
                        }
                    }
                }
            }

            while (dependencies.Count != 0)
            {
                string name = dependencies.Dequeue();
                if (referenceSet.Contains(name))
                {
                    continue;
                }

                ScriptElement scriptElement        = configuredScripts.GetElement(name, _scriptFlavor);
                string[]      implicitDependencies = scriptElement.GetDependencyList();

                ScriptReference scriptReference =
                    new ScriptReference(name, scriptElement.Url, ScriptMode.OnDemand, implicitDependencies,
                                        scriptElement.Version);
                AddScriptReference(scriptReference);

                referenceSet.Add(name);

                if (implicitDependencies != null)
                {
                    foreach (string implicitDependency in implicitDependencies)
                    {
                        if (referenceSet.Contains(implicitDependency) == false)
                        {
                            dependencies.Enqueue(implicitDependency);
                        }
                    }
                }
            }
        }