01: package net.javacoding.jspider.core.rule.impl;
02:
03: import net.javacoding.jspider.api.model.Decision;
04: import net.javacoding.jspider.api.model.Site;
05: import net.javacoding.jspider.core.SpiderContext;
06: import net.javacoding.jspider.core.model.DecisionInternal;
07: import net.javacoding.jspider.spi.Rule;
08: import net.javacoding.jspider.core.rule.Ruleset;
09:
10: import java.net.URL;
11: import java.util.List;
12:
13: /**
14: *
15: * $Id: RuleSetImpl.java,v 1.9 2003/04/03 16:24:59 vanrogu Exp $
16: *
17: * @author Günther Van Roey
18: */
19: public class RuleSetImpl implements Ruleset {
20:
21: protected int type;
22: protected Ruleset generalRules;
23: protected List localRules;
24:
25: public RuleSetImpl(int type, List rules) {
26: this (type, null, rules);
27: }
28:
29: public RuleSetImpl(int type, Ruleset generalRules, List rules) {
30: this .type = type;
31: this .generalRules = generalRules;
32: this .localRules = rules;
33: }
34:
35: public Decision applyRules(SpiderContext context, Site site, URL url) {
36: Decision decision = null;
37:
38: if (generalRules != null) {
39: decision = generalRules.applyRules(context, site, url);
40: } else {
41: decision = new DecisionInternal();
42: }
43:
44: if (decision.isVetoable()) {
45:
46: Rule[] rules = (Rule[]) localRules
47: .toArray(new Rule[localRules.size()]);
48: for (int i = 0; i < rules.length; i++) {
49: Rule rule = rules[i];
50: Decision lastDecision = rule.apply(context, site, url);
51: decision.addStep(rule.getName(), type, lastDecision
52: .getDecision(), lastDecision.getComment());
53: decision.merge(lastDecision);
54:
55: if (!lastDecision.isVetoable()) {
56: break;
57: }
58: }
59: }
60:
61: decision.addStep("Ruleset", type, decision.getDecision(),
62: "ruleset final decision");
63:
64: return decision;
65: }
66:
67: public void addRule(Rule rule) {
68: localRules.add(rule);
69: }
70:
71: public int getType() {
72: return type;
73: }
74:
75: public String toString() {
76: StringBuffer sb = new StringBuffer();
77: sb.append("[");
78: sb.append(translate(type));
79: sb.append(" ruleset]");
80: return sb.toString();
81: }
82:
83: public static String translate(int type) {
84: switch (type) {
85: case Ruleset.RULESET_GENERAL:
86: return "GENERAL";
87: case Ruleset.RULESET_SITE:
88: return "SITE";
89: }
90: return "ERROR!";
91: }
92:
93: }
|