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

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.mod.rule;
  2. import net.javacoding.jspider.core.util.config.PropertySet;
  3. import net.javacoding.jspider.core.SpiderContext;
  4. import net.javacoding.jspider.core.rule.impl.BaseRuleImpl;
  5. import net.javacoding.jspider.core.model.DecisionInternal;
  6. import net.javacoding.jspider.api.model.Decision;
  7. import net.javacoding.jspider.api.model.Site;
  8. import java.net.URL;
  9. /**
  10.  * $Id: ForbiddenPathRule.java,v 1.1 2003/04/03 16:10:51 vanrogu Exp $
  11.  */
  12. public class ForbiddenPathRule extends BaseRuleImpl {
  13.     public static final String PATH = "path";
  14.     protected String forbiddenPath;
  15.     public ForbiddenPathRule(PropertySet config) {
  16.         forbiddenPath = config.getString(PATH, "/");
  17.     }
  18.     /**
  19.      * Applies the rule to a given URL
  20.      * @param context the spider context we're working in
  21.      * @param currentSite the site we're spidering
  22.      * @param url the url of the resource to be tested for spider permission
  23.      * @return Decision object expressing this rule's decision on the resource
  24.      */
  25.     public Decision apply(SpiderContext context, Site currentSite, URL url) {
  26.         String path = url.getPath();
  27.         Decision decision = new DecisionInternal();
  28.         if (matches(url, forbiddenPath)) {
  29.             decision = new DecisionInternal(Decision.RULE_FORBIDDEN, "access to '" + path + "' forbidden");
  30.         }
  31.         return decision;
  32.     }
  33.     public boolean matches(URL url, String forbiddenPath) {
  34.         String path = url.getPath();
  35.         if (path.length() == 0 && forbiddenPath.equals("/")) {
  36.             return true;
  37.         } else {
  38.             return url.getPath().startsWith(forbiddenPath);
  39.         }
  40.     }
  41. }