示例#1
0
        /// <summary>
        /// 获取用于分析 HTML 文档的分析器
        /// </summary>
        /// <param name="contentResult">文档内容加载结果</param>
        /// <returns>HTML 分析器相关信息</returns>
        public static IHtmlParser GetParser(HtmlContentResult contentResult)
        {
            if (contentResult == null)
            {
                throw new ArgumentNullException("contentResult");
            }



            lock ( _parserProvidersSync )
            {
                foreach (var provider in ParserProviders)
                {
                    var parser = provider.GetParser(contentResult.VirtualPath, contentResult.Content);

                    if (parser != null)
                    {
                        return(parser);
                    }
                }
            }


            //默认行为
            return(DefaultParserProvider.GetParser(contentResult.VirtualPath, contentResult.Content));
        }
示例#2
0
        /// <summary>
        /// 分析 HTML 文档,此方法会根据情况缓存文档模型
        /// </summary>
        /// <param name="contentResult">文档加载结果</param>
        /// <returns>HTML 文档对象</returns>
        public static IHtmlDocument ParseDocument(HtmlContentResult contentResult)
        {
            if (contentResult == null)
            {
                throw new ArgumentNullException("contentResult");
            }

            var parser = GetParser(contentResult);

            return(ParseDocument(contentResult, parser));
        }
示例#3
0
        /// <summary>
        /// 分析 HTML 文档,此方法会根据情况缓存文档模型
        /// </summary>
        /// <param name="context">当前请求的 HttpContext 对象</param>
        /// <param name="contentResult">文档加载结果</param>
        /// <returns>HTML 文档对象</returns>
        public static IHtmlDocument ParseDocument(HttpContextBase context, HtmlContentResult contentResult)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (contentResult == null)
            {
                throw new ArgumentNullException("contentResult");
            }



            var result = GetParser(context, contentResult.ContentUri, contentResult.Content);


            return(ParseDocument(context, contentResult, result));
        }
示例#4
0
        /// <summary>
        /// 获取用于分析 HTML 文档的分析器
        /// </summary>
        /// <param name="contentResult">文档内容加载结果</param>
        /// <returns>HTML 分析器相关信息</returns>
        public static IHtmlParser GetParser(HtmlContentResult contentResult)
        {
            if (contentResult == null)
            {
                throw new ArgumentNullException("contentResult");
            }



            foreach (var provider in WebServiceLocator.GetServices <IHtmlParserProvider>(contentResult.VirtualPath))
            {
                var parser = provider.GetParser(contentResult.VirtualPath, contentResult.Content);
                if (parser != null)
                {
                    return(parser);
                }
            }

            return(Default.GetParser(contentResult.VirtualPath));
        }
示例#5
0
        /// <summary>
        /// 分析 HTML 文档,此方法会根据情况缓存文档模型
        /// </summary>
        /// <param name="contentResult">文档加载结果</param>
        /// <param name="parser">HTML 解析器</param>
        /// <returns>HTML 文档对象</returns>
        public static IHtmlDocument ParseDocument(HtmlContentResult contentResult, IHtmlParser parser)
        {
            var domProvider = parser.DomProvider;

            if (contentResult.CacheKey != null && domProvider != null)//如果可以缓存
            {
                var key      = contentResult.CacheKey;
                var cacheKey = string.Format(CultureInfo.InvariantCulture, DocumentCacheKey, contentResult.VirtualPath);

                var createDocument = Cache.Get(cacheKey) as Func <IHtmlDomProvider, IHtmlDocument>;

                if (createDocument != null)
                {
                    return(createDocument(domProvider));
                }

                Trace("Document cache missed");


                var document = ParseDocument(parser, contentResult.Content, contentResult.VirtualPath);
                createDocument = document.Compile();//必须同步编译文档,否则文档对象可能被修改。

                new Action(delegate
                {
                    createDocument(domProvider);//可以异步预热,预热后再存入缓存。
                    Cache.Insert(cacheKey, createDocument, new CacheDependency(new string[0], new[] { key }), CacheItemPriority.High);
                }
                           ).BeginInvoke(null, null);//立即在新线程预热此方法



                return(document);
            }

            else
            {
                return(ParseDocument(parser, contentResult.Content, contentResult.VirtualPath));
            }
        }
示例#6
0
        /// <summary>
        /// 分析 HTML 文档,此方法会根据情况缓存文档模型
        /// </summary>
        /// <param name="context">当前请求的 HttpContext 对象</param>
        /// <param name="contentResult">文档加载结果</param>
        /// <param name="parserResult">解析器选择结果</param>
        /// <returns>HTML 文档对象</returns>
        public static IHtmlDocument ParseDocument(HttpContextBase context, HtmlContentResult contentResult, HtmlParserResult parserResult)
        {
            if (contentResult.CacheKey != null && parserResult.DomProvider != null)//如果可以缓存
            {
                var key      = contentResult.CacheKey;
                var cacheKey = string.Format(CultureInfo.InvariantCulture, DocumentCacheKey, contentResult.ContentUri.AbsoluteUri);

                var createDocument = Cache.Get(cacheKey) as Func <IHtmlDomProvider, IHtmlDocument>;

                if (createDocument != null)
                {
                    var provider = parserResult.DomProvider;
                    return(createDocument(provider));
                }

                context.Trace.Write("Jumony for ASP.NET", "Document cache missed");


                var document = ParseDocument(parserResult, contentResult.Content, contentResult.ContentUri);
                createDocument = document.Compile();//必须同步编译文档,否则文档对象可能被修改。

                new Action(delegate
                {
                    createDocument(parserResult.DomProvider);//可以异步预热,预热后再存入缓存。
                    Cache.Insert(cacheKey, createDocument, new CacheDependency(new string[0], new[] { key }), CacheItemPriority.High);
                }
                           ).BeginInvoke(null, null);//立即在新线程预热此方法



                return(document);
            }

            else
            {
                return(ParseDocument(parserResult, contentResult.Content, contentResult.ContentUri));
            }
        }
