KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > mod > rule > ForbiddenPathRule


1 package net.javacoding.jspider.mod.rule;
2
3 import net.javacoding.jspider.core.util.config.PropertySet;
4 import net.javacoding.jspider.core.SpiderContext;
5 import net.javacoding.jspider.core.rule.impl.BaseRuleImpl;
6 import net.javacoding.jspider.core.model.DecisionInternal;
7 import net.javacoding.jspider.api.model.Decision;
8 import net.javacoding.jspider.api.model.Site;
9
10 import java.net.URL JavaDoc;
11
12 /**
13  * $Id: ForbiddenPathRule.java,v 1.1 2003/04/03 16:10:51 vanrogu Exp $
14  */

15 public class ForbiddenPathRule extends BaseRuleImpl {
16
17     public static final String JavaDoc PATH = "path";
18
19     protected String JavaDoc forbiddenPath;
20
21
22     public ForbiddenPathRule(PropertySet config) {
23         forbiddenPath = config.getString(PATH, "/");
24     }
25
26     /**
27      * Applies the rule to a given URL
28      * @param context the spider context we're working in
29      * @param currentSite the site we're spidering
30      * @param url the url of the resource to be tested for spider permission
31      * @return Decision object expressing this rule's decision on the resource
32      */

33     public Decision apply(SpiderContext context, Site currentSite, URL JavaDoc url) {
34         String JavaDoc path = url.getPath();
35         Decision decision = new DecisionInternal();
36
37         if (matches(url, forbiddenPath)) {
38             decision = new DecisionInternal(Decision.RULE_FORBIDDEN, "access to '" + path + "' forbidden");
39         }
40         return decision;
41     }
42
43     public boolean matches(URL JavaDoc url, String JavaDoc forbiddenPath) {
44         String JavaDoc path = url.getPath();
45         if (path.length() == 0 && forbiddenPath.equals("/")) {
46             return true;
47         } else {
48             return url.getPath().startsWith(forbiddenPath);
49         }
50     }
51
52 }
Popular Tags