001 package com.croftsoft.apps.spider; 002 003 import java.net.URL; 004 005 import com.croftsoft.core.lang.NullArgumentException; 006 import com.croftsoft.core.role.Filter; 007 008 /********************************************************************* 009 * Web spider URL filter. 010 * 011 * @version 012 * 2003-04-09 013 * @since 014 * 2003-04-09 015 * @author 016 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 017 *********************************************************************/ 018 019 public class SpiderUrlFilter 020 implements Filter 021 ////////////////////////////////////////////////////////////////////// 022 ////////////////////////////////////////////////////////////////////// 023 { 024 025 protected final Spider spider; 026 027 // 028 029 protected boolean sameHostOnly; 030 031 protected boolean samePortOnly; 032 033 ////////////////////////////////////////////////////////////////////// 034 ////////////////////////////////////////////////////////////////////// 035 036 public SpiderUrlFilter ( Spider spider ) 037 ////////////////////////////////////////////////////////////////////// 038 { 039 NullArgumentException.check ( this.spider = spider ); 040 } 041 042 ////////////////////////////////////////////////////////////////////// 043 ////////////////////////////////////////////////////////////////////// 044 045 public void setSameHostOnly ( boolean sameHostOnly ) 046 ////////////////////////////////////////////////////////////////////// 047 { 048 this.sameHostOnly = sameHostOnly; 049 } 050 051 public void setSamePortOnly ( boolean samePortOnly ) 052 ////////////////////////////////////////////////////////////////////// 053 { 054 this.samePortOnly = samePortOnly; 055 } 056 057 ////////////////////////////////////////////////////////////////////// 058 ////////////////////////////////////////////////////////////////////// 059 060 public boolean isFiltrate ( Object o ) 061 ////////////////////////////////////////////////////////////////////// 062 { 063 URL newURL = ( URL ) o; 064 065 URL currentURL = spider.getCurrentURL ( ); 066 067 if ( ( currentURL != null ) && sameHostOnly ) 068 { 069 if ( !newURL.getHost ( ).equals ( currentURL.getHost ( ) ) ) 070 { 071 return false; 072 } 073 } 074 075 if ( ( currentURL != null ) && samePortOnly ) 076 { 077 int newPort = newURL.getPort ( ); 078 079 int oldPort = currentURL.getPort ( ); 080 081 if ( newPort == -1 ) 082 { 083 newPort = 80; 084 } 085 086 if ( oldPort == -1 ) 087 { 088 oldPort = 80; 089 } 090 091 if ( newPort != oldPort ) 092 { 093 return false; 094 } 095 } 096 097 return true; 098 } 099 100 ////////////////////////////////////////////////////////////////////// 101 ////////////////////////////////////////////////////////////////////// 102 }