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

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.rule;
  2. import net.javacoding.jspider.api.model.Site;
  3. import net.javacoding.jspider.core.rule.impl.RuleSetImpl;
  4. import net.javacoding.jspider.core.util.config.*;
  5. import net.javacoding.jspider.core.logging.LogFactory;
  6. import net.javacoding.jspider.core.logging.Log;
  7. import net.javacoding.jspider.spi.Rule;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.lang.reflect.Constructor;
  11. import java.lang.reflect.InvocationTargetException;
  12. /**
  13.  *
  14.  * $Id: RuleFactory.java,v 1.12 2003/04/29 17:53:48 vanrogu Exp $
  15.  *
  16.  * @author G黱ther Van Roey
  17.  */
  18. public class RuleFactory {
  19.     protected static Ruleset generalSpiderRules;
  20.     protected static Ruleset generalParserRules;
  21.     public static synchronized Ruleset createGeneralSpiderRules() {
  22.         if (generalSpiderRules == null) {
  23.             PropertySet props = ConfigurationFactory.getConfiguration().getJSpiderConfiguration();
  24.             PropertySet jspiderProps = new MappedPropertySet ( ConfigConstants.JSPIDER, props );
  25.             generalSpiderRules = createRules(jspiderProps, "spider", null);
  26.         }
  27.         return generalSpiderRules;
  28.     }
  29.     public static synchronized Ruleset createGeneralParserRules() {
  30.         if (generalParserRules == null) {
  31.             PropertySet props = ConfigurationFactory.getConfiguration().getJSpiderConfiguration();
  32.             PropertySet jspiderProps = new MappedPropertySet ( ConfigConstants.JSPIDER, props );
  33.             generalParserRules = createRules(jspiderProps, "parser", null);
  34.         }
  35.         return generalParserRules;
  36.     }
  37.     public static Ruleset createSiteSpiderRules(Site site) {
  38.         PropertySet props = ConfigurationFactory.getConfiguration().getSiteConfiguration(site);
  39.         PropertySet siteProps = new MappedPropertySet ( ConfigConstants.SITE, props );
  40.         return createRules(siteProps, "spider", generalSpiderRules);
  41.     }
  42.     public static Ruleset createSiteParserRules(Site site) {
  43.         PropertySet props = ConfigurationFactory.getConfiguration().getSiteConfiguration(site);
  44.         PropertySet siteProps = new MappedPropertySet ( ConfigConstants.SITE, props );
  45.         return createRules(siteProps, "parser", generalParserRules);
  46.     }
  47.     protected static Ruleset createRules(PropertySet props, String purpose, Ruleset generalRules) {
  48.         PropertySet rulesProps = new MappedPropertySet(ConfigConstants.RULES, props);
  49.         int rulesCount = rulesProps.getInteger(purpose + "." + ConfigConstants.RULES_COUNT, 0);
  50.         Log log = LogFactory.getLog(RuleFactory.class);
  51.         List rules = new ArrayList();
  52.         for (int i = 0; i < rulesCount; i++) {
  53.             String prefix = purpose + "." + (i + 1);
  54.             PropertySet ruleProps = new MappedPropertySet(prefix, rulesProps);
  55.             Class ruleClass = ruleProps.getClass(ConfigConstants.RULE_CLASS, null);
  56.             Rule rule = null;
  57.             if (ruleClass != null) {
  58.                 try {
  59.                     Class[] paramTypes = new Class[1];
  60.                     paramTypes[0] = PropertySet.class;
  61.                     Object params[] = new Object[1];
  62.                     params[0] = new MappedPropertySet ( ConfigConstants.RULE_CONFIG, ruleProps);
  63.                     Constructor constructor = ruleClass.getDeclaredConstructor(paramTypes);
  64.                     rule = (Rule) constructor.newInstance(params);
  65.                 } catch (NoSuchMethodException e) {
  66.                     log.debug("rule " + ruleClass.getName() + " hasn't got a config-param constructor");
  67.                 } catch (SecurityException e) {
  68.                     log.info("SecurityException finding constructor");
  69.                 } catch (InstantiationException e) {
  70.                     log.info("InstantiationException finding constructor");
  71.                 } catch (IllegalAccessException e) {
  72.                     log.info("IllegalAccessException finding constructor");
  73.                 } catch (InvocationTargetException e) {
  74.                     log.info("InvocationTargetException finding constructor");
  75.                 }
  76.                 if (rule == null) {
  77.                     try {
  78.                         rule = (Rule) ruleClass.newInstance();
  79.                     } catch (InstantiationException e) {
  80.                         log.error("InstantiationException on Rule", e);
  81.                     } catch (IllegalAccessException e) {
  82.                         log.error("IllegalAccessException on Rule instantiation", e);
  83.                     }
  84.                 }
  85.                 if (rule == null) {
  86.                     log.error("rule couldn't be instantiated");
  87.                 } else {
  88.                     log.debug ( "added rule " + rule.getClass().getName() + " to " + purpose + " ruleset");
  89.                     rules.add(rule);
  90.                 }
  91.             } else {
  92.                 log.error("cannot find rule class '" + ruleProps.getString(ConfigConstants.RULE_CLASS, "<unknown>") + "' for " + purpose + " rules");
  93.             }
  94.         }
  95.         if (generalRules == null) {
  96.             return new RuleSetImpl(Ruleset.RULESET_GENERAL, rules);
  97.         } else {
  98.             return new RuleSetImpl(Ruleset.RULESET_SITE, generalRules, rules);
  99.         }
  100.     }
  101. }