示例#7
0
 /// <summary>
 /// 分析文档内容
 /// </summary>
 /// <param name="parser">分析器</param>
 /// <param name="content">文档内容加载结果</param>
 /// <returns>分析结果</returns>
 public static IHtmlDocument Parse(this IHtmlParser parser, HtmlContentResult content)
 {
     return(parser.Parse(content.Content, CreateDocumentUri(content.VirtualPath)));
 }
示例#8
0
    /// <summary>
    /// 分析 HTML 文档,此方法会根据情况缓存文档模型
    /// </summary>
    /// <param name="contentResult">文档加载结果</param>
    /// <param name="parser">HTML 解析器</param>
    /// <returns>HTML 文档对象</returns>
    public static IHtmlDocument ParseDocument( HtmlContentResult contentResult, IHtmlParser parser )
    {

      var domProvider = parser.DomProvider;

      if ( contentResult.CacheKey != null && domProvider != null )//如果可以缓存
      {
        var key = contentResult.CacheKey;
        var cacheKey = string.Format( CultureInfo.InvariantCulture, DocumentCacheKey, contentResult.VirtualPath );

        var createDocument = Cache.Get( cacheKey ) as Func<IHtmlDomProvider, IHtmlDocument>;

        if ( createDocument != null )
        {
          return createDocument( domProvider );
        }

        WebServiceLocator.GetTraceService().Trace( TraceLevel.Info, "Jumony Web", "Document cache missed" );


        var document = ParseDocument( parser, contentResult.Content, contentResult.VirtualPath );
        createDocument = document.Compile();//必须同步编译文档,否则文档对象可能被修改。

        new Action( delegate
        {
          createDocument( domProvider );//可以异步预热,预热后再存入缓存。
          Cache.Insert( cacheKey, createDocument, new CacheDependency( new string[0], new[] { key } ), CacheItemPriority.High );
        }
          ).BeginInvoke( null, null );//立即在新线程预热此方法



        return document;
      }

      else

        return ParseDocument( parser, contentResult.Content, contentResult.VirtualPath );
    }
示例#9
0
    /// <summary>
    /// 分析 HTML 文档,此方法会根据情况缓存文档模型
    /// </summary>
    /// <param name="contentResult">文档加载结果</param>
    /// <returns>HTML 文档对象</returns>
    public static IHtmlDocument ParseDocument( HtmlContentResult contentResult )
    {

      if ( contentResult == null )
        throw new ArgumentNullException( "contentResult" );

      var parser = GetParser( contentResult );
      return ParseDocument( contentResult, parser );

    }
示例#10
0
    /// <summary>
    /// 获取用于分析 HTML 文档的分析器
    /// </summary>
    /// <param name="contentResult">文档内容加载结果</param>
    /// <returns>HTML 分析器相关信息</returns>
    public static IHtmlParser GetParser( HtmlContentResult contentResult )
    {

      if ( contentResult == null )
        throw new ArgumentNullException( "contentResult" );



      foreach ( var provider in WebServiceLocator.GetServices<IHtmlParserProvider>( contentResult.VirtualPath ) )
      {
        var parser = provider.GetParser( contentResult.VirtualPath, contentResult.Content );
        if ( parser != null )
          return parser;
      }

      return Default.GetParser( contentResult.VirtualPath );
    }
示例#11
0
 /// <summary>
 /// 分析文档内容
 /// </summary>
 /// <param name="parser">分析器</param>
 /// <param name="content">文档内容加载结果</param>
 /// <returns>分析结果</returns>
 public static IHtmlDocument Parse( this IHtmlParser parser, HtmlContentResult content )
 {
     return parser.Parse( content.Content, CreateDocumentUri( content.VirtualPath ) );
 }
示例#12
0
        /// <summary>
        /// 获取用于分析 HTML 文档的分析器
        /// </summary>
        /// <param name="contentResult">文档内容加载结果</param>
        /// <returns>HTML 分析器相关信息</returns>
        public static IHtmlParser GetParser( HtmlContentResult contentResult )
        {
            if ( contentResult == null )
            throw new ArgumentNullException( "contentResult" );

              lock ( _parserProvidersSync )
              {
            foreach ( var provider in ParserProviders )
            {
              var parser = provider.GetParser( contentResult.VirtualPath, contentResult.Content );

              if ( parser != null )
            return parser;

            }
              }

              //默认行为
              return DefaultParserProvider.GetParser( contentResult.VirtualPath, contentResult.Content );
        }
示例#13
0
 /// <summary>
 /// 分析文档内容
 /// </summary>
 /// <param name="parser">分析器</param>
 /// <param name="content">文档内容加载结果</param>
 /// <returns></returns>
 public static IHtmlDocument Parse(this IHtmlParser parser, HtmlContentResult content)
 {
     return(parser.Parse(content.Content, content.ContentUri));
 }