Ejemplo n.º 1
0
        public string CrawlCss(string cssText)
        {
            try
            {
                if (String.IsNullOrEmpty(cssText))
                {
                    throw new Exception("cssText cannot be null");
                }
                var urls = CssParser.GetAllUris(cssText);
                foreach (var url in urls)
                {
                    try
                    {
                        Uri absoluteUri = null;
                        if (!String.IsNullOrEmpty(url.Key))
                        {
                            Uri.TryCreate(RootUrl, url.Key, out absoluteUri);
                        }
                        absoluteUri = Utils.GenerateUriToProcess(absoluteUri, Resources.IgnoreFileNameChangeRegex);
                        string placeHolder = CheckUrlPresentInAssetOrNot(absoluteUri, Resources);
                        if (placeHolder != null && placeHolder.Equals("IGNORE", StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }
                        if (placeHolder == null)
                        {
                            placeHolder = String.Format("[Kitsune_{0}]", absoluteUri.AbsoluteUri);

                            AssetDetails cssFileLink = new AssetDetails
                            {
                                LinkUrl     = absoluteUri.AbsoluteUri,
                                PlaceHolder = placeHolder
                            };

                            //TODO: what if not added
                            Resources.UniqueAssetsDictionary.TryAdd(absoluteUri.AbsoluteUri, cssFileLink);
                        }
                        cssText = cssText.Replace(url.Key, placeHolder);
                    }
                    catch (Exception ex)
                    {
                        ErrorLogMethod(LOGTYPE.ERROR, String.Format("Error while crawling css of Url : {0}", url), ex);
                    }
                }

                urls = CssParser.GetAllImportUris(cssText);
                foreach (var url in urls)
                {
                    try
                    {
                        Uri absoluteUri = null;
                        if (!String.IsNullOrEmpty(url.Key))
                        {
                            Uri.TryCreate(RootUrl, url.Key, out absoluteUri);
                        }
                        absoluteUri = Utils.GenerateUriToProcess(absoluteUri, Resources.IgnoreFileNameChangeRegex);
                        string placeHolder = CheckUrlPresentInAssetOrNot(absoluteUri, Resources);
                        if (placeHolder != null && placeHolder.Equals("IGNORE", StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }
                        if (placeHolder == null)
                        {
                            placeHolder = String.Format("[Kitsune_{0}]", absoluteUri.AbsoluteUri);

                            AssetDetails cssFileLink = new AssetDetails
                            {
                                LinkUrl     = absoluteUri.AbsoluteUri,
                                PlaceHolder = placeHolder
                            };

                            //TODO : what if tryAdd fails
                            Resources.UniqueStylesDictionary.TryAdd(absoluteUri.AbsoluteUri, cssFileLink);
                        }
                        cssText = cssText.Replace(url.Value, "@import \"" + placeHolder + "\"");
                    }
                    catch (Exception ex)
                    {
                        ErrorLogMethod(LOGTYPE.ERROR, String.Format("Error while processing the Url:{0}", url), ex);
                    }
                }
                return(cssText);
            }
            catch (Exception ex)
            {
                ErrorLogMethod(LOGTYPE.ERROR, String.Format("Error while Crawling css for csstext:{0}", cssText), ex);
                return(cssText);
            }
        }
Ejemplo n.º 2
0
        public string CrawlCss(string cssText, Uri RootUrl)
        {
            try
            {
                if (String.IsNullOrEmpty(cssText))
                {
                    throw new Exception("cssText cannot be null");
                }
                var urls = CssParser.GetAllUris(cssText);
                foreach (var url in urls)
                {
                    try
                    {
                        Uri absoluteUri = null;
                        if (!String.IsNullOrEmpty(url.Key))
                        {
                            if (!Uri.TryCreate(RootUrl, url.Key, out absoluteUri))
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                        string placeHolder = CheckUrlPresentInAssetOrNot(absoluteUri.AbsoluteUri);

                        //  If asset not present create placeholder and add it to asset
                        if (placeHolder == null)
                        {
                            placeHolder = AddNewAsset(absoluteUri.AbsoluteUri, FileType.ASSET);
                        }
                        cssText = cssText.Replace(url.Key, placeHolder);
                    }
                    catch (Exception ex)
                    {
                        Context.ErrorLogMethod(LOGTYPE.ERROR, String.Format("Error while crawling css of Url : {0}", url), ex);
                    }
                }

                urls = CssParser.GetAllImportUris(cssText);
                foreach (var url in urls)
                {
                    try
                    {
                        Uri absoluteUri = null;
                        if (!String.IsNullOrEmpty(url.Key))
                        {
                            if (!Uri.TryCreate(RootUrl, url.Key, out absoluteUri))
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }

                        string placeHolder = CheckUrlPresentInAssetOrNot(absoluteUri.AbsoluteUri);

                        //  If asset not present create placeholder and add it to asset
                        if (placeHolder == null)
                        {
                            placeHolder = AddNewAsset(absoluteUri.AbsoluteUri, FileType.STYLE);
                        }
                        cssText = cssText.Replace(url.Value, "@import \"" + placeHolder + "\"");
                    }
                    catch (Exception ex)
                    {
                        Context.ErrorLogMethod(LOGTYPE.ERROR, String.Format("Error while processing the Url:{0}", url), ex);
                    }
                }
                return(cssText);
            }
            catch (Exception ex)
            {
                Context.ErrorLogMethod(LOGTYPE.INFORMATION, String.Format("Error while Crawling css for csstext:{0}", cssText), ex);
                return(cssText);
            }
        }