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

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.mod.rule;
  2. import net.javacoding.jspider.core.rule.impl.BaseRuleImpl;
  3. import net.javacoding.jspider.core.util.config.PropertySet;
  4. import net.javacoding.jspider.core.logging.LogFactory;
  5. import net.javacoding.jspider.core.logging.Log;
  6. import net.javacoding.jspider.core.SpiderContext;
  7. import net.javacoding.jspider.core.model.DecisionInternal;
  8. import net.javacoding.jspider.api.model.Decision;
  9. import net.javacoding.jspider.api.model.Site;
  10. import java.net.URL;
  11. /**
  12.  * $Id: MaxNumberOfURLParamsRule.java,v 1.1 2003/04/07 15:51:00 vanrogu Exp $
  13.  */
  14. public class MaxNumberOfURLParamsRule extends BaseRuleImpl {
  15.     public static final String MAX = "max";
  16.     protected int max;
  17.     public MaxNumberOfURLParamsRule ( PropertySet config ) {
  18.         Log log = LogFactory.getLog(MaxNumberOfURLParamsRule.class);
  19.         max = config.getInteger(MaxNumberOfURLParamsRule.MAX, 0);
  20.         log.info("max set to " + max);
  21.     }
  22.     public Decision apply(SpiderContext context, Site currentSite, URL url) {
  23.         Decision decision = null;
  24.         String query = url.getQuery();
  25.         int params;
  26.         if ( query == null || query.length() < 2  ) {
  27.             params = 0;
  28.         } else {
  29.             int amps = 0;
  30.             int pos = query.indexOf('&');
  31.             while ( pos != -1 ) {
  32.                 amps++;
  33.                 pos = query.indexOf('&', pos + 1);
  34.             }
  35.             params = amps + 1;
  36.         }
  37.         if ( params > max ) {
  38.             decision = new DecisionInternal(Decision.RULE_IGNORE, "params = " + params + ", max = " + max + ", url ingored");
  39.         } else {
  40.             decision = new DecisionInternal(Decision.RULE_ACCEPT, "params = " + params + ", max = " + max + ", url accepted");
  41.         }
  42.         return decision;
  43.     }
  44. }