RobotsTXTRule.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:3k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.rule.impl;
  2. import net.javacoding.jspider.api.model.Decision;
  3. import net.javacoding.jspider.api.model.Site;
  4. import net.javacoding.jspider.core.SpiderContext;
  5. import net.javacoding.jspider.core.model.DecisionInternal;
  6. import net.javacoding.jspider.core.model.SiteInternal;
  7. import net.javacoding.jspider.core.util.URLUtil;
  8. import net.javacoding.jspider.core.util.html.RobotsTXTLine;
  9. import net.javacoding.jspider.core.util.html.RobotsTXTLineSet;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.net.URL;
  13. /**
  14.  * Rule implementation that applies the rules expressed by a site's robots.txt
  15.  * file to the resources we want to fetch on that site.
  16.  * This file allows webmasters to exclude certain resources and folders not to
  17.  * be spidered by web robots, to disallow inclusion in search engines, etc ...
  18.  *
  19.  * $Id: RobotsTXTRule.java,v 1.13 2003/03/28 17:26:28 vanrogu Exp $
  20.  *
  21.  * @author G黱ther Van Roey
  22.  */
  23. public class RobotsTXTRule extends BaseRuleImpl {
  24.     /** user agent under which we're operating. */
  25.     protected String effectiveUserAgent;
  26.     /** user agent in the robots.txt file we're obeying */
  27.     protected String obeyedUserAgent;
  28.     /** all lines in the robots.txt file that apply to us and forbid access to a part of the site. */
  29.     protected RobotsTXTLine[] forbiddenPaths;
  30.     /**
  31.      * Public constructor.
  32.      * @param userAgent the userAgent under which we're operating
  33.      * @param is the inputstream to read the robots.txt file from
  34.      * @throws IOException in case something goes wrong reading the robots.txt
  35.      */
  36.     public RobotsTXTRule(String userAgent, InputStream is) throws IOException {
  37.         RobotsTXTLineSet lineSet = RobotsTXTLineSet.findLineSet(is, userAgent);
  38.         this.effectiveUserAgent = userAgent;
  39.         if (lineSet == null) {
  40.             this.obeyedUserAgent = null;
  41.             forbiddenPaths = new RobotsTXTLine[0];
  42.         } else {
  43.             this.obeyedUserAgent = lineSet.getUserAgent();
  44.             forbiddenPaths = lineSet.getLines();
  45.         }
  46.     }
  47.     /**
  48.      * Returns the user agent from robots.txt we're obeying. (can be '*').
  49.      * This user agent identification is the first match we encountered in the file,
  50.      * a match is given if our effective user agent contains the user agent
  51.      * identification as a substring in a case-insensitive way.
  52.      * @return the useragent selector we're obeying.
  53.      */
  54.     public String getObeyedUserAgent() {
  55.         return obeyedUserAgent;
  56.     }
  57.     /**
  58.      * Applies the rule to a given URL
  59.      * @param context the spider context we're working in
  60.      * @param currentSite the site we're spidering
  61.      * @param url the url of the resource to be tested for spider permission
  62.      * @return Decision object expressing this rule's decision on the resource
  63.      */
  64.     public Decision apply(SpiderContext context, Site currentSite, URL url) {
  65.         String path = url.getPath();
  66.         Decision decision = new DecisionInternal();
  67.         if ((context.getStorage().getSiteDAO().find(URLUtil.getSiteURL(url))).getObeyRobotsTXT()) {
  68.             for (int i = 0; i < forbiddenPaths.length; i++) {
  69.                 RobotsTXTLine forbiddenPath = forbiddenPaths[i];
  70.                 if (forbiddenPath.matches(url)) {
  71.                     decision = new DecisionInternal(Decision.RULE_FORBIDDEN, "access to '" + path + "' forbidden");
  72.                     break;
  73.                 }
  74.             }
  75.         }
  76.         return decision;
  77.     }
  78. }