Exemplo n.º 1
0
        public static string CacheTextFile(string sprocketPath, bool condenseJavaScript)
        {
            string   path     = HttpContext.Current.Request.PhysicalApplicationPath + '\\' + sprocketPath.Replace('/', '\\').Trim('\\');
            DateTime modified = new FileInfo(path).LastWriteTime;

            HttpContext.Current.Application.Lock();
            if (txtFileCache.ContainsKey(path))
            {
                if (txtFileCache[path].FileDate == modified)
                {
                    HttpContext.Current.Application.UnLock();
                    return(txtFileCache[path].FileText);
                }
                txtFileCache.Remove(path);
            }
            StreamReader sr  = new StreamReader(path, Encoding.Default);
            string       txt = sr.ReadToEnd();

            sr.Close();
            CachedFile file = new CachedFile();

            file.FileDate = modified;
            file.FileText = condenseJavaScript ? JavaScriptCondenser.Condense(txt) : txt;
            txtFileCache.Add(path, file);
            HttpContext.Current.Application.UnLock();
            return(txt);
        }
Exemplo n.º 2
0
        void WebEvents_OnBeforeLoadExistingFile(HandleFlag handled)
        {
            if (!SprocketPath.Value.EndsWith(".js"))
            {
                return;
            }
            FileInfo file        = new FileInfo(SprocketPath.Physical);
            TimeSpan maxCacheAge = new TimeSpan(24, 0, 0);

            HttpContext.Current.Response.Cache.SetLastModified(file.LastWriteTime);
            HttpContext.Current.Response.Cache.SetMaxAge(maxCacheAge);
            if (!CompressJavaScript)
            {
                return;
            }
            bool   rewrite  = false;
            string cachedJS = ContentCache.RetrieveText(SprocketPath.Value);

            if (cachedJS == null)
            {
                rewrite = true;
            }
            else if (!compressedJSFiles.ContainsKey(file.FullName))
            {
                rewrite = true;
            }
            else if (compressedJSFiles[file.FullName] != file.LastWriteTime)
            {
                rewrite = true;
            }
            HttpContext.Current.Response.ContentType = "text/javascript";
            if (rewrite)
            {
                try
                {
                    using (StreamReader reader = file.OpenText())
                    {
                        string s = JavaScriptCondenser.Condense(reader.ReadToEnd());
                        HttpContext.Current.Response.Write(s);
                        ContentCache.StoreText(SprocketPath.Value, maxCacheAge, true, s);
                        reader.Close();
                        compressedJSFiles[file.FullName] = file.LastWriteTime;
                    }
                }
                catch
                {
                    return;                     // if an error occurs, let the system serve up the file normally
                }
            }
            else
            {
                HttpContext.Current.Response.Write(cachedJS);
            }
            handled.Set();
        }
Exemplo n.º 3
0
        public string GetStandardScripts(params string[] restrictToAjaxModules)
        {
            string str = ResourceLoader.LoadTextResource(typeof(WebClientScripts).Assembly, "Sprocket.Web.javascript.generic.js")
                         + ResourceLoader.LoadTextResource(typeof(WebClientScripts).Assembly, "Sprocket.Web.javascript.json.js")
                         + ResourceLoader.LoadTextResource(typeof(WebClientScripts).Assembly, "Sprocket.Web.javascript.ajax.js")
                         + ResourceLoader.LoadTextResource(typeof(WebClientScripts).Assembly, "Sprocket.Web.javascript.browser-tools.js")
                         .Replace("$APPLICATIONROOT$", WebUtility.BasePath)
                         .Replace("$LOADTIMESTAMP$", AjaxRequestHandler.Instance.PageTimeStamp.Ticks.ToString())
                         + GetAjaxMethodsScript(restrictToAjaxModules);

            return(CompressJavaScript ? JavaScriptCondenser.Condense(str) : str);
        }
Exemplo n.º 4
0
        public static string CacheTextFile(string sprocketPath, bool condenseJavaScript)
        {
            string   path     = HttpContext.Current.Request.PhysicalApplicationPath + '\\' + sprocketPath.Replace('/', '\\').Trim('\\');
            DateTime modified = new FileInfo(path).LastWriteTime;

            HttpContext.Current.Application.Lock();
            if (txtFileCache.ContainsKey(path))
            {
                if (txtFileCache[path].FileDate == modified)
                {
                    HttpContext.Current.Application.UnLock();
                    return(txtFileCache[path].FileText);
                }
                txtFileCache.Remove(path);
            }
            StreamReader sr  = new StreamReader(path, Encoding.Default);
            string       txt = sr.ReadToEnd();

            sr.Close();
            CachedFile file = new CachedFile();

            file.FileDate = modified;
            if (condenseJavaScript)
            {
                if (sprocketPath.EndsWith(".js"))
                {
                    file.FileText = JavaScriptCondenser.Condense(txt);
                }
                else if (sprocketPath.EndsWith(".htm"))
                {
                    foreach (Match match in Regex.Matches(txt, @"\<script\>(?<js>([\s\S](?!\/script\>))*)\</script\>",
                                                          RegexOptions.IgnoreCase | RegexOptions.Multiline))
                    {
                        txt = txt.Replace(match.Value, "<script>" + JavaScriptCondenser.Condense(match.Groups["js"].Value) + "</script>");
                    }
                    file.FileText = txt;
                }
                else
                {
                    file.FileText = txt;
                }
            }
            else
            {
                file.FileText = txt;
            }
            //file.FileText = condenseJavaScript ? JavaScriptCondenser.Condense(txt) : txt;
            txtFileCache.Add(path, file);
            HttpContext.Current.Application.UnLock();
            return(txt);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Writes all registered scripts into a single string surrounded by html script tags
        /// </summary>
        /// <returns>HTML script tags with containing javascript</returns>
        public string CreateScriptTags()
        {
            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair <string, string> script in scripts)
            {
                string js = script.Value;
                foreach (KeyValuePair <string, object> key in keys)
                {
                    js = js.Replace(key.Key, key.Value.ToString());
                }
                sb.Append("<script language=\"JavaScript\">");
                sb.Append(Environment.NewLine);
                if (SprocketSettings.GetBooleanValue("CompressJavaScript"))
                {
                    js = JavaScriptCondenser.Condense(js);
                }
                sb.Append(js);
                sb.Append(Environment.NewLine);
                sb.Append("</script>");
                sb.Append(Environment.NewLine);
            }
            return(sb.ToString());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Only used for ASP.Net web forms with server form tags. Registers all contained
        /// javascripts in script tags within the server form.
        /// </summary>
        public void RegisterScripts()
        {
            Page page = (Page)HttpContext.Current.Handler;

            foreach (KeyValuePair <string, string> script in scripts)
            {
                string js = script.Value;
                foreach (KeyValuePair <string, object> key in keys)
                {
                    js = js.Replace(key.Key, key.Value.ToString());
                }
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), script.Key, JavaScriptCondenser.Condense(js));
            }
        }