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 ; 12 import java.util.List ; 13 import java.lang.reflect.Constructor ; 14 import java.lang.reflect.InvocationTargetException ; 15 16 17 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 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 rules = new ArrayList (); 66 67 for (int i = 0; i < rulesCount; i++) { 68 String prefix = purpose + "." + (i + 1); 69 PropertySet ruleProps = new MappedPropertySet(prefix, rulesProps); 70 Class ruleClass = ruleProps.getClass(ConfigConstants.RULE_CLASS, null); 71 Rule rule = null; 72 73 if (ruleClass != null) { 74 75 try { 76 Class [] paramTypes = new Class [1]; 77 paramTypes[0] = PropertySet.class; 78 79 Object params[] = new Object [1]; 80 params[0] = new MappedPropertySet ( ConfigConstants.RULE_CONFIG, ruleProps); 81 82 Constructor constructor = ruleClass.getDeclaredConstructor(paramTypes); 83 rule = (Rule) constructor.newInstance(params); 84 85 } catch (NoSuchMethodException e) { 86 log.debug("rule " + ruleClass.getName() + " hasn't got a config-param constructor"); 87 } catch (SecurityException e) { 88 log.info("SecurityException finding constructor"); 89 } catch (InstantiationException e) { 90 log.info("InstantiationException finding constructor"); 91 } catch (IllegalAccessException e) { 92 log.info("IllegalAccessException finding constructor"); 93 } catch (InvocationTargetException e) { 94 log.info("InvocationTargetException finding constructor"); 95 } 96 97 if (rule == null) { 98 try { 99 rule = (Rule) ruleClass.newInstance(); 100 } catch (InstantiationException e) { 101 log.error("InstantiationException on Rule", e); 102 } catch (IllegalAccessException 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 |