KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > core > rule > RuleFactory


1 package net.javacoding.jspider.core.rule;
2
3
4 import net.javacoding.jspider.api.model.Site;
5 import net.javacoding.jspider.core.rule.impl.RuleSetImpl;
6 import net.javacoding.jspider.core.util.config.*;
7 import net.javacoding.jspider.core.logging.LogFactory;
8 import net.javacoding.jspider.core.logging.Log;
9 import net.javacoding.jspider.spi.Rule;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import java.lang.reflect.Constructor JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16
17 /**
18  *
19  * $Id: RuleFactory.java,v 1.12 2003/04/29 17:53:48 vanrogu Exp $
20  *
21  * @author Günther Van Roey
22  */

23 public class RuleFactory {
24
25     protected static Ruleset generalSpiderRules;
26     protected static Ruleset generalParserRules;
27
28     public static synchronized Ruleset createGeneralSpiderRules() {
29         if (generalSpiderRules == null) {
30             PropertySet props = ConfigurationFactory.getConfiguration().getJSpiderConfiguration();
31             PropertySet jspiderProps = new MappedPropertySet ( ConfigConstants.JSPIDER, props );
32             generalSpiderRules = createRules(jspiderProps, "spider", null);
33         }
34         return generalSpiderRules;
35     }
36
37     public static synchronized Ruleset createGeneralParserRules() {
38         if (generalParserRules == null) {
39             PropertySet props = ConfigurationFactory.getConfiguration().getJSpiderConfiguration();
40             PropertySet jspiderProps = new MappedPropertySet ( ConfigConstants.JSPIDER, props );
41             generalParserRules = createRules(jspiderProps, "parser", null);
42         }
43         return generalParserRules;
44     }
45
46     public static Ruleset createSiteSpiderRules(Site site) {
47         PropertySet props = ConfigurationFactory.getConfiguration().getSiteConfiguration(site);
48         PropertySet siteProps = new MappedPropertySet ( ConfigConstants.SITE, props );
49         return createRules(siteProps, "spider", generalSpiderRules);
50     }
51
52     public static Ruleset createSiteParserRules(Site site) {
53         PropertySet props = ConfigurationFactory.getConfiguration().getSiteConfiguration(site);
54         PropertySet siteProps = new MappedPropertySet ( ConfigConstants.SITE, props );
55         return createRules(siteProps, "parser", generalParserRules);
56     }
57
58     protected static Ruleset createRules(PropertySet props, String JavaDoc purpose, Ruleset generalRules) {
59         PropertySet rulesProps = new MappedPropertySet(ConfigConstants.RULES, props);
60
61         int rulesCount = rulesProps.getInteger(purpose + "." + ConfigConstants.RULES_COUNT, 0);
62
63         Log log = LogFactory.getLog(RuleFactory.class);
64
65         List JavaDoc rules = new ArrayList JavaDoc();
66
67         for (int i = 0; i < rulesCount; i++) {
68             String JavaDoc prefix = purpose + "." + (i + 1);
69             PropertySet ruleProps = new MappedPropertySet(prefix, rulesProps);
70             Class JavaDoc ruleClass = ruleProps.getClass(ConfigConstants.RULE_CLASS, null);
71             Rule rule = null;
72
73             if (ruleClass != null) {
74
75                 try {
76                     Class JavaDoc[] paramTypes = new Class JavaDoc[1];
77                     paramTypes[0] = PropertySet.class;
78
79                     Object JavaDoc params[] = new Object JavaDoc[1];
80                     params[0] = new MappedPropertySet ( ConfigConstants.RULE_CONFIG, ruleProps);
81
82                     Constructor JavaDoc constructor = ruleClass.getDeclaredConstructor(paramTypes);
83                     rule = (Rule) constructor.newInstance(params);
84
85                 } catch (NoSuchMethodException JavaDoc e) {
86                     log.debug("rule " + ruleClass.getName() + " hasn't got a config-param constructor");
87                 } catch (SecurityException JavaDoc e) {
88                     log.info("SecurityException finding constructor");
89                 } catch (InstantiationException JavaDoc e) {
90                     log.info("InstantiationException finding constructor");
91                 } catch (IllegalAccessException JavaDoc e) {
92                     log.info("IllegalAccessException finding constructor");
93                 } catch (InvocationTargetException JavaDoc e) {
94                     log.info("InvocationTargetException finding constructor");
95                 }
96
97                 if (rule == null) {
98                     try {
99                         rule = (Rule) ruleClass.newInstance();
100                     } catch (InstantiationException JavaDoc e) {
101                         log.error("InstantiationException on Rule", e);
102                     } catch (IllegalAccessException JavaDoc e) {
103                         log.error("IllegalAccessException on Rule instantiation", e);
104                     }
105                 }
106                 if (rule == null) {
107                     log.error("rule couldn't be instantiated");
108                 } else {
109                     log.debug ( "added rule " + rule.getClass().getName() + " to " + purpose + " ruleset");
110                     rules.add(rule);
111                 }
112             } else {
113                 log.error("cannot find rule class '" + ruleProps.getString(ConfigConstants.RULE_CLASS, "<unknown>") + "' for " + purpose + " rules");
114             }
115         }
116         if (generalRules == null) {
117             return new RuleSetImpl(Ruleset.RULESET_GENERAL, rules);
118         } else {
119             return new RuleSetImpl(Ruleset.RULESET_SITE, generalRules, rules);
120         }
121     }
122 }
123
Popular Tags